@uniswap/router-sdk 1.23.0 → 2.0.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.
- package/dist/{approveAndCall.d.ts → cjs/src/approveAndCall.d.ts} +33 -33
- package/dist/cjs/src/approveAndCall.js +111 -0
- package/dist/cjs/src/approveAndCall.js.map +1 -0
- package/dist/{constants.d.ts → cjs/src/constants.d.ts} +13 -13
- package/dist/cjs/src/constants.js +22 -0
- package/dist/cjs/src/constants.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/mixedRoute/route.d.ts +29 -29
- package/dist/cjs/src/entities/mixedRoute/route.js +119 -0
- package/dist/cjs/src/entities/mixedRoute/route.js.map +1 -0
- package/dist/cjs/src/entities/mixedRoute/route.test.d.ts +1 -0
- package/dist/cjs/src/entities/mixedRoute/route.test.js +469 -0
- package/dist/cjs/src/entities/mixedRoute/route.test.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/mixedRoute/trade.d.ts +183 -183
- package/dist/cjs/src/entities/mixedRoute/trade.js +357 -0
- package/dist/cjs/src/entities/mixedRoute/trade.js.map +1 -0
- package/dist/cjs/src/entities/mixedRoute/trade.test.d.ts +1 -0
- package/dist/cjs/src/entities/mixedRoute/trade.test.js +1010 -0
- package/dist/cjs/src/entities/mixedRoute/trade.test.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/protocol.d.ts +6 -6
- package/dist/cjs/src/entities/protocol.js +11 -0
- package/dist/cjs/src/entities/protocol.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/route.d.ts +40 -40
- package/dist/cjs/src/entities/route.js +63 -0
- package/dist/cjs/src/entities/route.js.map +1 -0
- package/dist/cjs/src/entities/route.test.d.ts +1 -0
- package/dist/cjs/src/entities/route.test.js +192 -0
- package/dist/cjs/src/entities/route.test.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/trade.d.ts +127 -127
- package/dist/cjs/src/entities/trade.js +366 -0
- package/dist/cjs/src/entities/trade.js.map +1 -0
- package/dist/cjs/src/entities/trade.test.d.ts +1 -0
- package/dist/cjs/src/entities/trade.test.js +1267 -0
- package/dist/cjs/src/entities/trade.test.js.map +1 -0
- package/dist/{index.d.ts → cjs/src/index.d.ts} +14 -14
- package/dist/cjs/src/index.js +18 -0
- package/dist/cjs/src/index.js.map +1 -0
- package/dist/{multicallExtended.d.ts → cjs/src/multicallExtended.d.ts} +11 -11
- package/dist/cjs/src/multicallExtended.js +43 -0
- package/dist/cjs/src/multicallExtended.js.map +1 -0
- package/dist/cjs/src/multicallExtended.test.d.ts +1 -0
- package/dist/cjs/src/multicallExtended.test.js +22 -0
- package/dist/cjs/src/multicallExtended.test.js.map +1 -0
- package/dist/{paymentsExtended.d.ts → cjs/src/paymentsExtended.d.ts} +15 -15
- package/dist/cjs/src/paymentsExtended.js +65 -0
- package/dist/cjs/src/paymentsExtended.js.map +1 -0
- package/dist/cjs/src/paymentsExtended.test.d.ts +1 -0
- package/dist/cjs/src/paymentsExtended.test.js +60 -0
- package/dist/cjs/src/paymentsExtended.test.js.map +1 -0
- package/dist/{swapRouter.d.ts → cjs/src/swapRouter.d.ts} +95 -95
- package/dist/cjs/src/swapRouter.js +439 -0
- package/dist/cjs/src/swapRouter.js.map +1 -0
- package/dist/cjs/src/swapRouter.test.d.ts +1 -0
- package/dist/cjs/src/swapRouter.test.js +1161 -0
- package/dist/cjs/src/swapRouter.test.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/TPool.d.ts +4 -4
- package/dist/cjs/src/utils/TPool.js +3 -0
- package/dist/cjs/src/utils/TPool.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/encodeMixedRouteToPath.d.ts +9 -9
- package/dist/cjs/src/utils/encodeMixedRouteToPath.js +90 -0
- package/dist/cjs/src/utils/encodeMixedRouteToPath.js.map +1 -0
- package/dist/cjs/src/utils/encodeMixedRouteToPath.test.d.ts +1 -0
- package/dist/cjs/src/utils/encodeMixedRouteToPath.test.js +136 -0
- package/dist/cjs/src/utils/encodeMixedRouteToPath.test.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/index.d.ts +16 -16
- package/dist/cjs/src/utils/index.js +51 -0
- package/dist/cjs/src/utils/index.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/pathCurrency.d.ts +4 -4
- package/dist/cjs/src/utils/pathCurrency.js +35 -0
- package/dist/cjs/src/utils/pathCurrency.js.map +1 -0
- package/dist/cjs/src/utils/pathCurrency.test.d.ts +1 -0
- package/dist/cjs/src/utils/pathCurrency.test.js +17 -0
- package/dist/cjs/src/utils/pathCurrency.test.js.map +1 -0
- package/dist/esm/src/approveAndCall.d.ts +33 -0
- package/dist/esm/src/approveAndCall.js +105 -0
- package/dist/esm/src/approveAndCall.js.map +1 -0
- package/dist/esm/src/constants.d.ts +13 -0
- package/dist/esm/src/constants.js +18 -0
- package/dist/esm/src/constants.js.map +1 -0
- package/dist/esm/src/entities/mixedRoute/route.d.ts +29 -0
- package/dist/esm/src/entities/mixedRoute/route.js +114 -0
- package/dist/esm/src/entities/mixedRoute/route.js.map +1 -0
- package/dist/esm/src/entities/mixedRoute/route.test.d.ts +1 -0
- package/dist/esm/src/entities/mixedRoute/route.test.js +467 -0
- package/dist/esm/src/entities/mixedRoute/route.test.js.map +1 -0
- package/dist/esm/src/entities/mixedRoute/trade.d.ts +183 -0
- package/dist/esm/src/entities/mixedRoute/trade.js +352 -0
- package/dist/esm/src/entities/mixedRoute/trade.js.map +1 -0
- package/dist/esm/src/entities/mixedRoute/trade.test.d.ts +1 -0
- package/dist/esm/src/entities/mixedRoute/trade.test.js +1008 -0
- package/dist/esm/src/entities/mixedRoute/trade.test.js.map +1 -0
- package/dist/esm/src/entities/protocol.d.ts +6 -0
- package/dist/esm/src/entities/protocol.js +8 -0
- package/dist/esm/src/entities/protocol.js.map +1 -0
- package/dist/esm/src/entities/route.d.ts +40 -0
- package/dist/esm/src/entities/route.js +55 -0
- package/dist/esm/src/entities/route.js.map +1 -0
- package/dist/esm/src/entities/route.test.d.ts +1 -0
- package/dist/esm/src/entities/route.test.js +190 -0
- package/dist/esm/src/entities/route.test.js.map +1 -0
- package/dist/esm/src/entities/trade.d.ts +127 -0
- package/dist/esm/src/entities/trade.js +362 -0
- package/dist/esm/src/entities/trade.js.map +1 -0
- package/dist/esm/src/entities/trade.test.d.ts +1 -0
- package/dist/esm/src/entities/trade.test.js +1265 -0
- package/dist/esm/src/entities/trade.test.js.map +1 -0
- package/dist/esm/src/index.d.ts +14 -0
- package/dist/esm/src/index.js +15 -0
- package/dist/esm/src/index.js.map +1 -0
- package/dist/esm/src/multicallExtended.d.ts +11 -0
- package/dist/esm/src/multicallExtended.js +39 -0
- package/dist/esm/src/multicallExtended.js.map +1 -0
- package/dist/esm/src/multicallExtended.test.d.ts +1 -0
- package/dist/esm/src/multicallExtended.test.js +20 -0
- package/dist/esm/src/multicallExtended.test.js.map +1 -0
- package/dist/esm/src/paymentsExtended.d.ts +15 -0
- package/dist/esm/src/paymentsExtended.js +61 -0
- package/dist/esm/src/paymentsExtended.js.map +1 -0
- package/dist/esm/src/paymentsExtended.test.d.ts +1 -0
- package/dist/esm/src/paymentsExtended.test.js +57 -0
- package/dist/esm/src/paymentsExtended.test.js.map +1 -0
- package/dist/esm/src/swapRouter.d.ts +95 -0
- package/dist/esm/src/swapRouter.js +434 -0
- package/dist/esm/src/swapRouter.js.map +1 -0
- package/dist/esm/src/swapRouter.test.d.ts +1 -0
- package/dist/esm/src/swapRouter.test.js +1159 -0
- package/dist/esm/src/swapRouter.test.js.map +1 -0
- package/dist/esm/src/utils/TPool.d.ts +4 -0
- package/dist/esm/src/utils/TPool.js +2 -0
- package/dist/esm/src/utils/TPool.js.map +1 -0
- package/dist/esm/src/utils/encodeMixedRouteToPath.d.ts +9 -0
- package/dist/esm/src/utils/encodeMixedRouteToPath.js +86 -0
- package/dist/esm/src/utils/encodeMixedRouteToPath.js.map +1 -0
- package/dist/esm/src/utils/encodeMixedRouteToPath.test.d.ts +1 -0
- package/dist/esm/src/utils/encodeMixedRouteToPath.test.js +134 -0
- package/dist/esm/src/utils/encodeMixedRouteToPath.test.js.map +1 -0
- package/dist/esm/src/utils/index.d.ts +16 -0
- package/dist/esm/src/utils/index.js +46 -0
- package/dist/esm/src/utils/index.js.map +1 -0
- package/dist/esm/src/utils/pathCurrency.d.ts +4 -0
- package/dist/esm/src/utils/pathCurrency.js +30 -0
- package/dist/esm/src/utils/pathCurrency.js.map +1 -0
- package/dist/esm/src/utils/pathCurrency.test.d.ts +1 -0
- package/dist/esm/src/utils/pathCurrency.test.js +15 -0
- package/dist/esm/src/utils/pathCurrency.test.js.map +1 -0
- package/dist/types/src/approveAndCall.d.ts +33 -0
- package/dist/types/src/constants.d.ts +13 -0
- package/dist/types/src/entities/mixedRoute/route.d.ts +29 -0
- package/dist/types/src/entities/mixedRoute/route.test.d.ts +1 -0
- package/dist/types/src/entities/mixedRoute/trade.d.ts +183 -0
- package/dist/types/src/entities/mixedRoute/trade.test.d.ts +1 -0
- package/dist/types/src/entities/protocol.d.ts +6 -0
- package/dist/types/src/entities/route.d.ts +40 -0
- package/dist/types/src/entities/route.test.d.ts +1 -0
- package/dist/types/src/entities/trade.d.ts +127 -0
- package/dist/types/src/entities/trade.test.d.ts +1 -0
- package/dist/types/src/index.d.ts +14 -0
- package/dist/types/src/multicallExtended.d.ts +11 -0
- package/dist/types/src/multicallExtended.test.d.ts +1 -0
- package/dist/types/src/paymentsExtended.d.ts +15 -0
- package/dist/types/src/paymentsExtended.test.d.ts +1 -0
- package/dist/types/src/swapRouter.d.ts +95 -0
- package/dist/types/src/swapRouter.test.d.ts +1 -0
- package/dist/types/src/utils/TPool.d.ts +4 -0
- package/dist/types/src/utils/encodeMixedRouteToPath.d.ts +9 -0
- package/dist/types/src/utils/encodeMixedRouteToPath.test.d.ts +1 -0
- package/dist/types/src/utils/index.d.ts +16 -0
- package/dist/types/src/utils/pathCurrency.d.ts +4 -0
- package/dist/types/src/utils/pathCurrency.test.d.ts +1 -0
- package/package.json +29 -14
- package/dist/index.js +0 -8
- package/dist/router-sdk.cjs.development.js +0 -2485
- package/dist/router-sdk.cjs.development.js.map +0 -1
- package/dist/router-sdk.cjs.production.min.js +0 -2
- package/dist/router-sdk.cjs.production.min.js.map +0 -1
- package/dist/router-sdk.esm.js +0 -2452
- package/dist/router-sdk.esm.js.map +0 -1
|
@@ -0,0 +1,1010 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const sdk_core_1 = require("@uniswap/sdk-core");
|
|
5
|
+
const v2_sdk_1 = require("@uniswap/v2-sdk");
|
|
6
|
+
const v3_sdk_1 = require("@uniswap/v3-sdk");
|
|
7
|
+
const v4_sdk_1 = require("@uniswap/v4-sdk");
|
|
8
|
+
const jsbi_1 = tslib_1.__importDefault(require("jsbi"));
|
|
9
|
+
const route_1 = require("./route");
|
|
10
|
+
const trade_1 = require("./trade");
|
|
11
|
+
const constants_1 = require("../../constants");
|
|
12
|
+
describe('MixedRouteTrade', () => {
|
|
13
|
+
const ETHER = sdk_core_1.Ether.onChain(1);
|
|
14
|
+
const token0 = new sdk_core_1.Token(1, '0x0000000000000000000000000000000000000001', 18, 't0', 'token0');
|
|
15
|
+
const token1 = new sdk_core_1.Token(1, '0x0000000000000000000000000000000000000002', 18, 't1', 'token1');
|
|
16
|
+
const token2 = new sdk_core_1.Token(1, '0x0000000000000000000000000000000000000003', 18, 't2', 'token2');
|
|
17
|
+
const token3 = new sdk_core_1.Token(1, '0x0000000000000000000000000000000000000004', 18, 't3', 'token3');
|
|
18
|
+
function v2StylePool(reserve0, reserve1, v4Pool = false) {
|
|
19
|
+
const currency0 = reserve0.currency;
|
|
20
|
+
const currency1 = reserve1.currency;
|
|
21
|
+
const fee = v3_sdk_1.FeeAmount.MEDIUM;
|
|
22
|
+
const sqrtRatioX96 = (0, v3_sdk_1.encodeSqrtRatioX96)(reserve1.quotient, reserve0.quotient);
|
|
23
|
+
const liquidity = (0, sdk_core_1.sqrt)(jsbi_1.default.multiply(reserve0.quotient, reserve1.quotient));
|
|
24
|
+
const tick = v3_sdk_1.TickMath.getTickAtSqrtRatio(sqrtRatioX96);
|
|
25
|
+
const tickSpacing = v3_sdk_1.TICK_SPACINGS[fee];
|
|
26
|
+
const tickBitmap = [
|
|
27
|
+
{
|
|
28
|
+
index: (0, v3_sdk_1.nearestUsableTick)(v3_sdk_1.TickMath.MIN_TICK, tickSpacing),
|
|
29
|
+
liquidityNet: liquidity,
|
|
30
|
+
liquidityGross: liquidity,
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
index: (0, v3_sdk_1.nearestUsableTick)(v3_sdk_1.TickMath.MAX_TICK, tickSpacing),
|
|
34
|
+
liquidityNet: jsbi_1.default.multiply(liquidity, jsbi_1.default.BigInt(-1)),
|
|
35
|
+
liquidityGross: liquidity,
|
|
36
|
+
},
|
|
37
|
+
];
|
|
38
|
+
if (!v4Pool) {
|
|
39
|
+
return new v3_sdk_1.Pool(currency0.wrapped, currency1.wrapped, fee, sqrtRatioX96, liquidity, tick, tickBitmap);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
return new v4_sdk_1.Pool(currency0, currency1, fee, tickSpacing, constants_1.ADDRESS_ZERO, sqrtRatioX96, liquidity, tick, tickBitmap);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const pool_v4_0_weth = v2StylePool(sdk_core_1.CurrencyAmount.fromRawAmount(token0, 120000), sdk_core_1.CurrencyAmount.fromRawAmount(sdk_core_1.WETH9[1], 130000), true);
|
|
46
|
+
const pool_v4_0_eth = v2StylePool(sdk_core_1.CurrencyAmount.fromRawAmount(token0, 120000), sdk_core_1.CurrencyAmount.fromRawAmount(ETHER, 130000), true);
|
|
47
|
+
const pool_v3_0_1 = v2StylePool(sdk_core_1.CurrencyAmount.fromRawAmount(token0, 100000), sdk_core_1.CurrencyAmount.fromRawAmount(token1, 100000));
|
|
48
|
+
const pool_v3_0_2 = v2StylePool(sdk_core_1.CurrencyAmount.fromRawAmount(token0, 100000), sdk_core_1.CurrencyAmount.fromRawAmount(token2, 110000));
|
|
49
|
+
const pool_v3_0_3 = v2StylePool(sdk_core_1.CurrencyAmount.fromRawAmount(token0, 100000), sdk_core_1.CurrencyAmount.fromRawAmount(token3, 90000));
|
|
50
|
+
const pool_v3_1_2 = v2StylePool(sdk_core_1.CurrencyAmount.fromRawAmount(token1, 120000), sdk_core_1.CurrencyAmount.fromRawAmount(token2, 100000));
|
|
51
|
+
const pool_v3_1_3 = v2StylePool(sdk_core_1.CurrencyAmount.fromRawAmount(token1, 120000), sdk_core_1.CurrencyAmount.fromRawAmount(token3, 130000));
|
|
52
|
+
const pool_v3_weth_0 = v2StylePool(sdk_core_1.CurrencyAmount.fromRawAmount(sdk_core_1.WETH9[1], jsbi_1.default.BigInt(100000)), sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100000)));
|
|
53
|
+
const pool_v3_weth_1 = v2StylePool(sdk_core_1.CurrencyAmount.fromRawAmount(sdk_core_1.WETH9[1], jsbi_1.default.BigInt(100000)), sdk_core_1.CurrencyAmount.fromRawAmount(token1, jsbi_1.default.BigInt(100000)));
|
|
54
|
+
const pool_v3_weth_2 = v2StylePool(sdk_core_1.CurrencyAmount.fromRawAmount(sdk_core_1.WETH9[1], jsbi_1.default.BigInt(100000)), sdk_core_1.CurrencyAmount.fromRawAmount(token2, jsbi_1.default.BigInt(100000)));
|
|
55
|
+
const pair_0_1 = new v2_sdk_1.Pair(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(1000)), sdk_core_1.CurrencyAmount.fromRawAmount(token1, jsbi_1.default.BigInt(1000)));
|
|
56
|
+
const pair_0_2 = new v2_sdk_1.Pair(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(1000)), sdk_core_1.CurrencyAmount.fromRawAmount(token2, jsbi_1.default.BigInt(1100)));
|
|
57
|
+
const pair_0_3 = new v2_sdk_1.Pair(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(1000)), sdk_core_1.CurrencyAmount.fromRawAmount(token3, jsbi_1.default.BigInt(900)));
|
|
58
|
+
const pair_1_2 = new v2_sdk_1.Pair(sdk_core_1.CurrencyAmount.fromRawAmount(token1, jsbi_1.default.BigInt(1200)), sdk_core_1.CurrencyAmount.fromRawAmount(token2, jsbi_1.default.BigInt(1000)));
|
|
59
|
+
const pair_1_3 = new v2_sdk_1.Pair(sdk_core_1.CurrencyAmount.fromRawAmount(token1, jsbi_1.default.BigInt(1200)), sdk_core_1.CurrencyAmount.fromRawAmount(token3, jsbi_1.default.BigInt(1300)));
|
|
60
|
+
const pair_weth_0 = new v2_sdk_1.Pair(sdk_core_1.CurrencyAmount.fromRawAmount(sdk_core_1.WETH9[1], jsbi_1.default.BigInt(1000)), sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(1000)));
|
|
61
|
+
const empty_pair_0_1 = new v2_sdk_1.Pair(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(0)), sdk_core_1.CurrencyAmount.fromRawAmount(token1, jsbi_1.default.BigInt(0)));
|
|
62
|
+
/// @dev copied over from v3-sdk trade.test.ts
|
|
63
|
+
describe('is backwards compatible with pure v3 routes', () => {
|
|
64
|
+
describe('#fromRoute', () => {
|
|
65
|
+
it('can be constructed with ETHER as input', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
66
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pool_v3_weth_0], ETHER, token0), sdk_core_1.CurrencyAmount.fromRawAmount(sdk_core_1.Ether.onChain(1), jsbi_1.default.BigInt(10000)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
67
|
+
expect(trade.inputAmount.currency).toEqual(ETHER);
|
|
68
|
+
expect(trade.outputAmount.currency).toEqual(token0);
|
|
69
|
+
}));
|
|
70
|
+
it('can be constructed with ETHER as output for exact input', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
71
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pool_v3_weth_0], token0, ETHER), sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10000)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
72
|
+
expect(trade.inputAmount.currency).toEqual(token0);
|
|
73
|
+
expect(trade.outputAmount.currency).toEqual(ETHER);
|
|
74
|
+
}));
|
|
75
|
+
it('throws regardless for exact output', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
76
|
+
yield expect(trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pool_v3_weth_0], ETHER, token0), sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10000)), sdk_core_1.TradeType.EXACT_OUTPUT)).rejects.toThrow('TRADE_TYPE');
|
|
77
|
+
}));
|
|
78
|
+
});
|
|
79
|
+
describe('#fromRoutes', () => {
|
|
80
|
+
it('can be constructed with ETHER as input with multiple routes', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
81
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoutes([
|
|
82
|
+
{
|
|
83
|
+
amount: sdk_core_1.CurrencyAmount.fromRawAmount(sdk_core_1.Ether.onChain(1), jsbi_1.default.BigInt(10000)),
|
|
84
|
+
route: new route_1.MixedRouteSDK([pool_v3_weth_0], ETHER, token0),
|
|
85
|
+
},
|
|
86
|
+
], sdk_core_1.TradeType.EXACT_INPUT);
|
|
87
|
+
expect(trade.inputAmount.currency).toEqual(ETHER);
|
|
88
|
+
expect(trade.outputAmount.currency).toEqual(token0);
|
|
89
|
+
}));
|
|
90
|
+
it('can be constructed with ETHER as output for exact input with multiple routes', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
91
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoutes([
|
|
92
|
+
{
|
|
93
|
+
amount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(3000)),
|
|
94
|
+
route: new route_1.MixedRouteSDK([pool_v3_weth_0], token0, ETHER),
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
amount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(7000)),
|
|
98
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pool_v3_weth_1], token0, ETHER),
|
|
99
|
+
},
|
|
100
|
+
], sdk_core_1.TradeType.EXACT_INPUT);
|
|
101
|
+
expect(trade.inputAmount.currency).toEqual(token0);
|
|
102
|
+
expect(trade.outputAmount.currency).toEqual(ETHER);
|
|
103
|
+
}));
|
|
104
|
+
it('throws if pools are re-used between routes', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
105
|
+
yield expect(trade_1.MixedRouteTrade.fromRoutes([
|
|
106
|
+
{
|
|
107
|
+
amount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(4500)),
|
|
108
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pool_v3_weth_1], token0, ETHER),
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
amount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(5500)),
|
|
112
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pool_v3_1_2, pool_v3_weth_2], token0, ETHER),
|
|
113
|
+
},
|
|
114
|
+
], sdk_core_1.TradeType.EXACT_INPUT)).rejects.toThrow('POOLS_DUPLICATED');
|
|
115
|
+
}));
|
|
116
|
+
it('throws if created with exact output', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
117
|
+
yield expect(trade_1.MixedRouteTrade.fromRoutes([
|
|
118
|
+
{
|
|
119
|
+
amount: sdk_core_1.CurrencyAmount.fromRawAmount(sdk_core_1.Ether.onChain(1), jsbi_1.default.BigInt(10000)),
|
|
120
|
+
route: new route_1.MixedRouteSDK([pool_v3_weth_0], ETHER, token0),
|
|
121
|
+
},
|
|
122
|
+
], sdk_core_1.TradeType.EXACT_OUTPUT)).rejects.toThrow('TRADE_TYPE');
|
|
123
|
+
}));
|
|
124
|
+
});
|
|
125
|
+
describe('#createUncheckedTrade', () => {
|
|
126
|
+
it('throws if input currency does not match route', () => {
|
|
127
|
+
expect(() => trade_1.MixedRouteTrade.createUncheckedTrade({
|
|
128
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1], token0, token1),
|
|
129
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 10000),
|
|
130
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, 10000),
|
|
131
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
132
|
+
})).toThrow('INPUT_CURRENCY_MATCH');
|
|
133
|
+
});
|
|
134
|
+
it('throws if output currency does not match route', () => {
|
|
135
|
+
expect(() => trade_1.MixedRouteTrade.createUncheckedTrade({
|
|
136
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1], token0, token1),
|
|
137
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 10000),
|
|
138
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 10000),
|
|
139
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
140
|
+
})).toThrow('OUTPUT_CURRENCY_MATCH');
|
|
141
|
+
});
|
|
142
|
+
it('throws if tradeType is exactOutput', () => {
|
|
143
|
+
try {
|
|
144
|
+
trade_1.MixedRouteTrade.createUncheckedTrade({
|
|
145
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1], token1, token0),
|
|
146
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, 10000),
|
|
147
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 100000),
|
|
148
|
+
tradeType: sdk_core_1.TradeType.EXACT_OUTPUT,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
catch (err) {
|
|
152
|
+
// @ts-ignore
|
|
153
|
+
expect(err.message).toEqual('Invariant failed: TRADE_TYPE');
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
it('can create an exact input trade without simulating', () => {
|
|
157
|
+
trade_1.MixedRouteTrade.createUncheckedTrade({
|
|
158
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1], token0, token1),
|
|
159
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 10000),
|
|
160
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, 100000),
|
|
161
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
describe('#createUncheckedTradeWithMultipleRoutes', () => {
|
|
166
|
+
it('throws if input currency does not match route with multiple routes', () => {
|
|
167
|
+
expect(() => trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
168
|
+
routes: [
|
|
169
|
+
{
|
|
170
|
+
route: new route_1.MixedRouteSDK([pool_v3_1_2], token2, token1),
|
|
171
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 2000),
|
|
172
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, 2000),
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1], token0, token1),
|
|
176
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 8000),
|
|
177
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, 8000),
|
|
178
|
+
},
|
|
179
|
+
],
|
|
180
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
181
|
+
})).toThrow('INPUT_CURRENCY_MATCH');
|
|
182
|
+
});
|
|
183
|
+
it('throws if output currency does not match route with multiple routes', () => {
|
|
184
|
+
expect(() => trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
185
|
+
routes: [
|
|
186
|
+
{
|
|
187
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_2], token0, token2),
|
|
188
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 10000),
|
|
189
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 10000),
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1], token0, token1),
|
|
193
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 10000),
|
|
194
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 10000),
|
|
195
|
+
},
|
|
196
|
+
],
|
|
197
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
198
|
+
})).toThrow('OUTPUT_CURRENCY_MATCH');
|
|
199
|
+
});
|
|
200
|
+
it('throws if tradeType is exact output', () => {
|
|
201
|
+
try {
|
|
202
|
+
trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
203
|
+
routes: [
|
|
204
|
+
{
|
|
205
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1], token0, token1),
|
|
206
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 5000),
|
|
207
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, 50000),
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_2, pool_v3_1_2], token0, token1),
|
|
211
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 5000),
|
|
212
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, 50000),
|
|
213
|
+
},
|
|
214
|
+
],
|
|
215
|
+
tradeType: sdk_core_1.TradeType.EXACT_OUTPUT,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
catch (err) {
|
|
219
|
+
// @ts-ignore
|
|
220
|
+
expect(err.message).toEqual('Invariant failed: TRADE_TYPE');
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
it('can create an exact input trade without simulating with multiple routes', () => {
|
|
224
|
+
trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
225
|
+
routes: [
|
|
226
|
+
{
|
|
227
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1], token0, token1),
|
|
228
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 5000),
|
|
229
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, 50000),
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_2, pool_v3_1_2], token0, token1),
|
|
233
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 5000),
|
|
234
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, 50000),
|
|
235
|
+
},
|
|
236
|
+
],
|
|
237
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
describe('#route and #swaps', () => {
|
|
242
|
+
const singleRoute = trade_1.MixedRouteTrade.createUncheckedTrade({
|
|
243
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pool_v3_1_2], token0, token2),
|
|
244
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 100),
|
|
245
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 69),
|
|
246
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
247
|
+
});
|
|
248
|
+
const multiRoute = trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
249
|
+
routes: [
|
|
250
|
+
{
|
|
251
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pool_v3_1_2], token0, token2),
|
|
252
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 50),
|
|
253
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 35),
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_2], token0, token2),
|
|
257
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 50),
|
|
258
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 34),
|
|
259
|
+
},
|
|
260
|
+
],
|
|
261
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
262
|
+
});
|
|
263
|
+
it('can access route for single route trade if less than 0', () => {
|
|
264
|
+
expect(singleRoute.swaps).toBeDefined();
|
|
265
|
+
});
|
|
266
|
+
it('can access routes for both single and multi route trades', () => {
|
|
267
|
+
expect(singleRoute.swaps).toBeDefined();
|
|
268
|
+
expect(singleRoute.swaps).toHaveLength(1);
|
|
269
|
+
expect(multiRoute.swaps).toBeDefined();
|
|
270
|
+
expect(multiRoute.swaps).toHaveLength(2);
|
|
271
|
+
});
|
|
272
|
+
it('throws if access route on multi route trade', () => {
|
|
273
|
+
expect(() => multiRoute.route).toThrow('MULTIPLE_ROUTES');
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
describe('#worstExecutionPrice', () => {
|
|
277
|
+
describe('tradeType = EXACT_INPUT', () => {
|
|
278
|
+
const exactIn = trade_1.MixedRouteTrade.createUncheckedTrade({
|
|
279
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pool_v3_1_2], token0, token2),
|
|
280
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 100),
|
|
281
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 69),
|
|
282
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
283
|
+
});
|
|
284
|
+
const exactInMultiRoute = trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
285
|
+
routes: [
|
|
286
|
+
{
|
|
287
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pool_v3_1_2], token0, token2),
|
|
288
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 50),
|
|
289
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 35),
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_2], token0, token2),
|
|
293
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 50),
|
|
294
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 34),
|
|
295
|
+
},
|
|
296
|
+
],
|
|
297
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
298
|
+
});
|
|
299
|
+
it('throws if less than 0', () => {
|
|
300
|
+
expect(() => exactIn.minimumAmountOut(new sdk_core_1.Percent(-1, 100))).toThrow('SLIPPAGE_TOLERANCE');
|
|
301
|
+
});
|
|
302
|
+
it('returns exact if 0', () => {
|
|
303
|
+
expect(exactIn.worstExecutionPrice(new sdk_core_1.Percent(0, 100))).toEqual(exactIn.executionPrice);
|
|
304
|
+
});
|
|
305
|
+
it('returns exact if nonzero', () => {
|
|
306
|
+
expect(exactIn.worstExecutionPrice(new sdk_core_1.Percent(0, 100))).toEqual(new sdk_core_1.Price(token0, token2, 100, 69));
|
|
307
|
+
expect(exactIn.worstExecutionPrice(new sdk_core_1.Percent(5, 100))).toEqual(new sdk_core_1.Price(token0, token2, 100, 65));
|
|
308
|
+
expect(exactIn.worstExecutionPrice(new sdk_core_1.Percent(200, 100))).toEqual(new sdk_core_1.Price(token0, token2, 100, 23));
|
|
309
|
+
});
|
|
310
|
+
it('returns exact if nonzero with multiple routes', () => {
|
|
311
|
+
expect(exactInMultiRoute.worstExecutionPrice(new sdk_core_1.Percent(0, 100))).toEqual(new sdk_core_1.Price(token0, token2, 100, 69));
|
|
312
|
+
expect(exactInMultiRoute.worstExecutionPrice(new sdk_core_1.Percent(5, 100))).toEqual(new sdk_core_1.Price(token0, token2, 100, 65));
|
|
313
|
+
expect(exactInMultiRoute.worstExecutionPrice(new sdk_core_1.Percent(200, 100))).toEqual(new sdk_core_1.Price(token0, token2, 100, 23));
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
describe('#priceImpact', () => {
|
|
318
|
+
describe('100% v3 route', () => {
|
|
319
|
+
describe('tradeType = EXACT_INPUT', () => {
|
|
320
|
+
const exactIn = trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
321
|
+
routes: [
|
|
322
|
+
{
|
|
323
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pool_v3_1_2], token0, token2),
|
|
324
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 100),
|
|
325
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 69),
|
|
326
|
+
},
|
|
327
|
+
],
|
|
328
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
329
|
+
});
|
|
330
|
+
const exactInMultipleRoutes = trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
331
|
+
routes: [
|
|
332
|
+
{
|
|
333
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pool_v3_1_2], token0, token2),
|
|
334
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 90),
|
|
335
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 62),
|
|
336
|
+
},
|
|
337
|
+
{
|
|
338
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_2], token0, token2),
|
|
339
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 10),
|
|
340
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 7),
|
|
341
|
+
},
|
|
342
|
+
],
|
|
343
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
344
|
+
});
|
|
345
|
+
it('is correct', () => {
|
|
346
|
+
expect(exactIn.priceImpact.toSignificant(3)).toEqual('17.2');
|
|
347
|
+
});
|
|
348
|
+
it('is correct with multiple routes', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
349
|
+
expect(exactInMultipleRoutes.priceImpact.toSignificant(3)).toEqual('19.8');
|
|
350
|
+
}));
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
describe('mixed route', () => {
|
|
354
|
+
describe('tradeType = EXACT_INPUT', () => {
|
|
355
|
+
const exactIn = trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
356
|
+
routes: [
|
|
357
|
+
{
|
|
358
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pair_1_2], token0, token2),
|
|
359
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 100),
|
|
360
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 69),
|
|
361
|
+
},
|
|
362
|
+
],
|
|
363
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
364
|
+
});
|
|
365
|
+
const exactInMultipleRoutes = trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
366
|
+
routes: [
|
|
367
|
+
{
|
|
368
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pair_1_2], token0, token2),
|
|
369
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 90),
|
|
370
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 62),
|
|
371
|
+
},
|
|
372
|
+
{
|
|
373
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_2], token0, token2),
|
|
374
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 10),
|
|
375
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 7),
|
|
376
|
+
},
|
|
377
|
+
],
|
|
378
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
379
|
+
});
|
|
380
|
+
it('is correct', () => {
|
|
381
|
+
expect(exactIn.priceImpact.toSignificant(3)).toEqual('17.2');
|
|
382
|
+
});
|
|
383
|
+
it('is correct with multiple routes', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
384
|
+
expect(exactInMultipleRoutes.priceImpact.toSignificant(3)).toEqual('19.8');
|
|
385
|
+
}));
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
describe('#bestTradeExactIn', () => {
|
|
390
|
+
it('throws with empty pools', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
391
|
+
yield expect(trade_1.MixedRouteTrade.bestTradeExactIn([], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10000)), token2)).rejects.toThrow('POOLS');
|
|
392
|
+
}));
|
|
393
|
+
it('throws with max hops of 0', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
394
|
+
yield expect(trade_1.MixedRouteTrade.bestTradeExactIn([pool_v3_0_2], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10000)), token2, {
|
|
395
|
+
maxHops: 0,
|
|
396
|
+
})).rejects.toThrow('MAX_HOPS');
|
|
397
|
+
}));
|
|
398
|
+
it('provides best route', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
399
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pool_v3_0_1, pool_v3_0_2, pool_v3_1_2], sdk_core_1.CurrencyAmount.fromRawAmount(token0, 10000), token2);
|
|
400
|
+
expect(result).toHaveLength(2);
|
|
401
|
+
expect(result[0].swaps[0].route.pools).toHaveLength(1); // 0 -> 2 at 10:11
|
|
402
|
+
expect(result[0].swaps[0].route.path).toEqual([token0, token2]);
|
|
403
|
+
expect(result[0].inputAmount.equalTo(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10000)))).toBeTruthy();
|
|
404
|
+
expect(result[0].outputAmount.equalTo(sdk_core_1.CurrencyAmount.fromRawAmount(token2, jsbi_1.default.BigInt(9971)))).toBeTruthy();
|
|
405
|
+
expect(result[1].swaps[0].route.pools).toHaveLength(2); // 0 -> 1 -> 2 at 12:12:10
|
|
406
|
+
expect(result[1].swaps[0].route.path).toEqual([token0, token1, token2]);
|
|
407
|
+
expect(result[1].inputAmount.equalTo(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10000)))).toBeTruthy();
|
|
408
|
+
expect(result[1].outputAmount.equalTo(sdk_core_1.CurrencyAmount.fromRawAmount(token2, jsbi_1.default.BigInt(7004)))).toBeTruthy();
|
|
409
|
+
}));
|
|
410
|
+
it('respects maxHops', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
411
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pool_v3_0_1, pool_v3_0_2, pool_v3_1_2], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10)), token2, { maxHops: 1 });
|
|
412
|
+
expect(result).toHaveLength(1);
|
|
413
|
+
expect(result[0].swaps[0].route.pools).toHaveLength(1); // 0 -> 2 at 10:11
|
|
414
|
+
expect(result[0].swaps[0].route.path).toEqual([token0, token2]);
|
|
415
|
+
}));
|
|
416
|
+
it('insufficient input for one pool', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
417
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pool_v3_0_1, pool_v3_0_2, pool_v3_1_2], sdk_core_1.CurrencyAmount.fromRawAmount(token0, 1), token2);
|
|
418
|
+
expect(result).toHaveLength(2);
|
|
419
|
+
expect(result[0].swaps[0].route.pools).toHaveLength(1); // 0 -> 2 at 10:11
|
|
420
|
+
expect(result[0].swaps[0].route.path).toEqual([token0, token2]);
|
|
421
|
+
expect(result[0].outputAmount).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token2, 0));
|
|
422
|
+
}));
|
|
423
|
+
it('respects n', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
424
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pool_v3_0_1, pool_v3_0_2, pool_v3_1_2], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10)), token2, { maxNumResults: 1 });
|
|
425
|
+
expect(result).toHaveLength(1);
|
|
426
|
+
}));
|
|
427
|
+
it('no path', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
428
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pool_v3_0_1, pool_v3_0_3, pool_v3_1_3], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10)), token2);
|
|
429
|
+
expect(result).toHaveLength(0);
|
|
430
|
+
}));
|
|
431
|
+
it('works for ETHER currency input', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
432
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pool_v3_weth_0, pool_v3_0_1, pool_v3_0_3, pool_v3_1_3], sdk_core_1.CurrencyAmount.fromRawAmount(sdk_core_1.Ether.onChain(1), jsbi_1.default.BigInt(100)), token3);
|
|
433
|
+
expect(result).toHaveLength(2);
|
|
434
|
+
expect(result[0].inputAmount.currency).toEqual(ETHER);
|
|
435
|
+
expect(result[0].swaps[0].route.path).toEqual([sdk_core_1.WETH9[1], token0, token1, token3]);
|
|
436
|
+
expect(result[0].outputAmount.currency).toEqual(token3);
|
|
437
|
+
expect(result[1].inputAmount.currency).toEqual(ETHER);
|
|
438
|
+
expect(result[1].swaps[0].route.path).toEqual([sdk_core_1.WETH9[1], token0, token3]);
|
|
439
|
+
expect(result[1].outputAmount.currency).toEqual(token3);
|
|
440
|
+
}));
|
|
441
|
+
it('works for ETHER currency output', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
442
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pool_v3_weth_0, pool_v3_0_1, pool_v3_0_3, pool_v3_1_3], sdk_core_1.CurrencyAmount.fromRawAmount(token3, jsbi_1.default.BigInt(100)), ETHER);
|
|
443
|
+
expect(result).toHaveLength(2);
|
|
444
|
+
expect(result[0].inputAmount.currency).toEqual(token3);
|
|
445
|
+
expect(result[0].swaps[0].route.path).toEqual([token3, token0, sdk_core_1.WETH9[1]]);
|
|
446
|
+
expect(result[0].outputAmount.currency).toEqual(ETHER);
|
|
447
|
+
expect(result[1].inputAmount.currency).toEqual(token3);
|
|
448
|
+
expect(result[1].swaps[0].route.path).toEqual([token3, token1, token0, sdk_core_1.WETH9[1]]);
|
|
449
|
+
expect(result[1].outputAmount.currency).toEqual(ETHER);
|
|
450
|
+
}));
|
|
451
|
+
});
|
|
452
|
+
describe('#maximumAmountIn', () => {
|
|
453
|
+
describe('tradeType = EXACT_INPUT', () => {
|
|
454
|
+
let exactIn;
|
|
455
|
+
beforeEach(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
456
|
+
exactIn = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pool_v3_0_1, pool_v3_1_2], token0, token2), sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
457
|
+
}));
|
|
458
|
+
it('throws if less than 0', () => {
|
|
459
|
+
expect(() => exactIn.maximumAmountIn(new sdk_core_1.Percent(jsbi_1.default.BigInt(-1), jsbi_1.default.BigInt(100)))).toThrow('SLIPPAGE_TOLERANCE');
|
|
460
|
+
});
|
|
461
|
+
it('returns exact if 0', () => {
|
|
462
|
+
expect(exactIn.maximumAmountIn(new sdk_core_1.Percent(jsbi_1.default.BigInt(0), jsbi_1.default.BigInt(100)))).toEqual(exactIn.inputAmount);
|
|
463
|
+
});
|
|
464
|
+
it('returns exact if nonzero', () => {
|
|
465
|
+
expect(exactIn
|
|
466
|
+
.maximumAmountIn(new sdk_core_1.Percent(jsbi_1.default.BigInt(0), jsbi_1.default.BigInt(100)))
|
|
467
|
+
.equalTo(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)))).toBeTruthy();
|
|
468
|
+
expect(exactIn
|
|
469
|
+
.maximumAmountIn(new sdk_core_1.Percent(jsbi_1.default.BigInt(5), jsbi_1.default.BigInt(100)))
|
|
470
|
+
.equalTo(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)))).toBeTruthy();
|
|
471
|
+
expect(exactIn
|
|
472
|
+
.maximumAmountIn(new sdk_core_1.Percent(jsbi_1.default.BigInt(200), jsbi_1.default.BigInt(100)))
|
|
473
|
+
.equalTo(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)))).toBeTruthy();
|
|
474
|
+
});
|
|
475
|
+
});
|
|
476
|
+
});
|
|
477
|
+
describe('#minimumAmountOut', () => {
|
|
478
|
+
describe('tradeType = EXACT_INPUT', () => {
|
|
479
|
+
let exactIn;
|
|
480
|
+
beforeEach(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
481
|
+
return (exactIn = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pool_v3_0_1, pool_v3_1_2], token0, token2), sdk_core_1.CurrencyAmount.fromRawAmount(token0, 10000), sdk_core_1.TradeType.EXACT_INPUT));
|
|
482
|
+
}));
|
|
483
|
+
it('throws if less than 0', () => {
|
|
484
|
+
expect(() => exactIn.minimumAmountOut(new sdk_core_1.Percent(jsbi_1.default.BigInt(-1), 100))).toThrow('SLIPPAGE_TOLERANCE');
|
|
485
|
+
});
|
|
486
|
+
it('returns exact if 0', () => {
|
|
487
|
+
expect(exactIn.minimumAmountOut(new sdk_core_1.Percent(jsbi_1.default.BigInt(0), 10000))).toEqual(exactIn.outputAmount);
|
|
488
|
+
});
|
|
489
|
+
it('returns exact if nonzero', () => {
|
|
490
|
+
expect(exactIn.minimumAmountOut(new sdk_core_1.Percent(jsbi_1.default.BigInt(0), 100))).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token2, 7004));
|
|
491
|
+
expect(exactIn.minimumAmountOut(new sdk_core_1.Percent(jsbi_1.default.BigInt(5), 100))).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token2, 6670));
|
|
492
|
+
expect(exactIn.minimumAmountOut(new sdk_core_1.Percent(jsbi_1.default.BigInt(200), 100))).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token2, 2334));
|
|
493
|
+
});
|
|
494
|
+
});
|
|
495
|
+
});
|
|
496
|
+
});
|
|
497
|
+
/// @dev copied over from v2-sdk trade.test.ts
|
|
498
|
+
describe('is backwards compatible with pure v2 routes', () => {
|
|
499
|
+
it('can be constructed with ETHER as input', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
500
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pair_weth_0], ETHER, token0), sdk_core_1.CurrencyAmount.fromRawAmount(sdk_core_1.Ether.onChain(1), jsbi_1.default.BigInt(100)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
501
|
+
expect(trade.inputAmount.currency).toEqual(ETHER);
|
|
502
|
+
expect(trade.outputAmount.currency).toEqual(token0);
|
|
503
|
+
}));
|
|
504
|
+
it('can be constructed with ETHER as output for exact input', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
505
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pair_weth_0], token0, ETHER), sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
506
|
+
expect(trade.inputAmount.currency).toEqual(token0);
|
|
507
|
+
expect(trade.outputAmount.currency).toEqual(ETHER);
|
|
508
|
+
}));
|
|
509
|
+
describe('#bestTradeExactIn', () => {
|
|
510
|
+
it('throws with empty pairs', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
511
|
+
yield expect(trade_1.MixedRouteTrade.bestTradeExactIn([], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)), token2)).rejects.toThrow('POOLS');
|
|
512
|
+
}));
|
|
513
|
+
it('throws with max hops of 0', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
514
|
+
yield expect(trade_1.MixedRouteTrade.bestTradeExactIn([pair_0_2], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)), token2, {
|
|
515
|
+
maxHops: 0,
|
|
516
|
+
})).rejects.toThrow('MAX_HOPS');
|
|
517
|
+
}));
|
|
518
|
+
it('provides best route', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
519
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pair_0_1, pair_0_2, pair_1_2], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)), token2);
|
|
520
|
+
expect(result).toHaveLength(2);
|
|
521
|
+
expect(result[0].swaps[0].route.pools).toHaveLength(1); // 0 -> 2 at 10:11
|
|
522
|
+
expect(result[0].swaps[0].route.path).toEqual([token0, token2]);
|
|
523
|
+
expect(result[0].inputAmount).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)));
|
|
524
|
+
expect(result[0].outputAmount).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token2, jsbi_1.default.BigInt(99)));
|
|
525
|
+
expect(result[1].swaps[0].route.pools).toHaveLength(2); // 0 -> 1 -> 2 at 12:12:10
|
|
526
|
+
expect(result[1].swaps[0].route.path).toEqual([token0, token1, token2]);
|
|
527
|
+
expect(result[1].inputAmount).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)));
|
|
528
|
+
expect(result[1].outputAmount).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token2, jsbi_1.default.BigInt(69)));
|
|
529
|
+
}));
|
|
530
|
+
it('doesnt throw for zero liquidity pairs', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
531
|
+
expect(yield trade_1.MixedRouteTrade.bestTradeExactIn([empty_pair_0_1], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)), token1)).toHaveLength(0);
|
|
532
|
+
}));
|
|
533
|
+
it('respects maxHops', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
534
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pair_0_1, pair_0_2, pair_1_2], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10)), token2, { maxHops: 1 });
|
|
535
|
+
expect(result).toHaveLength(1);
|
|
536
|
+
expect(result[0].swaps[0].route.pools).toHaveLength(1); // 0 -> 2 at 10:11
|
|
537
|
+
expect(result[0].swaps[0].route.path).toEqual([token0, token2]);
|
|
538
|
+
}));
|
|
539
|
+
it('insufficient input for one pair', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
540
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pair_0_1, pair_0_2, pair_1_2], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(1)), token2);
|
|
541
|
+
expect(result).toHaveLength(1);
|
|
542
|
+
expect(result[0].swaps[0].route.pools).toHaveLength(1); // 0 -> 2 at 10:11
|
|
543
|
+
expect(result[0].swaps[0].route.path).toEqual([token0, token2]);
|
|
544
|
+
expect(result[0].outputAmount).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token2, jsbi_1.default.BigInt(1)));
|
|
545
|
+
}));
|
|
546
|
+
it('respects n', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
547
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pair_0_1, pair_0_2, pair_1_2], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10)), token2, { maxNumResults: 1 });
|
|
548
|
+
expect(result).toHaveLength(1);
|
|
549
|
+
}));
|
|
550
|
+
it('no path', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
551
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pair_0_1, pair_0_3, pair_1_3], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10)), token2);
|
|
552
|
+
expect(result).toHaveLength(0);
|
|
553
|
+
}));
|
|
554
|
+
it('works for ETHER currency input', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
555
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pair_weth_0, pair_0_1, pair_0_3, pair_1_3], sdk_core_1.CurrencyAmount.fromRawAmount(sdk_core_1.Ether.onChain(1), jsbi_1.default.BigInt(100)), token3);
|
|
556
|
+
expect(result).toHaveLength(2);
|
|
557
|
+
expect(result[0].inputAmount.currency).toEqual(ETHER);
|
|
558
|
+
expect(result[0].swaps[0].route.path).toEqual([sdk_core_1.WETH9[1], token0, token1, token3]);
|
|
559
|
+
expect(result[0].outputAmount.currency).toEqual(token3);
|
|
560
|
+
expect(result[1].inputAmount.currency).toEqual(ETHER);
|
|
561
|
+
expect(result[1].swaps[0].route.path).toEqual([sdk_core_1.WETH9[1], token0, token3]);
|
|
562
|
+
expect(result[1].outputAmount.currency).toEqual(token3);
|
|
563
|
+
}));
|
|
564
|
+
it('works for ETHER currency output', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
565
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pair_weth_0, pair_0_1, pair_0_3, pair_1_3], sdk_core_1.CurrencyAmount.fromRawAmount(token3, jsbi_1.default.BigInt(100)), ETHER);
|
|
566
|
+
expect(result).toHaveLength(2);
|
|
567
|
+
expect(result[0].inputAmount.currency).toEqual(token3);
|
|
568
|
+
expect(result[0].swaps[0].route.path).toEqual([token3, token0, sdk_core_1.WETH9[1]]);
|
|
569
|
+
expect(result[0].outputAmount.currency).toEqual(ETHER);
|
|
570
|
+
expect(result[1].inputAmount.currency).toEqual(token3);
|
|
571
|
+
expect(result[1].swaps[0].route.path).toEqual([token3, token1, token0, sdk_core_1.WETH9[1]]);
|
|
572
|
+
expect(result[1].outputAmount.currency).toEqual(ETHER);
|
|
573
|
+
}));
|
|
574
|
+
});
|
|
575
|
+
describe('#maximumAmountIn', () => {
|
|
576
|
+
describe('tradeType = EXACT_INPUT', () => {
|
|
577
|
+
let exactIn;
|
|
578
|
+
beforeAll(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
579
|
+
exactIn = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pair_0_1, pair_1_2], token0, token2), sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
580
|
+
}));
|
|
581
|
+
it('throws if less than 0', () => {
|
|
582
|
+
expect(() => exactIn.maximumAmountIn(new sdk_core_1.Percent(jsbi_1.default.BigInt(-1), jsbi_1.default.BigInt(100)))).toThrow('SLIPPAGE_TOLERANCE');
|
|
583
|
+
});
|
|
584
|
+
it('returns exact if 0', () => {
|
|
585
|
+
expect(exactIn.maximumAmountIn(new sdk_core_1.Percent(jsbi_1.default.BigInt(0), jsbi_1.default.BigInt(100)))).toEqual(exactIn.inputAmount);
|
|
586
|
+
});
|
|
587
|
+
it('returns exact if nonzero', () => {
|
|
588
|
+
expect(exactIn.maximumAmountIn(new sdk_core_1.Percent(jsbi_1.default.BigInt(0), jsbi_1.default.BigInt(100)))).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)));
|
|
589
|
+
expect(exactIn.maximumAmountIn(new sdk_core_1.Percent(jsbi_1.default.BigInt(5), jsbi_1.default.BigInt(100)))).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)));
|
|
590
|
+
expect(exactIn.maximumAmountIn(new sdk_core_1.Percent(jsbi_1.default.BigInt(200), jsbi_1.default.BigInt(100)))).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)));
|
|
591
|
+
});
|
|
592
|
+
});
|
|
593
|
+
});
|
|
594
|
+
describe('#minimumAmountOut', () => {
|
|
595
|
+
describe('tradeType = EXACT_INPUT', () => {
|
|
596
|
+
let exactIn;
|
|
597
|
+
beforeAll(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
598
|
+
exactIn = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pair_0_1, pair_1_2], token0, token2), sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
599
|
+
}));
|
|
600
|
+
it('throws if less than 0', () => {
|
|
601
|
+
expect(() => exactIn.minimumAmountOut(new sdk_core_1.Percent(jsbi_1.default.BigInt(-1), jsbi_1.default.BigInt(100)))).toThrow('SLIPPAGE_TOLERANCE');
|
|
602
|
+
});
|
|
603
|
+
it('returns exact if 0', () => {
|
|
604
|
+
expect(exactIn.minimumAmountOut(new sdk_core_1.Percent(jsbi_1.default.BigInt(0), jsbi_1.default.BigInt(100)))).toEqual(exactIn.outputAmount);
|
|
605
|
+
});
|
|
606
|
+
it('returns exact if nonzero', () => {
|
|
607
|
+
expect(exactIn.minimumAmountOut(new sdk_core_1.Percent(jsbi_1.default.BigInt(0), jsbi_1.default.BigInt(100)))).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token2, jsbi_1.default.BigInt(69)));
|
|
608
|
+
expect(exactIn.minimumAmountOut(new sdk_core_1.Percent(jsbi_1.default.BigInt(5), jsbi_1.default.BigInt(100)))).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token2, jsbi_1.default.BigInt(65)));
|
|
609
|
+
expect(exactIn.minimumAmountOut(new sdk_core_1.Percent(jsbi_1.default.BigInt(200), jsbi_1.default.BigInt(100)))).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token2, jsbi_1.default.BigInt(23)));
|
|
610
|
+
});
|
|
611
|
+
});
|
|
612
|
+
});
|
|
613
|
+
describe('#worstExecutionPrice', () => {
|
|
614
|
+
describe('tradeType = EXACT_INPUT', () => {
|
|
615
|
+
let exactIn;
|
|
616
|
+
beforeAll(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
617
|
+
exactIn = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pair_0_1, pair_1_2], token0, token2), sdk_core_1.CurrencyAmount.fromRawAmount(token0, 100), sdk_core_1.TradeType.EXACT_INPUT);
|
|
618
|
+
}));
|
|
619
|
+
it('throws if less than 0', () => {
|
|
620
|
+
expect(() => exactIn.minimumAmountOut(new sdk_core_1.Percent(-1, 100))).toThrow('SLIPPAGE_TOLERANCE');
|
|
621
|
+
});
|
|
622
|
+
it('returns exact if 0', () => {
|
|
623
|
+
expect(exactIn.worstExecutionPrice(new sdk_core_1.Percent(0, 100))).toEqual(exactIn.executionPrice);
|
|
624
|
+
});
|
|
625
|
+
it('returns exact if nonzero', () => {
|
|
626
|
+
expect(exactIn.worstExecutionPrice(new sdk_core_1.Percent(0, 100))).toEqual(new sdk_core_1.Price(token0, token2, 100, 69));
|
|
627
|
+
expect(exactIn.worstExecutionPrice(new sdk_core_1.Percent(5, 100))).toEqual(new sdk_core_1.Price(token0, token2, 100, 65));
|
|
628
|
+
expect(exactIn.worstExecutionPrice(new sdk_core_1.Percent(200, 100))).toEqual(new sdk_core_1.Price(token0, token2, 100, 23));
|
|
629
|
+
});
|
|
630
|
+
});
|
|
631
|
+
});
|
|
632
|
+
});
|
|
633
|
+
describe('multihop v2 + v3', () => {
|
|
634
|
+
describe('#fromRoute', () => {
|
|
635
|
+
it('can be constructed with ETHER as input', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
636
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pool_v3_weth_0, pair_0_1], ETHER, token1), sdk_core_1.CurrencyAmount.fromRawAmount(sdk_core_1.Ether.onChain(1), jsbi_1.default.BigInt(10000)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
637
|
+
expect(trade.inputAmount.currency).toEqual(ETHER);
|
|
638
|
+
expect(trade.outputAmount.currency).toEqual(token1);
|
|
639
|
+
}));
|
|
640
|
+
it('can be constructed with ETHER as output for exact input', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
641
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pair_0_1, pool_v3_weth_0], token1, ETHER), sdk_core_1.CurrencyAmount.fromRawAmount(token1, jsbi_1.default.BigInt(10000)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
642
|
+
expect(trade.inputAmount.currency).toEqual(token1);
|
|
643
|
+
expect(trade.outputAmount.currency).toEqual(ETHER);
|
|
644
|
+
}));
|
|
645
|
+
it('allows using input tokens as intermediary', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
646
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pair_0_1, pool_v3_0_1, pair_0_2], token0, token2), sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
647
|
+
expect(trade.inputAmount.currency).toEqual(token0);
|
|
648
|
+
expect(trade.outputAmount.currency).toEqual(token2);
|
|
649
|
+
}));
|
|
650
|
+
});
|
|
651
|
+
describe('#fromRoutes', () => {
|
|
652
|
+
it('can be constructed with ETHER as input with multiple routes', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
653
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoutes([
|
|
654
|
+
{
|
|
655
|
+
amount: sdk_core_1.CurrencyAmount.fromRawAmount(sdk_core_1.Ether.onChain(1), jsbi_1.default.BigInt(10000)),
|
|
656
|
+
route: new route_1.MixedRouteSDK([pool_v3_weth_0, pair_0_1], ETHER, token1),
|
|
657
|
+
},
|
|
658
|
+
], sdk_core_1.TradeType.EXACT_INPUT);
|
|
659
|
+
expect(trade.inputAmount.currency).toEqual(ETHER);
|
|
660
|
+
expect(trade.outputAmount.currency).toEqual(token1);
|
|
661
|
+
}));
|
|
662
|
+
it('can be constructed with ETHER as output for exact input with multiple routes', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
663
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoutes([
|
|
664
|
+
{
|
|
665
|
+
amount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, jsbi_1.default.BigInt(3000)),
|
|
666
|
+
route: new route_1.MixedRouteSDK([pair_0_1, pool_v3_weth_0], token1, ETHER),
|
|
667
|
+
},
|
|
668
|
+
{
|
|
669
|
+
amount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, jsbi_1.default.BigInt(7000)),
|
|
670
|
+
route: new route_1.MixedRouteSDK([pair_1_2, pool_v3_weth_2], token1, ETHER),
|
|
671
|
+
},
|
|
672
|
+
], sdk_core_1.TradeType.EXACT_INPUT);
|
|
673
|
+
expect(trade.inputAmount.currency).toEqual(token1);
|
|
674
|
+
expect(trade.outputAmount.currency).toEqual(ETHER);
|
|
675
|
+
}));
|
|
676
|
+
/// no test for pool duplication because both v3 and v2 tests above cover it
|
|
677
|
+
});
|
|
678
|
+
describe('#createUncheckedTrade', () => {
|
|
679
|
+
it('throws if input currency does not match route', () => {
|
|
680
|
+
expect(() => trade_1.MixedRouteTrade.createUncheckedTrade({
|
|
681
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pair_1_2], token0, token2),
|
|
682
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 10000),
|
|
683
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, 10000),
|
|
684
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
685
|
+
})).toThrow('INPUT_CURRENCY_MATCH');
|
|
686
|
+
});
|
|
687
|
+
it('throws if output currency does not match route', () => {
|
|
688
|
+
expect(() => trade_1.MixedRouteTrade.createUncheckedTrade({
|
|
689
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pair_1_2], token0, token2),
|
|
690
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 10000),
|
|
691
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token3, 10000),
|
|
692
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
693
|
+
})).toThrow('OUTPUT_CURRENCY_MATCH');
|
|
694
|
+
});
|
|
695
|
+
it('can create an exact input trade without simulating', () => {
|
|
696
|
+
trade_1.MixedRouteTrade.createUncheckedTrade({
|
|
697
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pair_1_2], token0, token2),
|
|
698
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 10000),
|
|
699
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 100000),
|
|
700
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
701
|
+
});
|
|
702
|
+
});
|
|
703
|
+
});
|
|
704
|
+
describe('#createUncheckedTradeWithMultipleRoutes', () => {
|
|
705
|
+
it('throws if input currency does not match route with multiple routes', () => {
|
|
706
|
+
expect(() => trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
707
|
+
routes: [
|
|
708
|
+
{
|
|
709
|
+
route: new route_1.MixedRouteSDK([pool_v3_1_2], token2, token1),
|
|
710
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 2000),
|
|
711
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, 2000),
|
|
712
|
+
},
|
|
713
|
+
{
|
|
714
|
+
route: new route_1.MixedRouteSDK([pair_0_1], token0, token1),
|
|
715
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 8000),
|
|
716
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, 8000),
|
|
717
|
+
},
|
|
718
|
+
],
|
|
719
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
720
|
+
})).toThrow('INPUT_CURRENCY_MATCH');
|
|
721
|
+
});
|
|
722
|
+
it('throws if output currency does not match route with multiple routes', () => {
|
|
723
|
+
expect(() => trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
724
|
+
routes: [
|
|
725
|
+
{
|
|
726
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_2], token0, token2),
|
|
727
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 10000),
|
|
728
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 10000),
|
|
729
|
+
},
|
|
730
|
+
{
|
|
731
|
+
route: new route_1.MixedRouteSDK([pair_0_1], token0, token1),
|
|
732
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 10000),
|
|
733
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 10000),
|
|
734
|
+
},
|
|
735
|
+
],
|
|
736
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
737
|
+
})).toThrow('OUTPUT_CURRENCY_MATCH');
|
|
738
|
+
});
|
|
739
|
+
it('can create an exact input trade without simulating with multiple routes', () => {
|
|
740
|
+
trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
741
|
+
routes: [
|
|
742
|
+
{
|
|
743
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1], token0, token1),
|
|
744
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 5000),
|
|
745
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, 50000),
|
|
746
|
+
},
|
|
747
|
+
{
|
|
748
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_2, pair_1_2], token0, token1),
|
|
749
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 5000),
|
|
750
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token1, 50000),
|
|
751
|
+
},
|
|
752
|
+
],
|
|
753
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
754
|
+
});
|
|
755
|
+
});
|
|
756
|
+
});
|
|
757
|
+
describe('#route and #swaps', () => {
|
|
758
|
+
const singleRoute = trade_1.MixedRouteTrade.createUncheckedTrade({
|
|
759
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pair_1_2], token0, token2),
|
|
760
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 100),
|
|
761
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 69),
|
|
762
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
763
|
+
});
|
|
764
|
+
const multiRoute = trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
765
|
+
routes: [
|
|
766
|
+
{
|
|
767
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_1, pair_1_2], token0, token2),
|
|
768
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 50),
|
|
769
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 35),
|
|
770
|
+
},
|
|
771
|
+
{
|
|
772
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_2], token0, token2),
|
|
773
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 50),
|
|
774
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 34),
|
|
775
|
+
},
|
|
776
|
+
],
|
|
777
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
778
|
+
});
|
|
779
|
+
it('can access route for single route trade if less than 0', () => {
|
|
780
|
+
expect(singleRoute.route).toBeDefined();
|
|
781
|
+
});
|
|
782
|
+
it('can access routes for both single and multi route trades', () => {
|
|
783
|
+
expect(singleRoute.swaps).toBeDefined();
|
|
784
|
+
expect(singleRoute.swaps).toHaveLength(1);
|
|
785
|
+
expect(multiRoute.swaps).toBeDefined();
|
|
786
|
+
expect(multiRoute.swaps).toHaveLength(2);
|
|
787
|
+
});
|
|
788
|
+
it('throws if access route on multi route trade', () => {
|
|
789
|
+
expect(() => multiRoute.route).toThrow('MULTIPLE_ROUTES');
|
|
790
|
+
});
|
|
791
|
+
});
|
|
792
|
+
describe('#worstExecutionPrice', () => {
|
|
793
|
+
describe('tradeType = EXACT_INPUT', () => {
|
|
794
|
+
const exactIn = trade_1.MixedRouteTrade.createUncheckedTrade({
|
|
795
|
+
route: new route_1.MixedRouteSDK([pair_0_1, pool_v3_1_2], token0, token2),
|
|
796
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 100),
|
|
797
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 69),
|
|
798
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
799
|
+
});
|
|
800
|
+
const exactInMultiRoute = trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
801
|
+
routes: [
|
|
802
|
+
{
|
|
803
|
+
route: new route_1.MixedRouteSDK([pair_0_1, pool_v3_1_2], token0, token2),
|
|
804
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 50),
|
|
805
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 35),
|
|
806
|
+
},
|
|
807
|
+
{
|
|
808
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_2], token0, token2),
|
|
809
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 50),
|
|
810
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 34),
|
|
811
|
+
},
|
|
812
|
+
],
|
|
813
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
814
|
+
});
|
|
815
|
+
it('throws if less than 0', () => {
|
|
816
|
+
expect(() => exactIn.minimumAmountOut(new sdk_core_1.Percent(-1, 100))).toThrow('SLIPPAGE_TOLERANCE');
|
|
817
|
+
});
|
|
818
|
+
it('returns exact if 0', () => {
|
|
819
|
+
expect(exactIn.worstExecutionPrice(new sdk_core_1.Percent(0, 100))).toEqual(exactIn.executionPrice);
|
|
820
|
+
});
|
|
821
|
+
it('returns exact if nonzero', () => {
|
|
822
|
+
expect(exactIn.worstExecutionPrice(new sdk_core_1.Percent(0, 100))).toEqual(new sdk_core_1.Price(token0, token2, 100, 69));
|
|
823
|
+
expect(exactIn.worstExecutionPrice(new sdk_core_1.Percent(5, 100))).toEqual(new sdk_core_1.Price(token0, token2, 100, 65));
|
|
824
|
+
expect(exactIn.worstExecutionPrice(new sdk_core_1.Percent(200, 100))).toEqual(new sdk_core_1.Price(token0, token2, 100, 23));
|
|
825
|
+
});
|
|
826
|
+
it('returns exact if nonzero with multiple routes', () => {
|
|
827
|
+
expect(exactInMultiRoute.worstExecutionPrice(new sdk_core_1.Percent(0, 100))).toEqual(new sdk_core_1.Price(token0, token2, 100, 69));
|
|
828
|
+
expect(exactInMultiRoute.worstExecutionPrice(new sdk_core_1.Percent(5, 100))).toEqual(new sdk_core_1.Price(token0, token2, 100, 65));
|
|
829
|
+
expect(exactInMultiRoute.worstExecutionPrice(new sdk_core_1.Percent(200, 100))).toEqual(new sdk_core_1.Price(token0, token2, 100, 23));
|
|
830
|
+
});
|
|
831
|
+
});
|
|
832
|
+
});
|
|
833
|
+
describe('#priceImpact', () => {
|
|
834
|
+
describe('tradeType = EXACT_INPUT', () => {
|
|
835
|
+
const exactIn = trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
836
|
+
routes: [
|
|
837
|
+
{
|
|
838
|
+
route: new route_1.MixedRouteSDK([pair_0_1, pool_v3_1_2], token0, token2),
|
|
839
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 100),
|
|
840
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 69),
|
|
841
|
+
},
|
|
842
|
+
],
|
|
843
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
844
|
+
});
|
|
845
|
+
const exactInMultipleRoutes = trade_1.MixedRouteTrade.createUncheckedTradeWithMultipleRoutes({
|
|
846
|
+
routes: [
|
|
847
|
+
{
|
|
848
|
+
route: new route_1.MixedRouteSDK([pair_0_1, pool_v3_1_2], token0, token2),
|
|
849
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 90),
|
|
850
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 62),
|
|
851
|
+
},
|
|
852
|
+
{
|
|
853
|
+
route: new route_1.MixedRouteSDK([pool_v3_0_2], token0, token2),
|
|
854
|
+
inputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token0, 10),
|
|
855
|
+
outputAmount: sdk_core_1.CurrencyAmount.fromRawAmount(token2, 7),
|
|
856
|
+
},
|
|
857
|
+
],
|
|
858
|
+
tradeType: sdk_core_1.TradeType.EXACT_INPUT,
|
|
859
|
+
});
|
|
860
|
+
it('is correct', () => {
|
|
861
|
+
expect(exactIn.priceImpact.toSignificant(3)).toEqual('17.2');
|
|
862
|
+
});
|
|
863
|
+
it('is correct with multiple routes', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
864
|
+
expect(exactInMultipleRoutes.priceImpact.toSignificant(3)).toEqual('19.8');
|
|
865
|
+
}));
|
|
866
|
+
});
|
|
867
|
+
});
|
|
868
|
+
describe('#bestTradeExactIn', () => {
|
|
869
|
+
/// no empty check because covered by v3 backward compatibility test
|
|
870
|
+
it('throws with max hops of 0', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
871
|
+
yield expect(trade_1.MixedRouteTrade.bestTradeExactIn([pool_v3_0_2, pair_1_2], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10000)), token1, {
|
|
872
|
+
maxHops: 0,
|
|
873
|
+
})).rejects.toThrow('MAX_HOPS');
|
|
874
|
+
}));
|
|
875
|
+
it('provides best route', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
876
|
+
const large_pair_0_1 = new v2_sdk_1.Pair(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100000)), sdk_core_1.CurrencyAmount.fromRawAmount(token1, jsbi_1.default.BigInt(100000)));
|
|
877
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([large_pair_0_1, pool_v3_0_2, pool_v3_1_2], sdk_core_1.CurrencyAmount.fromRawAmount(token0, 10000), token2);
|
|
878
|
+
expect(result).toHaveLength(2);
|
|
879
|
+
expect(result[0].swaps[0].route.pools).toHaveLength(1); // 0 -> 2 at 10:11
|
|
880
|
+
expect(result[0].swaps[0].route.path).toEqual([token0, token2]);
|
|
881
|
+
expect(result[0].inputAmount.equalTo(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10000)))).toBeTruthy();
|
|
882
|
+
expect(result[0].outputAmount.equalTo(sdk_core_1.CurrencyAmount.fromRawAmount(token2, jsbi_1.default.BigInt(9971)))).toBeTruthy();
|
|
883
|
+
expect(result[1].swaps[0].route.pools).toHaveLength(2); // 0 -> 1 -> 2 at 12:12:10
|
|
884
|
+
expect(result[1].swaps[0].route.path).toEqual([token0, token1, token2]);
|
|
885
|
+
expect(result[1].inputAmount.equalTo(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10000)))).toBeTruthy();
|
|
886
|
+
expect(result[1].outputAmount.equalTo(sdk_core_1.CurrencyAmount.fromRawAmount(token2, jsbi_1.default.BigInt(7004)))).toBeTruthy();
|
|
887
|
+
}));
|
|
888
|
+
it('respects maxHops', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
889
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pool_v3_0_1, pool_v3_0_2, pair_1_2], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10)), token2, { maxHops: 1 });
|
|
890
|
+
expect(result).toHaveLength(1);
|
|
891
|
+
expect(result[0].swaps[0].route.pools).toHaveLength(1); // 0 -> 2 at 10:11
|
|
892
|
+
expect(result[0].swaps[0].route.path).toEqual([token0, token2]);
|
|
893
|
+
}));
|
|
894
|
+
it('insufficient input for one pool', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
895
|
+
/// pairs are just skipped
|
|
896
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pair_0_1, pool_v3_0_2, pair_1_2], sdk_core_1.CurrencyAmount.fromRawAmount(token0, 1), token2);
|
|
897
|
+
expect(result).toHaveLength(1);
|
|
898
|
+
expect(result[0].swaps[0].route.pools).toHaveLength(1);
|
|
899
|
+
expect(result[0].swaps[0].route.path).toEqual([token0, token2]);
|
|
900
|
+
expect(result[0].outputAmount).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token2, 0));
|
|
901
|
+
}));
|
|
902
|
+
it('respects n', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
903
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pool_v3_0_1, pair_0_2, pool_v3_1_2], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10)), token2, { maxNumResults: 1 });
|
|
904
|
+
expect(result).toHaveLength(1);
|
|
905
|
+
}));
|
|
906
|
+
it('no path between v2 and v3', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
907
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pool_v3_0_1, pair_0_3, pool_v3_1_3], sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(10)), token2);
|
|
908
|
+
expect(result).toHaveLength(0);
|
|
909
|
+
}));
|
|
910
|
+
it('works for ETHER currency input', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
911
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pool_v3_weth_0, pair_0_1, pool_v3_0_3, pair_1_3], sdk_core_1.CurrencyAmount.fromRawAmount(sdk_core_1.Ether.onChain(1), jsbi_1.default.BigInt(100)), token3);
|
|
912
|
+
expect(result).toHaveLength(2);
|
|
913
|
+
expect(result[0].inputAmount.currency).toEqual(ETHER);
|
|
914
|
+
expect(result[0].swaps[0].route.path).toEqual([sdk_core_1.WETH9[1], token0, token1, token3]);
|
|
915
|
+
expect(result[0].outputAmount.currency).toEqual(token3);
|
|
916
|
+
expect(result[1].inputAmount.currency).toEqual(ETHER);
|
|
917
|
+
expect(result[1].swaps[0].route.path).toEqual([sdk_core_1.WETH9[1], token0, token3]);
|
|
918
|
+
expect(result[1].outputAmount.currency).toEqual(token3);
|
|
919
|
+
}));
|
|
920
|
+
it('works for ETHER currency output', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
921
|
+
const result = yield trade_1.MixedRouteTrade.bestTradeExactIn([pool_v3_weth_0, pool_v3_0_1, pair_0_3, pair_1_3], sdk_core_1.CurrencyAmount.fromRawAmount(token3, jsbi_1.default.BigInt(100)), ETHER);
|
|
922
|
+
expect(result).toHaveLength(2);
|
|
923
|
+
expect(result[0].inputAmount.currency).toEqual(token3);
|
|
924
|
+
expect(result[0].swaps[0].route.path).toEqual([token3, token0, sdk_core_1.WETH9[1]]);
|
|
925
|
+
expect(result[0].outputAmount.currency).toEqual(ETHER);
|
|
926
|
+
expect(result[1].inputAmount.currency).toEqual(token3);
|
|
927
|
+
expect(result[1].swaps[0].route.path).toEqual([token3, token1, token0, sdk_core_1.WETH9[1]]);
|
|
928
|
+
expect(result[1].outputAmount.currency).toEqual(ETHER);
|
|
929
|
+
}));
|
|
930
|
+
});
|
|
931
|
+
describe('#maximumAmountIn', () => {
|
|
932
|
+
describe('tradeType = EXACT_INPUT', () => {
|
|
933
|
+
let exactIn;
|
|
934
|
+
beforeEach(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
935
|
+
exactIn = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pool_v3_0_1, pair_1_2], token0, token2), sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
936
|
+
}));
|
|
937
|
+
it('throws if less than 0', () => {
|
|
938
|
+
expect(() => exactIn.maximumAmountIn(new sdk_core_1.Percent(jsbi_1.default.BigInt(-1), jsbi_1.default.BigInt(100)))).toThrow('SLIPPAGE_TOLERANCE');
|
|
939
|
+
});
|
|
940
|
+
it('returns exact if 0', () => {
|
|
941
|
+
expect(exactIn.maximumAmountIn(new sdk_core_1.Percent(jsbi_1.default.BigInt(0), jsbi_1.default.BigInt(100)))).toEqual(exactIn.inputAmount);
|
|
942
|
+
});
|
|
943
|
+
it('returns exact if nonzero', () => {
|
|
944
|
+
expect(exactIn
|
|
945
|
+
.maximumAmountIn(new sdk_core_1.Percent(jsbi_1.default.BigInt(0), jsbi_1.default.BigInt(100)))
|
|
946
|
+
.equalTo(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)))).toBeTruthy();
|
|
947
|
+
expect(exactIn
|
|
948
|
+
.maximumAmountIn(new sdk_core_1.Percent(jsbi_1.default.BigInt(5), jsbi_1.default.BigInt(100)))
|
|
949
|
+
.equalTo(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)))).toBeTruthy();
|
|
950
|
+
expect(exactIn
|
|
951
|
+
.maximumAmountIn(new sdk_core_1.Percent(jsbi_1.default.BigInt(200), jsbi_1.default.BigInt(100)))
|
|
952
|
+
.equalTo(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)))).toBeTruthy();
|
|
953
|
+
});
|
|
954
|
+
});
|
|
955
|
+
});
|
|
956
|
+
describe('#minimumAmountOut', () => {
|
|
957
|
+
describe('tradeType = EXACT_INPUT', () => {
|
|
958
|
+
let exactIn;
|
|
959
|
+
const large_pair_0_1 = new v2_sdk_1.Pair(sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100000)), sdk_core_1.CurrencyAmount.fromRawAmount(token1, jsbi_1.default.BigInt(100000)));
|
|
960
|
+
beforeEach(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
961
|
+
return (exactIn = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([large_pair_0_1, pool_v3_1_2], token0, token2), sdk_core_1.CurrencyAmount.fromRawAmount(token0, 10000), sdk_core_1.TradeType.EXACT_INPUT));
|
|
962
|
+
}));
|
|
963
|
+
it('throws if less than 0', () => {
|
|
964
|
+
expect(() => exactIn.minimumAmountOut(new sdk_core_1.Percent(jsbi_1.default.BigInt(-1), 100))).toThrow('SLIPPAGE_TOLERANCE');
|
|
965
|
+
});
|
|
966
|
+
it('returns exact if 0', () => {
|
|
967
|
+
expect(exactIn.minimumAmountOut(new sdk_core_1.Percent(jsbi_1.default.BigInt(0), 10000))).toEqual(exactIn.outputAmount);
|
|
968
|
+
});
|
|
969
|
+
it('returns exact if nonzero', () => {
|
|
970
|
+
expect(exactIn.minimumAmountOut(new sdk_core_1.Percent(jsbi_1.default.BigInt(0), 100))).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token2, 7004));
|
|
971
|
+
expect(exactIn.minimumAmountOut(new sdk_core_1.Percent(jsbi_1.default.BigInt(5), 100))).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token2, 6670));
|
|
972
|
+
expect(exactIn.minimumAmountOut(new sdk_core_1.Percent(jsbi_1.default.BigInt(200), 100))).toEqual(sdk_core_1.CurrencyAmount.fromRawAmount(token2, 2334));
|
|
973
|
+
});
|
|
974
|
+
});
|
|
975
|
+
});
|
|
976
|
+
});
|
|
977
|
+
describe('multihop v2 + v3 + v4', () => {
|
|
978
|
+
it('can be constructed with a weth output from a v4 pool', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
979
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pool_v3_0_1, pool_v4_0_weth], token1, sdk_core_1.WETH9[1]), sdk_core_1.CurrencyAmount.fromRawAmount(token1, jsbi_1.default.BigInt(100)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
980
|
+
expect(trade.inputAmount.currency).toEqual(token1);
|
|
981
|
+
expect(trade.outputAmount.currency).toEqual(sdk_core_1.WETH9[1]);
|
|
982
|
+
}));
|
|
983
|
+
it('can be constructed with an eth output from a v4 pool', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
984
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pool_v3_0_1, pool_v4_0_eth], token1, ETHER), sdk_core_1.CurrencyAmount.fromRawAmount(token1, jsbi_1.default.BigInt(100)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
985
|
+
expect(trade.inputAmount.currency).toEqual(token1);
|
|
986
|
+
expect(trade.outputAmount.currency).toEqual(ETHER);
|
|
987
|
+
}));
|
|
988
|
+
it('can be constructed with an eth output from a v4 weth pool', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
989
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pool_v3_0_1, pool_v4_0_weth], token1, ETHER), sdk_core_1.CurrencyAmount.fromRawAmount(token1, jsbi_1.default.BigInt(100)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
990
|
+
expect(trade.inputAmount.currency).toEqual(token1);
|
|
991
|
+
expect(trade.outputAmount.currency).toEqual(ETHER);
|
|
992
|
+
}));
|
|
993
|
+
it('can be constructed with an intermediate conversion WETH->ETH when trading to v4 pool', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
994
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pool_v3_weth_0, pool_v4_0_eth], token0, ETHER), sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
995
|
+
expect(trade.inputAmount.currency).toEqual(token0);
|
|
996
|
+
expect(trade.outputAmount.currency).toEqual(ETHER);
|
|
997
|
+
}));
|
|
998
|
+
it('can be constructed with an intermediate conversion ETH->WETH when trading from a v4 pool', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
999
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pool_v4_0_eth, pool_v3_weth_0], token0, sdk_core_1.WETH9[1]), sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
1000
|
+
expect(trade.inputAmount.currency).toEqual(token0);
|
|
1001
|
+
expect(trade.outputAmount.currency).toEqual(sdk_core_1.WETH9[1]);
|
|
1002
|
+
}));
|
|
1003
|
+
it('can be constructed with an intermediate conversion ETH->WETH when trading from a v4 pool with ETH output', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
1004
|
+
const trade = yield trade_1.MixedRouteTrade.fromRoute(new route_1.MixedRouteSDK([pool_v4_0_eth, pool_v3_weth_0], token0, ETHER), sdk_core_1.CurrencyAmount.fromRawAmount(token0, jsbi_1.default.BigInt(100)), sdk_core_1.TradeType.EXACT_INPUT);
|
|
1005
|
+
expect(trade.inputAmount.currency).toEqual(token0);
|
|
1006
|
+
expect(trade.outputAmount.currency).toEqual(ETHER);
|
|
1007
|
+
}));
|
|
1008
|
+
});
|
|
1009
|
+
});
|
|
1010
|
+
//# sourceMappingURL=trade.test.js.map
|