@gearbox-protocol/sdk 0.0.100 → 0.0.103

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 (163) hide show
  1. package/.eslintignore +4 -0
  2. package/.eslintrc.js +52 -0
  3. package/.husky/pre-push +4 -0
  4. package/lib/apy/convexAPY.d.ts +8 -0
  5. package/lib/apy/convexAPY.js +200 -0
  6. package/lib/apy/lidoAPY.d.ts +4 -0
  7. package/lib/apy/lidoAPY.js +103 -0
  8. package/lib/config.d.ts +1 -0
  9. package/lib/config.js +2 -1
  10. package/lib/contracts/contracts.d.ts +9 -4
  11. package/lib/contracts/contracts.js +52 -14
  12. package/lib/contracts/contractsRegister.js +16 -4
  13. package/lib/core/constants.d.ts +7 -1
  14. package/lib/core/constants.js +11 -5
  15. package/lib/core/creditAccount.d.ts +3 -1
  16. package/lib/core/creditAccount.js +33 -31
  17. package/lib/core/creditManager.d.ts +13 -2
  18. package/lib/core/creditManager.js +77 -33
  19. package/lib/core/creditSession.js +14 -3
  20. package/lib/core/errors.d.ts +10 -0
  21. package/lib/core/errors.js +16 -1
  22. package/lib/core/eventOrTx.d.ts +1 -1
  23. package/lib/core/events.d.ts +19 -19
  24. package/lib/core/events.js +32 -30
  25. package/lib/core/multicall.d.ts +1 -1
  26. package/lib/core/pool.d.ts +2 -2
  27. package/lib/core/pool.js +8 -12
  28. package/lib/core/price.d.ts +2 -0
  29. package/lib/core/price.js +14 -0
  30. package/lib/core/strategy.d.ts +36 -0
  31. package/lib/core/strategy.js +57 -0
  32. package/lib/core/tokenDistributor.js +1 -1
  33. package/lib/core/transactions.d.ts +18 -3
  34. package/lib/core/transactions.js +39 -3
  35. package/lib/index.d.ts +8 -3
  36. package/lib/index.js +9 -4
  37. package/lib/oracles/priceFeeds.js +1 -1
  38. package/lib/pathfinder/convexLP.d.ts +1 -1
  39. package/lib/pathfinder/convexLP.js +26 -29
  40. package/lib/pathfinder/curveLP.d.ts +1 -1
  41. package/lib/pathfinder/curveLP.js +5 -7
  42. package/lib/pathfinder/path.d.ts +1 -1
  43. package/lib/pathfinder/path.js +38 -42
  44. package/lib/pathfinder/trade.d.ts +1 -2
  45. package/lib/pathfinder/tradeTypes.d.ts +5 -5
  46. package/lib/pathfinder/yVault.d.ts +2 -2
  47. package/lib/pathfinder/yVault.js +22 -19
  48. package/lib/payload/creditAccount.d.ts +4 -26
  49. package/lib/payload/creditManager.d.ts +3 -25
  50. package/lib/payload/pool.d.ts +2 -17
  51. package/lib/payload/pool.js +0 -1
  52. package/lib/strategies/curve.d.ts +6 -60
  53. package/lib/strategies/curve.js +6 -0
  54. package/lib/strategies/uniswapV2.js +3 -19
  55. package/lib/strategies/uniswapV3.js +1 -1
  56. package/lib/strategies/yearn.js +15 -11
  57. package/lib/tokens/convex.d.ts +3 -3
  58. package/lib/tokens/curveLP.d.ts +2 -2
  59. package/lib/tokens/curveLP.js +1 -1
  60. package/lib/tokens/gear.d.ts +2 -2
  61. package/lib/tokens/gear.js +1 -1
  62. package/lib/tokens/normal.d.ts +1 -1
  63. package/lib/tokens/token.js +9 -9
  64. package/lib/tokens/tokenData.d.ts +3 -1
  65. package/lib/tokens/tokenData.js +10 -8
  66. package/lib/tokens/yearn.d.ts +3 -3
  67. package/lib/utils/errors.d.ts +6 -0
  68. package/lib/utils/errors.js +13 -0
  69. package/lib/utils/formatter.d.ts +2 -1
  70. package/lib/utils/formatter.js +23 -15
  71. package/lib/utils/loading.d.ts +2 -1
  72. package/lib/utils/loading.js +9 -13
  73. package/lib/utils/mappers.d.ts +2 -1
  74. package/lib/utils/mappers.js +13 -5
  75. package/lib/utils/multicall.js +4 -3
  76. package/lib/utils/network.js +2 -2
  77. package/lib/utils/repeater.js +12 -24
  78. package/lib/utils/types.d.ts +1 -0
  79. package/lib/utils/validate.js +1 -1
  80. package/package.json +26 -8
  81. package/src/apy/convexAPY.ts +250 -0
  82. package/src/apy/lidoAPY.ts +89 -0
  83. package/src/config.ts +2 -1
  84. package/src/contracts/contracts.ts +73 -19
  85. package/src/contracts/contractsRegister.ts +19 -4
  86. package/src/core/constants.ts +11 -4
  87. package/src/core/creditAccount.ts +69 -37
  88. package/src/core/creditManager.ts +256 -148
  89. package/src/core/creditOperation.ts +7 -7
  90. package/src/core/creditSession.ts +25 -5
  91. package/src/core/errors.ts +25 -0
  92. package/src/core/eventOrTx.ts +8 -5
  93. package/src/core/events.ts +978 -911
  94. package/src/core/history.ts +46 -46
  95. package/src/core/multicall.ts +1 -1
  96. package/src/core/operations.ts +6 -0
  97. package/src/core/pool.ts +75 -58
  98. package/src/core/price.ts +13 -0
  99. package/src/core/strategy.ts +134 -0
  100. package/src/core/tokenDistributor.ts +2 -2
  101. package/src/core/transactions.ts +427 -350
  102. package/src/index.ts +9 -4
  103. package/src/oracles/priceFeeds.ts +523 -523
  104. package/src/pathfinder/contracts.ts +15 -13
  105. package/src/pathfinder/convexLP.ts +29 -25
  106. package/src/pathfinder/curveLP.ts +57 -53
  107. package/src/pathfinder/path.ts +158 -150
  108. package/src/pathfinder/priority.ts +11 -11
  109. package/src/pathfinder/trade.ts +84 -77
  110. package/src/pathfinder/tradeTypes.ts +98 -94
  111. package/src/pathfinder/yVault.ts +79 -63
  112. package/src/payload/creditAccount.ts +4 -26
  113. package/src/payload/creditManager.ts +4 -26
  114. package/src/payload/pool.ts +2 -19
  115. package/src/payload/token.ts +3 -3
  116. package/src/strategies/convex.ts +217 -210
  117. package/src/strategies/creditFacade.ts +70 -53
  118. package/src/strategies/curve.ts +263 -194
  119. package/src/strategies/lido.ts +33 -37
  120. package/src/strategies/uniswapV2.ts +90 -112
  121. package/src/strategies/uniswapV3.ts +114 -91
  122. package/src/strategies/yearn.ts +58 -62
  123. package/src/tokens/connectors.ts +6 -6
  124. package/src/tokens/convex.ts +297 -296
  125. package/src/tokens/curveLP.ts +170 -165
  126. package/src/tokens/gear.ts +47 -45
  127. package/src/tokens/normal.ts +801 -802
  128. package/src/tokens/token.ts +19 -10
  129. package/src/tokens/tokenData.ts +19 -9
  130. package/src/tokens/tokenType.ts +11 -11
  131. package/src/tokens/yearn.ts +126 -124
  132. package/src/utils/errors.ts +11 -0
  133. package/src/utils/formatter.ts +26 -18
  134. package/src/utils/loading.ts +14 -6
  135. package/src/utils/mappers.ts +15 -6
  136. package/src/utils/multicall.ts +6 -9
  137. package/src/utils/network.ts +21 -21
  138. package/src/utils/repeater.ts +2 -2
  139. package/src/utils/types.ts +6 -2
  140. package/src/utils/validate.ts +1 -1
  141. package/lib/core/adapters.d.ts +0 -15
  142. package/lib/core/adapters.js +0 -19
  143. package/lib/core/contracts.d.ts +0 -67
  144. package/lib/core/contracts.js +0 -254
  145. package/lib/core/contractsRegister.d.ts +0 -2
  146. package/lib/core/contractsRegister.js +0 -58
  147. package/lib/core/creditCard.d.ts +0 -13
  148. package/lib/core/creditCard.js +0 -2
  149. package/lib/core/oracles.d.ts +0 -38
  150. package/lib/core/oracles.js +0 -12
  151. package/lib/core/priceFeeds.d.ts +0 -3
  152. package/lib/core/priceFeeds.js +0 -492
  153. package/lib/core/protocols.d.ts +0 -13
  154. package/lib/core/protocols.js +0 -39
  155. package/lib/core/swap.d.ts +0 -4
  156. package/lib/core/swap.js +0 -8
  157. package/lib/core/trade.d.ts +0 -35
  158. package/lib/core/trade.js +0 -62
  159. package/lib/core/tradeTypes.d.ts +0 -79
  160. package/lib/core/tradeTypes.js +0 -21
  161. package/lib/utils/events.d.ts +0 -2
  162. package/lib/utils/events.js +0 -13
  163. package/src/utils/events.ts +0 -10
