@curvefi/llamalend-api 1.0.34 → 1.0.36

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.
@@ -397,6 +397,10 @@
397
397
  {
398
398
  "name": "callback_args",
399
399
  "type": "uint256[]"
400
+ },
401
+ {
402
+ "name": "callback_bytes",
403
+ "type": "bytes"
400
404
  }
401
405
  ],
402
406
  "name": "create_loan_extended",
@@ -448,6 +452,34 @@
448
452
  "stateMutability": "payable",
449
453
  "type": "function"
450
454
  },
455
+ {
456
+ "stateMutability": "nonpayable",
457
+ "type": "function",
458
+ "name": "borrow_more_extended",
459
+ "inputs": [
460
+ {
461
+ "name": "collateral",
462
+ "type": "uint256"
463
+ },
464
+ {
465
+ "name": "debt",
466
+ "type": "uint256"
467
+ },
468
+ {
469
+ "name": "callbacker",
470
+ "type": "address"
471
+ },
472
+ {
473
+ "name": "callback_args",
474
+ "type": "uint256[]"
475
+ },
476
+ {
477
+ "name": "callback_bytes",
478
+ "type": "bytes"
479
+ }
480
+ ],
481
+ "outputs": []
482
+ },
451
483
  {
452
484
  "inputs": [
453
485
  {
@@ -477,6 +509,10 @@
477
509
  {
478
510
  "name": "callback_args",
479
511
  "type": "uint256[]"
512
+ },
513
+ {
514
+ "name": "callback_bytes",
515
+ "type": "bytes"
480
516
  }
481
517
  ],
482
518
  "name": "repay_extended",
@@ -14,6 +14,7 @@ type UserCollateral = {
14
14
  export declare const _getUserCollateral: ((network: INetworkName, controller: string, user: string) => Promise<UserCollateral>) & memoize.Memoized<(network: INetworkName, controller: string, user: string) => Promise<UserCollateral>>;
15
15
  export declare const _getUserCollateralForce: (network: INetworkName, controller: string, user: string, newTx: string) => Promise<void>;
16
16
  export declare const _getUserCollateralCrvUsd: ((network: INetworkName, controller: string, user: string) => Promise<string>) & memoize.Memoized<(network: INetworkName, controller: string, user: string) => Promise<string>>;
17
+ export declare const _getUserCollateralCrvUsdFull: ((network: INetworkName, controller: string, user: string) => Promise<UserCollateral>) & memoize.Memoized<(network: INetworkName, controller: string, user: string) => Promise<UserCollateral>>;
17
18
  export declare const _getMarketsData: ((network: INetworkName) => Promise<IMarketData>) & memoize.Memoized<(network: INetworkName) => Promise<IMarketData>>;
18
19
  export declare function _getQuoteOdos(this: Llamalend, fromToken: string, toToken: string, _amount: bigint, blacklist: string, pathVizImage: boolean, slippage?: number): Promise<IQuoteOdos>;
19
20
  export declare function _getExpectedOdos(this: Llamalend, fromToken: string, toToken: string, _amount: bigint, blacklist: string): Promise<string>;
@@ -119,6 +119,14 @@ export const _getUserCollateralCrvUsd = memoize((network, controller, user) => _
119
119
  promise: true,
120
120
  maxAge: 60 * 1000, // 1m
121
121
  });
122
+ export const _getUserCollateralCrvUsdFull = memoize((network, controller, user) => __awaiter(void 0, void 0, void 0, function* () {
123
+ const url = `https://prices.curve.finance/v1/crvusd/collateral_events/${network}/${controller}/${user}`;
124
+ const response = yield fetch(url);
125
+ return yield response.json();
126
+ }), {
127
+ promise: true,
128
+ maxAge: 60 * 1000, // 1m
129
+ });
122
130
  export const _getMarketsData = memoize((network) => __awaiter(void 0, void 0, void 0, function* () {
123
131
  const url = `https://api.curve.finance/api/getLendingVaults/${network}/oneway`;
124
132
  const response = yield fetch(url, { headers: { "accept": "application/json" } });
@@ -329,5 +329,8 @@ export declare class MintMarketTemplate {
329
329
  private _deleverageRepay;
330
330
  private deleverageRepayEstimateGas;
331
331
  private deleverageRepay;
332
+ private _checkLeverageForStats;
333
+ currentLeverage(userAddress?: string): Promise<string>;
334
+ currentPnL(userAddress?: string): Promise<Record<string, string>>;
332
335
  getLlamalend(): Llamalend;
333
336
  }
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import memoize from "memoizee";
11
11
  import BigNumber from "bignumber.js";
12
12
  import { _getAddress, parseUnits, BN, toBN, fromBN, getBalances, ensureAllowance, hasAllowance, ensureAllowanceEstimateGas, isEth, _cutZeros, formatUnits, smartNumber, MAX_ALLOWANCE, MAX_ACTIVE_BAND, _mulBy1_3, DIGas, } from "../utils";
13
- import { _getUserCollateralCrvUsd } from "../external-api.js";
13
+ import { _getUserCollateralCrvUsd, _getUserCollateralCrvUsdFull } from "../external-api.js";
14
14
  import { LeverageV2Module } from "./modules";
15
15
  export class MintMarketTemplate {
16
16
  constructor(id, llammaData, llamalend) {
@@ -1740,6 +1740,61 @@ export class MintMarketTemplate {
1740
1740
  return yield this._deleverageRepay(collateral, slippage, false);
1741
1741
  });
1742
1742
  }
1743
+ // ---------------- CURRENT LEVERAGE & PNL ----------------
1744
+ _checkLeverageForStats() {
1745
+ const leverageV2Module = new LeverageV2Module(this);
1746
+ if (!leverageV2Module.hasLeverage()) {
1747
+ throw Error("This market does not support leverage");
1748
+ }
1749
+ }
1750
+ currentLeverage() {
1751
+ return __awaiter(this, arguments, void 0, function* (userAddress = '') {
1752
+ var _a;
1753
+ this._checkLeverageForStats();
1754
+ userAddress = _getAddress.call(this.llamalend, userAddress);
1755
+ const [userCollateral, { collateral }] = yield Promise.all([
1756
+ _getUserCollateralCrvUsdFull(this.llamalend.constants.NETWORK_NAME, this.controller, userAddress),
1757
+ this.userState(userAddress),
1758
+ ]);
1759
+ const total_deposit_from_user = (_a = userCollateral.total_deposit_from_user_precise) !== null && _a !== void 0 ? _a : userCollateral.total_deposit_precise;
1760
+ return BN(collateral).div(total_deposit_from_user).toString();
1761
+ });
1762
+ }
1763
+ currentPnL() {
1764
+ return __awaiter(this, arguments, void 0, function* (userAddress = '') {
1765
+ var _a;
1766
+ this._checkLeverageForStats();
1767
+ userAddress = _getAddress.call(this.llamalend, userAddress);
1768
+ const calls = [
1769
+ this.llamalend.contracts[this.controller].multicallContract.user_state(userAddress, this.llamalend.constantOptions),
1770
+ this.llamalend.contracts[this.address].multicallContract.price_oracle(this.llamalend.constantOptions),
1771
+ ];
1772
+ const [userState, oraclePrice] = yield this.llamalend.multicallProvider.all(calls);
1773
+ if (!(userState || oraclePrice)) {
1774
+ throw new Error('Multicall error');
1775
+ }
1776
+ const debt = userState[2];
1777
+ const userCollateral = yield _getUserCollateralCrvUsdFull(this.llamalend.constants.NETWORK_NAME, this.controller, userAddress);
1778
+ const totalDepositUsdValueFull = userCollateral.total_deposit_usd_value;
1779
+ const totalDepositUsdValueUser = (_a = userCollateral.total_deposit_from_user_usd_value) !== null && _a !== void 0 ? _a : userCollateral.total_deposit_usd_value;
1780
+ const totalBorrowed = userCollateral.total_borrowed;
1781
+ const oraclePriceFormatted = this.llamalend.formatUnits(oraclePrice, 18);
1782
+ const debtFormatted = this.llamalend.formatUnits(debt, 18);
1783
+ const { _collateral: ammCollateral, _stablecoin: ammBorrowed } = yield this._userState(userAddress);
1784
+ const [ammCollateralFormatted, ammBorrowedFormatted] = [this.llamalend.formatUnits(ammCollateral, this.collateralDecimals), this.llamalend.formatUnits(ammBorrowed, 18)];
1785
+ const collateralValueUsd = BN(ammCollateralFormatted).times(oraclePriceFormatted);
1786
+ const repaidDebt = BN(totalBorrowed).minus(debtFormatted);
1787
+ const currentPosition = collateralValueUsd.plus(ammBorrowedFormatted).plus(repaidDebt);
1788
+ const currentProfit = currentPosition.minus(totalDepositUsdValueFull);
1789
+ const percentage = currentProfit.div(totalDepositUsdValueUser).times(100);
1790
+ return {
1791
+ currentPosition: currentPosition.toString(),
1792
+ deposited: totalDepositUsdValueUser.toString(),
1793
+ currentProfit: currentProfit.toString(),
1794
+ percentage: percentage.toString(),
1795
+ };
1796
+ });
1797
+ }
1743
1798
  getLlamalend() {
1744
1799
  return this.llamalend;
1745
1800
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@curvefi/llamalend-api",
3
- "version": "1.0.34",
3
+ "version": "1.0.36",
4
4
  "description": "JavaScript library for Curve Lending",
5
5
  "main": "lib/index.js",
6
6
  "author": "Macket",
@@ -397,6 +397,10 @@
397
397
  {
398
398
  "name": "callback_args",
399
399
  "type": "uint256[]"
400
+ },
401
+ {
402
+ "name": "callback_bytes",
403
+ "type": "bytes"
400
404
  }
401
405
  ],
402
406
  "name": "create_loan_extended",
@@ -448,6 +452,34 @@
448
452
  "stateMutability": "payable",
449
453
  "type": "function"
450
454
  },
455
+ {
456
+ "stateMutability": "nonpayable",
457
+ "type": "function",
458
+ "name": "borrow_more_extended",
459
+ "inputs": [
460
+ {
461
+ "name": "collateral",
462
+ "type": "uint256"
463
+ },
464
+ {
465
+ "name": "debt",
466
+ "type": "uint256"
467
+ },
468
+ {
469
+ "name": "callbacker",
470
+ "type": "address"
471
+ },
472
+ {
473
+ "name": "callback_args",
474
+ "type": "uint256[]"
475
+ },
476
+ {
477
+ "name": "callback_bytes",
478
+ "type": "bytes"
479
+ }
480
+ ],
481
+ "outputs": []
482
+ },
451
483
  {
452
484
  "inputs": [
453
485
  {
@@ -477,6 +509,10 @@
477
509
  {
478
510
  "name": "callback_args",
479
511
  "type": "uint256[]"
512
+ },
513
+ {
514
+ "name": "callback_bytes",
515
+ "type": "bytes"
480
516
  }
481
517
  ],
482
518
  "name": "repay_extended",
@@ -141,6 +141,18 @@ export const _getUserCollateralCrvUsd = memoize(
141
141
  }
142
142
  )
143
143
 
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
+
144
156
  export const _getMarketsData = memoize(
145
157
  async (network: INetworkName): Promise<IMarketData> => {
146
158
  const url = `https://api.curve.finance/api/getLendingVaults/${network}/oneway`;
@@ -21,7 +21,7 @@ import {
21
21
  DIGas,
22
22
  } from "../utils";
23
23
  import {IDict, ILlamma, TGas} from "../interfaces";
24
- import {_getUserCollateralCrvUsd} from "../external-api.js";
24
+ import {_getUserCollateralCrvUsd, _getUserCollateralCrvUsdFull} from "../external-api.js";
25
25
  import { ILeverageV2 } from "./interfaces/leverage.js";
26
26
  import { LeverageV2Module } from "./modules";
27
27
 
@@ -1871,6 +1871,72 @@ export class MintMarketTemplate {
1871
1871
  return await this._deleverageRepay(collateral, slippage, false) as string;
1872
1872
  }
1873
1873
 
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
+
1874
1940
  public getLlamalend(): Llamalend {
1875
1941
  return this.llamalend;
1876
1942
  }