@gearbox-protocol/sdk 0.0.65 → 0.0.68

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 (67) hide show
  1. package/lib/contracts/adapters.d.ts +15 -0
  2. package/lib/contracts/adapters.js +19 -0
  3. package/lib/contracts/contracts.d.ts +60 -0
  4. package/lib/contracts/contracts.js +242 -0
  5. package/lib/contracts/contractsRegister.d.ts +2 -0
  6. package/lib/contracts/contractsRegister.js +58 -0
  7. package/lib/contracts/protocols.d.ts +13 -0
  8. package/lib/contracts/protocols.js +39 -0
  9. package/lib/core/contracts.js +3 -3
  10. package/lib/core/events.js +1 -1
  11. package/lib/core/tradeTypes.d.ts +1 -1
  12. package/lib/core/transactions.js +1 -1
  13. package/lib/index.d.ts +8 -9
  14. package/lib/index.js +10 -10
  15. package/lib/oracles/oracles.d.ts +38 -0
  16. package/lib/oracles/oracles.js +12 -0
  17. package/lib/oracles/priceFeeds.d.ts +3 -0
  18. package/lib/oracles/priceFeeds.js +492 -0
  19. package/lib/pathfinder/convexLP.d.ts +1 -1
  20. package/lib/pathfinder/convexLP.js +2 -20
  21. package/lib/pathfinder/curveLP.d.ts +2 -2
  22. package/lib/pathfinder/curveLP.js +8 -27
  23. package/lib/pathfinder/path.d.ts +2 -8
  24. package/lib/pathfinder/path.js +27 -34
  25. package/lib/pathfinder/trade.d.ts +35 -0
  26. package/lib/pathfinder/trade.js +62 -0
  27. package/lib/pathfinder/tradeTypes.d.ts +83 -0
  28. package/lib/pathfinder/tradeTypes.js +26 -0
  29. package/lib/pathfinder/yVault.d.ts +1 -1
  30. package/lib/pathfinder/yVault.js +4 -23
  31. package/lib/tokens/convex.d.ts +2 -2
  32. package/lib/tokens/convex.js +1 -1
  33. package/lib/tokens/curveLP.d.ts +1 -1
  34. package/lib/tokens/curveLP.js +1 -1
  35. package/lib/tokens/gear.d.ts +1 -1
  36. package/lib/tokens/normal.d.ts +1 -1
  37. package/lib/tokens/normal.js +1 -1
  38. package/lib/tokens/token.d.ts +1 -1
  39. package/lib/tokens/token.js +5 -5
  40. package/lib/tokens/yearn.d.ts +2 -2
  41. package/lib/tokens/yearn.js +3 -3
  42. package/package.json +1 -1
  43. package/src/{core → contracts}/adapters.ts +0 -0
  44. package/src/{core → contracts}/contracts.ts +6 -6
  45. package/src/{core → contracts}/contractsRegister.ts +0 -0
  46. package/src/{core → contracts}/protocols.ts +0 -0
  47. package/src/core/events.ts +932 -915
  48. package/src/core/transactions.ts +360 -352
  49. package/src/index.ts +8 -9
  50. package/src/{core → oracles}/oracles.ts +0 -0
  51. package/src/oracles/priceFeeds.ts +528 -0
  52. package/src/pathfinder/convexLP.ts +1 -1
  53. package/src/pathfinder/curveLP.ts +3 -4
  54. package/src/pathfinder/path.ts +23 -53
  55. package/src/pathfinder/trade.ts +83 -0
  56. package/src/{core → pathfinder}/tradeTypes.ts +6 -6
  57. package/src/pathfinder/yVault.ts +1 -2
  58. package/src/tokens/convex.ts +2 -2
  59. package/src/tokens/curveLP.ts +1 -1
  60. package/src/tokens/gear.ts +1 -1
  61. package/src/tokens/normal.ts +1 -1
  62. package/src/tokens/token.ts +17 -7
  63. package/src/tokens/yearn.ts +4 -4
  64. package/src/core/creditCard.ts +0 -15
  65. package/src/core/priceFeeds.ts +0 -528
  66. package/src/core/swap.ts +0 -4
  67. package/src/core/trade.ts +0 -83
