@curvefi/llamalend-api 1.0.16 → 1.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -7
- package/lib/external-api.d.ts +3 -0
- package/lib/external-api.js +3 -0
- package/lib/lendMarkets/LendMarketTemplate.js +20 -22
- package/lib/utils.js +1 -1
- package/package.json +1 -1
- package/src/external-api.ts +4 -1
- package/src/lendMarkets/LendMarketTemplate.ts +23 -23
- package/src/utils.ts +1 -1
package/README.md
CHANGED
|
@@ -829,13 +829,16 @@ import llamalend from "@curvefi/llamalend-api";
|
|
|
829
829
|
|
|
830
830
|
await lendMarket.currentLeverage()
|
|
831
831
|
//0.94083266399502623316
|
|
832
|
-
|
|
833
|
-
await lendMarket.
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
832
|
+
|
|
833
|
+
await lendMarket.currentPnL()
|
|
834
|
+
/*
|
|
835
|
+
{
|
|
836
|
+
currentPosition:"9.383656846426222260"
|
|
837
|
+
currentProfit:"0.007205653033021260"
|
|
838
|
+
deposited:"1.572195559253977"
|
|
839
|
+
percentage:"0.46"
|
|
840
|
+
}
|
|
841
|
+
*/
|
|
839
842
|
|
|
840
843
|
await lendMarket.tokensToLiquidate(addressToLiquidate);
|
|
841
844
|
// 301.533523886491869218
|
package/lib/external-api.d.ts
CHANGED
|
@@ -5,6 +5,9 @@ type UserCollateral = {
|
|
|
5
5
|
total_deposit_precise: string;
|
|
6
6
|
total_deposit_from_user: number;
|
|
7
7
|
total_deposit_usd_value: number;
|
|
8
|
+
total_borrowed: number;
|
|
9
|
+
total_deposit_from_user_precise: number;
|
|
10
|
+
total_deposit_from_user_usd_value: number;
|
|
8
11
|
};
|
|
9
12
|
export declare const _getUserCollateral: ((network: INetworkName, controller: string, user: string) => Promise<UserCollateral>) & memoize.Memoized<(network: INetworkName, controller: string, user: string) => Promise<UserCollateral>>;
|
|
10
13
|
export declare const _getUserCollateralCrvUsd: ((network: INetworkName, controller: string, user: string) => Promise<string>) & memoize.Memoized<(network: INetworkName, controller: string, user: string) => Promise<string>>;
|
package/lib/external-api.js
CHANGED
|
@@ -112,9 +112,12 @@ export const _getUserCollateral = memoize((network, controller, user) => __await
|
|
|
112
112
|
const response = yield fetch(url);
|
|
113
113
|
const data = yield response.json();
|
|
114
114
|
return {
|
|
115
|
+
total_borrowed: data.total_borrowed,
|
|
116
|
+
total_deposit_from_user_precise: data.total_deposit_from_user_precise, // Total deposit
|
|
115
117
|
total_deposit_precise: data.total_deposit_precise,
|
|
116
118
|
total_deposit_from_user: data.total_deposit_from_user,
|
|
117
119
|
total_deposit_usd_value: data.total_deposit_usd_value,
|
|
120
|
+
total_deposit_from_user_usd_value: data.total_deposit_from_user_usd_value,
|
|
118
121
|
};
|
|
119
122
|
}), {
|
|
120
123
|
promise: true,
|
|
@@ -2655,46 +2655,44 @@ export class LendMarketTemplate {
|
|
|
2655
2655
|
currentLeverage() {
|
|
2656
2656
|
return __awaiter(this, arguments, void 0, function* (userAddress = '') {
|
|
2657
2657
|
userAddress = _getAddress(userAddress);
|
|
2658
|
-
const [userCollateral,
|
|
2658
|
+
const [userCollateral, { collateral }] = yield Promise.all([
|
|
2659
2659
|
_getUserCollateral(llamalend.constants.NETWORK_NAME, this.addresses.controller, userAddress),
|
|
2660
|
-
|
|
2660
|
+
this.userState(userAddress),
|
|
2661
2661
|
]);
|
|
2662
|
-
const total_deposit_from_user = userCollateral.
|
|
2663
|
-
|
|
2664
|
-
return BN(current_collateral_estimation).div(total_deposit_from_user).toString();
|
|
2662
|
+
const total_deposit_from_user = userCollateral.total_deposit_from_user_precise;
|
|
2663
|
+
return BN(collateral).div(total_deposit_from_user).toString();
|
|
2665
2664
|
});
|
|
2666
2665
|
}
|
|
2667
2666
|
currentPnL() {
|
|
2668
2667
|
return __awaiter(this, arguments, void 0, function* (userAddress = '') {
|
|
2669
2668
|
userAddress = _getAddress(userAddress);
|
|
2670
2669
|
const calls = [
|
|
2671
|
-
llamalend.contracts[this.addresses.amm].multicallContract.get_y_up(userAddress),
|
|
2672
2670
|
llamalend.contracts[this.addresses.controller].multicallContract.user_state(userAddress, llamalend.constantOptions),
|
|
2673
2671
|
llamalend.contracts[this.addresses.amm].multicallContract.price_oracle(userAddress),
|
|
2674
2672
|
];
|
|
2675
|
-
const [
|
|
2676
|
-
if (!(
|
|
2673
|
+
const [userState, oraclePrice] = yield llamalend.multicallProvider.all(calls);
|
|
2674
|
+
if (!(userState || oraclePrice)) {
|
|
2677
2675
|
throw new Error('Multicall error');
|
|
2678
2676
|
}
|
|
2679
2677
|
const debt = userState[2];
|
|
2680
2678
|
const userCollateral = yield _getUserCollateral(llamalend.constants.NETWORK_NAME, this.addresses.controller, userAddress);
|
|
2681
|
-
const
|
|
2682
|
-
const
|
|
2679
|
+
const totalDepositUsdValueFull = userCollateral.total_deposit_usd_value;
|
|
2680
|
+
const totalDepositUsdValueUser = userCollateral.total_deposit_from_user_usd_value;
|
|
2681
|
+
const totalBorrowed = userCollateral.total_borrowed;
|
|
2683
2682
|
const oraclePriceFormatted = llamalend.formatUnits(oraclePrice, 18);
|
|
2684
2683
|
const debtFormatted = llamalend.formatUnits(debt, 18);
|
|
2685
|
-
const
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
const
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
.times(100)
|
|
2693
|
-
.toString();
|
|
2684
|
+
const { _collateral: AmmCollateral, _borrowed: AmmBorrowed } = yield this._userState(userAddress);
|
|
2685
|
+
const [AmmCollateralFormatted, AmmBorrowedFormatted] = [llamalend.formatUnits(AmmCollateral, this.collateral_token.decimals), llamalend.formatUnits(AmmBorrowed, this.borrowed_token.decimals)];
|
|
2686
|
+
const a = BN(AmmCollateralFormatted).times(oraclePriceFormatted);
|
|
2687
|
+
const b = BN(totalBorrowed).minus(debtFormatted);
|
|
2688
|
+
const currentPosition = a.plus(AmmBorrowedFormatted).plus(b);
|
|
2689
|
+
const currentProfit = currentPosition.minus(totalDepositUsdValueFull);
|
|
2690
|
+
const percentage = currentProfit.div(totalDepositUsdValueUser).times(100);
|
|
2694
2691
|
return {
|
|
2695
|
-
currentPosition: currentPosition,
|
|
2696
|
-
deposited:
|
|
2697
|
-
|
|
2692
|
+
currentPosition: currentPosition.toFixed(this.borrowed_token.decimals).toString(),
|
|
2693
|
+
deposited: totalDepositUsdValueUser.toString(),
|
|
2694
|
+
currentProfit: currentProfit.toFixed(this.borrowed_token.decimals).toString(),
|
|
2695
|
+
percentage: percentage.toFixed(2).toString(),
|
|
2698
2696
|
};
|
|
2699
2697
|
});
|
|
2700
2698
|
}
|
package/lib/utils.js
CHANGED
|
@@ -194,7 +194,7 @@ export const getAllowance = (coins, address, spender) => __awaiter(void 0, void
|
|
|
194
194
|
// coins can be either addresses or symbols
|
|
195
195
|
export const hasAllowance = (coins, amounts, address, spender) => __awaiter(void 0, void 0, void 0, function* () {
|
|
196
196
|
const coinAddresses = _getCoinAddresses(coins);
|
|
197
|
-
const decimals = _getCoinDecimals(coinAddresses);
|
|
197
|
+
const decimals = _getCoinDecimals(coinAddresses).map((item) => Number(item));
|
|
198
198
|
const _allowance = yield _getAllowance(coinAddresses, address, spender);
|
|
199
199
|
const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
|
|
200
200
|
return _allowance.map((a, i) => a >= _amounts[i]).reduce((a, b) => a && b);
|
package/package.json
CHANGED
package/src/external-api.ts
CHANGED
|
@@ -115,16 +115,19 @@ export const _getUsdPricesFromApi = async (): Promise<IDict<number>> => {
|
|
|
115
115
|
return priceDictByMaxTvl
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
type UserCollateral = { total_deposit_precise: string, total_deposit_from_user: number, total_deposit_usd_value: number }
|
|
118
|
+
type UserCollateral = { total_deposit_precise: string, total_deposit_from_user: number, total_deposit_usd_value: number , total_borrowed: number, total_deposit_from_user_precise: number, total_deposit_from_user_usd_value: number}
|
|
119
119
|
export const _getUserCollateral = memoize(
|
|
120
120
|
async (network: INetworkName, controller: string, user: string): Promise<UserCollateral> => {
|
|
121
121
|
const url = `https://prices.curve.finance/v1/lending/collateral_events/${network}/${controller}/${user}`;
|
|
122
122
|
const response = await fetch(url);
|
|
123
123
|
const data = await response.json() as UserCollateral;
|
|
124
124
|
return {
|
|
125
|
+
total_borrowed: data.total_borrowed,
|
|
126
|
+
total_deposit_from_user_precise: data.total_deposit_from_user_precise, // Total deposit
|
|
125
127
|
total_deposit_precise: data.total_deposit_precise,
|
|
126
128
|
total_deposit_from_user: data.total_deposit_from_user,
|
|
127
129
|
total_deposit_usd_value: data.total_deposit_usd_value,
|
|
130
|
+
total_deposit_from_user_usd_value: data.total_deposit_from_user_usd_value,
|
|
128
131
|
}
|
|
129
132
|
},
|
|
130
133
|
{
|
|
@@ -2991,57 +2991,57 @@ export class LendMarketTemplate {
|
|
|
2991
2991
|
|
|
2992
2992
|
public async currentLeverage(userAddress = ''): Promise<string> {
|
|
2993
2993
|
userAddress = _getAddress(userAddress);
|
|
2994
|
-
const [userCollateral,
|
|
2994
|
+
const [userCollateral, {collateral}] = await Promise.all([
|
|
2995
2995
|
_getUserCollateral(llamalend.constants.NETWORK_NAME, this.addresses.controller, userAddress),
|
|
2996
|
-
|
|
2996
|
+
this.userState(userAddress),
|
|
2997
2997
|
]);
|
|
2998
2998
|
|
|
2999
|
-
const total_deposit_from_user = userCollateral.
|
|
3000
|
-
const current_collateral_estimation = llamalend.formatUnits(_current_collateral_estimation, this.collateral_token.decimals);
|
|
3001
|
-
|
|
2999
|
+
const total_deposit_from_user = userCollateral.total_deposit_from_user_precise;
|
|
3002
3000
|
|
|
3003
|
-
return BN(
|
|
3001
|
+
return BN(collateral).div(total_deposit_from_user).toString();
|
|
3004
3002
|
}
|
|
3005
3003
|
|
|
3006
3004
|
public async currentPnL(userAddress = ''): Promise<Record<string, string>> {
|
|
3007
3005
|
userAddress = _getAddress(userAddress);
|
|
3008
3006
|
|
|
3009
3007
|
const calls = [
|
|
3010
|
-
llamalend.contracts[this.addresses.amm].multicallContract.get_y_up(userAddress),
|
|
3011
3008
|
llamalend.contracts[this.addresses.controller].multicallContract.user_state(userAddress, llamalend.constantOptions),
|
|
3012
3009
|
llamalend.contracts[this.addresses.amm].multicallContract.price_oracle(userAddress),
|
|
3013
3010
|
];
|
|
3014
3011
|
|
|
3015
|
-
const [
|
|
3012
|
+
const [userState, oraclePrice] = await llamalend.multicallProvider.all(calls) as [bigint[],bigint];
|
|
3016
3013
|
|
|
3017
|
-
if(!(
|
|
3014
|
+
if(!(userState || oraclePrice)) {
|
|
3018
3015
|
throw new Error('Multicall error')
|
|
3019
3016
|
}
|
|
3020
3017
|
|
|
3021
3018
|
const debt = userState[2];
|
|
3022
3019
|
|
|
3023
3020
|
const userCollateral = await _getUserCollateral(llamalend.constants.NETWORK_NAME, this.addresses.controller, userAddress);
|
|
3024
|
-
const
|
|
3021
|
+
const totalDepositUsdValueFull = userCollateral.total_deposit_usd_value;
|
|
3022
|
+
const totalDepositUsdValueUser = userCollateral.total_deposit_from_user_usd_value;
|
|
3023
|
+
const totalBorrowed = userCollateral.total_borrowed;
|
|
3025
3024
|
|
|
3026
|
-
const currentCollateralEstimationFormatted = llamalend.formatUnits(currentCollateralEstimation, this.collateral_token.decimals);
|
|
3027
3025
|
const oraclePriceFormatted = llamalend.formatUnits(oraclePrice, 18);
|
|
3028
3026
|
const debtFormatted = llamalend.formatUnits(debt, 18);
|
|
3029
3027
|
|
|
3030
|
-
const
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3028
|
+
const {_collateral: AmmCollateral, _borrowed: AmmBorrowed} = await this._userState(userAddress)
|
|
3029
|
+
const [AmmCollateralFormatted, AmmBorrowedFormatted] = [llamalend.formatUnits(AmmCollateral, this.collateral_token.decimals), llamalend.formatUnits(AmmBorrowed, this.borrowed_token.decimals)];
|
|
3030
|
+
|
|
3031
|
+
const a = BN(AmmCollateralFormatted).times(oraclePriceFormatted);
|
|
3032
|
+
const b = BN(totalBorrowed).minus(debtFormatted)
|
|
3033
|
+
|
|
3034
|
+
const currentPosition = a.plus(AmmBorrowedFormatted).plus(b);
|
|
3035
|
+
|
|
3036
|
+
const currentProfit = currentPosition.minus(totalDepositUsdValueFull);
|
|
3034
3037
|
|
|
3035
|
-
const percentage =
|
|
3036
|
-
.div(totalDepositUsdValue)
|
|
3037
|
-
.minus(1)
|
|
3038
|
-
.times(100)
|
|
3039
|
-
.toString();
|
|
3038
|
+
const percentage = currentProfit.div(totalDepositUsdValueUser).times(100);
|
|
3040
3039
|
|
|
3041
3040
|
return {
|
|
3042
|
-
currentPosition: currentPosition,
|
|
3043
|
-
deposited:
|
|
3044
|
-
|
|
3041
|
+
currentPosition: currentPosition.toFixed(this.borrowed_token.decimals).toString(),
|
|
3042
|
+
deposited: totalDepositUsdValueUser.toString(),
|
|
3043
|
+
currentProfit: currentProfit.toFixed(this.borrowed_token.decimals).toString(),
|
|
3044
|
+
percentage: percentage.toFixed(2).toString(),
|
|
3045
3045
|
};
|
|
3046
3046
|
}
|
|
3047
3047
|
}
|
package/src/utils.ts
CHANGED
|
@@ -226,7 +226,7 @@ export const getAllowance = async (coins: string[], address: string, spender: st
|
|
|
226
226
|
// coins can be either addresses or symbols
|
|
227
227
|
export const hasAllowance = async (coins: string[], amounts: (number | string)[], address: string, spender: string): Promise<boolean> => {
|
|
228
228
|
const coinAddresses = _getCoinAddresses(coins);
|
|
229
|
-
const decimals = _getCoinDecimals(coinAddresses);
|
|
229
|
+
const decimals = _getCoinDecimals(coinAddresses).map((item) => Number(item));
|
|
230
230
|
const _allowance = await _getAllowance(coinAddresses, address, spender);
|
|
231
231
|
const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
|
|
232
232
|
|