Skip to content

Commit f67fd52

Browse files
Merge branch 'main' of github.com:curvefi/curve-llamalend.js into chore/node
2 parents b2e8101 + 6f65983 commit f67fd52

File tree

5 files changed

+86
-8
lines changed

5 files changed

+86
-8
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.36",
3+
"version": "1.0.38",
44
"description": "JavaScript library for Curve Lending",
55
"main": "lib/index.js",
66
"author": "Macket",

src/constants/llammas.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export const LLAMMAS: IDict<ILlamma> = lowerCaseLlammasAddresses({
4040
controller_address: '0x4e59541306910ad6dc1dac0ac9dfb29bd9f15c67',
4141
monetary_policy_address: '0x1E7d3bf98d3f8D8CE193236c3e0eC4b00e32DaaE',
4242
collateral_address: '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599',
43-
leverage_zap: '0xA2518b71ee64E910741f5Cf480b19E8e402de4d7',
44-
deleverage_zap: '0xb911D7e59BA82FDF477a2Ab22Ff25125072C9282',
43+
leverage_zap: '0x1eaAb18c70E23331857AA47701Bb516590C8Ba2A',
44+
deleverage_zap: '0xcd129c2433E3324129f19EBa42e8D115761B6f2B',
4545
health_calculator_zap: "0xCF61Ee62b136e3553fB545bd8fEc11fb7f830d6A",
4646
collateral_symbol: 'WBTC',
4747
collateral_decimals: 8,
@@ -86,8 +86,8 @@ export const LLAMMAS: IDict<ILlamma> = lowerCaseLlammasAddresses({
8686
controller_address: '0x1c91da0223c763d2e0173243eadaa0a2ea47e704',
8787
monetary_policy_address: '0xb8687d7dc9d8fa32fabde63e19b2dbc9bb8b2138',
8888
collateral_address: '0x18084fba666a33d37592fa2633fd49a74dd93a88',
89-
leverage_zap: '0xD79964C70Cb06224FdA4c48387B53E9819bcB71c',
90-
deleverage_zap: '0xAA25a6Fa9e4dADaE0d3EE59bEA19fbcf0284830C',
89+
leverage_zap: '0x52D18d48F92a3f8625aa34F90fb18e8469458A5d',
90+
deleverage_zap: '0x627E55e2A01f6bF73A956D24E8B2858c9BEB1fD7',
9191
collateral_symbol: 'tBTC',
9292
collateral_decimals: 18,
9393
min_bands: 4,

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)