@uniswap/router-sdk 2.7.2 → 2.8.0

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 (113) hide show
  1. package/dist/{approveAndCall.d.ts → cjs/src/approveAndCall.d.ts} +33 -33
  2. package/dist/cjs/src/approveAndCall.js +111 -0
  3. package/dist/cjs/src/approveAndCall.js.map +1 -0
  4. package/dist/{constants.d.ts → cjs/src/constants.d.ts} +13 -13
  5. package/dist/cjs/src/constants.js +22 -0
  6. package/dist/cjs/src/constants.js.map +1 -0
  7. package/dist/{entities → cjs/src/entities}/mixedRoute/route.d.ts +29 -29
  8. package/dist/cjs/src/entities/mixedRoute/route.js +119 -0
  9. package/dist/cjs/src/entities/mixedRoute/route.js.map +1 -0
  10. package/dist/{entities → cjs/src/entities}/mixedRoute/trade.d.ts +183 -183
  11. package/dist/cjs/src/entities/mixedRoute/trade.js +354 -0
  12. package/dist/cjs/src/entities/mixedRoute/trade.js.map +1 -0
  13. package/dist/{entities → cjs/src/entities}/protocol.d.ts +6 -6
  14. package/dist/cjs/src/entities/protocol.js +11 -0
  15. package/dist/cjs/src/entities/protocol.js.map +1 -0
  16. package/dist/{entities → cjs/src/entities}/route.d.ts +40 -40
  17. package/dist/cjs/src/entities/route.js +63 -0
  18. package/dist/cjs/src/entities/route.js.map +1 -0
  19. package/dist/{entities → cjs/src/entities}/trade.d.ts +136 -127
  20. package/dist/cjs/src/entities/trade.js +370 -0
  21. package/dist/cjs/src/entities/trade.js.map +1 -0
  22. package/dist/{index.d.ts → cjs/src/index.d.ts} +14 -14
  23. package/dist/cjs/src/index.js +18 -0
  24. package/dist/cjs/src/index.js.map +1 -0
  25. package/dist/{multicallExtended.d.ts → cjs/src/multicallExtended.d.ts} +11 -11
  26. package/dist/cjs/src/multicallExtended.js +44 -0
  27. package/dist/cjs/src/multicallExtended.js.map +1 -0
  28. package/dist/{paymentsExtended.d.ts → cjs/src/paymentsExtended.d.ts} +15 -15
  29. package/dist/cjs/src/paymentsExtended.js +66 -0
  30. package/dist/cjs/src/paymentsExtended.js.map +1 -0
  31. package/dist/{swapRouter.d.ts → cjs/src/swapRouter.d.ts} +95 -95
  32. package/dist/cjs/src/swapRouter.js +439 -0
  33. package/dist/cjs/src/swapRouter.js.map +1 -0
  34. package/dist/{utils → cjs/src/utils}/TPool.d.ts +4 -4
  35. package/dist/cjs/src/utils/TPool.js +3 -0
  36. package/dist/cjs/src/utils/TPool.js.map +1 -0
  37. package/dist/{utils → cjs/src/utils}/encodeMixedRouteToPath.d.ts +10 -10
  38. package/dist/cjs/src/utils/encodeMixedRouteToPath.js +91 -0
  39. package/dist/cjs/src/utils/encodeMixedRouteToPath.js.map +1 -0
  40. package/dist/{utils → cjs/src/utils}/index.d.ts +16 -16
  41. package/dist/cjs/src/utils/index.js +51 -0
  42. package/dist/cjs/src/utils/index.js.map +1 -0
  43. package/dist/{utils → cjs/src/utils}/pathCurrency.d.ts +4 -4
  44. package/dist/cjs/src/utils/pathCurrency.js +35 -0
  45. package/dist/cjs/src/utils/pathCurrency.js.map +1 -0
  46. package/dist/esm/src/approveAndCall.d.ts +33 -0
  47. package/dist/esm/src/approveAndCall.js +105 -0
  48. package/dist/esm/src/approveAndCall.js.map +1 -0
  49. package/dist/esm/src/constants.d.ts +13 -0
  50. package/dist/esm/src/constants.js +18 -0
  51. package/dist/esm/src/constants.js.map +1 -0
  52. package/dist/esm/src/entities/mixedRoute/route.d.ts +29 -0
  53. package/dist/esm/src/entities/mixedRoute/route.js +114 -0
  54. package/dist/esm/src/entities/mixedRoute/route.js.map +1 -0
  55. package/dist/esm/src/entities/mixedRoute/trade.d.ts +183 -0
  56. package/dist/esm/src/entities/mixedRoute/trade.js +348 -0
  57. package/dist/esm/src/entities/mixedRoute/trade.js.map +1 -0
  58. package/dist/esm/src/entities/protocol.d.ts +6 -0
  59. package/dist/esm/src/entities/protocol.js +8 -0
  60. package/dist/esm/src/entities/protocol.js.map +1 -0
  61. package/dist/esm/src/entities/route.d.ts +40 -0
  62. package/dist/esm/src/entities/route.js +55 -0
  63. package/dist/esm/src/entities/route.js.map +1 -0
  64. package/dist/esm/src/entities/trade.d.ts +136 -0
  65. package/dist/esm/src/entities/trade.js +365 -0
  66. package/dist/esm/src/entities/trade.js.map +1 -0
  67. package/dist/esm/src/index.d.ts +14 -0
  68. package/dist/esm/src/index.js +15 -0
  69. package/dist/esm/src/index.js.map +1 -0
  70. package/dist/esm/src/multicallExtended.d.ts +11 -0
  71. package/dist/esm/src/multicallExtended.js +39 -0
  72. package/dist/esm/src/multicallExtended.js.map +1 -0
  73. package/dist/esm/src/paymentsExtended.d.ts +15 -0
  74. package/dist/esm/src/paymentsExtended.js +61 -0
  75. package/dist/esm/src/paymentsExtended.js.map +1 -0
  76. package/dist/esm/src/swapRouter.d.ts +95 -0
  77. package/dist/esm/src/swapRouter.js +434 -0
  78. package/dist/esm/src/swapRouter.js.map +1 -0
  79. package/dist/esm/src/utils/TPool.d.ts +4 -0
  80. package/dist/esm/src/utils/TPool.js +2 -0
  81. package/dist/esm/src/utils/TPool.js.map +1 -0
  82. package/dist/esm/src/utils/encodeMixedRouteToPath.d.ts +10 -0
  83. package/dist/esm/src/utils/encodeMixedRouteToPath.js +87 -0
  84. package/dist/esm/src/utils/encodeMixedRouteToPath.js.map +1 -0
  85. package/dist/esm/src/utils/index.d.ts +16 -0
  86. package/dist/esm/src/utils/index.js +46 -0
  87. package/dist/esm/src/utils/index.js.map +1 -0
  88. package/dist/esm/src/utils/pathCurrency.d.ts +4 -0
  89. package/dist/esm/src/utils/pathCurrency.js +30 -0
  90. package/dist/esm/src/utils/pathCurrency.js.map +1 -0
  91. package/dist/types/src/approveAndCall.d.ts +33 -0
  92. package/dist/types/src/constants.d.ts +13 -0
  93. package/dist/types/src/entities/mixedRoute/route.d.ts +29 -0
  94. package/dist/types/src/entities/mixedRoute/trade.d.ts +183 -0
  95. package/dist/types/src/entities/protocol.d.ts +6 -0
  96. package/dist/types/src/entities/route.d.ts +40 -0
  97. package/dist/types/src/entities/trade.d.ts +136 -0
  98. package/dist/types/src/index.d.ts +14 -0
  99. package/dist/types/src/multicallExtended.d.ts +11 -0
  100. package/dist/types/src/paymentsExtended.d.ts +15 -0
  101. package/dist/types/src/swapRouter.d.ts +95 -0
  102. package/dist/types/src/utils/TPool.d.ts +4 -0
  103. package/dist/types/src/utils/encodeMixedRouteToPath.d.ts +10 -0
  104. package/dist/types/src/utils/index.d.ts +16 -0
  105. package/dist/types/src/utils/pathCurrency.d.ts +4 -0
  106. package/package.json +31 -12
  107. package/dist/index.js +0 -8
  108. package/dist/router-sdk.cjs.development.js +0 -2250
  109. package/dist/router-sdk.cjs.development.js.map +0 -1
  110. package/dist/router-sdk.cjs.production.min.js +0 -2
  111. package/dist/router-sdk.cjs.production.min.js.map +0 -1
  112. package/dist/router-sdk.esm.js +0 -2217
  113. package/dist/router-sdk.esm.js.map +0 -1
