@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
@@ -1,17 +1,19 @@
1
- import {NetworkType} from "../core/constants";
1
+ import { NetworkType } from "../core/constants";
2
2
 
3
3
  export type PathFinderContracts = "PATH_FINDER" | "CONVEX_PATH_FINDER";
4
4
 
5
- export const pathFindersByNetwork: Record<NetworkType,
6
- Record<PathFinderContracts, string>> = {
7
- Mainnet: {
8
- // PATH_FINDER
9
- PATH_FINDER: "",
10
- CONVEX_PATH_FINDER: ""
11
- },
12
- Kovan: {
13
- // PATH_FINDER
14
- PATH_FINDER: "",
15
- CONVEX_PATH_FINDER: ""
16
- }
5
+ export const pathFindersByNetwork: Record<
6
+ NetworkType,
7
+ Record<PathFinderContracts, string>
8
+ > = {
9
+ Mainnet: {
10
+ // PATH_FINDER
11
+ PATH_FINDER: "",
12
+ CONVEX_PATH_FINDER: ""
13
+ },
14
+ Kovan: {
15
+ // PATH_FINDER
16
+ PATH_FINDER: "",
17
+ CONVEX_PATH_FINDER: ""
18
+ }
17
19
  };
@@ -1,33 +1,37 @@
1
- import {LPWithdrawPathFinder, Path} from "./path";
2
- import {ConvexLPToken, convexTokens} from "../tokens/convex";
1
+ import { convexTokens } from "../tokens/convex";
2
+ import { objectEntries } from "../utils/mappers";
3
+ import type { LPWithdrawPathFinder, Path } from "./path";
3
4
 
4
5
  export class ConvexLPPathFinder implements LPWithdrawPathFinder {
5
- async findWithdrawPaths(p: Path): Promise<Array<Path>> {
6
- const pids = new Set<number>();
7
-
8
- for (let [cvxToken, tokenData] of Object.entries(convexTokens)) {
9
- const currentBalance = p.popBalance(cvxToken as ConvexLPToken);
10
- if (currentBalance.gt(1)) {
11
- pids.add(tokenData.pid);
12
- }
6
+ // eslint-disable-next-line class-methods-use-this
7
+ async findWithdrawPaths(p: Path): Promise<Array<Path>> {
8
+ const pids = objectEntries(convexTokens).reduce(
9
+ (acc, [cvxToken, tokenData]) => {
10
+ const currentBalance = p.popBalance(cvxToken);
11
+ if (currentBalance.gt(1)) {
12
+ pids.add(tokenData.pid);
13
13
  }
14
+ return acc;
15
+ },
16
+ new Set<number>()
17
+ );
14
18
 
15
- // const convexPathFinder = ConvexPathFinder__factory.connect(
16
- // pathFindersByNetwork[p.networkType].CONVEX_PATH_FINDER,
17
- // p.provider
18
- // );
19
+ // const convexPathFinder = ConvexPathFinder__factory.connect(
20
+ // pathFindersByNetwork[p.networkType].CONVEX_PATH_FINDER,
21
+ // p.provider
22
+ // );
19
23
 
20
- // const tokenBalances = await convexPathFinder.calcRewards(
21
- // p.creditAccount.addr,
22
- // Array.from(pids)
23
- // );
24
+ // const tokenBalances = await convexPathFinder.calcRewards(
25
+ // p.creditAccount.addr,
26
+ // Array.from(pids)
27
+ // );
24
28
 
25
- // for (let balance of tokenBalances) {
26
- // p.balances[balance.token] = p.balances[balance.token].add(
27
- // balance.balance
28
- // );
29
- // }
29
+ // for (let balance of tokenBalances) {
30
+ // p.balances[balance.token] = p.balances[balance.token].add(
31
+ // balance.balance
32
+ // );
33
+ // }
30
34
 
31
- return await p.withdrawTokens();
32
- }
35
+ return p.withdrawTokens();
36
+ }
33
37
  }
@@ -1,71 +1,75 @@
1
- import {LPWithdrawPathFinder, Path} from "./path";
2
- import {CurveLPToken, curveTokens} from "../tokens/curveLP";
3
- import {contractParams, contractsByAddress, CurveParams, CurvePoolContract} from "../contracts/contracts";
4
- import {TradeType} from "./tradeTypes";
5
- import {CallData, MultiCallContract} from "../utils/multicall";
6
- import {ICurvePool__factory} from "../types";
7
- import {ICurvePoolInterface} from "../types/contracts/integrations/curve/ICurvePool";
8
- import {BigNumber} from "ethers";
1
+ import { BigNumber } from "ethers";
2
+ import type { LPWithdrawPathFinder, Path } from "./path";
3
+ import { CurveLPToken, curveTokens } from "../tokens/curveLP";
4
+ import {
5
+ contractParams,
6
+ contractsByAddress,
7
+ CurveParams,
8
+ CurvePoolContract
9
+ } from "../contracts/contracts";
10
+ import { TradeType } from "./tradeTypes";
11
+ import { CallData, MultiCallContract } from "../utils/multicall";
12
+ import { ICurvePool__factory } from "../types";
13
+ import { ICurvePoolInterface } from "../types/contracts/integrations/curve/ICurvePool";
9
14
 
10
15
  export class CurvePathFinder implements LPWithdrawPathFinder {
11
- lpToken: CurveLPToken;
12
- contract: CurvePoolContract;
16
+ lpToken: CurveLPToken;
13
17
 
14
- constructor(token: CurveLPToken) {
15
- this.lpToken = token;
16
- const curvePools = curveTokens[this.lpToken].lpActions.filter(
17
- a => a.type == TradeType.CurveWithdrawLP
18
- );
18
+ contract: CurvePoolContract;
19
19
 
20
- if (curvePools.length !== 1) throw new Error("Cant find Withdrawer");
20
+ constructor(token: CurveLPToken) {
21
+ this.lpToken = token;
22
+ const curvePools = curveTokens[this.lpToken].lpActions.filter(
23
+ a => a.type === TradeType.CurveWithdrawLP
24
+ );
21
25
 
22
- this.contract = curvePools[0].contract as CurvePoolContract;
26
+ if (curvePools.length !== 1) throw new Error("Cant find Withdrawer");
23
27
 
24
- const wrapper = (contractParams[curvePools[0].contract] as CurveParams)
25
- .wrapper;
28
+ this.contract = curvePools[0].contract as CurvePoolContract;
26
29
 
27
- if (wrapper) {
28
- this.contract = wrapper;
29
- }
30
- }
30
+ const { wrapper } = contractParams[curvePools[0].contract] as CurveParams;
31
31
 
32
- async findWithdrawPaths(p: Path): Promise<Array<Path>> {
33
- const coins = (contractParams[this.contract] as CurveParams).tokens;
34
- const nCoins = coins.length;
32
+ if (wrapper) {
33
+ this.contract = wrapper;
34
+ }
35
+ }
35
36
 
36
- const lpBalance = p.popBalance(this.lpToken);
37
+ async findWithdrawPaths(p: Path): Promise<Array<Path>> {
38
+ const coins = (contractParams[this.contract] as CurveParams).tokens;
39
+ const nCoins = coins.length;
37
40
 
38
- const multiCallContract = new MultiCallContract(
39
- contractsByAddress[this.contract],
40
- ICurvePool__factory.createInterface(),
41
- p.provider
42
- );
41
+ const lpBalance = p.popBalance(this.lpToken);
43
42
 
44
- const data: Array<CallData<ICurvePoolInterface>> = [];
43
+ const multiCallContract = new MultiCallContract(
44
+ contractsByAddress[this.contract],
45
+ ICurvePool__factory.createInterface(),
46
+ p.provider
47
+ );
45
48
 
46
- for (let i = 0; i < nCoins; i++) {
47
- data.push({
48
- // @ts-ignore
49
- method: "calc_withdraw_one_coin(uint256,int128)",
50
- params: [lpBalance, i]
51
- });
52
- }
49
+ const data: Array<CallData<ICurvePoolInterface>> = [];
53
50
 
54
- const balances = await multiCallContract.call<Array<BigNumber>>(data);
51
+ for (let i = 0; i < nCoins; i += 1) {
52
+ data.push({
53
+ method: "calc_withdraw_one_coin(uint256,int128)",
54
+ params: [lpBalance, i]
55
+ });
56
+ }
55
57
 
56
- console.log(balances)
58
+ const balances = await multiCallContract.call<Array<BigNumber>>(data);
57
59
 
58
- const paths: Array<Path> = [];
59
- for (let i = 0; i < nCoins; i++) {
60
- const newPath = p.clone();
61
- // newPath.balances[coins[i]] = newPath.balances[coins[i]].add(balances[i]);
62
- // newPath.calls.push({
63
- // targetContract: contractsByNetwork[p.networkType][this.contract],
64
- // callData: ICurveV1Adapter__factory.createInterface().encodeFunctionData("remove_all_liquidity_one_coin", [i])
65
- // })
66
- paths.push(newPath);
67
- }
60
+ console.debug(balances);
68
61
 
69
- return paths;
62
+ const paths: Array<Path> = [];
63
+ for (let i = 0; i < nCoins; i += 1) {
64
+ const newPath = p.clone();
65
+ // newPath.balances[coins[i]] = newPath.balances[coins[i]].add(balances[i]);
66
+ // newPath.calls.push({
67
+ // targetContract: contractsByNetwork[p.networkType][this.contract],
68
+ // callData: ICurveV1Adapter__factory.createInterface().encodeFunctionData("remove_all_liquidity_one_coin", [i])
69
+ // })
70
+ paths.push(newPath);
70
71
  }
72
+
73
+ return paths;
74
+ }
71
75
  }
@@ -23,12 +23,19 @@ import { TokenType } from "../tokens/tokenType";
23
23
 
24
24
  export class Path {
25
25
  public readonly calls: Array<MultiCall> = [];
26
+
26
27
  public readonly balances: PartialRecord<SupportedToken, BigNumber>;
28
+
27
29
  public readonly underlying: SupportedToken;
30
+
28
31
  public readonly creditManager: CreditManagerData;
32
+
29
33
  public readonly creditAccount: CreditAccountData;
34
+
30
35
  public readonly networkType: NetworkType;
36
+
31
37
  public readonly provider: ethers.providers.Provider;
38
+
32
39
  public totalGasLimit: number;
33
40
 
34
41
  constructor(opts: {
@@ -59,9 +66,9 @@ export class Path {
59
66
  return currentBalance.sub(1);
60
67
  }
61
68
 
62
- private comparedByPriority(
63
- [tokenA, _balanceA]: [string, BigNumber],
64
- [tokenB, _balanceB]: [string, BigNumber]
69
+ private static comparedByPriority(
70
+ [tokenA]: [string, BigNumber],
71
+ [tokenB]: [string, BigNumber]
65
72
  ): number {
66
73
  const priorityTokenA =
67
74
  priority[supportedTokens[tokenA as SupportedToken].type];
@@ -70,7 +77,8 @@ export class Path {
70
77
 
71
78
  if (priorityTokenA > priorityTokenB) {
72
79
  return -1;
73
- } else if (priorityTokenA < priorityTokenB) {
80
+ }
81
+ if (priorityTokenA < priorityTokenB) {
74
82
  return 1;
75
83
  }
76
84
  return 0;
@@ -110,15 +118,15 @@ export class Path {
110
118
  provider
111
119
  );
112
120
 
113
- console.log(lpPaths);
114
- console.log(pathFinder);
121
+ console.debug(lpPaths);
122
+ console.debug(pathFinder);
115
123
  // const bestPath = await pathFinder.bestPath();
116
124
  }
117
125
 
118
126
  async withdrawTokens(): Promise<Array<Path>> {
119
127
  const existingTokens = Object.entries(this.balances)
120
- .filter(([_token, balance]) => balance.gt(1))
121
- .sort(this.comparedByPriority);
128
+ .filter(([, balance]) => balance.gt(1))
129
+ .sort(Path.comparedByPriority);
122
130
 
123
131
  // TODO: Add checks for lenght
124
132
  if (existingTokens.length === 0)
@@ -153,7 +161,7 @@ export class Path {
153
161
  throw new Error("Token type not supported yet");
154
162
  }
155
163
 
156
- return await lpPathFinder.findWithdrawPaths(this);
164
+ return lpPathFinder.findWithdrawPaths(this);
157
165
  }
158
166
 
159
167
  clone(): Path {
@@ -1,14 +1,14 @@
1
- import {TokenType} from "../tokens/tokenType";
1
+ import { TokenType } from "../tokens/tokenType";
2
2
 
3
3
  export const priority: Record<TokenType, number> = {
4
- [TokenType.CONNECTOR]: 1,
5
- [TokenType.NORMAL_TOKEN]: 2,
6
- [TokenType.CURVE_LP]: 3,
7
- [TokenType.YEARN_VAULT]: 3,
8
- [TokenType.META_CURVE_LP]: 4,
9
- [TokenType.YEARN_VAULT_OF_CURVE_LP]: 4,
10
- [TokenType.CONVEX_LP_TOKEN]: 5,
11
- [TokenType.YEARN_VAULT_OF_META_CURVE_LP]: 5,
12
- [TokenType.CONVEX_STAKED_PHANTOM_TOKEN]: 5,
13
- [TokenType.DIESEL_LP_TOKEN]: 6
4
+ [TokenType.CONNECTOR]: 1,
5
+ [TokenType.NORMAL_TOKEN]: 2,
6
+ [TokenType.CURVE_LP]: 3,
7
+ [TokenType.YEARN_VAULT]: 3,
8
+ [TokenType.META_CURVE_LP]: 4,
9
+ [TokenType.YEARN_VAULT_OF_CURVE_LP]: 4,
10
+ [TokenType.CONVEX_LP_TOKEN]: 5,
11
+ [TokenType.YEARN_VAULT_OF_META_CURVE_LP]: 5,
12
+ [TokenType.CONVEX_STAKED_PHANTOM_TOKEN]: 5,
13
+ [TokenType.DIESEL_LP_TOKEN]: 6
14
14
  };
@@ -1,83 +1,90 @@
1
- import {BigNumber} from "ethers";
2
- import {BytesLike} from "@ethersproject/bytes";
3
- import {PERCENTAGE_FACTOR, WAD} from "../core/constants";
4
- import {SwapType} from "./tradeTypes";
1
+ import { BigNumber, BytesLike } from "ethers";
2
+ import { PERCENTAGE_FACTOR, WAD } from "../core/constants";
3
+ import { SwapType } from "./tradeTypes";
5
4
 
6
5
  export interface CloseTradePath {
7
- path: Array<string>;
8
- amountOutMin: BigNumber;
6
+ path: Array<string>;
7
+ amountOutMin: BigNumber;
9
8
  }
10
9
 
11
10
  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
- }
11
+ public readonly swapType: SwapType;
12
+
13
+ public readonly amount: BigNumber;
14
+
15
+ public readonly rate: BigNumber;
16
+
17
+ public readonly path: Array<string>;
18
+
19
+ public readonly expectedAmount: BigNumber;
20
+
21
+ public readonly pathUniV3: BytesLike | undefined;
22
+
23
+ public readonly i: number | undefined;
24
+
25
+ public readonly j: number | undefined;
26
+
27
+ public readonly operationName: string | undefined;
28
+
29
+ constructor(params: {
30
+ swapType: SwapType;
31
+ amount: BigNumber;
32
+ path: Array<string>;
33
+ expectedAmount: BigNumber;
34
+ pathUniV3?: BytesLike;
35
+ i?: number;
36
+ j?: number;
37
+ operationName?: string;
38
+ }) {
39
+ this.swapType = params.swapType;
40
+ this.amount = params.amount;
41
+ this.path = params.path;
42
+ this.expectedAmount = params.expectedAmount;
43
+ this.pathUniV3 = params.pathUniV3;
44
+ this.rate =
45
+ params.swapType !== SwapType.ExactInput
46
+ ? params.expectedAmount.mul(WAD).div(params.amount)
47
+ : WAD.mul(params.amount).div(params.expectedAmount);
48
+ this.i = params.i;
49
+ this.j = params.j;
50
+ this.operationName = params.operationName;
51
+ }
52
+
53
+ getExpectedAmountWithSlippage(slippage: number): BigNumber {
54
+ return this.swapType === SwapType.ExactInput
55
+ ? this.getAmountOutMin(slippage)
56
+ : this.getAmountInMax(slippage);
57
+ }
58
+
59
+ getAmountInMax(slippage: number): BigNumber {
60
+ return this.expectedAmount
61
+ .mul(PERCENTAGE_FACTOR + slippage)
62
+ .div(PERCENTAGE_FACTOR);
63
+ }
64
+
65
+ getAmountOutMin(slippage: number): BigNumber {
66
+ return this.expectedAmount
67
+ .mul(PERCENTAGE_FACTOR)
68
+ .div(PERCENTAGE_FACTOR + slippage);
69
+ }
70
+
71
+ public get from(): string {
72
+ return this.path[0];
73
+ }
74
+
75
+ public get to(): string {
76
+ return this.path[this.path.length - 1];
77
+ }
78
+
79
+ getFromAmount(slippage: number): BigNumber {
80
+ return this.swapType === SwapType.ExactInput
81
+ ? this.amount
82
+ : this.getExpectedAmountWithSlippage(slippage);
83
+ }
84
+
85
+ getToAmount(slippage: number): BigNumber {
86
+ return this.swapType === SwapType.ExactOutput
87
+ ? this.amount
88
+ : this.getExpectedAmountWithSlippage(slippage);
89
+ }
83
90
  }