@@ -21,7 +21,7 @@ import {
2121 DIGas ,
2222} from "../utils" ;
2323import { IDict , ILlamma , TGas } from "../interfaces" ;
24- import { _getUserCollateralCrvUsd } from "../external-api.js" ;
24+ import { _getUserCollateralCrvUsd , _getUserCollateralCrvUsdFull } from "../external-api.js" ;
2525import { ILeverageV2 } from "./interfaces/leverage.js" ;
2626import { LeverageV2Module } from "./modules" ;
2727
@@ -1871,6 +1871,72 @@ export class MintMarketTemplate {
18711871 return await this . _deleverageRepay ( collateral , slippage , false ) as string ;
18721872 }
18731873
1874+ // ---------------- CURRENT LEVERAGE & PNL ----------------
1875+
1876+ private _checkLeverageForStats ( ) : void {
1877+ const leverageV2Module = new LeverageV2Module ( this ) ;
1878+ if ( ! leverageV2Module . hasLeverage ( ) ) {
1879+ throw Error ( "This market does not support leverage" ) ;
1880+ }
1881+ }
1882+
1883+ public async currentLeverage ( userAddress = '' ) : Promise < string > {
1884+ this . _checkLeverageForStats ( ) ;
1885+ userAddress = _getAddress . call ( this . llamalend , userAddress ) ;
1886+
1887+ const [ userCollateral , { collateral} ] = await Promise . all ( [
1888+ _getUserCollateralCrvUsdFull ( this . llamalend . constants . NETWORK_NAME , this . controller , userAddress ) ,
1889+ this . userState ( userAddress ) ,
1890+ ] ) ;
1891+
1892+ const total_deposit_from_user = userCollateral . total_deposit_from_user_precise ?? userCollateral . total_deposit_precise ;
1893+
1894+ return BN ( collateral ) . div ( total_deposit_from_user ) . toString ( ) ;
1895+ }
1896+
1897+ public async currentPnL ( userAddress = '' ) : Promise < Record < string , string > > {
1898+ this . _checkLeverageForStats ( ) ;
1899+ userAddress = _getAddress . call ( this . llamalend , userAddress ) ;
1900+
1901+ const calls = [
1902+ this . llamalend . contracts [ this . controller ] . multicallContract . user_state ( userAddress , this . llamalend . constantOptions ) ,
1903+ this . llamalend . contracts [ this . address ] . multicallContract . price_oracle ( this . llamalend . constantOptions ) ,
1904+ ] ;
1905+
1906+ const [ userState , oraclePrice ] = await this . llamalend . multicallProvider . all ( calls ) as [ bigint [ ] , bigint ] ;
1907+
1908+ if ( ! ( userState || oraclePrice ) ) {
1909+ throw new Error ( 'Multicall error' )
1910+ }
1911+
1912+ const debt = userState [ 2 ] ;
1913+
1914+ const userCollateral = await _getUserCollateralCrvUsdFull ( this . llamalend . constants . NETWORK_NAME , this . controller , userAddress ) ;
1915+ const totalDepositUsdValueFull = userCollateral . total_deposit_usd_value ;
1916+ const totalDepositUsdValueUser = userCollateral . total_deposit_from_user_usd_value ?? userCollateral . total_deposit_usd_value ;
1917+ const totalBorrowed = userCollateral . total_borrowed ;
1918+
1919+ const oraclePriceFormatted = this . llamalend . formatUnits ( oraclePrice , 18 ) ;
1920+ const debtFormatted = this . llamalend . formatUnits ( debt , 18 ) ;
1921+
1922+ const { _collateral : ammCollateral , _stablecoin : ammBorrowed } = await this . _userState ( userAddress )
1923+ const [ ammCollateralFormatted , ammBorrowedFormatted ] = [ this . llamalend . formatUnits ( ammCollateral , this . collateralDecimals ) , this . llamalend . formatUnits ( ammBorrowed , 18 ) ] ;
1924+
1925+ const collateralValueUsd = BN ( ammCollateralFormatted ) . times ( oraclePriceFormatted ) ;
1926+ const repaidDebt = BN ( totalBorrowed ) . minus ( debtFormatted )
1927+
1928+ const currentPosition = collateralValueUsd . plus ( ammBorrowedFormatted ) . plus ( repaidDebt ) ;
1929+ const currentProfit = currentPosition . minus ( totalDepositUsdValueFull ) ;
1930+ const percentage = currentProfit . div ( totalDepositUsdValueUser ) . times ( 100 ) ;
1931+
1932+ return {
1933+ currentPosition : currentPosition . toString ( ) ,
1934+ deposited : totalDepositUsdValueUser . toString ( ) ,
1935+ currentProfit : currentProfit . toString ( ) ,
1936+ percentage : percentage . toString ( ) ,
1937+ } ;
1938+ }
1939+
18741940 public getLlamalend ( ) : Llamalend {
18751941 return this . llamalend ;
18761942 }
0 commit comments