@curvefi/llamalend-api 1.0.29 → 1.0.31

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.
@@ -518,4 +518,5 @@ export declare class LendMarketTemplate {
518
518
  private leverageRepay;
519
519
  currentLeverage(userAddress?: string): Promise<string>;
520
520
  currentPnL(userAddress?: string): Promise<Record<string, string>>;
521
+ userBoost(address?: string): Promise<string>;
521
522
  }
@@ -2774,4 +2774,29 @@ export class LendMarketTemplate {
2774
2774
  };
2775
2775
  });
2776
2776
  }
2777
+ userBoost() {
2778
+ return __awaiter(this, arguments, void 0, function* (address = "") {
2779
+ if (this.addresses.gauge === this.llamalend.constants.ZERO_ADDRESS) {
2780
+ throw Error(`${this.name} doesn't have gauge`);
2781
+ }
2782
+ if (this.vaultRewardsOnly()) {
2783
+ throw Error(`${this.name} has Rewards-Only Gauge. Use stats.rewardsApy instead`);
2784
+ }
2785
+ address = _getAddress.call(this.llamalend, address);
2786
+ const gaugeContract = this.llamalend.contracts[this.addresses.gauge].multicallContract;
2787
+ const [workingBalanceBN, balanceBN] = (yield this.llamalend.multicallProvider.all([
2788
+ gaugeContract.working_balances(address),
2789
+ gaugeContract.balanceOf(address),
2790
+ ])).map((value) => toBN(value));
2791
+ if (balanceBN.isZero()) {
2792
+ return '1.0';
2793
+ }
2794
+ const boostBN = workingBalanceBN.div(0.4).div(balanceBN);
2795
+ if (boostBN.lt(1))
2796
+ return '1.0';
2797
+ if (boostBN.gt(2.5))
2798
+ return '2.5';
2799
+ return boostBN.toFixed(4).replace(/([0-9])0+$/, '$1');
2800
+ });
2801
+ }
2777
2802
  }
@@ -1,5 +1,10 @@
1
1
  import { LendMarketTemplate } from "./LendMarketTemplate.js";
2
2
  export const getLendMarket = function (lendMarketId) {
3
- const marketData = this.constants.ONE_WAY_MARKETS[lendMarketId];
4
- return new LendMarketTemplate(lendMarketId, marketData, this);
3
+ if (!(lendMarketId in this.lendMarkets)) {
4
+ const marketData = this.constants.ONE_WAY_MARKETS[lendMarketId];
5
+ if (!marketData)
6
+ throw new Error(`Lend market with id ${lendMarketId} not found`);
7
+ this.lendMarkets[lendMarketId] = new LendMarketTemplate(lendMarketId, marketData, this);
8
+ }
9
+ return this.lendMarkets[lendMarketId];
5
10
  };
@@ -1,6 +1,8 @@
1
1
  import { ethers, Networkish, BigNumberish, Numeric } from "ethers";
2
2
  import { Provider as MulticallProvider } from '@curvefi/ethcall';
3
3
  import { IChainId, ILlamalend, ILlamma, IDict, INetworkName, ICurveContract, IOneWayMarket, ICoin } from "./interfaces.js";
4
+ import { MintMarketTemplate } from "./mintMarkets";
5
+ import { LendMarketTemplate } from "./lendMarkets";
4
6
  export declare const NETWORK_CONSTANTS: {
5
7
  [index: number]: any;
6
8
  };