@@ -1,4 +1,9 @@
1
- import { keyToLowercase, objectEntries, swapKeyValue } from "../utils/mappers";
1
+ import {
2
+ keyToLowercase,
3
+ objectEntries,
4
+ swapKeyValue,
5
+ filterEmptyKeys
6
+ } from "../utils/mappers";
2
7
  import { NetworkType } from "../core/constants";
3
8
  import { NormalToken, NormalTokenData, normalTokens } from "./normal";
4
9
  import {
@@ -134,7 +139,7 @@ export const tokenDataByNetwork: Record<
134
139
  yvCurve_stETH: "0xdCD90C7f6324cfa40d7169ef80b12031770B4325",
135
140
  yvCurve_FRAX: "0xB4AdA607B9d6b2c9Ee07A275e9616B84AC560139",
136
141
 
137
- //GEARBOX
142
+ // GEARBOX
138
143
  dDAI: "0x6CFaF95457d7688022FC53e7AbE052ef8DFBbdBA",
139
144
  dUSDC: "0xc411dB5f5Eb3f7d552F9B8454B2D74097ccdE6E3",
140
145
  dWBTC: "0xe753260F1955e8678DCeA8887759e07aa57E8c54",
@@ -212,11 +217,11 @@ export const tokenDataByNetwork: Record<
212
217
  stkcvxsteCRV: "0xd8bAbB7EfBF7F931a69B209f71b27421F8b18516",
213
218
  stkcvxcrvPlain3andSUSD: "0x6b45Cab756B41a7204973b71362ef5F73E67F6DA",
214
219
 
215
- //GEARBOX
216
- dDAI: "0x0d06D2e31781A0585feE334F63beF43543496e5d",
217
- dUSDC: "0xcCc83353856A79c94EaDa39Df76B9c7C4c762dBF",
218
- dWBTC: "0x4cCb374472C8c73b067c8c40b9420a221Ef014ca",
219
- dWETH: "0x3B1e0Db6C960b32beEDf82af9db2da63F15Ba01E",
220
+ // GEARBOX
221
+ dDAI: "0x077a3Ce0D572b72436F6644793Fc7721353aF1f4",
222
+ dUSDC: "0x8147e00456c8A3128182730bFFdFd9D1E6bbC048",
223
+ dWBTC: "0x34D9B3c13B25632879B2DaabBde702F612902238",
224
+ dWETH: "0x19e4F905749D3b487CA4927b54e55b64625a1143",
220
225
 
221
226
  GEAR: "0xe01c5d0297c56e992dab3886057a1441485ff7c7"
222
227
  }
