@fenine/router-sdk 1.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/LICENSE +21 -0
- package/README.md +16 -0
- package/dist/cjs/src/approveAndCall.d.ts +33 -0
- package/dist/cjs/src/approveAndCall.js +111 -0
- package/dist/cjs/src/approveAndCall.js.map +1 -0
- package/dist/cjs/src/constants.d.ts +13 -0
- package/dist/cjs/src/constants.js +22 -0
- package/dist/cjs/src/constants.js.map +1 -0
- package/dist/cjs/src/entities/mixedRoute/route.d.ts +29 -0
- 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/trade.d.ts +183 -0
- package/dist/cjs/src/entities/mixedRoute/trade.js +354 -0
- package/dist/cjs/src/entities/mixedRoute/trade.js.map +1 -0
- package/dist/cjs/src/entities/protocol.d.ts +6 -0
- package/dist/cjs/src/entities/protocol.js +11 -0
- package/dist/cjs/src/entities/protocol.js.map +1 -0
- package/dist/cjs/src/entities/route.d.ts +40 -0
- package/dist/cjs/src/entities/route.js +63 -0
- package/dist/cjs/src/entities/route.js.map +1 -0
- package/dist/cjs/src/entities/trade.d.ts +136 -0
- package/dist/cjs/src/entities/trade.js +370 -0
- package/dist/cjs/src/entities/trade.js.map +1 -0
- package/dist/cjs/src/index.d.ts +14 -0
- package/dist/cjs/src/index.js +18 -0
- package/dist/cjs/src/index.js.map +1 -0
- package/dist/cjs/src/multicallExtended.d.ts +11 -0
- package/dist/cjs/src/multicallExtended.js +44 -0
- package/dist/cjs/src/multicallExtended.js.map +1 -0
- package/dist/cjs/src/paymentsExtended.d.ts +15 -0
- package/dist/cjs/src/paymentsExtended.js +66 -0
- package/dist/cjs/src/paymentsExtended.js.map +1 -0
- package/dist/cjs/src/swapRouter.d.ts +95 -0
- package/dist/cjs/src/swapRouter.js +439 -0
- package/dist/cjs/src/swapRouter.js.map +1 -0
- package/dist/cjs/src/utils/TPool.d.ts +4 -0
- package/dist/cjs/src/utils/TPool.js +3 -0
- package/dist/cjs/src/utils/TPool.js.map +1 -0
- package/dist/cjs/src/utils/encodeMixedRouteToPath.d.ts +10 -0
- package/dist/cjs/src/utils/encodeMixedRouteToPath.js +91 -0
- package/dist/cjs/src/utils/encodeMixedRouteToPath.js.map +1 -0
- package/dist/cjs/src/utils/index.d.ts +16 -0
- package/dist/cjs/src/utils/index.js +51 -0
- package/dist/cjs/src/utils/index.js.map +1 -0
- package/dist/cjs/src/utils/pathCurrency.d.ts +4 -0
- package/dist/cjs/src/utils/pathCurrency.js +35 -0
- package/dist/cjs/src/utils/pathCurrency.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/trade.d.ts +183 -0
- package/dist/esm/src/entities/mixedRoute/trade.js +348 -0
- package/dist/esm/src/entities/mixedRoute/trade.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/trade.d.ts +136 -0
- package/dist/esm/src/entities/trade.js +365 -0
- package/dist/esm/src/entities/trade.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/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/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/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 +10 -0
- package/dist/esm/src/utils/encodeMixedRouteToPath.js +87 -0
- package/dist/esm/src/utils/encodeMixedRouteToPath.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/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/trade.d.ts +183 -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/trade.d.ts +136 -0
- package/dist/types/src/index.d.ts +14 -0
- package/dist/types/src/multicallExtended.d.ts +11 -0
- package/dist/types/src/paymentsExtended.d.ts +15 -0
- package/dist/types/src/swapRouter.d.ts +95 -0
- package/dist/types/src/utils/TPool.d.ts +4 -0
- package/dist/types/src/utils/encodeMixedRouteToPath.d.ts +10 -0
- package/dist/types/src/utils/index.d.ts +16 -0
- package/dist/types/src/utils/pathCurrency.d.ts +4 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Uniswap Labs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Fenine Router SDK
|
|
2
|
+
|
|
3
|
+
`@fenine/router-sdk` helps compose swap routes and calldata for Fenine-compatible
|
|
4
|
+
V2, V3, and mixed-route trades.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install @fenine/router-sdk
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Notes
|
|
13
|
+
|
|
14
|
+
- This package is intended for Fenine-compatible routing flows.
|
|
15
|
+
- It interoperates with `@fenine/sdk-core` and `@fenine/v3-sdk`.
|
|
16
|
+
- Contract execution still targets swap-router style contracts.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Interface } from '@ethersproject/abi';
|
|
2
|
+
import { Currency, Percent, Token } from '@fenine/sdk-core';
|
|
3
|
+
import { MintSpecificOptions, IncreaseSpecificOptions, Position } from '@fenine/v3-sdk';
|
|
4
|
+
export type CondensedAddLiquidityOptions = Omit<MintSpecificOptions, 'createPool'> | IncreaseSpecificOptions;
|
|
5
|
+
export declare enum ApprovalTypes {
|
|
6
|
+
NOT_REQUIRED = 0,
|
|
7
|
+
MAX = 1,
|
|
8
|
+
MAX_MINUS_ONE = 2,
|
|
9
|
+
ZERO_THEN_MAX = 3,
|
|
10
|
+
ZERO_THEN_MAX_MINUS_ONE = 4
|
|
11
|
+
}
|
|
12
|
+
export declare function isMint(options: CondensedAddLiquidityOptions): options is Omit<MintSpecificOptions, 'createPool'>;
|
|
13
|
+
export declare abstract class ApproveAndCall {
|
|
14
|
+
static INTERFACE: Interface;
|
|
15
|
+
/**
|
|
16
|
+
* Cannot be constructed.
|
|
17
|
+
*/
|
|
18
|
+
private constructor();
|
|
19
|
+
static encodeApproveMax(token: Token): string;
|
|
20
|
+
static encodeApproveMaxMinusOne(token: Token): string;
|
|
21
|
+
static encodeApproveZeroThenMax(token: Token): string;
|
|
22
|
+
static encodeApproveZeroThenMaxMinusOne(token: Token): string;
|
|
23
|
+
static encodeCallPositionManager(calldatas: string[]): string;
|
|
24
|
+
/**
|
|
25
|
+
* Encode adding liquidity to a position in the nft manager contract
|
|
26
|
+
* @param position Forcasted position with expected amount out from swap
|
|
27
|
+
* @param minimalPosition Forcasted position with custom minimal token amounts
|
|
28
|
+
* @param addLiquidityOptions Options for adding liquidity
|
|
29
|
+
* @param slippageTolerance Defines maximum slippage
|
|
30
|
+
*/
|
|
31
|
+
static encodeAddLiquidity(position: Position, minimalPosition: Position, addLiquidityOptions: CondensedAddLiquidityOptions, slippageTolerance: Percent): string;
|
|
32
|
+
static encodeApprove(token: Currency, approvalType: ApprovalTypes): string;
|
|
33
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApproveAndCall = exports.isMint = exports.ApprovalTypes = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const abi_1 = require("@ethersproject/abi");
|
|
6
|
+
const tiny_invariant_1 = tslib_1.__importDefault(require("tiny-invariant"));
|
|
7
|
+
const IApproveAndCall_json_1 = tslib_1.__importDefault(require("@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IApproveAndCall.sol/IApproveAndCall.json"));
|
|
8
|
+
const v3_sdk_1 = require("@fenine/v3-sdk");
|
|
9
|
+
const jsbi_1 = tslib_1.__importDefault(require("jsbi"));
|
|
10
|
+
var ApprovalTypes;
|
|
11
|
+
(function (ApprovalTypes) {
|
|
12
|
+
ApprovalTypes[ApprovalTypes["NOT_REQUIRED"] = 0] = "NOT_REQUIRED";
|
|
13
|
+
ApprovalTypes[ApprovalTypes["MAX"] = 1] = "MAX";
|
|
14
|
+
ApprovalTypes[ApprovalTypes["MAX_MINUS_ONE"] = 2] = "MAX_MINUS_ONE";
|
|
15
|
+
ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX"] = 3] = "ZERO_THEN_MAX";
|
|
16
|
+
ApprovalTypes[ApprovalTypes["ZERO_THEN_MAX_MINUS_ONE"] = 4] = "ZERO_THEN_MAX_MINUS_ONE";
|
|
17
|
+
})(ApprovalTypes = exports.ApprovalTypes || (exports.ApprovalTypes = {}));
|
|
18
|
+
// type guard
|
|
19
|
+
function isMint(options) {
|
|
20
|
+
return Object.keys(options).some((k) => k === 'recipient');
|
|
21
|
+
}
|
|
22
|
+
exports.isMint = isMint;
|
|
23
|
+
class ApproveAndCall {
|
|
24
|
+
/**
|
|
25
|
+
* Cannot be constructed.
|
|
26
|
+
*/
|
|
27
|
+
constructor() { }
|
|
28
|
+
static encodeApproveMax(token) {
|
|
29
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('approveMax', [token.address]);
|
|
30
|
+
}
|
|
31
|
+
static encodeApproveMaxMinusOne(token) {
|
|
32
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('approveMaxMinusOne', [token.address]);
|
|
33
|
+
}
|
|
34
|
+
static encodeApproveZeroThenMax(token) {
|
|
35
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMax', [token.address]);
|
|
36
|
+
}
|
|
37
|
+
static encodeApproveZeroThenMaxMinusOne(token) {
|
|
38
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMaxMinusOne', [token.address]);
|
|
39
|
+
}
|
|
40
|
+
static encodeCallPositionManager(calldatas) {
|
|
41
|
+
(0, tiny_invariant_1.default)(calldatas.length > 0, 'NULL_CALLDATA');
|
|
42
|
+
if (calldatas.length === 1) {
|
|
43
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', calldatas);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
const encodedMulticall = v3_sdk_1.NonfungiblePositionManager.INTERFACE.encodeFunctionData('multicall', [calldatas]);
|
|
47
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', [encodedMulticall]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Encode adding liquidity to a position in the nft manager contract
|
|
52
|
+
* @param position Forcasted position with expected amount out from swap
|
|
53
|
+
* @param minimalPosition Forcasted position with custom minimal token amounts
|
|
54
|
+
* @param addLiquidityOptions Options for adding liquidity
|
|
55
|
+
* @param slippageTolerance Defines maximum slippage
|
|
56
|
+
*/
|
|
57
|
+
static encodeAddLiquidity(position, minimalPosition, addLiquidityOptions, slippageTolerance) {
|
|
58
|
+
let { amount0: amount0Min, amount1: amount1Min } = position.mintAmountsWithSlippage(slippageTolerance);
|
|
59
|
+
// position.mintAmountsWithSlippage() can create amounts not dependenable in scenarios
|
|
60
|
+
// such as range orders. Allow the option to provide a position with custom minimum amounts
|
|
61
|
+
// for these scenarios
|
|
62
|
+
if (jsbi_1.default.lessThan(minimalPosition.amount0.quotient, amount0Min)) {
|
|
63
|
+
amount0Min = minimalPosition.amount0.quotient;
|
|
64
|
+
}
|
|
65
|
+
if (jsbi_1.default.lessThan(minimalPosition.amount1.quotient, amount1Min)) {
|
|
66
|
+
amount1Min = minimalPosition.amount1.quotient;
|
|
67
|
+
}
|
|
68
|
+
if (isMint(addLiquidityOptions)) {
|
|
69
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('mint', [
|
|
70
|
+
{
|
|
71
|
+
token0: position.pool.token0.address,
|
|
72
|
+
token1: position.pool.token1.address,
|
|
73
|
+
fee: position.pool.fee,
|
|
74
|
+
tickLower: position.tickLower,
|
|
75
|
+
tickUpper: position.tickUpper,
|
|
76
|
+
amount0Min: (0, v3_sdk_1.toHex)(amount0Min),
|
|
77
|
+
amount1Min: (0, v3_sdk_1.toHex)(amount1Min),
|
|
78
|
+
recipient: addLiquidityOptions.recipient,
|
|
79
|
+
},
|
|
80
|
+
]);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
return ApproveAndCall.INTERFACE.encodeFunctionData('increaseLiquidity', [
|
|
84
|
+
{
|
|
85
|
+
token0: position.pool.token0.address,
|
|
86
|
+
token1: position.pool.token1.address,
|
|
87
|
+
amount0Min: (0, v3_sdk_1.toHex)(amount0Min),
|
|
88
|
+
amount1Min: (0, v3_sdk_1.toHex)(amount1Min),
|
|
89
|
+
tokenId: (0, v3_sdk_1.toHex)(addLiquidityOptions.tokenId),
|
|
90
|
+
},
|
|
91
|
+
]);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
static encodeApprove(token, approvalType) {
|
|
95
|
+
switch (approvalType) {
|
|
96
|
+
case ApprovalTypes.MAX:
|
|
97
|
+
return ApproveAndCall.encodeApproveMax(token.wrapped);
|
|
98
|
+
case ApprovalTypes.MAX_MINUS_ONE:
|
|
99
|
+
return ApproveAndCall.encodeApproveMaxMinusOne(token.wrapped);
|
|
100
|
+
case ApprovalTypes.ZERO_THEN_MAX:
|
|
101
|
+
return ApproveAndCall.encodeApproveZeroThenMax(token.wrapped);
|
|
102
|
+
case ApprovalTypes.ZERO_THEN_MAX_MINUS_ONE:
|
|
103
|
+
return ApproveAndCall.encodeApproveZeroThenMaxMinusOne(token.wrapped);
|
|
104
|
+
default:
|
|
105
|
+
throw new Error('Error: invalid ApprovalType');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.ApproveAndCall = ApproveAndCall;
|
|
110
|
+
ApproveAndCall.INTERFACE = new abi_1.Interface(IApproveAndCall_json_1.default.abi);
|
|
111
|
+
//# sourceMappingURL=approveAndCall.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"approveAndCall.js","sourceRoot":"","sources":["../../../src/approveAndCall.ts"],"names":[],"mappings":";;;;AAAA,4CAA8C;AAC9C,4EAAsC;AACtC,0KAAoI;AAEpI,2CAMuB;AACvB,wDAAuB;AAKvB,IAAY,aAMX;AAND,WAAY,aAAa;IACvB,iEAAgB,CAAA;IAChB,+CAAO,CAAA;IACP,mEAAiB,CAAA;IACjB,mEAAiB,CAAA;IACjB,uFAA2B,CAAA;AAC7B,CAAC,EANW,aAAa,GAAb,qBAAa,KAAb,qBAAa,QAMxB;AAED,aAAa;AACb,SAAgB,MAAM,CAAC,OAAqC;IAC1D,OAAO,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,WAAW,CAAC,CAAA;AAC5D,CAAC;AAFD,wBAEC;AAED,MAAsB,cAAc;IAGlC;;OAEG;IACH,gBAAuB,CAAC;IAEjB,MAAM,CAAC,gBAAgB,CAAC,KAAY;QACzC,OAAO,cAAc,CAAC,SAAS,CAAC,kBAA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,OAAO,EAAE,IAAA,cAAK,EAAC,mBAAmB,CAAC,OAAO,CAAC;iBAC5C;aACF,CAAC,CAAA;SACH;IACH,CAAC;IAEM,MAAM,CAAC,aAAa,CAAC,KAAe,EAAE,YAA2B;QACtE,QAAQ,YAAY,EAAE;YACpB,KAAK,aAAa,CAAC,GAAG;gBACpB,OAAO,cAAc,CAAC,gBAAgB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;YACvD,KAAK,aAAa,CAAC,aAAa;gBAC9B,OAAO,cAAc,CAAC,wBAAwB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;YAC/D,KAAK,aAAa,CAAC,aAAa;gBAC9B,OAAO,cAAc,CAAC,wBAAwB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;YAC/D,KAAK,aAAa,CAAC,uBAAuB;gBACxC,OAAO,cAAc,CAAC,gCAAgC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;YACvE;gBACE,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAA;SACjD;IACH,CAAC;;AAlGH,wCAmGC;AAlGe,wBAAS,GAAc,IAAI,eAAS,CAAC,8BAAe,CAAC,GAAG,CAAC,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Percent } from '@fenine/sdk-core';
|
|
2
|
+
import JSBI from 'jsbi';
|
|
3
|
+
export declare const ADDRESS_ZERO = "0x0000000000000000000000000000000000000000";
|
|
4
|
+
export declare const MSG_SENDER = "0x0000000000000000000000000000000000000001";
|
|
5
|
+
export declare const ADDRESS_THIS = "0x0000000000000000000000000000000000000002";
|
|
6
|
+
export declare const ZERO: JSBI;
|
|
7
|
+
export declare const ONE: JSBI;
|
|
8
|
+
export declare const MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER: number;
|
|
9
|
+
export declare const MIXED_QUOTER_V2_V2_FEE_PATH_PLACEHOLDER: number;
|
|
10
|
+
export declare const MIXED_QUOTER_V2_V3_FEE_PATH_PLACEHOLDER: number;
|
|
11
|
+
export declare const MIXED_QUOTER_V2_V4_FEE_PATH_PLACEHOLDER: number;
|
|
12
|
+
export declare const ZERO_PERCENT: Percent;
|
|
13
|
+
export declare const ONE_HUNDRED_PERCENT: Percent;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ONE_HUNDRED_PERCENT = exports.ZERO_PERCENT = exports.MIXED_QUOTER_V2_V4_FEE_PATH_PLACEHOLDER = exports.MIXED_QUOTER_V2_V3_FEE_PATH_PLACEHOLDER = exports.MIXED_QUOTER_V2_V2_FEE_PATH_PLACEHOLDER = exports.MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER = exports.ONE = exports.ZERO = exports.ADDRESS_THIS = exports.MSG_SENDER = exports.ADDRESS_ZERO = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const sdk_core_1 = require("@fenine/sdk-core");
|
|
6
|
+
const jsbi_1 = tslib_1.__importDefault(require("jsbi"));
|
|
7
|
+
exports.ADDRESS_ZERO = '0x0000000000000000000000000000000000000000';
|
|
8
|
+
exports.MSG_SENDER = '0x0000000000000000000000000000000000000001';
|
|
9
|
+
exports.ADDRESS_THIS = '0x0000000000000000000000000000000000000002';
|
|
10
|
+
exports.ZERO = jsbi_1.default.BigInt(0);
|
|
11
|
+
exports.ONE = jsbi_1.default.BigInt(1);
|
|
12
|
+
// = 1 << 23 or 0b0100000000000000000000000
|
|
13
|
+
exports.MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER = 1 << 23;
|
|
14
|
+
// = 10 << 4 or 0b00100000
|
|
15
|
+
exports.MIXED_QUOTER_V2_V2_FEE_PATH_PLACEHOLDER = 2 << 4;
|
|
16
|
+
// = 11 << 20 or 0b001100000000000000000000
|
|
17
|
+
exports.MIXED_QUOTER_V2_V3_FEE_PATH_PLACEHOLDER = 3 << 20;
|
|
18
|
+
// = 100 << 20 or 0b010000000000000000000000
|
|
19
|
+
exports.MIXED_QUOTER_V2_V4_FEE_PATH_PLACEHOLDER = 4 << 20;
|
|
20
|
+
exports.ZERO_PERCENT = new sdk_core_1.Percent(exports.ZERO);
|
|
21
|
+
exports.ONE_HUNDRED_PERCENT = new sdk_core_1.Percent(100, 100);
|
|
22
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/constants.ts"],"names":[],"mappings":";;;;AAAA,+CAA0C;AAC1C,wDAAuB;AAEV,QAAA,YAAY,GAAG,4CAA4C,CAAA;AAC3D,QAAA,UAAU,GAAG,4CAA4C,CAAA;AACzD,QAAA,YAAY,GAAG,4CAA4C,CAAA;AAE3D,QAAA,IAAI,GAAG,cAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;AACrB,QAAA,GAAG,GAAG,cAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;AAEjC,2CAA2C;AAC9B,QAAA,uCAAuC,GAAG,CAAC,IAAI,EAAE,CAAA;AAE9D,0BAA0B;AACb,QAAA,uCAAuC,GAAG,CAAC,IAAI,CAAC,CAAA;AAE7D,2CAA2C;AAC9B,QAAA,uCAAuC,GAAG,CAAC,IAAI,EAAE,CAAA;AAE9D,4CAA4C;AAC/B,QAAA,uCAAuC,GAAG,CAAC,IAAI,EAAE,CAAA;AAEjD,QAAA,YAAY,GAAG,IAAI,kBAAO,CAAC,YAAI,CAAC,CAAA;AAChC,QAAA,mBAAmB,GAAG,IAAI,kBAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Currency, Price } from '@fenine/sdk-core';
|
|
2
|
+
import { TPool } from '../../utils/TPool';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a list of pools or pairs through which a swap can occur
|
|
5
|
+
* @template TInput The input token
|
|
6
|
+
* @template TOutput The output token
|
|
7
|
+
*/
|
|
8
|
+
export declare class MixedRouteSDK<TInput extends Currency, TOutput extends Currency> {
|
|
9
|
+
readonly pools: TPool[];
|
|
10
|
+
readonly path: Currency[];
|
|
11
|
+
readonly input: TInput;
|
|
12
|
+
readonly output: TOutput;
|
|
13
|
+
readonly pathInput: Currency;
|
|
14
|
+
readonly pathOutput: Currency;
|
|
15
|
+
private _midPrice;
|
|
16
|
+
/**
|
|
17
|
+
* Creates an instance of route.
|
|
18
|
+
* @param pools An array of `TPool` objects (pools or pairs), ordered by the route the swap will take
|
|
19
|
+
* @param input The input token
|
|
20
|
+
* @param output The output token
|
|
21
|
+
* @param retainsFakePool Set to true to filter out a pool that has a fake eth-weth pool
|
|
22
|
+
*/
|
|
23
|
+
constructor(pools: TPool[], input: TInput, output: TOutput, retainFakePools?: boolean);
|
|
24
|
+
get chainId(): number;
|
|
25
|
+
/**
|
|
26
|
+
* Returns the mid price of the route
|
|
27
|
+
*/
|
|
28
|
+
get midPrice(): Price<TInput, TOutput>;
|
|
29
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MixedRouteSDK = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const tiny_invariant_1 = tslib_1.__importDefault(require("tiny-invariant"));
|
|
6
|
+
const sdk_core_1 = require("@fenine/sdk-core");
|
|
7
|
+
const v4_sdk_1 = require("@uniswap/v4-sdk");
|
|
8
|
+
const pathCurrency_1 = require("../../utils/pathCurrency");
|
|
9
|
+
/**
|
|
10
|
+
* Represents a list of pools or pairs through which a swap can occur
|
|
11
|
+
* @template TInput The input token
|
|
12
|
+
* @template TOutput The output token
|
|
13
|
+
*/
|
|
14
|
+
class MixedRouteSDK {
|
|
15
|
+
/**
|
|
16
|
+
* Creates an instance of route.
|
|
17
|
+
* @param pools An array of `TPool` objects (pools or pairs), ordered by the route the swap will take
|
|
18
|
+
* @param input The input token
|
|
19
|
+
* @param output The output token
|
|
20
|
+
* @param retainsFakePool Set to true to filter out a pool that has a fake eth-weth pool
|
|
21
|
+
*/
|
|
22
|
+
constructor(pools, input, output, retainFakePools = false) {
|
|
23
|
+
this._midPrice = null;
|
|
24
|
+
pools = retainFakePools ? pools : pools.filter((pool) => !(pool instanceof v4_sdk_1.Pool && pool.tickSpacing === 0));
|
|
25
|
+
(0, tiny_invariant_1.default)(pools.length > 0, 'POOLS');
|
|
26
|
+
// there is a pool mismatched to the path if we do not retain the fake eth-weth pools
|
|
27
|
+
const chainId = pools[0].chainId;
|
|
28
|
+
const allOnSameChain = pools.every((pool) => pool.chainId === chainId);
|
|
29
|
+
(0, tiny_invariant_1.default)(allOnSameChain, 'CHAIN_IDS');
|
|
30
|
+
this.pathInput = (0, pathCurrency_1.getPathCurrency)(input, pools[0]);
|
|
31
|
+
this.pathOutput = (0, pathCurrency_1.getPathCurrency)(output, pools[pools.length - 1]);
|
|
32
|
+
if (!(pools[0] instanceof v4_sdk_1.Pool)) {
|
|
33
|
+
(0, tiny_invariant_1.default)(pools[0].involvesToken(this.pathInput), 'INPUT');
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
(0, tiny_invariant_1.default)(pools[0].v4InvolvesToken(this.pathInput), 'INPUT');
|
|
37
|
+
}
|
|
38
|
+
const lastPool = pools[pools.length - 1];
|
|
39
|
+
if (lastPool instanceof v4_sdk_1.Pool) {
|
|
40
|
+
(0, tiny_invariant_1.default)(lastPool.v4InvolvesToken(output) || lastPool.v4InvolvesToken(output.wrapped), 'OUTPUT');
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
(0, tiny_invariant_1.default)(lastPool.involvesToken(output.wrapped), 'OUTPUT');
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Normalizes token0-token1 order and selects the next token/fee step to add to the path
|
|
47
|
+
* */
|
|
48
|
+
const tokenPath = [this.pathInput];
|
|
49
|
+
pools[0].token0.equals(this.pathInput) ? tokenPath.push(pools[0].token1) : tokenPath.push(pools[0].token0);
|
|
50
|
+
for (let i = 1; i < pools.length; i++) {
|
|
51
|
+
const pool = pools[i];
|
|
52
|
+
const inputToken = tokenPath[i];
|
|
53
|
+
let outputToken;
|
|
54
|
+
if (
|
|
55
|
+
// we hit an edge case if it's a v4 pool and neither of the tokens are in the pool OR it is not a v4 pool but the input currency is eth
|
|
56
|
+
(pool instanceof v4_sdk_1.Pool && !pool.involvesToken(inputToken)) ||
|
|
57
|
+
(!(pool instanceof v4_sdk_1.Pool) && inputToken.isNative)) {
|
|
58
|
+
// We handle the case where the inputToken =/= pool.token0 or pool.token1. There are 2 specific cases.
|
|
59
|
+
if (inputToken.equals(pool.token0.wrapped)) {
|
|
60
|
+
// 1) the inputToken is WETH and the current pool has ETH
|
|
61
|
+
// for example, pools: USDC-WETH, ETH-PEPE, path: USDC, WETH, PEPE
|
|
62
|
+
// second pool is a v4 pool, the first could be any version
|
|
63
|
+
outputToken = pool.token1;
|
|
64
|
+
}
|
|
65
|
+
else if (inputToken.wrapped.equals(pool.token0) || inputToken.wrapped.equals(pool.token1)) {
|
|
66
|
+
// 2) the inputToken is ETH and the current pool has WETH
|
|
67
|
+
// for example, pools: USDC-ETH, WETH-PEPE, path: USDC, ETH, PEPE
|
|
68
|
+
// first pool is a v4 pool, the second could be any version
|
|
69
|
+
outputToken = inputToken.wrapped.equals(pool.token0) ? pool.token1 : pool.token0;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
throw new Error(`POOL_MISMATCH pool: ${JSON.stringify(pool)} inputToken: ${JSON.stringify(inputToken)}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
// then the input token must equal either token0 or token1
|
|
77
|
+
(0, tiny_invariant_1.default)(inputToken.equals(pool.token0) || inputToken.equals(pool.token1), `PATH pool ${JSON.stringify(pool)} inputToken ${JSON.stringify(inputToken)}`);
|
|
78
|
+
outputToken = inputToken.equals(pool.token0) ? pool.token1 : pool.token0;
|
|
79
|
+
}
|
|
80
|
+
tokenPath.push(outputToken);
|
|
81
|
+
}
|
|
82
|
+
this.pools = pools;
|
|
83
|
+
this.path = tokenPath;
|
|
84
|
+
this.input = input;
|
|
85
|
+
this.output = output !== null && output !== void 0 ? output : tokenPath[tokenPath.length - 1];
|
|
86
|
+
}
|
|
87
|
+
get chainId() {
|
|
88
|
+
return this.pools[0].chainId;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Returns the mid price of the route
|
|
92
|
+
*/
|
|
93
|
+
get midPrice() {
|
|
94
|
+
if (this._midPrice !== null)
|
|
95
|
+
return this._midPrice;
|
|
96
|
+
const price = this.pools.slice(1).reduce(({ nextInput, price }, pool) => {
|
|
97
|
+
return nextInput.equals(pool.token0)
|
|
98
|
+
? {
|
|
99
|
+
nextInput: pool.token1,
|
|
100
|
+
price: price.multiply(pool.token0Price.asFraction),
|
|
101
|
+
}
|
|
102
|
+
: {
|
|
103
|
+
nextInput: pool.token0,
|
|
104
|
+
price: price.multiply(pool.token1Price.asFraction),
|
|
105
|
+
};
|
|
106
|
+
}, this.pools[0].token0.equals(this.pathInput)
|
|
107
|
+
? {
|
|
108
|
+
nextInput: this.pools[0].token1,
|
|
109
|
+
price: this.pools[0].token0Price.asFraction,
|
|
110
|
+
}
|
|
111
|
+
: {
|
|
112
|
+
nextInput: this.pools[0].token0,
|
|
113
|
+
price: this.pools[0].token1Price.asFraction,
|
|
114
|
+
}).price;
|
|
115
|
+
return (this._midPrice = new sdk_core_1.Price(this.input, this.output, price.denominator, price.numerator));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
exports.MixedRouteSDK = MixedRouteSDK;
|
|
119
|
+
//# sourceMappingURL=route.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route.js","sourceRoot":"","sources":["../../../../../src/entities/mixedRoute/route.ts"],"names":[],"mappings":";;;;AAAA,4EAAsC;AACtC,+CAAyD;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AAK,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,UAAU;aAC5C,CACN,CAAC,KAAK,CAAA;QAEP,OAAO,CAAC,IAAI,CAAC,SAAS,GAAG,IAAI,gBAAK,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,WAAW,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC,CAAA;IAClG,CAAC;CACF;AA5HD,sCA4HC"}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { Currency, Percent, Price, CurrencyAmount, TradeType } from '@fenine/sdk-core';
|
|
2
|
+
import { BestTradeOptions } from '@fenine/v3-sdk';
|
|
3
|
+
import { MixedRouteSDK } from './route';
|
|
4
|
+
import { TPool } from '../../utils/TPool';
|
|
5
|
+
/**
|
|
6
|
+
* Trades comparator, an extension of the input output comparator that also considers other dimensions of the trade in ranking them
|
|
7
|
+
* @template TInput The input token, either Ether or an ERC-20
|
|
8
|
+
* @template TOutput The output token, either Ether or an ERC-20
|
|
9
|
+
* @template TTradeType The trade type, either exact input or exact output
|
|
10
|
+
* @param a The first trade to compare
|
|
11
|
+
* @param b The second trade to compare
|
|
12
|
+
* @returns A sorted ordering for two neighboring elements in a trade array
|
|
13
|
+
*/
|
|
14
|
+
export declare function tradeComparator<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(a: MixedRouteTrade<TInput, TOutput, TTradeType>, b: MixedRouteTrade<TInput, TOutput, TTradeType>): number;
|
|
15
|
+
/**
|
|
16
|
+
* Represents a trade executed against a set of routes where some percentage of the input is
|
|
17
|
+
* split across each route.
|
|
18
|
+
*
|
|
19
|
+
* Each route has its own set of pools. Pools can not be re-used across routes.
|
|
20
|
+
*
|
|
21
|
+
* Does not account for slippage, i.e., changes in price environment that can occur between
|
|
22
|
+
* the time the trade is submitted and when it is executed.
|
|
23
|
+
* @notice This class is functionally the same as the `Trade` class in the `@uniswap/v3-sdk` package, aside from typing and some input validation.
|
|
24
|
+
* @template TInput The input token, either Ether or an ERC-20
|
|
25
|
+
* @template TOutput The output token, either Ether or an ERC-20
|
|
26
|
+
* @template TTradeType The trade type, either exact input or exact output
|
|
27
|
+
*/
|
|
28
|
+
export declare class MixedRouteTrade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
|
|
29
|
+
/**
|
|
30
|
+
* @deprecated Deprecated in favor of 'swaps' property. If the trade consists of multiple routes
|
|
31
|
+
* this will return an error.
|
|
32
|
+
*
|
|
33
|
+
* When the trade consists of just a single route, this returns the route of the trade,
|
|
34
|
+
* i.e. which pools the trade goes through.
|
|
35
|
+
*/
|
|
36
|
+
get route(): MixedRouteSDK<TInput, TOutput>;
|
|
37
|
+
/**
|
|
38
|
+
* The swaps of the trade, i.e. which routes and how much is swapped in each that
|
|
39
|
+
* make up the trade.
|
|
40
|
+
*/
|
|
41
|
+
readonly swaps: {
|
|
42
|
+
route: MixedRouteSDK<TInput, TOutput>;
|
|
43
|
+
inputAmount: CurrencyAmount<TInput>;
|
|
44
|
+
outputAmount: CurrencyAmount<TOutput>;
|
|
45
|
+
}[];
|
|
46
|
+
/**
|
|
47
|
+
* The type of the trade, either exact in or exact out.
|
|
48
|
+
*/
|
|
49
|
+
readonly tradeType: TTradeType;
|
|
50
|
+
/**
|
|
51
|
+
* The cached result of the input amount computation
|
|
52
|
+
* @private
|
|
53
|
+
*/
|
|
54
|
+
private _inputAmount;
|
|
55
|
+
/**
|
|
56
|
+
* The input amount for the trade assuming no slippage.
|
|
57
|
+
*/
|
|
58
|
+
get inputAmount(): CurrencyAmount<TInput>;
|
|
59
|
+
/**
|
|
60
|
+
* The cached result of the output amount computation
|
|
61
|
+
* @private
|
|
62
|
+
*/
|
|
63
|
+
private _outputAmount;
|
|
64
|
+
/**
|
|
65
|
+
* The output amount for the trade assuming no slippage.
|
|
66
|
+
*/
|
|
67
|
+
get outputAmount(): CurrencyAmount<TOutput>;
|
|
68
|
+
/**
|
|
69
|
+
* The cached result of the computed execution price
|
|
70
|
+
* @private
|
|
71
|
+
*/
|
|
72
|
+
private _executionPrice;
|
|
73
|
+
/**
|
|
74
|
+
* The price expressed in terms of output amount/input amount.
|
|
75
|
+
*/
|
|
76
|
+
get executionPrice(): Price<TInput, TOutput>;
|
|
77
|
+
/**
|
|
78
|
+
* The cached result of the price impact computation
|
|
79
|
+
* @private
|
|
80
|
+
*/
|
|
81
|
+
private _priceImpact;
|
|
82
|
+
/**
|
|
83
|
+
* Returns the percent difference between the route's mid price and the price impact
|
|
84
|
+
*/
|
|
85
|
+
get priceImpact(): Percent;
|
|
86
|
+
/**
|
|
87
|
+
* Constructs a trade by simulating swaps through the given route
|
|
88
|
+
* @template TInput The input token, either Ether or an ERC-20.
|
|
89
|
+
* @template TOutput The output token, either Ether or an ERC-20.
|
|
90
|
+
* @template TTradeType The type of the trade, either exact in or exact out.
|
|
91
|
+
* @param route route to swap through
|
|
92
|
+
* @param amount the amount specified, either input or output, depending on tradeType
|
|
93
|
+
* @param tradeType whether the trade is an exact input or exact output swap
|
|
94
|
+
* @returns The route
|
|
95
|
+
*/
|
|
96
|
+
static fromRoute<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(route: MixedRouteSDK<TInput, TOutput>, amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>, tradeType: TTradeType): Promise<MixedRouteTrade<TInput, TOutput, TTradeType>>;
|
|
97
|
+
/**
|
|
98
|
+
* Constructs a trade from routes by simulating swaps
|
|
99
|
+
*
|
|
100
|
+
* @template TInput The input token, either Ether or an ERC-20.
|
|
101
|
+
* @template TOutput The output token, either Ether or an ERC-20.
|
|
102
|
+
* @template TTradeType The type of the trade, either exact in or exact out.
|
|
103
|
+
* @param routes the routes to swap through and how much of the amount should be routed through each
|
|
104
|
+
* @param tradeType whether the trade is an exact input or exact output swap
|
|
105
|
+
* @returns The trade
|
|
106
|
+
*/
|
|
107
|
+
static fromRoutes<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(routes: {
|
|
108
|
+
amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>;
|
|
109
|
+
route: MixedRouteSDK<TInput, TOutput>;
|
|
110
|
+
}[], tradeType: TTradeType): Promise<MixedRouteTrade<TInput, TOutput, TTradeType>>;
|
|
111
|
+
/**
|
|
112
|
+
* Creates a trade without computing the result of swapping through the route. Useful when you have simulated the trade
|
|
113
|
+
* elsewhere and do not have any tick data
|
|
114
|
+
* @template TInput The input token, either Ether or an ERC-20
|
|
115
|
+
* @template TOutput The output token, either Ether or an ERC-20
|
|
116
|
+
* @template TTradeType The type of the trade, either exact in or exact out
|
|
117
|
+
* @param constructorArguments The arguments passed to the trade constructor
|
|
118
|
+
* @returns The unchecked trade
|
|
119
|
+
*/
|
|
120
|
+
static createUncheckedTrade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(constructorArguments: {
|
|
121
|
+
route: MixedRouteSDK<TInput, TOutput>;
|
|
122
|
+
inputAmount: CurrencyAmount<TInput>;
|
|
123
|
+
outputAmount: CurrencyAmount<TOutput>;
|
|
124
|
+
tradeType: TTradeType;
|
|
125
|
+
}): MixedRouteTrade<TInput, TOutput, TTradeType>;
|
|
126
|
+
/**
|
|
127
|
+
* Creates a trade without computing the result of swapping through the routes. Useful when you have simulated the trade
|
|
128
|
+
* elsewhere and do not have any tick data
|
|
129
|
+
* @template TInput The input token, either Ether or an ERC-20
|
|
130
|
+
* @template TOutput The output token, either Ether or an ERC-20
|
|
131
|
+
* @template TTradeType The type of the trade, either exact in or exact out
|
|
132
|
+
* @param constructorArguments The arguments passed to the trade constructor
|
|
133
|
+
* @returns The unchecked trade
|
|
134
|
+
*/
|
|
135
|
+
static createUncheckedTradeWithMultipleRoutes<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(constructorArguments: {
|
|
136
|
+
routes: {
|
|
137
|
+
route: MixedRouteSDK<TInput, TOutput>;
|
|
138
|
+
inputAmount: CurrencyAmount<TInput>;
|
|
139
|
+
outputAmount: CurrencyAmount<TOutput>;
|
|
140
|
+
}[];
|
|
141
|
+
tradeType: TTradeType;
|
|
142
|
+
}): MixedRouteTrade<TInput, TOutput, TTradeType>;
|
|
143
|
+
/**
|
|
144
|
+
* Construct a trade by passing in the pre-computed property values
|
|
145
|
+
* @param routes The routes through which the trade occurs
|
|
146
|
+
* @param tradeType The type of trade, exact input or exact output
|
|
147
|
+
*/
|
|
148
|
+
private constructor();
|
|
149
|
+
/**
|
|
150
|
+
* Get the minimum amount that must be received from this trade for the given slippage tolerance
|
|
151
|
+
* @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
|
|
152
|
+
* @returns The amount out
|
|
153
|
+
*/
|
|
154
|
+
minimumAmountOut(slippageTolerance: Percent, amountOut?: CurrencyAmount<TOutput>): CurrencyAmount<TOutput>;
|
|
155
|
+
/**
|
|
156
|
+
* Get the maximum amount in that can be spent via this trade for the given slippage tolerance
|
|
157
|
+
* @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
|
|
158
|
+
* @returns The amount in
|
|
159
|
+
*/
|
|
160
|
+
maximumAmountIn(slippageTolerance: Percent, amountIn?: CurrencyAmount<TInput>): CurrencyAmount<TInput>;
|
|
161
|
+
/**
|
|
162
|
+
* Return the execution price after accounting for slippage tolerance
|
|
163
|
+
* @param slippageTolerance the allowed tolerated slippage
|
|
164
|
+
* @returns The execution price
|
|
165
|
+
*/
|
|
166
|
+
worstExecutionPrice(slippageTolerance: Percent): Price<TInput, TOutput>;
|
|
167
|
+
/**
|
|
168
|
+
* Given a list of pools, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
|
|
169
|
+
* amount to an output token, making at most `maxHops` hops.
|
|
170
|
+
* Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
|
|
171
|
+
* the amount in among multiple routes.
|
|
172
|
+
* @param pools the pools to consider in finding the best trade
|
|
173
|
+
* @param nextAmountIn exact amount of input currency to spend
|
|
174
|
+
* @param currencyOut the desired currency out
|
|
175
|
+
* @param maxNumResults maximum number of results to return
|
|
176
|
+
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pool
|
|
177
|
+
* @param currentPools used in recursion; the current list of pools
|
|
178
|
+
* @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
|
|
179
|
+
* @param bestTrades used in recursion; the current list of best trades
|
|
180
|
+
* @returns The exact in trade
|
|
181
|
+
*/
|
|
182
|
+
static bestTradeExactIn<TInput extends Currency, TOutput extends Currency>(pools: TPool[], currencyAmountIn: CurrencyAmount<TInput>, currencyOut: TOutput, { maxNumResults, maxHops }?: BestTradeOptions, currentPools?: TPool[], nextAmountIn?: CurrencyAmount<Currency>, bestTrades?: MixedRouteTrade<TInput, TOutput, TradeType.EXACT_INPUT>[]): Promise<MixedRouteTrade<TInput, TOutput, TradeType.EXACT_INPUT>[]>;
|
|
183
|
+
}
|