@@ -1,127 +1,136 @@
1
- import { Currency, CurrencyAmount, Percent, Price, TradeType } from '@uniswap/sdk-core';
2
- import { Pair, Route as V2RouteSDK } from '@uniswap/v2-sdk';
3
- import { Pool as V3Pool, Route as V3RouteSDK } from '@uniswap/v3-sdk';
4
- import { Pool as V4Pool, Route as V4RouteSDK } from '@uniswap/v4-sdk';
5
- import { MixedRouteSDK } from './mixedRoute/route';
6
- import { IRoute } from './route';
7
- export declare class Trade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
8
- readonly routes: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[];
9
- readonly tradeType: TTradeType;
10
- private _outputAmount;
11
- private _inputAmount;
12
- private _nativeInputRoutes;
13
- private _wethInputRoutes;
14
- /**
15
- * The swaps of the trade, i.e. which routes and how much is swapped in each that
16
- * make up the trade. May consist of swaps in v2 or v3.
17
- */
18
- readonly swaps: {
19
- route: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>;
20
- inputAmount: CurrencyAmount<TInput>;
21
- outputAmount: CurrencyAmount<TOutput>;
22
- }[];
23
- constructor({ v2Routes, v3Routes, v4Routes, mixedRoutes, tradeType, }: {
24
- v2Routes?: {
25
- routev2: V2RouteSDK<TInput, TOutput>;
26
- inputAmount: CurrencyAmount<TInput>;
27
- outputAmount: CurrencyAmount<TOutput>;
28
- }[];
29
- v3Routes?: {
30
- routev3: V3RouteSDK<TInput, TOutput>;
31
- inputAmount: CurrencyAmount<TInput>;
32
- outputAmount: CurrencyAmount<TOutput>;
33
- }[];
34
- v4Routes?: {
35
- routev4: V4RouteSDK<TInput, TOutput>;
36
- inputAmount: CurrencyAmount<TInput>;
37
- outputAmount: CurrencyAmount<TOutput>;
38
- }[];
39
- mixedRoutes?: {
40
- mixedRoute: MixedRouteSDK<TInput, TOutput>;
41
- inputAmount: CurrencyAmount<TInput>;
42
- outputAmount: CurrencyAmount<TOutput>;
43
- }[];
44
- tradeType: TTradeType;
45
- });
46
- get inputAmount(): CurrencyAmount<TInput>;
47
- get outputAmount(): CurrencyAmount<TOutput>;
48
- /**
49
- * Returns the sum of all swaps within the trade
50
- * @returns
51
- * inputAmount: total input amount
52
- * inputAmountNative: total amount of native currency required for ETH input paths
53
- * - 0 if inputAmount is native but no native input paths
54
- * - undefined if inputAmount is not native
55
- * outputAmount: total output amount
56
- * outputAmountNative: total amount of native currency returned from ETH output paths
57
- * - 0 if outputAmount is native but no native output paths
58
- * - undefined if outputAmount is not native
59
- */
60
- get amounts(): {
61
- inputAmount: CurrencyAmount<TInput>;
62
- inputAmountNative: CurrencyAmount<TInput> | undefined;
63
- outputAmount: CurrencyAmount<TOutput>;
64
- outputAmountNative: CurrencyAmount<TOutput> | undefined;
65
- };
66
- get numberOfInputWraps(): number;
67
- get numberOfInputUnwraps(): number;
68
- get nativeInputRoutes(): IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[];
69
- get wethInputRoutes(): IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[];
70
- private _executionPrice;
71
- /**
72
- * The price expressed in terms of output amount/input amount.
73
- */
74
- get executionPrice(): Price<TInput, TOutput>;
75
- /**
76
- * Returns the sell tax of the input token
77
- */
78
- get inputTax(): Percent;
79
- /**
80
- * Returns the buy tax of the output token
81
- */
82
- get outputTax(): Percent;
83
- private isWrappedNative;
84
- /**
85
- * The cached result of the price impact computation
86
- * @private
87
- */
88
- private _priceImpact;
89
- /**
90
- * Returns the percent difference between the route's mid price and the expected execution price
91
- * In order to exclude token taxes from the price impact calculation, the spot price is calculated
92
- * using a ratio of values that go into the pools, which are the post-tax input amount and pre-tax output amount.
93
- */
94
- get priceImpact(): Percent;
95
- /**
96
- * Get the minimum amount that must be received from this trade for the given slippage tolerance
97
- * @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
98
- * @returns The amount out
99
- */
100
- minimumAmountOut(slippageTolerance: Percent, amountOut?: CurrencyAmount<TOutput>): CurrencyAmount<TOutput>;
101
- /**
102
- * Get the maximum amount in that can be spent via this trade for the given slippage tolerance
103
- * @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
104
- * @returns The amount in
105
- */
106
- maximumAmountIn(slippageTolerance: Percent, amountIn?: CurrencyAmount<TInput>): CurrencyAmount<TInput>;
107
- /**
108
- * Return the execution price after accounting for slippage tolerance
109
- * @param slippageTolerance the allowed tolerated slippage
110
- * @returns The execution price
111
- */
112
- worstExecutionPrice(slippageTolerance: Percent): Price<TInput, TOutput>;
113
- static fromRoutes<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(v2Routes: {
114
- routev2: V2RouteSDK<TInput, TOutput>;
115
- amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
116
- }[], v3Routes: {
117
- routev3: V3RouteSDK<TInput, TOutput>;
118
- amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
119
- }[], tradeType: TTradeType, mixedRoutes?: {
120
- mixedRoute: MixedRouteSDK<TInput, TOutput>;
121
- amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
122
- }[], v4Routes?: {
123
- routev4: V4RouteSDK<TInput, TOutput>;
124
- amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
125
- }[]): Promise<Trade<TInput, TOutput, TTradeType>>;
126
- static fromRoute<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(route: V2RouteSDK<TInput, TOutput> | V3RouteSDK<TInput, TOutput> | V4RouteSDK<TInput, TOutput> | MixedRouteSDK<TInput, TOutput>, amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>, tradeType: TTradeType): Promise<Trade<TInput, TOutput, TTradeType>>;
127
- }
1
+ import { Currency, CurrencyAmount, Percent, Price, TradeType } from '@uniswap/sdk-core';
2
+ import { Pair, Route as V2RouteSDK } from '@uniswap/v2-sdk';
3
+ import { Pool as V3Pool, Route as V3RouteSDK } from '@uniswap/v3-sdk';
4
+ import { Pool as V4Pool, Route as V4RouteSDK } from '@uniswap/v4-sdk';
5
+ import { MixedRouteSDK } from './mixedRoute/route';
6
+ import { IRoute } from './route';
7
+ export declare class Trade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
8
+ readonly routes: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[];
9
+ readonly tradeType: TTradeType;
10
+ private _outputAmount;
11
+ private _inputAmount;
12
+ private _nativeInputRoutes;
13
+ private _wethInputRoutes;
14
+ /**
15
+ * The swaps of the trade, i.e. which routes and how much is swapped in each that
16
+ * make up the trade. May consist of swaps in v2 or v3.
17
+ */
18
+ readonly swaps: {
19
+ route: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>;
20
+ inputAmount: CurrencyAmount<TInput>;
21
+ outputAmount: CurrencyAmount<TOutput>;
22
+ maxHopSlippage?: bigint[];
23
+ }[];
24
+ constructor({ v2Routes, v3Routes, v4Routes, mixedRoutes, tradeType, }: {
25
+ v2Routes?: {
26
+ routev2: V2RouteSDK<TInput, TOutput>;
27
+ inputAmount: CurrencyAmount<TInput>;
28
+ outputAmount: CurrencyAmount<TOutput>;
29
+ maxHopSlippage?: bigint[];
30
+ }[];
31
+ v3Routes?: {
32
+ routev3: V3RouteSDK<TInput, TOutput>;
33
+ inputAmount: CurrencyAmount<TInput>;
34
+ outputAmount: CurrencyAmount<TOutput>;
35
+ maxHopSlippage?: bigint[];
36
+ }[];
37
+ v4Routes?: {
38
+ routev4: V4RouteSDK<TInput, TOutput>;
39
+ inputAmount: CurrencyAmount<TInput>;
40
+ outputAmount: CurrencyAmount<TOutput>;
41
+ maxHopSlippage?: bigint[];
42
+ }[];
43
+ mixedRoutes?: {
44
+ mixedRoute: MixedRouteSDK<TInput, TOutput>;
45
+ inputAmount: CurrencyAmount<TInput>;
46
+ outputAmount: CurrencyAmount<TOutput>;
47
+ maxHopSlippage?: bigint[];
48
+ }[];
49
+ tradeType: TTradeType;
50
+ });
51
+ get inputAmount(): CurrencyAmount<TInput>;
52
+ get outputAmount(): CurrencyAmount<TOutput>;
53
+ /**
54
+ * Returns the sum of all swaps within the trade
55
+ * @returns
56
+ * inputAmount: total input amount
57
+ * inputAmountNative: total amount of native currency required for ETH input paths
58
+ * - 0 if inputAmount is native but no native input paths
59
+ * - undefined if inputAmount is not native
60
+ * outputAmount: total output amount
61
+ * outputAmountNative: total amount of native currency returned from ETH output paths
62
+ * - 0 if outputAmount is native but no native output paths
63
+ * - undefined if outputAmount is not native
64
+ */
65
+ get amounts(): {
66
+ inputAmount: CurrencyAmount<TInput>;
67
+ inputAmountNative: CurrencyAmount<TInput> | undefined;
68
+ outputAmount: CurrencyAmount<TOutput>;
69
+ outputAmountNative: CurrencyAmount<TOutput> | undefined;
70
+ };
71
+ get numberOfInputWraps(): number;
72
+ get numberOfInputUnwraps(): number;
73
+ get nativeInputRoutes(): IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[];
74
+ get wethInputRoutes(): IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[];
75
+ private _executionPrice;
76
+ /**
77
+ * The price expressed in terms of output amount/input amount.
78
+ */
79
+ get executionPrice(): Price<TInput, TOutput>;
80
+ /**
81
+ * Returns the sell tax of the input token
82
+ */
83
+ get inputTax(): Percent;
84
+ /**
85
+ * Returns the buy tax of the output token
86
+ */
87
+ get outputTax(): Percent;
88
+ private isWrappedNative;
89
+ /**
90
+ * The cached result of the price impact computation
91
+ * @private
92
+ */
93
+ private _priceImpact;
94
+ /**
95
+ * Returns the percent difference between the route's mid price and the expected execution price
96
+ * In order to exclude token taxes from the price impact calculation, the spot price is calculated
97
+ * using a ratio of values that go into the pools, which are the post-tax input amount and pre-tax output amount.
98
+ */
99
+ get priceImpact(): Percent;
100
+ /**
101
+ * Get the minimum amount that must be received from this trade for the given slippage tolerance
102
+ * @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
103
+ * @returns The amount out
104
+ */
105
+ minimumAmountOut(slippageTolerance: Percent, amountOut?: CurrencyAmount<TOutput>): CurrencyAmount<TOutput>;
106
+ /**
107
+ * Get the maximum amount in that can be spent via this trade for the given slippage tolerance
108
+ * @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
109
+ * @returns The amount in
110
+ */
111
+ maximumAmountIn(slippageTolerance: Percent, amountIn?: CurrencyAmount<TInput>): CurrencyAmount<TInput>;
112
+ /**
113
+ * Return the execution price after accounting for slippage tolerance
114
+ * @param slippageTolerance the allowed tolerated slippage
115
+ * @returns The execution price
116
+ */
117
+ worstExecutionPrice(slippageTolerance: Percent): Price<TInput, TOutput>;
118
+ static fromRoutes<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(v2Routes: {
119
+ routev2: V2RouteSDK<TInput, TOutput>;
120
+ amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
121
+ maxHopSlippage?: bigint[];
122
+ }[], v3Routes: {
123
+ routev3: V3RouteSDK<TInput, TOutput>;
124
+ amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
125
+ maxHopSlippage?: bigint[];
126
+ }[], tradeType: TTradeType, mixedRoutes?: {
127
+ mixedRoute: MixedRouteSDK<TInput, TOutput>;
128
+ amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
129
+ maxHopSlippage?: bigint[];
130
+ }[], v4Routes?: {
131
+ routev4: V4RouteSDK<TInput, TOutput>;
132
+ amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
133
+ maxHopSlippage?: bigint[];
134
+ }[]): Promise<Trade<TInput, TOutput, TTradeType>>;
135
+ static fromRoute<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(route: V2RouteSDK<TInput, TOutput> | V3RouteSDK<TInput, TOutput> | V4RouteSDK<TInput, TOutput> | MixedRouteSDK<TInput, TOutput>, amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>, tradeType: TTradeType): Promise<Trade<TInput, TOutput, TTradeType>>;
136
+ }
@@ -0,0 +1,370 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Trade = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const sdk_core_1 = require("@uniswap/sdk-core");
6
+ const v2_sdk_1 = require("@uniswap/v2-sdk");
7
+ const v3_sdk_1 = require("@uniswap/v3-sdk");
8
+ const v4_sdk_1 = require("@uniswap/v4-sdk");
9
+ const tiny_invariant_1 = tslib_1.__importDefault(require("tiny-invariant"));
10
+ const constants_1 = require("../constants");
11
+ const route_1 = require("./mixedRoute/route");
12
+ const trade_1 = require("./mixedRoute/trade");
13
+ const route_2 = require("./route");
14
+ class Trade {
15
+ // construct a trade across v2 and v3 routes from pre-computed amounts
16
+ constructor({ v2Routes = [], v3Routes = [], v4Routes = [], mixedRoutes = [], tradeType, }) {
17
+ this.swaps = [];
18
+ this.routes = [];
19
+ // wrap v2 routes
20
+ for (const { routev2, inputAmount, outputAmount, maxHopSlippage } of v2Routes) {
21
+ const route = new route_2.RouteV2(routev2);
22
+ this.routes.push(route);
23
+ this.swaps.push({
24
+ route,
25
+ inputAmount,
26
+ outputAmount,
27
+ maxHopSlippage,
28
+ });
29
+ }
30
+ // wrap v3 routes
31
+ for (const { routev3, inputAmount, outputAmount, maxHopSlippage } of v3Routes) {
32
+ const route = new route_2.RouteV3(routev3);
33
+ this.routes.push(route);
34
+ this.swaps.push({
35
+ route,
36
+ inputAmount,
37
+ outputAmount,
38
+ maxHopSlippage,
39
+ });
40
+ }
41
+ // wrap v4 routes
42
+ for (const { routev4, inputAmount, outputAmount, maxHopSlippage } of v4Routes) {
43
+ const route = new route_2.RouteV4(routev4);
44
+ this.routes.push(route);
45
+ this.swaps.push({
46
+ route,
47
+ inputAmount,
48
+ outputAmount,
49
+ maxHopSlippage,
50
+ });
51
+ }
52
+ for (const { mixedRoute, inputAmount, outputAmount, maxHopSlippage } of mixedRoutes) {
53
+ const route = new route_2.MixedRoute(mixedRoute);
54
+ this.routes.push(route);
55
+ this.swaps.push({
56
+ route,
57
+ inputAmount,
58
+ outputAmount,
59
+ maxHopSlippage,
60
+ });
61
+ }
62
+ if (this.swaps.length === 0) {
63
+ throw new Error('No routes provided when calling Trade constructor');
64
+ }
65
+ this.tradeType = tradeType;
66
+ // each route must have the same input and output currency
67
+ const inputCurrency = this.swaps[0].inputAmount.currency;
68
+ const outputCurrency = this.swaps[0].outputAmount.currency;
69
+ (0, tiny_invariant_1.default)(this.swaps.every(({ route }) => inputCurrency.wrapped.equals(route.input.wrapped)), 'INPUT_CURRENCY_MATCH');
70
+ (0, tiny_invariant_1.default)(this.swaps.every(({ route }) => outputCurrency.wrapped.equals(route.output.wrapped)), 'OUTPUT_CURRENCY_MATCH');
71
+ // pools must be unique inter protocols
72
+ const numPools = this.swaps.map(({ route }) => route.pools.length).reduce((total, cur) => total + cur, 0);
73
+ const poolIdentifierSet = new Set();
74
+ for (const { route } of this.swaps) {
75
+ for (const pool of route.pools) {
76
+ if (pool instanceof v4_sdk_1.Pool) {
77
+ poolIdentifierSet.add(pool.poolId);
78
+ }
79
+ else if (pool instanceof v3_sdk_1.Pool) {
80
+ poolIdentifierSet.add(v3_sdk_1.Pool.getAddress(pool.token0, pool.token1, pool.fee));
81
+ }
82
+ else if (pool instanceof v2_sdk_1.Pair) {
83
+ const pair = pool;
84
+ poolIdentifierSet.add(v2_sdk_1.Pair.getAddress(pair.token0, pair.token1));
85
+ }
86
+ else {
87
+ throw new Error('Unexpected pool type in route when constructing trade object');
88
+ }
89
+ }
90
+ }
91
+ (0, tiny_invariant_1.default)(numPools === poolIdentifierSet.size, 'POOLS_DUPLICATED');
92
+ }
93
+ get inputAmount() {
94
+ if (this._inputAmount) {
95
+ return this._inputAmount;
96
+ }
97
+ const inputAmountCurrency = this.swaps[0].inputAmount.currency;
98
+ const totalInputFromRoutes = this.swaps
99
+ .map(({ inputAmount: routeInputAmount }) => routeInputAmount)
100
+ .reduce((total, cur) => total.add(cur), sdk_core_1.CurrencyAmount.fromRawAmount(inputAmountCurrency, 0));
101
+ this._inputAmount = totalInputFromRoutes;
102
+ return this._inputAmount;
103
+ }
104
+ get outputAmount() {
105
+ if (this._outputAmount) {
106
+ return this._outputAmount;
107
+ }
108
+ const outputCurrency = this.swaps[0].outputAmount.currency;
109
+ const totalOutputFromRoutes = this.swaps
110
+ .map(({ outputAmount: routeOutputAmount }) => routeOutputAmount)
111
+ .reduce((total, cur) => total.add(cur), sdk_core_1.CurrencyAmount.fromRawAmount(outputCurrency, 0));
112
+ this._outputAmount = totalOutputFromRoutes;
113
+ return this._outputAmount;
114
+ }
115
+ /**
116
+ * Returns the sum of all swaps within the trade
117
+ * @returns
118
+ * inputAmount: total input amount
119
+ * inputAmountNative: total amount of native currency required for ETH input paths
120
+ * - 0 if inputAmount is native but no native input paths
121
+ * - undefined if inputAmount is not native
122
+ * outputAmount: total output amount
123
+ * outputAmountNative: total amount of native currency returned from ETH output paths
124
+ * - 0 if outputAmount is native but no native output paths
125
+ * - undefined if outputAmount is not native
126
+ */
127
+ get amounts() {
128
+ var _a, _b;
129
+ // Find native currencies for reduce below
130
+ const inputNativeCurrency = (_a = this.swaps.find(({ inputAmount }) => inputAmount.currency.isNative)) === null || _a === void 0 ? void 0 : _a.inputAmount.currency;
131
+ const outputNativeCurrency = (_b = this.swaps.find(({ outputAmount }) => outputAmount.currency.isNative)) === null || _b === void 0 ? void 0 : _b.outputAmount.currency;
132
+ return {
133
+ inputAmount: this.inputAmount,
134
+ inputAmountNative: inputNativeCurrency
135
+ ? this.swaps.reduce((total, swap) => {
136
+ return swap.route.pathInput.isNative ? total.add(swap.inputAmount) : total;
137
+ }, sdk_core_1.CurrencyAmount.fromRawAmount(inputNativeCurrency, 0))
138
+ : undefined,
139
+ outputAmount: this.outputAmount,
140
+ outputAmountNative: outputNativeCurrency
141
+ ? this.swaps.reduce((total, swap) => {
142
+ return swap.route.pathOutput.isNative ? total.add(swap.outputAmount) : total;
143
+ }, sdk_core_1.CurrencyAmount.fromRawAmount(outputNativeCurrency, 0))
144
+ : undefined,
145
+ };
146
+ }
147
+ get numberOfInputWraps() {
148
+ // if the trade's input is eth it may require a wrap
149
+ if (this.inputAmount.currency.isNative) {
150
+ return this.wethInputRoutes.length;
151
+ }
152
+ else
153
+ return 0;
154
+ }
155
+ get numberOfInputUnwraps() {
156
+ // if the trade's input is weth, it may require an unwrap
157
+ if (this.isWrappedNative(this.inputAmount.currency)) {
158
+ return this.nativeInputRoutes.length;
159
+ }
160
+ else
161
+ return 0;
162
+ }
163
+ get nativeInputRoutes() {
164
+ if (this._nativeInputRoutes) {
165
+ return this._nativeInputRoutes;
166
+ }
167
+ this._nativeInputRoutes = this.routes.filter((route) => route.pathInput.isNative);
168
+ return this._nativeInputRoutes;
169
+ }
170
+ get wethInputRoutes() {
171
+ if (this._wethInputRoutes) {
172
+ return this._wethInputRoutes;
173
+ }
174
+ this._wethInputRoutes = this.routes.filter((route) => this.isWrappedNative(route.pathInput));
175
+ return this._wethInputRoutes;
176
+ }
177
+ /**
178
+ * The price expressed in terms of output amount/input amount.
179
+ */
180
+ get executionPrice() {
181
+ var _a;
182
+ return ((_a = this._executionPrice) !== null && _a !== void 0 ? _a : (this._executionPrice = new sdk_core_1.Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient)));
183
+ }
184
+ /**
185
+ * Returns the sell tax of the input token
186
+ */
187
+ get inputTax() {
188
+ const inputCurrency = this.inputAmount.currency;
189
+ if (inputCurrency.isNative || !inputCurrency.wrapped.sellFeeBps)
190
+ return constants_1.ZERO_PERCENT;
191
+ return new sdk_core_1.Percent(inputCurrency.wrapped.sellFeeBps.toNumber(), 10000);
192
+ }
193
+ /**
194
+ * Returns the buy tax of the output token
195
+ */
196
+ get outputTax() {
197
+ const outputCurrency = this.outputAmount.currency;
198
+ if (outputCurrency.isNative || !outputCurrency.wrapped.buyFeeBps)
199
+ return constants_1.ZERO_PERCENT;
200
+ return new sdk_core_1.Percent(outputCurrency.wrapped.buyFeeBps.toNumber(), 10000);
201
+ }
202
+ isWrappedNative(currency) {
203
+ const chainId = currency.chainId;
204
+ return currency.equals(sdk_core_1.Ether.onChain(chainId).wrapped);
205
+ }
206
+ /**
207
+ * Returns the percent difference between the route's mid price and the expected execution price
208
+ * In order to exclude token taxes from the price impact calculation, the spot price is calculated
209
+ * using a ratio of values that go into the pools, which are the post-tax input amount and pre-tax output amount.
210
+ */
211
+ get priceImpact() {
212
+ if (this._priceImpact) {
213
+ return this._priceImpact;
214
+ }
215
+ // returns 0% price impact even though this may be inaccurate as a swap may have occured.
216
+ // because we're unable to derive the pre-buy-tax amount, use 0% as a placeholder.
217
+ if (this.outputTax.equalTo(constants_1.ONE_HUNDRED_PERCENT))
218
+ return constants_1.ZERO_PERCENT;
219
+ let spotOutputAmount = sdk_core_1.CurrencyAmount.fromRawAmount(this.outputAmount.currency, 0);
220
+ for (const { route, inputAmount } of this.swaps) {
221
+ const midPrice = route.midPrice;
222
+ const postTaxInputAmount = inputAmount.multiply(new sdk_core_1.Fraction(constants_1.ONE).subtract(this.inputTax));
223
+ spotOutputAmount = spotOutputAmount.add(midPrice.quote(postTaxInputAmount));
224
+ }
225
+ // if the total output of this trade is 0, then most likely the post-tax input was also 0, and therefore this swap
226
+ // does not move the pools' market price
227
+ if (spotOutputAmount.equalTo(constants_1.ZERO))
228
+ return constants_1.ZERO_PERCENT;
229
+ const preTaxOutputAmount = this.outputAmount.divide(new sdk_core_1.Fraction(constants_1.ONE).subtract(this.outputTax));
230
+ const priceImpact = spotOutputAmount.subtract(preTaxOutputAmount).divide(spotOutputAmount);
231
+ this._priceImpact = new sdk_core_1.Percent(priceImpact.numerator, priceImpact.denominator);
232
+ return this._priceImpact;
233
+ }
234
+ /**
235
+ * Get the minimum amount that must be received from this trade for the given slippage tolerance
236
+ * @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
237
+ * @returns The amount out
238
+ */
239
+ minimumAmountOut(slippageTolerance, amountOut = this.outputAmount) {
240
+ (0, tiny_invariant_1.default)(!slippageTolerance.lessThan(constants_1.ZERO), 'SLIPPAGE_TOLERANCE');
241
+ if (this.tradeType === sdk_core_1.TradeType.EXACT_OUTPUT) {
242
+ return amountOut;
243
+ }
244
+ else {
245
+ const slippageAdjustedAmountOut = new sdk_core_1.Fraction(constants_1.ONE)
246
+ .add(slippageTolerance)
247
+ .invert()
248
+ .multiply(amountOut.quotient).quotient;
249
+ return sdk_core_1.CurrencyAmount.fromRawAmount(amountOut.currency, slippageAdjustedAmountOut);
250
+ }
251
+ }
252
+ /**
253
+ * Get the maximum amount in that can be spent via this trade for the given slippage tolerance
254
+ * @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
255
+ * @returns The amount in
256
+ */
257
+ maximumAmountIn(slippageTolerance, amountIn = this.inputAmount) {
258
+ (0, tiny_invariant_1.default)(!slippageTolerance.lessThan(constants_1.ZERO), 'SLIPPAGE_TOLERANCE');
259
+ if (this.tradeType === sdk_core_1.TradeType.EXACT_INPUT) {
260
+ return amountIn;
261
+ }
262
+ else {
263
+ const slippageAdjustedAmountIn = new sdk_core_1.Fraction(constants_1.ONE).add(slippageTolerance).multiply(amountIn.quotient).quotient;
264
+ return sdk_core_1.CurrencyAmount.fromRawAmount(amountIn.currency, slippageAdjustedAmountIn);
265
+ }
266
+ }
267
+ /**
268
+ * Return the execution price after accounting for slippage tolerance
269
+ * @param slippageTolerance the allowed tolerated slippage
270
+ * @returns The execution price
271
+ */
272
+ worstExecutionPrice(slippageTolerance) {
273
+ return new sdk_core_1.Price(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
274
+ }
275
+ static async fromRoutes(v2Routes, v3Routes, tradeType, mixedRoutes, v4Routes) {
276
+ const populatedV2Routes = [];
277
+ const populatedV3Routes = [];
278
+ const populatedV4Routes = [];
279
+ const populatedMixedRoutes = [];
280
+ for (const { routev2, amount, maxHopSlippage } of v2Routes) {
281
+ const v2Trade = new v2_sdk_1.Trade(routev2, amount, tradeType);
282
+ const { inputAmount, outputAmount } = v2Trade;
283
+ populatedV2Routes.push({
284
+ routev2,
285
+ inputAmount,
286
+ outputAmount,
287
+ maxHopSlippage,
288
+ });
289
+ }
290
+ for (const { routev3, amount, maxHopSlippage } of v3Routes) {
291
+ const v3Trade = await v3_sdk_1.Trade.fromRoute(routev3, amount, tradeType);
292
+ const { inputAmount, outputAmount } = v3Trade;
293
+ populatedV3Routes.push({
294
+ routev3,
295
+ inputAmount,
296
+ outputAmount,
297
+ maxHopSlippage,
298
+ });
299
+ }
300
+ if (v4Routes) {
301
+ for (const { routev4, amount, maxHopSlippage } of v4Routes) {
302
+ const v4Trade = await v4_sdk_1.Trade.fromRoute(routev4, amount, tradeType);
303
+ const { inputAmount, outputAmount } = v4Trade;
304
+ populatedV4Routes.push({
305
+ routev4,
306
+ inputAmount,
307
+ outputAmount,
308
+ maxHopSlippage,
309
+ });
310
+ }
311
+ }
312
+ if (mixedRoutes) {
313
+ for (const { mixedRoute, amount, maxHopSlippage } of mixedRoutes) {
314
+ const mixedRouteTrade = await trade_1.MixedRouteTrade.fromRoute(mixedRoute, amount, tradeType);
315
+ const { inputAmount, outputAmount } = mixedRouteTrade;
316
+ populatedMixedRoutes.push({
317
+ mixedRoute,
318
+ inputAmount,
319
+ outputAmount,
320
+ maxHopSlippage,
321
+ });
322
+ }
323
+ }
324
+ return new Trade({
325
+ v2Routes: populatedV2Routes,
326
+ v3Routes: populatedV3Routes,
327
+ v4Routes: populatedV4Routes,
328
+ mixedRoutes: populatedMixedRoutes,
329
+ tradeType,
330
+ });
331
+ }
332
+ static async fromRoute(route, amount, tradeType) {
333
+ let v2Routes = [];
334
+ let v3Routes = [];
335
+ let v4Routes = [];
336
+ let mixedRoutes = [];
337
+ if (route instanceof v2_sdk_1.Route) {
338
+ const v2Trade = new v2_sdk_1.Trade(route, amount, tradeType);
339
+ const { inputAmount, outputAmount } = v2Trade;
340
+ v2Routes = [{ routev2: route, inputAmount, outputAmount }];
341
+ }
342
+ else if (route instanceof v3_sdk_1.Route) {
343
+ const v3Trade = await v3_sdk_1.Trade.fromRoute(route, amount, tradeType);
344
+ const { inputAmount, outputAmount } = v3Trade;
345
+ v3Routes = [{ routev3: route, inputAmount, outputAmount }];
346
+ }
347
+ else if (route instanceof v4_sdk_1.Route) {
348
+ const v4Trade = await v4_sdk_1.Trade.fromRoute(route, amount, tradeType);
349
+ const { inputAmount, outputAmount } = v4Trade;
350
+ v4Routes = [{ routev4: route, inputAmount, outputAmount }];
351
+ }
352
+ else if (route instanceof route_1.MixedRouteSDK) {
353
+ const mixedRouteTrade = await trade_1.MixedRouteTrade.fromRoute(route, amount, tradeType);
354
+ const { inputAmount, outputAmount } = mixedRouteTrade;
355
+ mixedRoutes = [{ mixedRoute: route, inputAmount, outputAmount }];
356
+ }
357
+ else {
358
+ throw new Error('Invalid route type');
359
+ }
360
+ return new Trade({
361
+ v2Routes,
362
+ v3Routes,
363
+ v4Routes,
364
+ mixedRoutes,
365
+ tradeType,
366
+ });
367
+ }
368
+ }
369
+ exports.Trade = Trade;
370
+ //# sourceMappingURL=trade.js.map