@@ -224,6 +229,10 @@ export const tokenDataByNetwork: Record<
224
229
 
225
230
  export const tokenSymbolByAddress = objectEntries(tokenDataByNetwork).reduce<
226
231
  Record<string, SupportedToken>
227
- >((sum, [_, tokens]) => {
228
- return { ...sum, ...keyToLowercase(swapKeyValue(tokens)) };
229
- }, {});
232
+ >(
233
+ (acc, [, tokens]) => ({
234
+ ...acc,
235
+ ...filterEmptyKeys(keyToLowercase(swapKeyValue(tokens)))
236
+ }),
237
+ {}
238
+ );
@@ -4,22 +4,32 @@ import { STATIC_TOKEN } from "../config";
4
4
  import { NetworkType } from "../core/constants";
5
5
  import { SupportedToken, tokenDataByNetwork } from "./token";
6
6
 
7
+ type SymbolReplacements = Record<string, string>;
8
+ const defaultSymbolReplacement: SymbolReplacements = {
9
+ dWETH: "dETH",
10
+ WETH: "ETH"
11
+ };
12
+
7
13
  export class TokenData {
8
14
  public readonly id: string;
15
+
9
16
  // public readonly name: string;
10
17
  public readonly symbol: string;
18
+
11
19
  public readonly address: string;
20
+
12
21
  public readonly decimals: number;
22
+
13
23
  public readonly icon?: string;
14
24
 
15
- constructor(payload: TokenDataPayload) {
25
+ constructor(
26
+ payload: TokenDataPayload,
27
+ symbolReplacements: SymbolReplacements = defaultSymbolReplacement
28
+ ) {
16
29
  this.id = payload.addr;
17
30
  this.address = payload.addr;
18
31
  // this.name = payload.name;
19
- this.symbol = payload.symbol;
20
- if (this.symbol === "WETH" || this.symbol === "dWETH") {
21
- this.symbol = this.symbol.replace("WETH", "ETH");
22
- }
32
+ this.symbol = symbolReplacements[payload.symbol] || payload.symbol;
23
33
  this.decimals = payload.decimals;
24
34
  this.icon = `${STATIC_TOKEN}${payload.symbol?.toLowerCase()}.svg`;
25
35
  }
@@ -41,7 +51,7 @@ export interface TokenAllowance {
41
51
 
42
52
  export const WETHToken: Record<NetworkType, string> = {
43
53
  Mainnet: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
44
- Kovan: "0xd0a1e359811322d97991e03f863a0c30c2cf029c",
54
+ Kovan: "0xd0a1e359811322d97991e03f863a0c30c2cf029c"
45
55
  };
46
56
 
47
57
  export const connectors: Record<NetworkType, Array<SupportedToken>> = {
@@ -51,7 +61,7 @@ export const connectors: Record<NetworkType, Array<SupportedToken>> = {
51
61
  "DAI",
52
62
  "USDC",
53
63
  // "USDT",
54
- "WBTC",
64
+ "WBTC"
55
65
  // "stETH",
56
66
  // "PAX",
57
67
  // "TUSD",
@@ -59,11 +69,11 @@ export const connectors: Record<NetworkType, Array<SupportedToken>> = {
59
69
  // "BAL",
60
70
  // "sUSD",
61
71
  ],
62
- Kovan: ["WETH", "DAI", "USDC", "WBTC"],
72
+ Kovan: ["WETH", "DAI", "USDC", "WBTC"]
63
73
  };
64
74
 
65
75
  export function getConnectors(networkType: NetworkType) {
66
- return connectors[networkType].map((e) => {
76
+ return connectors[networkType].map(e => {
67
77
  const result =
68
78
  e === "WETH"
69
79
  ? WETHToken[networkType]
@@ -1,12 +1,12 @@
1
1
  export enum TokenType {
2
- CONNECTOR,
3
- NORMAL_TOKEN,
4
- CURVE_LP,
5
- META_CURVE_LP,
6
- YEARN_VAULT,
7
- YEARN_VAULT_OF_CURVE_LP,
8
- YEARN_VAULT_OF_META_CURVE_LP,
9
- CONVEX_LP_TOKEN,
10
- CONVEX_STAKED_PHANTOM_TOKEN,
11
- DIESEL_LP_TOKEN
12
- }
2
+ CONNECTOR,
3
+ NORMAL_TOKEN,
4
+ CURVE_LP,
5
+ META_CURVE_LP,
6
+ YEARN_VAULT,
7
+ YEARN_VAULT_OF_CURVE_LP,
8
+ YEARN_VAULT_OF_META_CURVE_LP,
9
+ CONVEX_LP_TOKEN,
10
+ CONVEX_STAKED_PHANTOM_TOKEN,
11
+ DIESEL_LP_TOKEN
12
+ }
@@ -1,137 +1,139 @@
1
- import {TradeAction, TradeType} from "../pathfinder/tradeTypes";
2
- import {TokenBase} from "./token";
3
- import {CurveLPToken} from "./curveLP";
4
- import {NormalToken} from "./normal";
5
- import {TokenType} from "./tokenType";
1
+ import { TradeAction, TradeType } from "../pathfinder/tradeTypes";
2
+ import type { TokenBase } from "./token";
3
+ import type { CurveLPToken } from "./curveLP";
4
+ import type { NormalToken } from "./normal";
5
+ import { TokenType } from "./tokenType";
6
6
 
7
7
  export type YearnLPToken =
8
- | "yvDAI"
9
- | "yvUSDC"
10
- | "yvWETH"
11
- | "yvWBTC"
12
- | "yvCurve_stETH"
13
- | "yvCurve_FRAX";
8
+ | "yvDAI"
9
+ | "yvUSDC"
10
+ | "yvWETH"
11
+ | "yvWBTC"
12
+ | "yvCurve_stETH"
13
+ | "yvCurve_FRAX";
14
14
 
15
15
  export type YearnVaultTokenData = {
16
- symbol: YearnLPToken;
17
- type: TokenType.YEARN_VAULT;
18
- underlying: NormalToken;
19
- lpActions: Array<TradeAction>;
16
+ symbol: YearnLPToken;
17
+ type: TokenType.YEARN_VAULT;
18
+ underlying: NormalToken;
19
+ lpActions: Array<TradeAction>;
20
20
  } & TokenBase;
21
21
 
22
22
  export type YearnVaultOfCurveLPTokenData = {
23
- symbol: YearnLPToken;
24
- type: TokenType.YEARN_VAULT_OF_CURVE_LP;
25
- underlying: CurveLPToken;
26
- lpActions: Array<TradeAction>;
23
+ symbol: YearnLPToken;
24
+ type: TokenType.YEARN_VAULT_OF_CURVE_LP;
25
+ underlying: CurveLPToken;
26
+ lpActions: Array<TradeAction>;
27
27
  } & TokenBase;
28
28
 
29
29
  export type YearnVaultOfMetaCurveLPTokenData = {
30
- symbol: YearnLPToken;
31
- type: TokenType.YEARN_VAULT_OF_META_CURVE_LP;
32
- underlying: CurveLPToken;
33
- lpActions: Array<TradeAction>;
30
+ symbol: YearnLPToken;
31
+ type: TokenType.YEARN_VAULT_OF_META_CURVE_LP;
32
+ underlying: CurveLPToken;
33
+ lpActions: Array<TradeAction>;
34
34
  } & TokenBase;
35
35
 
36
- export const yearnTokens: Record<YearnLPToken,
37
- | YearnVaultTokenData
38
- | YearnVaultOfCurveLPTokenData
39
- | YearnVaultOfMetaCurveLPTokenData> = {
40
- // YEARN TOKENS
41
- yvDAI: {
42
- name: "yvDAI",
43
- decimals: 18,
44
-
45
- symbol: "yvDAI",
46
- type: TokenType.YEARN_VAULT,
47
- underlying: "DAI",
48
- lpActions: [
49
- {
50
- type: TradeType.YearnWithdraw,
51
- contract: "YEARN_DAI_VAULT",
52
- tokenOut: "DAI"
53
- }
54
- ]
55
- },
56
-
57
- yvUSDC: {
58
- name: "yvUSDC",
59
- decimals: 6,
60
-
61
- symbol: "yvUSDC",
62
- type: TokenType.YEARN_VAULT,
63
- underlying: "USDC",
64
- lpActions: [
65
- {
66
- type: TradeType.YearnWithdraw,
67
- contract: "YEARN_USDC_VAULT",
68
- tokenOut: "USDC"
69
- }
70
- ]
71
- },
72
-
73
- yvWETH: {
74
- name: "yvWETH",
75
- decimals: 18,
76
-
77
- symbol: "yvWETH",
78
- type: TokenType.YEARN_VAULT,
79
- underlying: "WETH",
80
- lpActions: [
81
- {
82
- type: TradeType.YearnWithdraw,
83
- contract: "YEARN_WETH_VAULT",
84
- tokenOut: "WETH"
85
- }
86
- ]
87
- },
88
-
89
- yvWBTC: {
90
- name: "yvWBTC",
91
- decimals: 8,
92
-
93
- symbol: "yvWBTC",
94
- type: TokenType.YEARN_VAULT,
95
- underlying: "WBTC",
96
- lpActions: [
97
- {
98
- type: TradeType.YearnWithdraw,
99
- contract: "YEARN_WBTC_VAULT",
100
- tokenOut: "WBTC"
101
- }
102
- ]
103
- },
104
-
105
- // YEARN- CURVE TOKENS
106
- yvCurve_stETH: {
107
- name: "yvCurve-stETH",
108
- decimals: 18,
109
-
110
- symbol: "yvCurve_stETH",
111
- type: TokenType.YEARN_VAULT_OF_CURVE_LP,
112
- underlying: "steCRV",
113
- lpActions: [
114
- {
115
- type: TradeType.YearnWithdraw,
116
- contract: "YEARN_CURVE_STETH_VAULT",
117
- tokenOut: "steCRV"
118
- }
119
- ]
120
- },
121
-
122
- yvCurve_FRAX: {
123
- name: "yvCurve-FRAX",
124
- decimals: 18,
125
-
126
- symbol: "yvCurve_FRAX",
127
- type: TokenType.YEARN_VAULT_OF_META_CURVE_LP,
128
- underlying: "FRAX3CRV",
129
- lpActions: [
130
- {
131
- type: TradeType.YearnWithdraw,
132
- contract: "YEARN_CURVE_FRAX_VAULT",
133
- tokenOut: "FRAX3CRV"
134
- }
135
- ]
136
- }
36
+ export const yearnTokens: Record<
37
+ YearnLPToken,
38
+ | YearnVaultTokenData
39
+ | YearnVaultOfCurveLPTokenData
40
+ | YearnVaultOfMetaCurveLPTokenData
41
+ > = {
42
+ // YEARN TOKENS
43
+ yvDAI: {
44
+ name: "yvDAI",
45
+ decimals: 18,
46
+
47
+ symbol: "yvDAI",
48
+ type: TokenType.YEARN_VAULT,
49
+ underlying: "DAI",
50
+ lpActions: [
51
+ {
52
+ type: TradeType.YearnWithdraw,
53
+ contract: "YEARN_DAI_VAULT",
54
+ tokenOut: "DAI"
55
+ }
56
+ ]
57
+ },
58
+
59
+ yvUSDC: {
60
+ name: "yvUSDC",
61
+ decimals: 6,
62
+
63
+ symbol: "yvUSDC",
64
+ type: TokenType.YEARN_VAULT,
65
+ underlying: "USDC",
66
+ lpActions: [
67
+ {
68
+ type: TradeType.YearnWithdraw,
69
+ contract: "YEARN_USDC_VAULT",
70
+ tokenOut: "USDC"
71
+ }
72
+ ]
73
+ },
74
+
75
+ yvWETH: {
76
+ name: "yvWETH",
77
+ decimals: 18,
78
+
79
+ symbol: "yvWETH",
80
+ type: TokenType.YEARN_VAULT,
81
+ underlying: "WETH",
82
+ lpActions: [
83
+ {
84
+ type: TradeType.YearnWithdraw,
85
+ contract: "YEARN_WETH_VAULT",
86
+ tokenOut: "WETH"
87
+ }
88
+ ]
89
+ },
90
+
91
+ yvWBTC: {
92
+ name: "yvWBTC",
93
+ decimals: 8,
94
+
95
+ symbol: "yvWBTC",
96
+ type: TokenType.YEARN_VAULT,
97
+ underlying: "WBTC",
98
+ lpActions: [
99
+ {
100
+ type: TradeType.YearnWithdraw,
101
+ contract: "YEARN_WBTC_VAULT",
102
+ tokenOut: "WBTC"
103
+ }
104
+ ]
105
+ },
106
+
107
+ // YEARN- CURVE TOKENS
108
+ yvCurve_stETH: {
109
+ name: "yvCurve-stETH",
110
+ decimals: 18,
111
+
112
+ symbol: "yvCurve_stETH",
113
+ type: TokenType.YEARN_VAULT_OF_CURVE_LP,
114
+ underlying: "steCRV",
115
+ lpActions: [
116
+ {
117
+ type: TradeType.YearnWithdraw,
118
+ contract: "YEARN_CURVE_STETH_VAULT",
119
+ tokenOut: "steCRV"
120
+ }
121
+ ]
122
+ },
123
+
124
+ yvCurve_FRAX: {
125
+ name: "yvCurve-FRAX",
126
+ decimals: 18,
127
+
128
+ symbol: "yvCurve_FRAX",
129
+ type: TokenType.YEARN_VAULT_OF_META_CURVE_LP,
130
+ underlying: "FRAX3CRV",
131
+ lpActions: [
132
+ {
133
+ type: TradeType.YearnWithdraw,
134
+ contract: "YEARN_CURVE_FRAX_VAULT",
135
+ tokenOut: "FRAX3CRV"
136
+ }
137
+ ]
138
+ }
137
139
  };
@@ -0,0 +1,11 @@
1
+ interface IMetamaskError {
2
+ code: number;
3
+ message: string;
4
+ }
5
+
6
+ export const isMetamaskError = (e: any): e is IMetamaskError => {
7
+ if (!e) return false;
8
+ if (typeof e !== "object") return false;
9
+ if (typeof e.code !== "number" || typeof e.message !== "string") return false;
10
+ return true;
11
+ };
@@ -1,6 +1,6 @@
1
1
  import { BigNumber, BigNumberish } from "ethers";
2
- import { PERCENTAGE_FACTOR, RAY } from "../core/constants";
3
2
  import Decimal from "decimal.js-light";
3
+ import { PERCENTAGE_FACTOR, RAY, LEVERAGE_DECIMALS } from "../core/constants";
4
4
 
5
5
  export function rayToNumber(num: BigNumberish): number {
6
6
  return (
@@ -13,10 +13,13 @@ export function formatRAY(num?: BigNumber): string {
13
13
  }
14
14
 
15
15
  export function formatBN(
16
- num: BigNumberish | undefined,
16
+ numArg: BigNumberish | undefined,
17
17
  decimals: number,
18
- precision?: number
18
+ precisionArg?: number
19
19
  ): string {
20
+ let num = numArg;
21
+ let precision = precisionArg;
22
+
20
23
  if (!num) return "-";
21
24
 
22
25
  // if (BigNumber.from(num).gt(BigNumber.from(10).pow(37))) {
@@ -64,23 +67,24 @@ export function formatBn4dig(num: BigNumber, precision: number = 2): string {
64
67
  let numStr = num.toString();
65
68
  if (numStr.length < 6) numStr = "0".repeat(6 - numStr.length) + numStr;
66
69
  return numStr.length <= 6
67
- ? "0." + numStr.slice(0, precision)
68
- : numStr.slice(0, numStr.length - 6) +
69
- "." +
70
- numStr.slice(numStr.length - 6, numStr.length - 6 + precision);
70
+ ? `0.${numStr.slice(0, precision)}`
71
+ : `${numStr.slice(0, numStr.length - 6)}.${numStr.slice(
72
+ numStr.length - 6,
73
+ numStr.length - 6 + precision
74
+ )}`;
71
75
  }
72
76
 
73
77
  export function toHumanFormat(num: BigNumber, precision: number = 2): string {
74
78
  if (num.gte(1e15)) {
75
- return formatBn4dig(num.div(1e9), precision) + "Bn";
79
+ return `${formatBn4dig(num.div(1e9), precision)}Bn`;
76
80
  }
77
81
 
78
82
  if (num.gte(1e12)) {
79
- return formatBn4dig(num.div(1e6), precision) + "M";
83
+ return `${formatBn4dig(num.div(1e6), precision)}M`;
80
84
  }
81
85
 
82
86
  if (num.gte(1e9)) {
83
- return formatBn4dig(num.div(1e3), precision) + "K";
87
+ return `${formatBn4dig(num.div(1e3), precision)}K`;
84
88
  }
85
89
 
86
90
  return formatBn4dig(num, precision);
@@ -112,23 +116,23 @@ export function shortHash(address?: string): string {
112
116
 
113
117
  export const formatRate = (rate: BigNumberish | undefined) =>
114
118
  rate
115
- ? (
119
+ ? `${(
116
120
  BigNumber.from(rate)
117
121
  .mul(PERCENTAGE_FACTOR)
118
122
  .mul(100)
119
123
  .div(RAY)
120
124
  .toNumber() / PERCENTAGE_FACTOR
121
- ).toFixed(2) + "%"
125
+ ).toFixed(2)}%`
122
126
  : "0.00%";
123
127
 
124
128
  export function formatDate(date: Date): string {
125
- var d = new Date(date),
126
- month = "" + (d.getMonth() + 1),
127
- day = "" + d.getDate(),
128
- year = d.getFullYear();
129
+ const d = new Date(date);
130
+ let month = `${d.getMonth() + 1}`;
131
+ let day = `${d.getDate()}`;
132
+ const year = d.getFullYear();
129
133
 
130
- if (month.length < 2) month = "0" + month;
131
- if (day.length < 2) day = "0" + day;
134
+ if (month.length < 2) month = `0${month}`;
135
+ if (day.length < 2) day = `0${day}`;
132
136
 
133
137
  return [year, month, day].join("-");
134
138
  }
@@ -136,3 +140,7 @@ export function formatDate(date: Date): string {
136
140
  export function formatHf(healthFactor: number): string {
137
141
  return (healthFactor / 10000).toFixed(2);
138
142
  }
143
+
144
+ export function formatLeverage(leverage: number, decimals: number = 2) {
145
+ return (leverage / LEVERAGE_DECIMALS).toFixed(decimals);
146
+ }
@@ -1,9 +1,17 @@
1
- export function loadingProgress(...params: Array<unknown>): number {
2
- let isLoaded: number = 0;
1
+ const isLoaded = (v: unknown) => v !== undefined;
3
2
 
4
- for (const item of params) {
5
- if (item !== undefined) isLoaded++;
6
- }
3
+ export function allLoaded(itemsToLoad: Array<unknown>): boolean {
4
+ return itemsToLoad.reduce<boolean>(
5
+ (acc, item) => acc && isLoaded(item),
6
+ true
7
+ );
8
+ }
9
+
10
+ export function loadingProgress(itemsToLoad: Array<unknown>): number {
11
+ const loaded = itemsToLoad.reduce<number>(
12
+ (acc, item) => (isLoaded(item) ? acc + 1 : acc),
13
+ 0
14
+ );
7
15
 
8
- return Math.floor((isLoaded / params.length) * 100);
16
+ return Math.floor((loaded / itemsToLoad.length) * 100);
9
17
  }
@@ -7,17 +7,26 @@ const objectEntries = <K extends SupportedValue, T>(
7
7
  const swapKeyValue = <K extends SupportedValue, T extends SupportedValue>(
8
8
  o: Record<K, T>
9
9
  ): Record<T, K> =>
10
- objectEntries(o).reduce((sum, [key, value]) => {
11
- return { ...sum, [value]: key };
12
- }, {} as Record<T, K>);
10
+ objectEntries(o).reduce(
11
+ (acc, [key, value]) => ({ ...acc, [value]: key }),
12
+ {} as Record<T, K>
13
+ );
13
14
 
14
15
  const keyToLowercase = <K extends SupportedValue, T extends SupportedValue>(
15
16
  o: Record<K, T>
16
17
  ): Record<K, T> =>
17
- objectEntries(o).reduce((sum, [key, value]) => {
18
+ objectEntries(o).reduce((acc, [key, value]) => {
18
19
  const keyTransformed = typeof key === "string" ? key.toLowerCase() : key;
19
- return { ...sum, [keyTransformed]: value };
20
+ return { ...acc, [keyTransformed]: value };
20
21
  }, {} as Record<K, T>);
21
22
 
23
+ const filterEmptyKeys = <K extends SupportedValue, T extends SupportedValue>(
24
+ o: Record<K, T>
25
+ ): Record<K, T> =>
26
+ objectEntries(o).reduce(
27
+ (acc, [key, value]) => (key ? { ...acc, [key]: value } : acc),
28
+ {} as Record<K, T>
29
+ );
30
+
22
31
  export type { SupportedValue };
23
- export { objectEntries, swapKeyValue, keyToLowercase };
32
+ export { objectEntries, swapKeyValue, keyToLowercase, filterEmptyKeys };
@@ -1,4 +1,5 @@
1
1
  import { ethers } from "ethers";
2
+ import { MULTICALL_ADDRESS } from "../config";
2
3
  import { Multicall2, Multicall2__factory } from "../types";
3
4
 
4
5
  export interface CallData<T extends ethers.utils.Interface> {
@@ -17,10 +18,7 @@ export async function multicall<R extends Array<any>>(
17
18
  calls: Array<MCall<any>>,
18
19
  p: ethers.providers.Provider
19
20
  ): Promise<R> {
20
- const multiCallContract = Multicall2__factory.connect(
21
- "0x5ba1e12693dc8f9c48aad8770482f4739beed696",
22
- p
23
- );
21
+ const multiCallContract = Multicall2__factory.connect(MULTICALL_ADDRESS, p);
24
22
 
25
23
  const { returnData } = await multiCallContract.callStatic.aggregate(
26
24
  calls.map(c => ({
@@ -33,12 +31,14 @@ export async function multicall<R extends Array<any>>(
33
31
  .map((d, num) =>
34
32
  calls[num].interface.decodeFunctionResult(calls[num].method as string, d)
35
33
  )
36
- .map(r => r[0]) as R;
34
+ .map(r => (Array.isArray(r) && r.length <= 1 ? r[0] : r)) as R;
37
35
  }
38
36
 
39
37
  export class MultiCallContract<T extends ethers.utils.Interface> {
40
38
  private readonly _address: string;
39
+
41
40
  private readonly _interface: T;
41
+
42
42
  protected _multiCall: Multicall2;
43
43
 
44
44
  constructor(
@@ -49,10 +49,7 @@ export class MultiCallContract<T extends ethers.utils.Interface> {
49
49
  this._address = address;
50
50
  this._interface = intrerface;
51
51
 
52
- this._multiCall = Multicall2__factory.connect(
53
- "0x5ba1e12693dc8f9c48aad8770482f4739beed696",
54
- provider
55
- );
52
+ this._multiCall = Multicall2__factory.connect(MULTICALL_ADDRESS, provider);
56
53
  }
57
54
 
58
55
  async call<R extends Array<any>>(data: Array<CallData<T>>): Promise<R> {
@@ -1,28 +1,28 @@
1
- import {ethers} from "ethers";
2
- import {tokenDataByNetwork} from "../tokens/token";
3
- import {ERC20__factory} from "../types";
4
- import {ADDRESS_0x0, NetworkType} from "../core/constants";
1
+ import { ethers } from "ethers";
2
+ import { tokenDataByNetwork } from "../tokens/token";
3
+ import { ERC20__factory } from "../types";
4
+ import { ADDRESS_0X0, NetworkType } from "../core/constants";
5
5
 
6
6
  export async function detectNetwork(
7
- provider: ethers.providers.Provider
7
+ provider: ethers.providers.Provider
8
8
  ): Promise<NetworkType> {
9
+ try {
10
+ const usdcMainnet = ERC20__factory.connect(
11
+ tokenDataByNetwork.Mainnet.USDC,
12
+ provider
13
+ );
14
+ await usdcMainnet.balanceOf(ADDRESS_0X0);
15
+ return "Mainnet";
16
+ } catch {
9
17
  try {
10
- const usdcMainnet = ERC20__factory.connect(
11
- tokenDataByNetwork.Mainnet.USDC,
12
- provider
13
- );
14
- await usdcMainnet.balanceOf(ADDRESS_0x0);
15
- return "Mainnet";
18
+ const usdcMainnet = ERC20__factory.connect(
19
+ tokenDataByNetwork.Kovan.USDC,
20
+ provider
21
+ );
22
+ await usdcMainnet.balanceOf(ADDRESS_0X0);
23
+ return "Kovan";
16
24
  } catch {
17
- try {
18
- const usdcMainnet = ERC20__factory.connect(
19
- tokenDataByNetwork.Kovan.USDC,
20
- provider
21
- );
22
- await usdcMainnet.balanceOf(ADDRESS_0x0);
23
- return "Kovan";
24
- } catch {
25
- throw new Error("Unknown network");
26
- }
25
+ throw new Error("Unknown network");
27
26
  }
27
+ }
28
28
  }