@gearbox-protocol/sdk 0.0.99 → 0.0.102

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.
Files changed (86) hide show
  1. package/lib/apy/convexAPY.d.ts +8 -0
  2. package/lib/apy/convexAPY.js +202 -0
  3. package/lib/apy/lidoAPY.d.ts +4 -0
  4. package/lib/apy/lidoAPY.js +104 -0
  5. package/lib/config.d.ts +1 -0
  6. package/lib/config.js +2 -1
  7. package/lib/contracts/contracts.d.ts +6 -1
  8. package/lib/contracts/contracts.js +52 -14
  9. package/lib/contracts/contractsRegister.js +16 -4
  10. package/lib/core/constants.d.ts +6 -0
  11. package/lib/core/constants.js +10 -4
  12. package/lib/core/creditAccount.d.ts +3 -1
  13. package/lib/core/creditAccount.js +31 -31
  14. package/lib/core/creditManager.d.ts +13 -2
  15. package/lib/core/creditManager.js +79 -29
  16. package/lib/core/errors.d.ts +10 -0
  17. package/lib/core/errors.js +16 -1
  18. package/lib/core/events.js +9 -9
  19. package/lib/core/multicall.d.ts +1 -1
  20. package/lib/core/pool.d.ts +1 -1
  21. package/lib/core/pool.js +7 -5
  22. package/lib/core/price.d.ts +2 -0
  23. package/lib/core/price.js +9 -0
  24. package/lib/core/strategy.d.ts +38 -0
  25. package/lib/core/strategy.js +56 -0
  26. package/lib/index.d.ts +7 -1
  27. package/lib/index.js +8 -3
  28. package/lib/pathfinder/path.js +1 -1
  29. package/lib/pathfinder/yVault.js +5 -5
  30. package/lib/payload/creditAccount.d.ts +4 -26
  31. package/lib/payload/creditManager.d.ts +3 -25
  32. package/lib/payload/pool.d.ts +2 -17
  33. package/lib/payload/pool.js +0 -1
  34. package/lib/tokens/token.js +8 -8
  35. package/lib/utils/formatter.d.ts +1 -0
  36. package/lib/utils/formatter.js +6 -1
  37. package/lib/utils/mappers.d.ts +2 -1
  38. package/lib/utils/mappers.js +13 -5
  39. package/lib/utils/multicall.js +4 -3
  40. package/lib/utils/types.d.ts +1 -0
  41. package/package.json +1 -1
  42. package/src/apy/convexAPY.ts +250 -0
  43. package/src/apy/lidoAPY.ts +88 -0
  44. package/src/config.ts +2 -1
  45. package/src/contracts/contracts.ts +69 -16
  46. package/src/contracts/contractsRegister.ts +19 -4
  47. package/src/core/constants.ts +10 -3
  48. package/src/core/creditAccount.ts +46 -37
  49. package/src/core/creditManager.ts +231 -148
  50. package/src/core/errors.ts +24 -0
  51. package/src/core/events.ts +921 -915
  52. package/src/core/multicall.ts +1 -1
  53. package/src/core/pool.ts +56 -51
  54. package/src/core/price.ts +9 -0
  55. package/src/core/strategy.ts +130 -0
  56. package/src/index.ts +8 -1
  57. package/src/pathfinder/path.ts +154 -154
  58. package/src/pathfinder/yVault.ts +63 -62
  59. package/src/payload/creditAccount.ts +4 -26
  60. package/src/payload/creditManager.ts +4 -26
  61. package/src/payload/pool.ts +2 -19
  62. package/src/tokens/token.ts +14 -9
  63. package/src/utils/formatter.ts +5 -1
  64. package/src/utils/mappers.ts +12 -5
  65. package/src/utils/multicall.ts +4 -9
  66. package/src/utils/types.ts +6 -2
  67. package/lib/core/adapters.d.ts +0 -15
  68. package/lib/core/adapters.js +0 -19
  69. package/lib/core/contracts.d.ts +0 -67
  70. package/lib/core/contracts.js +0 -254
  71. package/lib/core/contractsRegister.d.ts +0 -2
  72. package/lib/core/contractsRegister.js +0 -58
  73. package/lib/core/creditCard.d.ts +0 -13
  74. package/lib/core/creditCard.js +0 -2
  75. package/lib/core/oracles.d.ts +0 -38
  76. package/lib/core/oracles.js +0 -12
  77. package/lib/core/priceFeeds.d.ts +0 -3
  78. package/lib/core/priceFeeds.js +0 -492
  79. package/lib/core/protocols.d.ts +0 -13
  80. package/lib/core/protocols.js +0 -39
  81. package/lib/core/swap.d.ts +0 -4
  82. package/lib/core/swap.js +0 -8
  83. package/lib/core/trade.d.ts +0 -35
  84. package/lib/core/trade.js +0 -62
  85. package/lib/core/tradeTypes.d.ts +0 -79
  86. package/lib/core/tradeTypes.js +0 -21
