@gearbox-protocol/sdk 0.0.101 → 0.0.104

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 (139) hide show
  1. package/.eslintignore +5 -0
  2. package/.eslintrc.js +52 -0
  3. package/.husky/pre-push +4 -0
  4. package/lib/apy/convexAPY.js +7 -9
  5. package/lib/apy/lidoAPY.js +35 -29
  6. package/lib/contracts/contracts.d.ts +5 -2
  7. package/lib/contracts/contracts.js +19 -18
  8. package/lib/contracts/contractsRegister.js +13 -1
  9. package/lib/core/constants.d.ts +5 -3
  10. package/lib/core/constants.js +8 -6
  11. package/lib/core/creditAccount.d.ts +2 -2
  12. package/lib/core/creditAccount.js +12 -11
  13. package/lib/core/creditManager.d.ts +1 -1
  14. package/lib/core/creditManager.js +4 -10
  15. package/lib/core/creditSession.js +14 -3
  16. package/lib/core/eventOrTx.d.ts +1 -1
  17. package/lib/core/events.d.ts +19 -19
  18. package/lib/core/events.js +23 -21
  19. package/lib/core/pool.d.ts +1 -1
  20. package/lib/core/pool.js +1 -7
  21. package/lib/core/price.d.ts +1 -3
  22. package/lib/core/price.js +9 -7
  23. package/lib/core/strategy.d.ts +8 -6
  24. package/lib/core/strategy.js +25 -25
  25. package/lib/core/tokenDistributor.js +1 -1
  26. package/lib/core/transactions.d.ts +18 -3
  27. package/lib/core/transactions.js +39 -3
  28. package/lib/index.d.ts +8 -2
  29. package/lib/index.js +8 -1
  30. package/lib/oracles/priceFeeds.js +1 -1
  31. package/lib/pathfinder/convexLP.d.ts +1 -1
  32. package/lib/pathfinder/convexLP.js +26 -29
  33. package/lib/pathfinder/curveLP.d.ts +1 -1
  34. package/lib/pathfinder/curveLP.js +5 -7
  35. package/lib/pathfinder/path.d.ts +1 -1
  36. package/lib/pathfinder/path.js +38 -42
  37. package/lib/pathfinder/trade.d.ts +1 -2
  38. package/lib/pathfinder/tradeTypes.d.ts +5 -5
  39. package/lib/pathfinder/yVault.d.ts +2 -2
  40. package/lib/pathfinder/yVault.js +22 -22
  41. package/lib/payload/creditAccount.d.ts +4 -4
  42. package/lib/payload/creditManager.d.ts +3 -13
  43. package/lib/payload/pool.d.ts +2 -2
  44. package/lib/strategies/convex.d.ts +12 -0
  45. package/lib/strategies/convex.js +74 -1
  46. package/lib/strategies/creditFacade.d.ts +1 -0
  47. package/lib/strategies/creditFacade.js +3 -0
  48. package/lib/strategies/curve.d.ts +15 -60
  49. package/lib/strategies/curve.js +74 -1
  50. package/lib/strategies/lido.d.ts +6 -0
  51. package/lib/strategies/lido.js +23 -1
  52. package/lib/strategies/uniswapV2.d.ts +1 -0
  53. package/lib/strategies/uniswapV2.js +6 -19
  54. package/lib/strategies/uniswapV3.d.ts +1 -0
  55. package/lib/strategies/uniswapV3.js +4 -1
  56. package/lib/strategies/yearn.d.ts +8 -0
  57. package/lib/strategies/yearn.js +92 -12
  58. package/lib/tokens/convex.d.ts +3 -3
  59. package/lib/tokens/curveLP.d.ts +7 -2
  60. package/lib/tokens/curveLP.js +8 -1
  61. package/lib/tokens/gear.d.ts +2 -2
  62. package/lib/tokens/gear.js +1 -1
  63. package/lib/tokens/normal.d.ts +1 -1
  64. package/lib/tokens/token.js +8 -8
  65. package/lib/tokens/tokenData.d.ts +3 -1
  66. package/lib/tokens/tokenData.js +10 -8
  67. package/lib/tokens/yearn.d.ts +6 -2
  68. package/lib/tokens/yearn.js +6 -0
  69. package/lib/utils/errors.d.ts +6 -0
  70. package/lib/utils/errors.js +13 -0
  71. package/lib/utils/formatter.d.ts +1 -1
  72. package/lib/utils/formatter.js +17 -14
  73. package/lib/utils/loading.d.ts +2 -1
  74. package/lib/utils/loading.js +9 -13
  75. package/lib/utils/mappers.js +2 -2
  76. package/lib/utils/network.js +2 -2
  77. package/lib/utils/repeater.js +12 -24
  78. package/lib/utils/validate.js +1 -1
  79. package/package.json +24 -7
  80. package/src/apy/convexAPY.ts +13 -12
  81. package/src/apy/lidoAPY.ts +35 -27
  82. package/src/contracts/contracts.ts +27 -17
  83. package/src/contracts/contractsRegister.ts +16 -1
  84. package/src/core/constants.ts +8 -5
  85. package/src/core/creditAccount.ts +42 -16
  86. package/src/core/creditManager.ts +33 -7
  87. package/src/core/creditOperation.ts +7 -7
  88. package/src/core/creditSession.ts +25 -5
  89. package/src/core/errors.ts +1 -0
  90. package/src/core/eventOrTx.ts +8 -5
  91. package/src/core/events.ts +85 -24
  92. package/src/core/history.ts +46 -46
  93. package/src/core/operations.ts +6 -0
  94. package/src/core/pool.ts +16 -4
  95. package/src/core/price.ts +10 -13
  96. package/src/core/strategy.ts +54 -43
  97. package/src/core/tokenDistributor.ts +2 -2
  98. package/src/core/transactions.ts +427 -350
  99. package/src/index.ts +10 -3
  100. package/src/oracles/priceFeeds.ts +523 -523
  101. package/src/pathfinder/contracts.ts +15 -13
  102. package/src/pathfinder/convexLP.ts +29 -25
  103. package/src/pathfinder/curveLP.ts +57 -53
  104. package/src/pathfinder/path.ts +17 -9
  105. package/src/pathfinder/priority.ts +11 -11
  106. package/src/pathfinder/trade.ts +84 -77
  107. package/src/pathfinder/tradeTypes.ts +98 -94
  108. package/src/pathfinder/yVault.ts +32 -17
  109. package/src/payload/creditAccount.ts +4 -4
  110. package/src/payload/creditManager.ts +3 -13
  111. package/src/payload/pool.ts +2 -2
  112. package/src/payload/token.ts +3 -3
  113. package/src/strategies/convex.ts +360 -186
  114. package/src/strategies/creditFacade.ts +74 -53
  115. package/src/strategies/curve.ts +400 -189
  116. package/src/strategies/lido.ts +70 -32
  117. package/src/strategies/uniswapV2.ts +93 -111
  118. package/src/strategies/uniswapV3.ts +118 -91
  119. package/src/strategies/yearn.ts +187 -60
  120. package/src/tokens/connectors.ts +6 -6
  121. package/src/tokens/convex.ts +297 -296
  122. package/src/tokens/curveLP.ts +176 -165
  123. package/src/tokens/gear.ts +47 -45
  124. package/src/tokens/normal.ts +801 -802
  125. package/src/tokens/token.ts +13 -9
  126. package/src/tokens/tokenData.ts +19 -9
  127. package/src/tokens/tokenType.ts +11 -11
  128. package/src/tokens/yearn.ts +130 -124
  129. package/src/utils/errors.ts +11 -0
  130. package/src/utils/formatter.ts +22 -18
  131. package/src/utils/loading.ts +14 -6
  132. package/src/utils/mappers.ts +8 -6
  133. package/src/utils/multicall.ts +2 -0
  134. package/src/utils/network.ts +21 -21
  135. package/src/utils/repeater.ts +2 -2
  136. package/src/utils/validate.ts +1 -1
  137. package/lib/utils/events.d.ts +0 -2
  138. package/lib/utils/events.js +0 -13
  139. package/src/utils/events.ts +0 -10