@@ -1,11 +1,15 @@
1
1
  import {BigNumber, ethers} from "ethers";
2
2
  import {CreditManagerData} from "../core/creditManager";
3
3
  import {MultiCall} from "../core/multicall";
4
- import {SupportedToken, supportedTokens} from "../tokens/token";
4
+ import {
5
+ SupportedToken,
6
+ supportedTokens,
7
+ tokenSymbolByAddress
8
+ } from "../tokens/token";
5
9
  import {NetworkType} from "../core/constants";
6
10
  import {CreditAccountData} from "../core/creditAccount";
7
11
 
8
- import {PathFinder__factory, UniswapV2Adapter__factory} from "../types";
12
+ import {PathFinder__factory} from "../types";
9
13
  import {priority} from "./priority";
10
14
  import {YearnLPToken} from "../tokens/yearn";
11
15
  import {YearnVaultPathFinder} from "./yVault";
@@ -77,9 +81,21 @@ export class Path {
77
81
  creditManager: CreditManagerData,
78
82
  provider: ethers.providers.Provider
79
83
  ) {
80
- const networkType = await detectNetwork(provider)
84
+ const networkType = await detectNetwork(provider);
85
+
86
+ const balances = Object.entries(creditAccount.balances)
87
+ .map(([address, balance]) => ({
88
+ token: tokenSymbolByAddress[address.toLowerCase()],
89
+ balance
90
+ }))
91
+ .filter(t => t.balance.gt(1))
92
+ .reduce(
93
+ (obj, curValue) => ({...obj, [curValue.token]: curValue.balance}),
94
+ {}
95
+ );
96
+
81
97
  const initialPath = new Path({
82
- balances: {}, // {...creditAccount.balances},
98
+ balances,
83
99
  creditAccount,
84
100
  creditManager,
85
101
  networkType,
@@ -94,7 +110,7 @@ export class Path {
94
110
  provider
95
111
  );
96
112
 
97
- console.log(lpPaths)
113
+ console.log(lpPaths);
98
114
  console.log(pathFinder);
99
115
  // const bestPath = await pathFinder.bestPath();
100
116
  }
@@ -145,52 +161,6 @@ export class Path {
145
161
  }
146
162
  }
147
163
 
148
- export interface ActionData {
149
- callData: MultiCall;
150
- amountOut: BigNumber;
151
- gasLimit: BigNumber;
152
- }
153
-
154
- export abstract class LPWithdrawPathFinder {
155
- abstract findWithdrawPaths(p: Path): Promise<Array<Path>>;
156
-
157
- async getUniswapV2SwapData(
158
- adapterAddress: string,
159
- currentTokenAddress: string,
160
- currentBalance: BigNumber,
161
- nextTokenAddress: string,
162
- p: Path
163
- ): Promise<ActionData> {
164
- const deadline = Math.floor(Date.now() / 1000) + 1200;
165
- const uniswapV2Adapter = UniswapV2Adapter__factory.connect(
166
- adapterAddress,
167
- p.provider
168
- );
169
- const path: Array<string> = [currentTokenAddress, nextTokenAddress];
170
- const amountsOut: Array<BigNumber> = await uniswapV2Adapter.getAmountsOut(
171
- currentBalance,
172
- path
173
- );
174
-
175
- const amountOut: BigNumber = amountsOut[amountsOut.length - 1];
176
- const gasLimit: BigNumber =
177
- await uniswapV2Adapter.estimateGas.swapExactTokensForTokens(
178
- currentBalance,
179
- amountOut,
180
- path,
181
- p.creditAccount.addr,
182
- deadline
183
- );
184
- const call: MultiCall = {
185
- targetContract: adapterAddress,
186
- callData: UniswapV2Adapter__factory.createInterface().encodeFunctionData(
187
- "swapExactTokensForTokens",
188
- [currentBalance, amountOut, path, p.creditAccount.addr, deadline]
189
- )
190
- };
191
-
192
- return {callData: call, amountOut: amountOut, gasLimit: gasLimit};
193
- }
194
-
195
-
164
+ export interface LPWithdrawPathFinder {
165
+ findWithdrawPaths(p: Path): Promise<Array<Path>>;
196
166
  }
@@ -0,0 +1,83 @@
1
+ import {BigNumber} from "ethers";
2
+ import {BytesLike} from "@ethersproject/bytes";
3
+ import {PERCENTAGE_FACTOR, WAD} from "../core/constants";
4
+ import {SwapType} from "./tradeTypes";
5
+
6
+ export interface CloseTradePath {
7
+ path: Array<string>;
8
+ amountOutMin: BigNumber;
9
+ }
10
+
11
+ export class TradePath {
12
+ public readonly swapType: SwapType;
13
+ public readonly amount: BigNumber;
14
+ public readonly rate: BigNumber;
15
+ public readonly path: Array<string>;
16
+ public readonly expectedAmount: BigNumber;
17
+ public readonly pathUniV3: BytesLike | undefined;
18
+ public readonly i: number | undefined;
19
+ public readonly j: number | undefined;
20
+ public readonly operationName: string | undefined;
21
+
22
+ constructor(params: {
23
+ swapType: SwapType;
24
+ amount: BigNumber;
25
+ path: Array<string>;
26
+ expectedAmount: BigNumber;
27
+ pathUniV3?: BytesLike;
28
+ i?: number;
29
+ j?: number;
30
+ operationName?: string;
31
+ }) {
32
+ this.swapType = params.swapType;
33
+ this.amount = params.amount;
34
+ this.path = params.path;
35
+ this.expectedAmount = params.expectedAmount;
36
+ this.pathUniV3 = params.pathUniV3;
37
+ this.rate =
38
+ params.swapType !== SwapType.ExactInput
39
+ ? params.expectedAmount.mul(WAD).div(params.amount)
40
+ : WAD.mul(params.amount).div(params.expectedAmount);
41
+ this.i = params.i;
42
+ this.j = params.j;
43
+ this.operationName = params.operationName;
44
+ }
45
+
46
+ getExpectedAmountWithSlippage(slippage: number): BigNumber {
47
+ return this.swapType === SwapType.ExactInput
48
+ ? this.getAmountOutMin(slippage)
49
+ : this.getAmountInMax(slippage);
50
+ }
51
+
52
+ getAmountInMax(slippage: number): BigNumber {
53
+ return this.expectedAmount
54
+ .mul(PERCENTAGE_FACTOR + slippage)
55
+ .div(PERCENTAGE_FACTOR);
56
+ }
57
+
58
+ getAmountOutMin(slippage: number): BigNumber {
59
+ return this.expectedAmount
60
+ .mul(PERCENTAGE_FACTOR)
61
+ .div(PERCENTAGE_FACTOR + slippage);
62
+ }
63
+
64
+ public get from(): string {
65
+ return this.path[0];
66
+ }
67
+
68
+ public get to(): string {
69
+ return this.path[this.path.length - 1];
70
+ }
71
+
72
+ getFromAmount(slippage: number): BigNumber {
73
+ return this.swapType === SwapType.ExactInput
74
+ ? this.amount
75
+ : this.getExpectedAmountWithSlippage(slippage);
76
+ }
77
+
78
+ getToAmount(slippage: number): BigNumber {
79
+ return this.swapType === SwapType.ExactOutput
80
+ ? this.amount
81
+ : this.getExpectedAmountWithSlippage(slippage);
82
+ }
83
+ }
@@ -1,9 +1,4 @@
1
- import {
2
- ConvexPoolContract,
3
- CurvePoolContract,
4
- UniswapV2Contract,
5
- YearnVaultContract
6
- } from "./contracts";
1
+ import {ConvexPoolContract, CurvePoolContract, UniswapV2Contract, YearnVaultContract} from "../contracts/contracts";
7
2
  import {NormalToken} from "../tokens/normal";
8
3
  import {CurveLPToken} from "../tokens/curveLP";
9
4
  import {YearnLPToken} from "../tokens/yearn";
@@ -99,3 +94,8 @@ export type TradeAction =
99
94
  contract: ConvexPoolContract;
100
95
  tokenOut: CurveLPToken;
101
96
  };
97
+
98
+ export enum SwapType {
99
+ ExactInput = 1,
100
+ ExactOutput = 2,
101
+ }
@@ -14,12 +14,11 @@ interface WithdrawBalance {
14
14
  balance: BigNumber;
15
15
  }
16
16
 
17
- export class YearnVaultPathFinder extends LPWithdrawPathFinder {
17
+ export class YearnVaultPathFinder implements LPWithdrawPathFinder {
18
18
  _vault: YearnLPToken;
19
19
  token: NormalToken | CurveLPToken;
20
20
 
21
21
  constructor(vault: YearnLPToken) {
22
- super();
23
22
  this._vault = vault;
24
23
 
25
24
  const currentTokenData = yearnTokens[vault];
@@ -1,6 +1,6 @@
1
- import {TradeAction, TradeType} from "../core/tradeTypes";
1
+ import {TradeAction, TradeType} from "../pathfinder/tradeTypes";
2
2
  import {TokenBase} from "./token";
3
- import {ConvexPoolContract} from "../core/contracts";
3
+ import {ConvexPoolContract} from "../contracts/contracts";
4
4
  import {CurveLPToken} from "./curveLP";
5
5
  import {TokenType} from "./tokenType";
6
6
 
@@ -1,4 +1,4 @@
1
- import {TradeAction, TradeType} from "../core/tradeTypes";
1
+ import {TradeAction, TradeType} from "../pathfinder/tradeTypes";
2
2
  import {SupportedToken, TokenBase} from "./token";
3
3
  import {PartialRecord} from "../utils/types";
4
4
  import {BigNumber} from "ethers";
@@ -1,5 +1,5 @@
1
1
  import {TokenBase} from "./token";
2
- import {TradeAction} from "../core/tradeTypes";
2
+ import {TradeAction} from "../pathfinder/tradeTypes";
3
3
  import {TokenType} from "./tokenType";
4
4
 
5
5
  export type DieselTokenTypes = "dDAI" | "dUSDC" | "dWBTC" | "dWETH";
@@ -1,4 +1,4 @@
1
- import {TradeAction, TradeType} from "../core/tradeTypes";
1
+ import {TradeAction, TradeType} from "../pathfinder/tradeTypes";
2
2
  import {TokenBase} from "./token";
3
3
  import {TokenType} from "./tokenType";
4
4
 
@@ -1,7 +1,12 @@
1
1
  import {keyToLowercase, objectEntries, swapKeyValue} from "../utils/mappers";
2
2
  import {NetworkType} from "../core/constants";
3
3
  import {NormalToken, NormalTokenData, normalTokens} from "./normal";
4
- import {CurveLPToken, CurveLPTokenData, curveTokens, MetaCurveLPTokenData} from "./curveLP";
4
+ import {
5
+ CurveLPToken,
6
+ CurveLPTokenData,
7
+ curveTokens,
8
+ MetaCurveLPTokenData
9
+ } from "./curveLP";
5
10
  import {
6
11
  YearnLPToken,
7
12
  yearnTokens,
@@ -16,8 +21,13 @@ import {
16
21
  ConvexStakedPhantomToken,
17
22
  convexTokens
18
23
  } from "./convex";
19
- import {DieselTokenData, DieselTokenTypes, GearboxToken, GearboxTokenData, gearTokens} from "./gear";
20
-
24
+ import {
25
+ DieselTokenData,
26
+ DieselTokenTypes,
27
+ GearboxToken,
28
+ GearboxTokenData,
29
+ gearTokens
30
+ } from "./gear";
21
31
 
22
32
  export type SupportedToken =
23
33
  | NormalToken
@@ -120,7 +130,7 @@ export const tokenDataByNetwork: Record<NetworkType,
120
130
 
121
131
  // YEARN- CURVE TOKENS
122
132
  yvCurve_stETH: "0xdCD90C7f6324cfa40d7169ef80b12031770B4325",
123
- yvCURVE_FRAX_POOL: "0xB4AdA607B9d6b2c9Ee07A275e9616B84AC560139",
133
+ yvCurve_FRAX: "0xB4AdA607B9d6b2c9Ee07A275e9616B84AC560139",
124
134
 
125
135
  //GEARBOX
126
136
  dDAI: "0x6CFaF95457d7688022FC53e7AbE052ef8DFBbdBA",
@@ -177,7 +187,7 @@ export const tokenDataByNetwork: Record<NetworkType,
177
187
  "3Crv": "0x8eA3677599d5199c2c93c468fD2Fa6F102d3605b",
178
188
  steCRV: "0x6636910D55D34505c08d53932Da526FB18D3869C",
179
189
  FRAX3CRV: "0x76508F5398E50EFEC53d5Afd4B506990B43AdBFB",
180
- LUSD3CRV: "0x04366d7ee3210B9eAd6f36696220d697b7885ED7",
190
+ LUSD3CRV: "0x2b72b528723DEe00e9BB02839f0794A66594922E",
181
191
  crvPlain3andSUSD: "0x540f5ea2A0601e27368661CB231AE04c23A14C24",
182
192
  gusd3CRV: "0x428A500D8209fef5a1b5a0A35C9412b71F03aAdA",
183
193
 
@@ -197,7 +207,7 @@ export const tokenDataByNetwork: Record<NetworkType,
197
207
 
198
208
  // YEARN- CURVE TOKENS
199
209
  yvCurve_stETH: "",
200
- yvCURVE_FRAX_POOL: "",
210
+ yvCurve_FRAX: "",
201
211
 
202
212
  //GEARBOX
203
213
  dDAI: "0x6CFaF95457d7688022FC53e7AbE052ef8DFBbdBA",
@@ -209,6 +219,6 @@ export const tokenDataByNetwork: Record<NetworkType,
209
219
  }
210
220
  };
211
221
 
212
- export const tokenDataByAddress = objectEntries(tokenDataByNetwork).reduce<Record<string, SupportedToken>>((sum, [_, tokens]) => {
222
+ export const tokenSymbolByAddress = objectEntries(tokenDataByNetwork).reduce<Record<string, SupportedToken>>((sum, [_, tokens]) => {
213
223
  return {...sum, ...keyToLowercase(swapKeyValue(tokens))};
214
224
  }, {});
@@ -1,4 +1,4 @@
1
- import {TradeAction, TradeType} from "../core/tradeTypes";
1
+ import {TradeAction, TradeType} from "../pathfinder/tradeTypes";
2
2
  import {TokenBase} from "./token";
3
3
  import {CurveLPToken} from "./curveLP";
4
4
  import {NormalToken} from "./normal";
@@ -10,7 +10,7 @@ export type YearnLPToken =
10
10
  | "yvWETH"
11
11
  | "yvWBTC"
12
12
  | "yvCurve_stETH"
13
- | "yvCURVE_FRAX_POOL";
13
+ | "yvCurve_FRAX";
14
14
 
15
15
  export type YearnVaultTokenData = {
16
16
  symbol: YearnLPToken;
@@ -119,11 +119,11 @@ export const yearnTokens: Record<YearnLPToken,
119
119
  ]
120
120
  },
121
121
 
122
- yvCURVE_FRAX_POOL: {
122
+ yvCurve_FRAX: {
123
123
  name: "yvCurve-FRAX",
124
124
  decimals: 18,
125
125
 
126
- symbol: "yvCURVE_FRAX_POOL",
126
+ symbol: "yvCurve_FRAX",
127
127
  type: TokenType.YEARN_VAULT_OF_META_CURVE_LP,
128
128
  underlying: "FRAX3CRV",
129
129
  lpActions: [
@@ -1,15 +0,0 @@
1
- export interface CardCustomisation {
2
- id: string;
3
- name: string;
4
- background: string;
5
- }
6
-
7
- export interface CardBackground {
8
- id: string;
9
- background: string
10
- }
11
-
12
- export interface CardCollection {
13
- id: string;
14
- backgrounds: Array<CardBackground>;
15
- }