@@ -3,7 +3,12 @@
3
3
  * Gearbox. Generalized leverage protocol, which allows to take leverage and then use it across other DeFi protocols and platforms in a composable way.
4
4
  * (c) Gearbox.fi, 2021
5
5
  */
6
- import { keyToLowercase, objectEntries, swapKeyValue } from "../utils/mappers";
6
+ import {
7
+ keyToLowercase,
8
+ objectEntries,
9
+ swapKeyValue,
10
+ filterEmptyKeys
11
+ } from "../utils/mappers";
7
12
  import { AdapterInterface } from "./adapters";
8
13
  import { NetworkType } from "../core/constants";
9
14
  import { Protocols } from "./protocols";
@@ -122,7 +127,7 @@ export const contractsByNetwork: Record<
122
127
  CONVEX_STECRV_POOL: "0x25edfea00131DAF9b23a8E07720Eea2382d58E71",
123
128
  CONVEX_SUSD_POOL: "0xE9998a1c357dc62550f2ED72b736a7CaEB262823",
124
129
  CONVEX_FRAX3CRV_POOL: "0xEBE0A1B948eB83333906EB80eE4b87Ee0b5CC84f",
125
- CONVEX_LUSD3CRV_POOL: "0xb099D099286CB7483dbc7D62b5C91656bd15DBF0",
130
+ CONVEX_LUSD3CRV_POOL: "0x493Bd59EEcFfCa1BF88611CEd478aA5b74427e06",
126
131
  CONVEX_GUSD_POOL: "0xE7370DA7FD3d511dAB095Dd7d10388653d5cDE21",
127
132
  CONVEX_CLAIM_ZAP: "0x1330C2E5Cd8c333683aE511550f82d25918FFdd5",
128
133
 
@@ -179,10 +184,16 @@ type ConvexParams = {
179
184
  | AdapterInterface.CONVEX_V1_CLAIM_ZAP;
180
185
  } & BaseContractParams;
181
186
 
