@curvefi/llamalend-api 1.0.15 → 1.0.17

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 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.currentPosition()
834
- // {
835
- // currentPosition: "95777.510614373750083"
836
- // deposited: "219533.5105208847"
837
- // percentage: "-56.372259347958530763"
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
@@ -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>>;
@@ -11,7 +11,7 @@ import { ethers } from "ethers";
11
11
  import memoize from "memoizee";
12
12
  import { llamalend } from "./llamalend.js";
13
13
  const _getPoolsFromApi = memoize((network, poolFactory) => __awaiter(void 0, void 0, void 0, function* () {
14
- const response = yield fetch(`https://d3dl9x5bpp6us7.cloudfront.net/api/getPools/${network}/${poolFactory}`);
14
+ const response = yield fetch(`https://api.curve.finance/api/getPools/${network}/${poolFactory}`);
15
15
  const { data } = yield response.json();
16
16
  return data !== null && data !== void 0 ? data : { poolData: [], tvl: 0, tvlAll: 0 };
17
17
  }), {
@@ -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,
@@ -130,7 +133,7 @@ export const _getUserCollateralCrvUsd = memoize((network, controller, user) => _
130
133
  maxAge: 60 * 1000, // 1m
131
134
  });
132
135
  export const _getMarketsData = memoize((network) => __awaiter(void 0, void 0, void 0, function* () {
133
- const url = `https://d3dl9x5bpp6us7.cloudfront.net/api/getLendingVaults/${network}/oneway`;
136
+ const url = `https://api.curve.finance/api/getLendingVaults/${network}/oneway`;
134
137
  const response = yield fetch(url, { headers: { "accept": "application/json" } });
135
138
  if (response.status !== 200) {
136
139
  throw Error(`Fetch error: ${response.status} ${response.statusText}`);
@@ -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, _current_collateral_estimation] = yield Promise.all([
2658
+ const [userCollateral, { collateral }] = yield Promise.all([
2659
2659
  _getUserCollateral(llamalend.constants.NETWORK_NAME, this.addresses.controller, userAddress),
2660
- llamalend.contracts[this.addresses.amm].contract.get_y_up(userAddress),
2660
+ this.userState(userAddress),
2661
2661
  ]);
2662
- const total_deposit_from_user = userCollateral.total_deposit_from_user;
2663
- const current_collateral_estimation = llamalend.formatUnits(_current_collateral_estimation, this.collateral_token.decimals);
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 [currentCollateralEstimation, userState, oraclePrice] = yield llamalend.multicallProvider.all(calls);
2676
- if (!(currentCollateralEstimation || userState || oraclePrice)) {
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 totalDepositUsdValue = userCollateral.total_deposit_usd_value;
2682
- const currentCollateralEstimationFormatted = llamalend.formatUnits(currentCollateralEstimation, this.collateral_token.decimals);
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 currentPosition = BN(currentCollateralEstimationFormatted)
2686
- .times(oraclePriceFormatted)
2687
- .minus(debtFormatted)
2688
- .toFixed(this.collateral_token.decimals);
2689
- const percentage = BN(currentPosition)
2690
- .div(totalDepositUsdValue)
2691
- .minus(1)
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: totalDepositUsdValue.toString(),
2697
- percentage: percentage,
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@curvefi/llamalend-api",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "description": "JavaScript library for Curve Lending",
5
5
  "main": "lib/index.js",
6
6
  "author": "Macket",
@@ -14,7 +14,7 @@ import {
14
14
 
15
15
  const _getPoolsFromApi = memoize(
16
16
  async (network: INetworkName, poolFactory: IPoolFactory ): Promise<IExtendedPoolDataFromApi> => {
17
- const response = await fetch(`https://d3dl9x5bpp6us7.cloudfront.net/api/getPools/${network}/${poolFactory}`);
17
+ const response = await fetch(`https://api.curve.finance/api/getPools/${network}/${poolFactory}`);
18
18
  const { data } = await response.json() as { data?: IExtendedPoolDataFromApi, success: boolean };
19
19
  return data ?? { poolData: [], tvl: 0, tvlAll: 0 };
20
20
  },
@@ -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
  {
@@ -148,7 +151,7 @@ export const _getUserCollateralCrvUsd = memoize(
148
151
 
149
152
  export const _getMarketsData = memoize(
150
153
  async (network: INetworkName): Promise<IMarketData> => {
151
- const url = `https://d3dl9x5bpp6us7.cloudfront.net/api/getLendingVaults/${network}/oneway`;
154
+ const url = `https://api.curve.finance/api/getLendingVaults/${network}/oneway`;
152
155
  const response = await fetch(url, { headers: {"accept": "application/json"} });
153
156
  if (response.status !== 200) {
154
157
  throw Error(`Fetch error: ${response.status} ${response.statusText}`);
@@ -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, _current_collateral_estimation] = await Promise.all([
2994
+ const [userCollateral, {collateral}] = await Promise.all([
2995
2995
  _getUserCollateral(llamalend.constants.NETWORK_NAME, this.addresses.controller, userAddress),
2996
- llamalend.contracts[this.addresses.amm].contract.get_y_up(userAddress),
2996
+ this.userState(userAddress),
2997
2997
  ]);
2998
2998
 
2999
- const total_deposit_from_user = userCollateral.total_deposit_from_user;
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(current_collateral_estimation).div(total_deposit_from_user).toString();
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 [currentCollateralEstimation, userState, oraclePrice] = await llamalend.multicallProvider.all(calls) as [bigint, bigint[],bigint];
3012
+ const [userState, oraclePrice] = await llamalend.multicallProvider.all(calls) as [bigint[],bigint];
3016
3013
 
3017
- if(!(currentCollateralEstimation || userState || oraclePrice)) {
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 totalDepositUsdValue = userCollateral.total_deposit_usd_value;
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 currentPosition = BN(currentCollateralEstimationFormatted)
3031
- .times(oraclePriceFormatted)
3032
- .minus(debtFormatted)
3033
- .toFixed(this.collateral_token.decimals)
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 = BN(currentPosition)
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: totalDepositUsdValue.toString(),
3044
- percentage: percentage,
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
  }