@@ -3,6 +3,7 @@
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 type { YearnLPToken } from "src/tokens/yearn";
6
7
  import {
7
8
  keyToLowercase,
8
9
  objectEntries,
@@ -14,7 +15,7 @@ import { NetworkType } from "../core/constants";
14
15
  import { Protocols } from "./protocols";
15
16
  import { tokenDataByNetwork } from "../tokens/token";
16
17
  import { ConvexStakedPhantomToken } from "../tokens/convex";
17
- import { CurveLPToken } from "../tokens/curveLP";
18
+ import type { CurveLPToken } from "../tokens/curveLP";
18
19
  import { NormalToken } from "../tokens/normal";
19
20
 
20
21
  export type UniswapV2Contract = "UNISWAP_V2_ROUTER" | "SUSHISWAP_ROUTER";
@@ -169,12 +170,14 @@ export type CurveSteCRVPoolParams = {
169
170
  protocol: Protocols.Curve;
170
171
  type: AdapterInterface.CURVE_V1_STECRV_POOL;
171
172
  pool: Record<NetworkType, string>;
173
+ tokens: ["WETH", "STETH"];
172
174
  lpToken: "steCRV";
173
175
  } & BaseContractParams;
174
176
 
175
- type YearnParams = {
177
+ export type YearnParams = {
176
178
  protocol: Protocols.Yearn;
177
179
  type: AdapterInterface.YEARN_V2;
180
+ shareToken: YearnLPToken;
178
181
  } & BaseContractParams;
179
182
 
180
183
  type ConvexParams = {
@@ -247,6 +250,7 @@ export const contractParams: Record<SupportedContract, ContractParams> = {
247
250
  Mainnet: "0xDC24316b9AE028F1497c275EB9192a3Ea0f67022",
248
251
  Kovan: "0xF5C73b58B70709e89aA1D322d48b0D0C71123cB4"
249
252
  },
253
+ tokens: ["WETH", "STETH"],
250
254
  lpToken: "steCRV"
251
255
  },
252
256
  CURVE_FRAX_POOL: {
@@ -254,14 +258,14 @@ export const contractParams: Record<SupportedContract, ContractParams> = {
254
258
  protocol: Protocols.Curve,
255
259
  type: AdapterInterface.CURVE_V1_2ASSETS,
256
260
  lpToken: "FRAX3CRV",
257
- tokens: ["3Crv", "FRAX"]
261
+ tokens: ["FRAX", "3Crv"]
258
262
  },
259
263
  CURVE_LUSD_POOL: {
260
264
  name: "Curve LUSD",
261
265
  protocol: Protocols.Curve,
262
266
  type: AdapterInterface.CURVE_V1_2ASSETS,
263
267
  lpToken: "LUSD3CRV",
264
- tokens: ["3Crv", "LUSD"]
268
+ tokens: ["LUSD", "3Crv"]
265
269
  },
266
270
  CURVE_SUSD_POOL: {
267
271
  name: "Curve SUSD",
@@ -277,8 +281,7 @@ export const contractParams: Record<SupportedContract, ContractParams> = {
277
281
  protocol: Protocols.Curve,
278
282
  type: AdapterInterface.CURVE_V1_WRAPPER,
279
283
  lpToken: "crvPlain3andSUSD",
280
- tokens: ["DAI", "USDC", "USDT", "sUSD"],
281
- wrapper: "CURVE_SUSD_DEPOSIT"
284
+ tokens: ["DAI", "USDC", "USDT", "sUSD"]
282
285
  },
283
286
 
284
287
  CURVE_GUSD_POOL: {
@@ -286,38 +289,44 @@ export const contractParams: Record<SupportedContract, ContractParams> = {
286
289
  protocol: Protocols.Curve,
287
290
  type: AdapterInterface.CURVE_V1_2ASSETS,
288
291
  lpToken: "gusd3CRV",
289
- tokens: ["3Crv", "FRAX"]
292
+ tokens: ["GUSD", "3Crv"]
290
293
  },
291
294
 
292
295
  YEARN_DAI_VAULT: {
293
296
  name: "Yearn DAI",
294
297
  protocol: Protocols.Yearn,
295
- type: AdapterInterface.YEARN_V2
298
+ type: AdapterInterface.YEARN_V2,
299
+ shareToken: "yvDAI"
296
300
  },
297
301
  YEARN_USDC_VAULT: {
298
302
  name: "Yearn USDC",
299
303
  protocol: Protocols.Yearn,
300
- type: AdapterInterface.YEARN_V2
304
+ type: AdapterInterface.YEARN_V2,
305
+ shareToken: "yvUSDC"
301
306
  },
302
307
  YEARN_WETH_VAULT: {
303
308
  name: "Yearn WETH",
304
309
  protocol: Protocols.Yearn,
305
- type: AdapterInterface.YEARN_V2
310
+ type: AdapterInterface.YEARN_V2,
311
+ shareToken: "yvWETH"
306
312
  },
307
313
  YEARN_WBTC_VAULT: {
308
314
  name: "Yearn WBTC",
309
315
  protocol: Protocols.Yearn,
310
- type: AdapterInterface.YEARN_V2
316
+ type: AdapterInterface.YEARN_V2,
317
+ shareToken: "yvWBTC"
311
318
  },
312
319
  YEARN_CURVE_FRAX_VAULT: {
313
320
  name: "Yearn Curve FRAX",
314
321
  protocol: Protocols.Yearn,
315
- type: AdapterInterface.YEARN_V2
322
+ type: AdapterInterface.YEARN_V2,
323
+ shareToken: "yvCurve_FRAX"
316
324
  },
317
325
  YEARN_CURVE_STETH_VAULT: {
318
326
  name: "Yearn Curve STETH",
319
327
  protocol: Protocols.Yearn,
320
- type: AdapterInterface.YEARN_V2
328
+ type: AdapterInterface.YEARN_V2,
329
+ shareToken: "yvCurve_stETH"
321
330
  },
322
331
 
323
332
  CONVEX_BOOSTER: {
@@ -420,9 +429,10 @@ export const contractParams: Record<SupportedContract, ContractParams> = {
420
429
 
421
430
  export const contractsByAddress = objectEntries(contractsByNetwork).reduce<
422
431
  Record<string, SupportedContract>
423
- >((acc, [_, contracts]) => {
424
- return {
432
+ >(
433
+ (acc, [, contracts]) => ({
425
434
  ...acc,
426
435
  ...filterEmptyKeys(keyToLowercase(swapKeyValue(contracts)))
427
- };
428
- }, {});
436
+ }),
437
+ {}
438
+ );
@@ -27,10 +27,25 @@ 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<
@@ -22,11 +22,14 @@ export const getNetworkType = (chainId: number): NetworkType => {
22
22
  }
23
23
  };
24
24
 
25
- export const RAY_DECIMALS = 27;
26
- export const RAY = BigNumber.from(10).pow(RAY_DECIMALS);
25
+ export const RAY_DECIMALS_POW = 27;
26
+ export const RAY = BigNumber.from(10).pow(RAY_DECIMALS_POW);
27
27
  export const halfRAY = RAY.div(2);
28
- export const WAD_DECIMALS = 18;
29
- export const WAD = BigNumber.from(10).pow(WAD_DECIMALS);
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);
30
33
 
31
34
  export const SECONDS_PER_YEAR = 365 * 24 * 3600;
32
35
 
@@ -45,4 +48,4 @@ export const timeRanges: Record<string, number> = {
45
48
 
46
49
  export const LEVERAGE_DECIMALS = 100;
47
50
  export const SLIPPAGE_DECIMALS = 100;
48
- export const ADDRESS_0x0 = "0x0000000000000000000000000000000000000000";
51
+ export const ADDRESS_0X0 = "0x0000000000000000000000000000000000000000";
@@ -3,9 +3,14 @@ import {
3
3
  CreditAccountDataExtendedPayload,
4
4
  CreditAccountDataPayload
5
5
  } from "../payload/creditAccount";
6
- import { PERCENTAGE_FACTOR, RAY, PERCENTAGE_DECIMALS } 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";
8
- import { priceCalc } from "./price";
13
+ import { calcTotalPrice } from "./price";
9
14
 
10
15
  export type Balance = { address: string; balance: BigNumber };
11
16
 
@@ -13,19 +18,33 @@ export class CreditAccountData {
13
18
  public readonly id: string;
14
19
 
15
20
  public readonly addr: string;
21
+
16
22
  public readonly borrower: string;
23
+
17
24
  public readonly inUse: boolean;
25
+
18
26
  public readonly creditManager: string;
27
+
19
28
  public readonly underlyingToken: string;
29
+
20
30
  public borrowedAmountPlusInterest: BigNumber;
31
+
21
32
  public totalValue: BigNumber;
33
+
22
34
  public healthFactor: number;
35
+
23
36
  public borrowRate: number;
37
+
24
38
  public readonly allowedTokens: Array<string> = [];
39
+
25
40
  public readonly allTokens: Array<string> = [];
41
+
26
42
  public balances: Record<string, BigNumber> = {};
43
+
27
44
  public allBalances: Record<string, BigNumber> = {};
45
+
28
46
  public isDeleting: boolean;
47
+
29
48
  public readonly version: number = 1;
30
49
 
31
50
  constructor(payload: CreditAccountDataPayload) {
@@ -62,12 +81,10 @@ export class CreditAccountData {
62
81
  }
63
82
 
64
83
  balancesSorted(
65
- prices: Record<string, number>,
84
+ prices: Record<string, BigNumber>,
66
85
  tokens: Record<string, TokenData>
67
86
  ): Array<Balance> {
68
- const safeBalances = this.balances || [];
69
-
70
- return sortBalances(safeBalances, prices, tokens).map(
87
+ return sortBalances(this.balances, prices, tokens).map(
71
88
  ([address, balance]) => ({ address, balance })
72
89
  );
73
90
  }
@@ -75,7 +92,7 @@ export class CreditAccountData {
75
92
 
76
93
  export function sortBalances(
77
94
  balances: Record<string, BigNumber>,
78
- prices: Record<string, number>,
95
+ prices: Record<string, BigNumber>,
79
96
  tokens: Record<string, TokenData>
80
97
  ) {
81
98
  return Object.entries(balances).sort(([addr1, amount1], [addr2, amount2]) => {
@@ -85,17 +102,21 @@ export function sortBalances(
85
102
  const token1 = tokens[addr1Lc];
86
103
  const token2 = tokens[addr2Lc];
87
104
 
88
- const price1 = prices[addr1Lc] || 1;
89
- const price2 = prices[addr2Lc] || 1;
105
+ const price1 = prices[addr1Lc] || PRICE_DECIMALS;
106
+ const price2 = prices[addr2Lc] || PRICE_DECIMALS;
107
+
108
+ const totalPrice1 = calcTotalPrice(price1, amount1, token1?.decimals);
109
+ const totalPrice2 = calcTotalPrice(price2, amount2, token2?.decimals);
110
+
111
+ if (totalPrice1.eq(totalPrice2)) {
112
+ return tokensAbcComparator(token1, token2);
113
+ }
90
114
 
91
- const assetValue1 = priceCalc(price1, amount1, token1);
92
- const assetValue2 = priceCalc(price2, amount2, token2);
115
+ if (totalPrice1.gt(totalPrice2)) {
116
+ return -1;
117
+ }
93
118
 
94
- return assetValue1.eq(assetValue2)
95
- ? tokensAbcComparator(token1, token2)
96
- : assetValue1.gt(assetValue2)
97
- ? -1
98
- : 1;
119
+ return 1;
99
120
  });
100
121
  }
101
122
 
@@ -108,10 +129,15 @@ export function tokensAbcComparator(t1?: TokenData, t2?: TokenData) {
108
129
 
109
130
  export class CreditAccountDataExtended extends CreditAccountData {
110
131
  public readonly repayAmount: BigNumber;
132
+
111
133
  public readonly liquidationAmount: BigNumber;
134
+
112
135
  public readonly borrowedAmount: BigNumber;
136
+
113
137
  public readonly canBeClosed: boolean;
138
+
114
139
  public readonly cumulativeIndexAtOpen: BigNumber;
140
+
115
141
  public readonly since: number;
116
142
 
117
143
  constructor(payload: CreditAccountDataExtendedPayload) {
@@ -22,26 +22,45 @@ import { OpenAccountError } from "./errors";
22
22
 
23
23
  export class CreditManagerData {
24
24
  public readonly id: string;
25
+
25
26
  public readonly address: string;
27
+
26
28
  public readonly underlyingToken: string;
29
+
27
30
  public readonly isWETH: boolean;
31
+
28
32
  public readonly canBorrow: boolean;
33
+
29
34
  public readonly borrowRate: number;
35
+
30
36
  public readonly minAmount: BigNumber;
37
+
31
38
  public readonly maxAmount: BigNumber;
39
+
32
40
  public readonly maxLeverageFactor: number; // for V1 only
41
+
33
42
  public readonly availableLiquidity: BigNumber;
43
+
34
44
  public readonly allowedTokens: Array<string>;
45
+
35
46
  public readonly adapters: Record<string, string>;
36
47
 
37
48
  public readonly liquidationThresholds: Record<string, BigNumber>;
49
+
38
50
  public readonly version: number;
51
+
39
52
  public readonly creditFacade: string; // V2 only: address of creditFacade
53
+
40
54
  public readonly isDegenMode: boolean; // V2 only: true if contract is in Degen mode
55
+
41
56
  public readonly degenNFT: string; // V2 only: degenNFT, address(0) if not in degen mode
57
+
42
58
  public readonly isIncreaseDebtForbidden: boolean; // V2 only: true if increasing debt is forbidden
59
+
43
60
  public readonly forbiddenTokenMask: BigNumber; // V2 only: mask which forbids some particular tokens
44
61
 
62
+ public readonly isPaused: boolean = false;
63
+
45
64
  constructor(payload: CreditManagerDataPayload) {
46
65
  this.id = payload.addr;
47
66
  this.address = payload.addr;
@@ -74,16 +93,17 @@ export class CreditManagerData {
74
93
  {}
75
94
  );
76
95
 
77
- this.liquidationThresholds = payload.liquidationThresholds.reduce<
96
+ this.liquidationThresholds = (payload.liquidationThresholds || []).reduce<
78
97
  Record<string, BigNumber>
79
98
  >((acc, threshold, index) => {
80
99
  const address = payload.collateralTokens[index];
81
100
 
82
- if (address) acc[address.toLowerCase()] = threshold;
101
+ if (address) acc[address.toLowerCase()] = BigNumber.from(threshold);
83
102
 
84
103
  return acc;
85
104
  }, {});
86
- this.version = payload.version || 1;
105
+
106
+ this.version = BigNumber.from(payload.version || 1).toNumber();
87
107
  this.creditFacade = payload.creditFacade || "";
88
108
  this.isDegenMode = payload.isDegenMode || false;
89
109
  this.degenNFT = payload.degenNFT || "";
@@ -91,10 +111,6 @@ export class CreditManagerData {
91
111
  this.forbiddenTokenMask = BigNumber.from(payload.forbiddenTokenMask || 0);
92
112
  }
93
113
 
94
- get isPaused(): boolean {
95
- return false;
96
- }
97
-
98
114
  getContractETH(signer: Signer | ethers.providers.Provider): ICreditManager {
99
115
  return ICreditManager__factory.connect(this.address, signer);
100
116
  }
@@ -228,15 +244,25 @@ export function calcHealthFactorAfterAddingCollateral(
228
244
 
229
245
  export class CreditManagerStat extends CreditManagerData {
230
246
  public readonly uniqueUsers: number;
247
+
231
248
  public readonly openedAccountsCount: number;
249
+
232
250
  public readonly totalOpenedAccounts: number;
251
+
233
252
  public readonly totalClosedAccounts: number;
253
+
234
254
  public readonly totalRepaidAccounts: number;
255
+
235
256
  public readonly totalLiquidatedAccounts: number;
257
+
236
258
  public readonly totalBorrowed: BigNumber;
259
+
237
260
  public readonly cumulativeBorrowed: BigNumber;
261
+
238
262
  public readonly totalRepaid: BigNumber;
263
+
239
264
  public readonly totalProfit: BigNumber;
265
+
240
266
  public readonly totalLosses: BigNumber;
241
267
 
242
268
  constructor(payload: CreditManagerStatPayload) {
@@ -1,9 +1,9 @@
1
1
  export interface CreditOperation {
2
- id: number,
3
- txHash: string,
4
- blockNum: number,
5
- protocol: string,
6
- operation: string,
7
- timestamp: number,
8
- date: string
2
+ id: number;
3
+ txHash: string;
4
+ blockNum: number;
5
+ protocol: string;
6
+ operation: string;
7
+ timestamp: number;
8
+ date: string;
9
9
  }
@@ -1,7 +1,7 @@
1
1
  import { BigNumber } from "ethers";
2
+ import moment from "moment";
2
3
  import { CreditOperation } from "./creditOperation";
3
4
  import { CreditSessionPayload } from "../payload/creditSession";
4
- import moment from "moment";
5
5
  import { PERCENTAGE_FACTOR } from "./constants";
6
6
 
7
7
  export type CreditSessionStatus = "active" | "closed" | "repaid" | "liquidated";
@@ -10,29 +10,46 @@ const statusEnum: Array<CreditSessionStatus> = [
10
10
  "active",
11
11
  "closed",
12
12
  "repaid",
13
- "liquidated",
13
+ "liquidated"
14
14
  ];
15
15
 
16
16
  export class CreditSession {
17
17
  public readonly id: string;
18
18
 
19
19
  public readonly status: CreditSessionStatus;
20
+
20
21
  public readonly name: string;
22
+
21
23
  public readonly background: string;
24
+
22
25
  public readonly borrower: string;
26
+
23
27
  public readonly creditManager: string;
28
+
24
29
  public readonly account: string;
30
+
25
31
  public readonly since: number;
32
+
26
33
  public readonly sinceDate: string;
34
+
27
35
  public readonly closedAt: number;
36
+
28
37
  public readonly closedAtDate: string;
38
+
29
39
  public readonly initialAmount: BigNumber;
40
+
30
41
  public readonly borrowedAmount: BigNumber;
42
+
31
43
  public readonly totalValue: BigNumber;
44
+
32
45
  public readonly healthFactor: number;
46
+
33
47
  public readonly profit: BigNumber;
48
+
34
49
  public readonly profitPercentage: number;
50
+
35
51
  public readonly score: number;
52
+
36
53
  public readonly operations: Array<CreditOperation>;
37
54
 
38
55
  constructor(payload: CreditSessionPayload) {
@@ -53,9 +70,12 @@ export class CreditSession {
53
70
  this.healthFactor =
54
71
  BigNumber.from(payload.healthFactor || 0).toNumber() / PERCENTAGE_FACTOR;
55
72
  this.score = payload.score;
56
- this.operations = (payload.operations || []).map((op) => {
57
- op.date = moment(op.timestamp * 1000).format("Do MMM YYYY");
58
- return op;
73
+ this.operations = (payload.operations || []).map(op => {
74
+ const formattedOp = {
75
+ ...op,
76
+ date: moment(op.timestamp * 1000).format("Do MMM YYYY")
77
+ };
78
+ return formattedOp;
59
79
  });
60
80
  this.sinceDate = moment(payload.sinceTimestamp * 1000).format(
61
81
  "Do MMM YYYY"
@@ -54,6 +54,7 @@ export type OpenAccountErrorTypes =
54
54
 
55
55
  export class OpenAccountError extends Error {
56
56
  message: OpenAccountErrorTypes;
57
+
57
58
  payload: { amount: BigNumber };
58
59
 
59
60
  constructor(errorType: OpenAccountErrorTypes, amount: BigNumber) {
@@ -1,5 +1,5 @@
1
1
  import { TokenData } from "../tokens/tokenData";
2
- import { TxSerialized } from "./transactions";
2
+ import type { TxSerialized } from "./transactions";
3
3
 
4
4
  export interface Display {
5
5
  toString(tokenData: Record<string, TokenData>): string;
@@ -16,8 +16,11 @@ export interface EventOrTxProps {
16
16
 
17
17
  export abstract class EventOrTx implements Display {
18
18
  public block: number;
19
+
19
20
  public readonly txHash: string;
21
+
20
22
  public readonly timestamp: number;
23
+
21
24
  protected _txStatus: TxStatus;
22
25
 
23
26
  constructor({ block, txHash, txStatus, timestamp = 0 }: EventOrTxProps) {
@@ -70,10 +73,10 @@ export abstract class EVMTx extends EventOrTx {
70
73
  timestamp = 0
71
74
  }: EVMTxProps) {
72
75
  super({
73
- block: block,
74
- txStatus: txStatus,
75
- txHash: txHash,
76
- timestamp: timestamp
76
+ block,
77
+ txStatus,
78
+ txHash,
79
+ timestamp
77
80
  });
78
81
  if (this.txStatus !== "pending" && this.block === 0) {
79
82
  throw new Error("Block not specified for non-pending tx");