182
- type ConvexPoolParams = {
187
+ type ConvexExtraPoolParams = {
188
+ rewardToken: NormalToken;
189
+ poolAddress: Record<NetworkType, string>;
190
+ };
191
+
192
+ export type ConvexPoolParams = {
183
193
  protocol: Protocols.Convex;
184
194
  type: AdapterInterface.CONVEX_V1_BASE_REWARD_POOL;
185
195
  stakedToken: ConvexStakedPhantomToken;
196
+ extraRewards: Array<ConvexExtraPoolParams>;
186
197
  } & BaseContractParams;
187
198
 
188
199
  export type LidoParams = {
@@ -314,46 +325,85 @@ export const contractParams: Record<SupportedContract, ContractParams> = {
314
325
  protocol: Protocols.Convex,
315
326
  type: AdapterInterface.CONVEX_V1_BOOSTER
316
327
  },
328
+ CONVEX_CLAIM_ZAP: {
329
+ name: "Convex ZAP",
330
+ protocol: Protocols.Convex,
331
+ type: AdapterInterface.CONVEX_V1_CLAIM_ZAP
332
+ },
333
+
317
334
  CONVEX_3CRV_POOL: {
318
335
  name: "Convex 3crv",
319
336
  protocol: Protocols.Convex,
320
337
  type: AdapterInterface.CONVEX_V1_BASE_REWARD_POOL,
321
- stakedToken: "stkcvx3Crv"
338
+ stakedToken: "stkcvx3Crv",
339
+ extraRewards: []
322
340
  },
323
341
  CONVEX_GUSD_POOL: {
324
342
  name: "Convex GUSD",
325
343
  protocol: Protocols.Convex,
326
344
  type: AdapterInterface.CONVEX_V1_BASE_REWARD_POOL,
327
- stakedToken: "stkcvxgusd3CRV"
345
+ stakedToken: "stkcvxgusd3CRV",
346
+ extraRewards: []
328
347
  },
329
348
  CONVEX_SUSD_POOL: {
330
349
  name: "Convex SUSD",
331
350
  protocol: Protocols.Convex,
332
351
  type: AdapterInterface.CONVEX_V1_BASE_REWARD_POOL,
333
- stakedToken: "stkcvxcrvPlain3andSUSD"
352
+ stakedToken: "stkcvxcrvPlain3andSUSD",
353
+ extraRewards: [
354
+ {
355
+ rewardToken: "SNX",
356
+ poolAddress: {
357
+ Mainnet: "0x81fCe3E10D12Da6c7266a1A169c4C96813435263",
358
+ Kovan: "0x26a535146557FA58FA37e3078FEAc523b554939C"
359
+ }
360
+ }
361
+ ]
334
362
  },
335
363
  CONVEX_STECRV_POOL: {
336
364
  name: "Convex STECRV",
337
365
  protocol: Protocols.Convex,
338
366
  type: AdapterInterface.CONVEX_V1_BASE_REWARD_POOL,
339
- stakedToken: "stkcvxsteCRV"
367
+ stakedToken: "stkcvxsteCRV",
368
+ extraRewards: [
369
+ {
370
+ rewardToken: "LDO",
371
+ poolAddress: {
372
+ Mainnet: "0x008aEa5036b819B4FEAEd10b2190FBb3954981E8",
373
+ Kovan: "0xd0B1CC3B4839363b1eC92F35eF45794CB07B1183"
374
+ }
375
+ }
376
+ ]
340
377
  },
341
378
  CONVEX_FRAX3CRV_POOL: {
342
379
  name: "Convex FRAX3CRV",
343
380
  protocol: Protocols.Convex,
344
381
  type: AdapterInterface.CONVEX_V1_BASE_REWARD_POOL,
345
- stakedToken: "stkcvxFRAX3CRV"
382
+ stakedToken: "stkcvxFRAX3CRV",
383
+ extraRewards: [
384
+ {
385
+ rewardToken: "FXS",
386
+ poolAddress: {
387
+ Mainnet: "0xcDEC6714eB482f28f4889A0c122868450CDBF0b0",
388
+ Kovan: "0x89869c2e79FC6EFf51e714F1239D53702B5CDFCD"
389
+ }
390
+ }
391
+ ]
346
392
  },
347
393
  CONVEX_LUSD3CRV_POOL: {
348
394
  name: "Convex LUSD3CRV",
349
395
  protocol: Protocols.Convex,
350
396
  type: AdapterInterface.CONVEX_V1_BASE_REWARD_POOL,
351
- stakedToken: "stkcvxLUSD3CRV"
352
- },
353
- CONVEX_CLAIM_ZAP: {
354
- name: "Convex ZAP",
355
- protocol: Protocols.Convex,
356
- type: AdapterInterface.CONVEX_V1_CLAIM_ZAP
397
+ stakedToken: "stkcvxLUSD3CRV",
398
+ extraRewards: [
399
+ {
400
+ rewardToken: "LQTY",
401
+ poolAddress: {
402
+ Mainnet: "0x55d59b791f06dc519B176791c4E037E8Cf2f6361",
403
+ Kovan: ""
404
+ }
405
+ }
406
+ ]
357
407
  },
358
408
 
359
409
  LIDO_STETH_GATEWAY: {
@@ -370,6 +420,9 @@ export const contractParams: Record<SupportedContract, ContractParams> = {
370
420
 
371
421
  export const contractsByAddress = objectEntries(contractsByNetwork).reduce<
372
422
  Record<string, SupportedContract>
373
- >((sum, [_, contracts]) => {
374
- return { ...sum, ...keyToLowercase(swapKeyValue(contracts)) };
423
+ >((acc, [_, contracts]) => {
424
+ return {
425
+ ...acc,
426
+ ...filterEmptyKeys(keyToLowercase(swapKeyValue(contracts)))
427
+ };
375
428
  }, {});
@@ -27,19 +27,34 @@ export const deployedContracts: Record<string, string> = {
27
27
  "0xe04b4db67127d1930D36f16B51653120C4285708": "WBTC",
28
28
  "0x600073357c29d169aAF3E543A4519749830553F1": "WETH",
29
29
  "0xdBAd1361d9A03B81Be8D3a54Ef0dc9e39a1bA5b3": "USDC",
30
- "0x50d1fA47b0D88BA0D108148B8481b4A762eFB99e": "DAI"
30
+ "0x50d1fA47b0D88BA0D108148B8481b4A762eFB99e": "DAI",
31
31
  // [YEARN_DAI_VAULT_KOVAN_MOCK]: "Yearn DAI",
32
32
  // [YEARN_USDC_VAULT_KOVAN_MOCK]: "Yearn USDC",
33
33
  // [SUSHISWAP_KOVAN]: "Sushiswap"
34
+
35
+ "0x94cd5F09727ae04d7D6fd36A9c9DD96D0d54646B": "DAI",
36
+ "0x53B4a7389EA369a9299DfF524f916EC3cB77ffF4": "USDC",
37
+ "0xE6631f7b18744651A385a02ea2C89634d20D7819": "WETH",
38
+ "0x4d2cBA0E43Ad3B3E1C3B1922fe6E8e910b8C3ae0": "WBTC",
39
+
40
+ "0x6Ae9Ed829AF469Df9526FA7443E876bfaBC79eff": "DAI V2",
41
+ "0x8E0a874E3475de1C16490E386b7E2904CdC03086": "USDC V2",
42
+ "0x816a74B75D60839247C848518545fb17cedDC460": "WETH V2",
43
+
44
+ // pools
45
+ "0xA7b1C8f322596C40A04dBd3DffaE37a0849B8b59": "DAI",
46
+ "0xb8ecDB926F07FCbd870eafE72d116413A0BEb236": "USDC",
47
+ "0xAda6b2747295b2ba6ba52ce2d0D5e681D25D63D8": "WETH",
48
+ "0x85c54CCCf27466eEa1C74a73F75CbE385C271EE4": "WBTC"
34
49
  };
35
50
 
36
51
  const contractNames = Object.entries(contractsByAddress).reduce<
37
52
  Record<string, string>
38
- >((sum, [addr, cSymbol]) => {
53
+ >((acc, [addr, cSymbol]) => {
39
54
  const params = contractParams[cSymbol];
40
- if (!params) return sum;
55
+ if (!params) return acc;
41
56
 
42
- return { ...sum, [addr]: params.name };
57
+ return { ...acc, [addr]: params.name };
43
58
  }, {});
44
59
 
45
60
  const contractsFullList = {
@@ -22,12 +22,18 @@ export const getNetworkType = (chainId: number): NetworkType => {
22
22
  }
23
23
  };
24
24
 
25
- export const RAY = BigNumber.from(10).pow(27);
25
+ export const RAY_DECIMALS_POW = 27;
26
+ export const RAY = BigNumber.from(10).pow(RAY_DECIMALS_POW);
26
27
  export const halfRAY = RAY.div(2);
27
- export const WAD = BigNumber.from(10).pow(18);
28
+ export const WAD_DECIMALS_POW = 18;
29
+ export const WAD = BigNumber.from(10).pow(WAD_DECIMALS_POW);
30
+
31
+ export const PRICE_DECIMALS_POW = 8;
32
+ export const PRICE_DECIMALS = BigNumber.from(10).pow(PRICE_DECIMALS_POW);
28
33
 
29
34
  export const SECONDS_PER_YEAR = 365 * 24 * 3600;
30
35
 
36
+ export const PERCENTAGE_DECIMALS = 100;
31
37
  export const PERCENTAGE_FACTOR = 1e4;
32
38
  export const LIQUIDATION_DISCOUNTED_SUM = 9500;
33
39
  export const UNDERLYING_TOKEN_LIQUIDATION_THRESHOLD = 9300;
@@ -37,8 +43,9 @@ export const timeRanges: Record<string, number> = {
37
43
  "1D": 3600 * 24,
38
44
  "1W": 3600 * 24 * 7,
39
45
  "1M": 3600 * 24 * 30,
40
- "1Y": 3600 * 24 * 365,
46
+ "1Y": 3600 * 24 * 365
41
47
  };
42
48
 
43
49
  export const LEVERAGE_DECIMALS = 100;
50
+ export const SLIPPAGE_DECIMALS = 100;
44
51
  export const ADDRESS_0x0 = "0x0000000000000000000000000000000000000000";
@@ -3,8 +3,14 @@ import {
3
3
  CreditAccountDataExtendedPayload,
4
4
  CreditAccountDataPayload
5
5
  } from "../payload/creditAccount";
6
- import { PERCENTAGE_FACTOR, RAY } from "./constants";
6
+ import {
7
+ PERCENTAGE_FACTOR,
8
+ RAY,
9
+ PERCENTAGE_DECIMALS,
10
+ PRICE_DECIMALS
11
+ } from "./constants";
7
12
  import { TokenData } from "../tokens/tokenData";
13
+ import { calcTotalPrice } from "./price";
8
14
 
9
15
  export type Balance = { address: string; balance: BigNumber };
10
16
 
@@ -33,7 +39,7 @@ export class CreditAccountData {
33
39
  this.borrower = payload.borrower;
34
40
  this.inUse = payload.inUse;
35
41
  this.creditManager = payload.creditManager;
36
- this.underlyingToken = payload.underlyingToken;
42
+ this.underlyingToken = payload.underlying;
37
43
  this.borrowedAmountPlusInterest = BigNumber.from(
38
44
  payload.borrowedAmountPlusInterest
39
45
  );
@@ -43,7 +49,7 @@ export class CreditAccountData {
43
49
  this.borrowRate =
44
50
  BigNumber.from(payload.borrowRate)
45
51
  .mul(PERCENTAGE_FACTOR)
46
- .mul(100)
52
+ .mul(PERCENTAGE_DECIMALS)
47
53
  .div(RAY)
48
54
  .toNumber() / PERCENTAGE_FACTOR;
49
55
 
@@ -61,45 +67,48 @@ export class CreditAccountData {
61
67
  }
62
68
 
63
69
  balancesSorted(
64
- prices: Record<string, number>,
70
+ prices: Record<string, BigNumber>,
65
71
  tokens: Record<string, TokenData>
66
72
  ): Array<Balance> {
67
- const priceCalc = (addr: string, amount: BigNumber) => {
68
- const price = prices[addr] || 0;
69
- const { decimals = 18 } = tokens[addr] || {};
70
-
71
- return amount
72
- .mul(Math.floor(1000 * price))
73
- .div(BigNumber.from(10).pow(decimals));
74
- };
75
-
76
- const tokensAbcComparator = (t1?: TokenData, t2?: TokenData) => {
77
- const { symbol: symbol1 = "" } = t1 || {};
78
- const { symbol: symbol2 = "" } = t2 || {};
79
-
80
- return symbol1 > symbol2 ? 1 : -1;
81
- };
82
-
83
- const safeBalances = this.balances || [];
84
-
85
- return Object.entries(safeBalances)
86
- .sort(([addr1, amount1], [addr2, amount2]) => {
87
- const addr1Lc = addr1.toLowerCase();
88
- const addr2Lc = addr2.toLowerCase();
89
-
90
- const price1 = priceCalc(addr1Lc, amount1);
91
- const price2 = priceCalc(addr2Lc, amount2);
92
-
93
- return price1.eq(price2)
94
- ? tokensAbcComparator(tokens[addr1Lc], tokens[addr2Lc])
95
- : price1.gt(price2)
96
- ? -1
97
- : 1;
98
- })
99
- .map(([address, balance]) => ({ address, balance }));
73
+ return sortBalances(this.balances, prices, tokens).map(
74
+ ([address, balance]) => ({ address, balance })
75
+ );
100
76
  }
101
77
  }
102
78
 
79
+ export function sortBalances(
80
+ balances: Record<string, BigNumber>,
81
+ prices: Record<string, BigNumber>,
82
+ tokens: Record<string, TokenData>
83
+ ) {
84
+ return Object.entries(balances).sort(([addr1, amount1], [addr2, amount2]) => {
85
+ const addr1Lc = addr1.toLowerCase();
86
+ const addr2Lc = addr2.toLowerCase();
87
+
88
+ const token1 = tokens[addr1Lc];
89
+ const token2 = tokens[addr2Lc];
90
+
91
+ const price1 = prices[addr1Lc] || PRICE_DECIMALS;
92
+ const price2 = prices[addr2Lc] || PRICE_DECIMALS;
93
+
94
+ const totalPrice1 = calcTotalPrice(price1, amount1, token1?.decimals);
95
+ const totalPrice2 = calcTotalPrice(price2, amount2, token2?.decimals);
96
+
97
+ return totalPrice1.eq(totalPrice2)
98
+ ? tokensAbcComparator(token1, token2)
99
+ : totalPrice1.gt(totalPrice2)
100
+ ? -1
101
+ : 1;
102
+ });
103
+ }
104
+
105
+ export function tokensAbcComparator(t1?: TokenData, t2?: TokenData) {
106
+ const { symbol: symbol1 = "" } = t1 || {};
107
+ const { symbol: symbol2 = "" } = t2 || {};
108
+
109
+ return symbol1 > symbol2 ? 1 : -1;
110
+ }
111
+
103
112
  export class CreditAccountDataExtended extends CreditAccountData {
104
113
  public readonly repayAmount: BigNumber;
105
114
  public readonly liquidationAmount: BigNumber;