@@ -15,6 +17,12 @@ declare class Llamalend implements ILlamalend {
15
17
  contracts: {
16
18
  [index: string]: ICurveContract;
17
19
  };
20
+ mintMarkets: {
21
+ [addres: string]: MintMarketTemplate;
22
+ };
23
+ lendMarkets: {
24
+ [addres: string]: LendMarketTemplate;
25
+ };
18
26
  feeData: {
19
27
  gasPrice?: number;
20
28
  maxFeePerGas?: number;
package/lib/llamalend.js CHANGED
@@ -352,6 +352,8 @@ class Llamalend {
352
352
  this.chainId = 1;
353
353
  this.multicallProvider = null;
354
354
  this.contracts = {};
355
+ this.mintMarkets = {};
356
+ this.lendMarkets = {};
355
357
  this.feeData = {};
356
358
  this.constantOptions = { gasLimit: 12000000 };
357
359
  this.options = {};
@@ -1,6 +1,6 @@
1
1
  import memoize from "memoizee";
2
2
  import type { Llamalend } from "../llamalend.js";
3
- import { IDict, TGas } from "../interfaces";
3
+ import { IDict, ILlamma, TGas } from "../interfaces";
4
4
  import { ILeverageV2 } from "./interfaces/leverage.js";
5
5
  export declare class MintMarketTemplate {
6
6
  private llamalend;
@@ -132,7 +132,7 @@ export declare class MintMarketTemplate {
132
132
  repay: (collateral: number | string, slippage?: number) => Promise<number>;
133
133
  };
134
134
  };
135
- constructor(id: string, llamalend: Llamalend);
135
+ constructor(id: string, llammaData: ILlamma, llamalend: Llamalend);
136
136
  statsParameters: (() => Promise<{
137
137
  fee: string;
138
138
  admin_fee: string;
@@ -13,7 +13,7 @@ import { _getAddress, parseUnits, BN, toBN, fromBN, getBalances, ensureAllowance
13
13
  import { _getUserCollateralCrvUsd } from "../external-api.js";
14
14
  import { LeverageV2Module } from "./modules";
15
15
  export class MintMarketTemplate {
16
- constructor(id, llamalend) {
16
+ constructor(id, llammaData, llamalend) {
17
17
  var _a;
18
18
  // ---------------- STATS ----------------
19
19
  this.statsParameters = memoize(() => __awaiter(this, void 0, void 0, function* () {
@@ -237,7 +237,6 @@ export class MintMarketTemplate {
237
237
  maxAge: 5 * 60 * 1000, // 5m
238
238
  });
239
239
  this.llamalend = llamalend;
240
- const llammaData = this.llamalend.constants.LLAMMAS[id];
241
240
  this.id = id;
242
241
  this.address = llammaData.amm_address;
243
242
  this.controller = llammaData.controller_address;
@@ -1600,12 +1599,9 @@ export class MintMarketTemplate {
1600
1599
  // d_k_effective: uint256 = (1 - loan_discount) * sqrt((A-1)/A) / N
1601
1600
  // k_effective = d_k_effective * sum_{0..N-1}(((A-1) / A)**k)
1602
1601
  const { loan_discount } = yield this.statsParameters();
1603
- console.log("loan_discount", loan_discount);
1604
1602
  const A = this.A;
1605
- console.log("A", A);
1606
1603
  const A_BN = BN(A);
1607
1604
  const A_ratio_BN = A_BN.minus(1).div(A_BN);
1608
- console.log("A_ratio_BN", A_ratio_BN, A_ratio_BN.toString());
1609
1605
  const d_k_effective_BN = BN(100).minus(loan_discount).div(100).times(A_ratio_BN.sqrt()).div(N);
1610
1606
  let S = BN(0);
1611
1607
  for (let n = 0; n < N; n++) {
@@ -1,4 +1,10 @@
1
1
  import { MintMarketTemplate } from "./MintMarketTemplate";
2
2
  export const getMintMarket = function (mintMarketId) {
3
- return new MintMarketTemplate(mintMarketId, this);
3
+ if (!(mintMarketId in this.mintMarkets)) {
4
+ const llammaData = this.constants.LLAMMAS[mintMarketId];
5
+ if (!llammaData)
6
+ throw new Error(`No market with id ${mintMarketId} found`);
7
+ this.mintMarkets[mintMarketId] = new MintMarketTemplate(mintMarketId, llammaData, this);
8
+ }
9
+ return this.mintMarkets[mintMarketId];
4
10
  };
@@ -93,7 +93,7 @@ export class LeverageV2Module {
93
93
  const key = `${inputCoinAddress}-${_amount}`;
94
94
  if (!(key in this.swapDataCache))
95
95
  throw Error("You must call corresponding `expected` method first " +
96
- "(leverage.createLoanExpectedCollateral, leverage.borrowMoreExpectedCollateral or leverage.repayExpectedBorrowed)");
96
+ "(leverageV2.createLoanExpectedCollateral, leverageV2.borrowMoreExpectedCollateral or leverageV2.repayExpectedBorrowed)");
97
97
  return this.swapDataCache[key];
98
98
  };
99
99
  this._leverageExpectedCollateral = (userCollateral, userBorrowed, debt, user) => __awaiter(this, void 0, void 0, function* () {
@@ -471,7 +471,7 @@ export class LeverageV2Module {
471
471
  const _debt = parseUnits(debt, this.market.coinDecimals[0]);
472
472
  const swapData = this._getSwapDataFromCache(this.market.coinAddresses[0], _debt + _userBorrowed);
473
473
  if (slippage !== swapData.slippage)
474
- throw Error(`You must call leverage.createLoanExpectedCollateral() with slippage=${slippage} first`);
474
+ throw Error(`You must call leverageV2.createLoanExpectedCollateral() with slippage=${slippage} first`);
475
475
  const calldata = yield _assembleTxOdos.call(this.llamalend, swapData.pathId);
476
476
  const contract = this.llamalend.contracts[this.market.controller].contract;
477
477
  const gas = yield contract.create_loan_extended.estimateGas(_userCollateral, _debt, range, this.llamalend.constants.ALIASES.leverage_zap, [1, parseUnits(this._getMarketId(), 0), _userBorrowed], calldata, Object.assign({}, this.llamalend.constantOptions));
@@ -616,7 +616,7 @@ export class LeverageV2Module {
616
616
  const _debt = parseUnits(debt, this.market.coinDecimals[0]);
617
617
  const swapData = this._getSwapDataFromCache(this.market.coinAddresses[0], _debt + _userBorrowed);
618
618
  if (slippage !== swapData.slippage)
619
- throw Error(`You must call leverage.borrowMoreExpectedCollateral() with slippage=${slippage} first`);
619
+ throw Error(`You must call leverageV2.borrowMoreExpectedCollateral() with slippage=${slippage} first`);
620
620
  const calldata = yield _assembleTxOdos.call(this.llamalend, swapData.pathId);
621
621
  const contract = this.llamalend.contracts[this.market.controller].contract;
622
622
  const gas = yield contract.borrow_more_extended.estimateGas(_userCollateral, _debt, this.llamalend.constants.ALIASES.leverage_zap, [1, parseUnits(this._getMarketId(), 0), _userBorrowed], calldata, Object.assign({}, this.llamalend.constantOptions));
@@ -757,7 +757,7 @@ export class LeverageV2Module {
757
757
  if (_stateCollateral + _userCollateral > BigInt(0)) {
758
758
  const swapData = this._getSwapDataFromCache(this.market.coinAddresses[1], _stateCollateral + _userCollateral);
759
759
  if (slippage !== swapData.slippage)
760
- throw Error(`You must call leverage.repayExpectedBorrowed() with slippage=${slippage} first`);
760
+ throw Error(`You must call leverageV2.repayExpectedBorrowed() with slippage=${slippage} first`);
761
761
  calldata = yield _assembleTxOdos.call(this.llamalend, swapData.pathId);
762
762
  }
763
763
  console.log('params', [1, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed], calldata);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@curvefi/llamalend-api",
3
- "version": "1.0.29",
3
+ "version": "1.0.31",
4
4
  "description": "JavaScript library for Curve Lending",
5
5
  "main": "lib/index.js",
6
6
  "author": "Macket",
@@ -3134,4 +3134,30 @@ export class LendMarketTemplate {
3134
3134
  percentage: percentage.toFixed(2).toString(),
3135
3135
  };
3136
3136
  }
3137
+
3138
+ public async userBoost(address = ""): Promise<string> {
3139
+ if (this.addresses.gauge === this.llamalend.constants.ZERO_ADDRESS) {
3140
+ throw Error(`${this.name} doesn't have gauge`);
3141
+ }
3142
+ if (this.vaultRewardsOnly()) {
3143
+ throw Error(`${this.name} has Rewards-Only Gauge. Use stats.rewardsApy instead`);
3144
+ }
3145
+ address = _getAddress.call(this.llamalend, address);
3146
+
3147
+ const gaugeContract = this.llamalend.contracts[this.addresses.gauge].multicallContract;
3148
+ const [workingBalanceBN, balanceBN] = (await this.llamalend.multicallProvider.all([
3149
+ gaugeContract.working_balances(address),
3150
+ gaugeContract.balanceOf(address),
3151
+ ]) as bigint[]).map((value: bigint) => toBN(value));
3152
+
3153
+ if (balanceBN.isZero()) {
3154
+ return '1.0';
3155
+ }
3156
+
3157
+ const boostBN = workingBalanceBN.div(0.4).div(balanceBN);
3158
+ if (boostBN.lt(1)) return '1.0';
3159
+ if (boostBN.gt(2.5)) return '2.5';
3160
+
3161
+ return boostBN.toFixed(4).replace(/([0-9])0+$/, '$1');
3162
+ }
3137
3163
  }
@@ -2,6 +2,10 @@ import { LendMarketTemplate} from "./LendMarketTemplate.js";
2
2
  import type { Llamalend } from "../llamalend.js";
3
3
 
4
4
  export const getLendMarket = function (this: Llamalend, lendMarketId: string): LendMarketTemplate {
5
- const marketData = this.constants.ONE_WAY_MARKETS[lendMarketId];
6
- return new LendMarketTemplate(lendMarketId, marketData, this)
5
+ if (!(lendMarketId in this.lendMarkets)) {
6
+ const marketData = this.constants.ONE_WAY_MARKETS[lendMarketId];
7
+ if (!marketData) throw new Error(`Lend market with id ${lendMarketId} not found`);
8
+ this.lendMarkets[lendMarketId] = new LendMarketTemplate(lendMarketId, marketData, this);
9
+ }
10
+ return this.lendMarkets[lendMarketId];
7
11
  }
package/src/llamalend.ts CHANGED
@@ -81,6 +81,8 @@ import {createCall, handleMultiCallResponse} from "./utils.js";
81
81
  import {cacheKey, cacheStats} from "./cache/index.js";
82
82
  import {_getMarketsData, _getHiddenPools} from "./external-api.js";
83
83
  import {extractDecimals} from "./constants/utils.js";
84
+ import {MintMarketTemplate} from "./mintMarkets";
85
+ import {LendMarketTemplate} from "./lendMarkets";
84
86
 
85
87
  export const NETWORK_CONSTANTS: { [index: number]: any } = {
86
88
  1: {
@@ -177,6 +179,8 @@ class Llamalend implements ILlamalend {
177
179
  signerAddress: string;
178
180
  chainId: IChainId;
179
181
  contracts: { [index: string]: ICurveContract };
182
+ mintMarkets: { [addres: string]: MintMarketTemplate };
183
+ lendMarkets: { [addres: string]: LendMarketTemplate };
180
184
  feeData: { gasPrice?: number, maxFeePerGas?: number, maxPriorityFeePerGas?: number };
181
185
  constantOptions: { gasLimit: number };
182
186
  options: { gasPrice?: number | bigint, maxFeePerGas?: number | bigint, maxPriorityFeePerGas?: number | bigint };
@@ -204,6 +208,8 @@ class Llamalend implements ILlamalend {
204
208
  this.chainId = 1;
205
209
  this.multicallProvider = null as unknown as MulticallProvider;
206
210
  this.contracts = {};
211
+ this.mintMarkets = {};
212
+ this.lendMarkets = {};
207
213
  this.feeData = {}
208
214
  this.constantOptions = { gasLimit: 12000000 }
209
215
  this.options = {};
@@ -20,7 +20,7 @@ import {
20
20
  _mulBy1_3,
21
21
  DIGas,
22
22
  } from "../utils";
23
- import {IDict, TGas} from "../interfaces";
23
+ import {IDict, ILlamma, TGas} from "../interfaces";
24
24
  import {_getUserCollateralCrvUsd} from "../external-api.js";
25
25
  import { ILeverageV2 } from "./interfaces/leverage.js";
26
26
  import { LeverageV2Module } from "./modules";
@@ -126,10 +126,8 @@ export class MintMarketTemplate {
126
126
  }
127
127
  }
128
128
 
129
- constructor(id: string, llamalend: Llamalend) {
129
+ constructor(id: string, llammaData: ILlamma, llamalend: Llamalend) {
130
130
  this.llamalend = llamalend;
131
- const llammaData = this.llamalend.constants.LLAMMAS[id];
132
-
133
131
  this.id = id;
134
132
  this.address = llammaData.amm_address;
135
133
  this.controller = llammaData.controller_address;
@@ -1694,12 +1692,9 @@ export class MintMarketTemplate {
1694
1692
  // d_k_effective: uint256 = (1 - loan_discount) * sqrt((A-1)/A) / N
1695
1693
  // k_effective = d_k_effective * sum_{0..N-1}(((A-1) / A)**k)
1696
1694
  const { loan_discount } = await this.statsParameters();
1697
- console.log("loan_discount", loan_discount);
1698
1695
  const A = this.A;
1699
- console.log("A", A);
1700
1696
  const A_BN = BN(A);
1701
1697
  const A_ratio_BN = A_BN.minus(1).div(A_BN);
1702
- console.log("A_ratio_BN", A_ratio_BN, A_ratio_BN.toString());
1703
1698
 
1704
1699
  const d_k_effective_BN = BN(100).minus(loan_discount).div(100).times(A_ratio_BN.sqrt()).div(N);
1705
1700
  let S = BN(0);
@@ -2,5 +2,10 @@ import { MintMarketTemplate} from "./MintMarketTemplate";
2
2
  import type { Llamalend } from "../llamalend.js";
3
3
 
4
4
  export const getMintMarket = function (this: Llamalend, mintMarketId: string): MintMarketTemplate {
5
- return new MintMarketTemplate(mintMarketId, this)
5
+ if (!(mintMarketId in this.mintMarkets)) {
6
+ const llammaData = this.constants.LLAMMAS[mintMarketId];
7
+ if (!llammaData) throw new Error(`No market with id ${mintMarketId} found`);
8
+ this.mintMarkets[mintMarketId] = new MintMarketTemplate(mintMarketId, llammaData, this)
9
+ }
10
+ return this.mintMarkets[mintMarketId]
6
11
  }
@@ -238,9 +238,8 @@ export class LeverageV2Module {
238
238
  const key = `${inputCoinAddress}-${_amount}`;
239
239
  if (!(key in this.swapDataCache)) throw Error(
240
240
  "You must call corresponding `expected` method first " +
241
- "(leverage.createLoanExpectedCollateral, leverage.borrowMoreExpectedCollateral or leverage.repayExpectedBorrowed)"
241
+ "(leverageV2.createLoanExpectedCollateral, leverageV2.borrowMoreExpectedCollateral or leverageV2.repayExpectedBorrowed)"
242
242
  );
243
-
244
243
  return this.swapDataCache[key]
245
244
  }
246
245
 
@@ -494,7 +493,7 @@ export class LeverageV2Module {
494
493
  const _userBorrowed = parseUnits(userBorrowed, this.market.coinDecimals[0]);
495
494
  const _debt = parseUnits(debt, this.market.coinDecimals[0]);
496
495
  const swapData = this._getSwapDataFromCache(this.market.coinAddresses[0], _debt + _userBorrowed);
497
- if (slippage !== swapData.slippage) throw Error(`You must call leverage.createLoanExpectedCollateral() with slippage=${slippage} first`);
496
+ if (slippage !== swapData.slippage) throw Error(`You must call leverageV2.createLoanExpectedCollateral() with slippage=${slippage} first`);
498
497
  const calldata = await _assembleTxOdos.call(this.llamalend, swapData.pathId as string);
499
498
  const contract = this.llamalend.contracts[this.market.controller].contract;
500
499
  const gas = await contract.create_loan_extended.estimateGas(
@@ -666,7 +665,7 @@ export class LeverageV2Module {
666
665
  const _userBorrowed = parseUnits(userBorrowed, this.market.coinDecimals[0]);
667
666
  const _debt = parseUnits(debt, this.market.coinDecimals[0]);
668
667
  const swapData = this._getSwapDataFromCache(this.market.coinAddresses[0], _debt + _userBorrowed);
669
- if (slippage !== swapData.slippage) throw Error(`You must call leverage.borrowMoreExpectedCollateral() with slippage=${slippage} first`)
668
+ if (slippage !== swapData.slippage) throw Error(`You must call leverageV2.borrowMoreExpectedCollateral() with slippage=${slippage} first`)
670
669
  const calldata = await _assembleTxOdos.call(this.llamalend, swapData.pathId as string);
671
670
  const contract = this.llamalend.contracts[this.market.controller].contract;
672
671
  const gas = await contract.borrow_more_extended.estimateGas(
@@ -894,7 +893,7 @@ export class LeverageV2Module {
894
893
  let calldata = "0x";
895
894
  if (_stateCollateral + _userCollateral > BigInt(0)) {
896
895
  const swapData = this._getSwapDataFromCache(this.market.coinAddresses[1], _stateCollateral + _userCollateral);
897
- if (slippage !== swapData.slippage) throw Error(`You must call leverage.repayExpectedBorrowed() with slippage=${slippage} first`)
896
+ if (slippage !== swapData.slippage) throw Error(`You must call leverageV2.repayExpectedBorrowed() with slippage=${slippage} first`)
898
897
  calldata = await _assembleTxOdos.call(this.llamalend, swapData.pathId as string);
899
898
  }
900
899