Skip to content

Commit 1ec8102

Browse files
authored
Merge pull request #48 from curvefi/feat/add-pnl-for-new-mint-markets
feat: add currentPnl and currentLeverage for new mint markets
2 parents 7fa6e0f + df9b9a8 commit 1ec8102

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@curvefi/llamalend-api",
3-
"version": "1.0.35",
3+
"version": "1.0.36",
44
"description": "JavaScript library for Curve Lending",
55
"main": "lib/index.js",
66
"author": "Macket",

src/external-api.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ export const _getUserCollateralCrvUsd = memoize(
141141
}
142142
)
143143

144+
export const _getUserCollateralCrvUsdFull = memoize(
145+
async (network: INetworkName, controller: string, user: string): Promise<UserCollateral> => {
146+
const url = `https://prices.curve.finance/v1/crvusd/collateral_events/${network}/${controller}/${user}`;
147+
const response = await fetch(url);
148+
return await response.json() as UserCollateral;
149+
},
150+
{
151+
promise: true,
152+
maxAge: 60 * 1000, // 1m
153+
}
154+
)
155+
144156
export const _getMarketsData = memoize(
145157
async (network: INetworkName): Promise<IMarketData> => {
146158
const url = `https://api.curve.finance/api/getLendingVaults/${network}/oneway`;

src/mintMarkets/MintMarketTemplate.ts

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
DIGas,
2222
} from "../utils";
2323
import {IDict, ILlamma, TGas} from "../interfaces";
24-
import {_getUserCollateralCrvUsd} from "../external-api.js";
24+
import {_getUserCollateralCrvUsd, _getUserCollateralCrvUsdFull} from "../external-api.js";
2525
import { ILeverageV2 } from "./interfaces/leverage.js";
2626
import { 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

Comments
 (0)