@kittycorn-labs/universal-router-sdk 1.4.43 → 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/README.md +312 -0
- package/dist/cjs/src/abis/UniversalRouter.json +740 -0
- package/dist/{entities → cjs/src/entities}/Command.d.ts +12 -12
- package/dist/cjs/src/entities/Command.js +9 -0
- package/dist/cjs/src/entities/Command.js.map +1 -0
- package/dist/cjs/src/entities/actions/across.d.ts +21 -0
- package/dist/cjs/src/entities/actions/across.js +7 -0
- package/dist/cjs/src/entities/actions/across.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/actions/index.d.ts +3 -2
- package/dist/cjs/src/entities/actions/index.js +7 -0
- package/dist/cjs/src/entities/actions/index.js.map +1 -0
- package/dist/cjs/src/entities/actions/uniswap.d.ts +94 -0
- package/dist/cjs/src/entities/actions/uniswap.js +740 -0
- package/dist/cjs/src/entities/actions/uniswap.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/actions/unwrapWETH.d.ts +12 -12
- package/dist/cjs/src/entities/actions/unwrapWETH.js +33 -0
- package/dist/cjs/src/entities/actions/unwrapWETH.js.map +1 -0
- package/dist/{entities → cjs/src/entities}/index.d.ts +2 -2
- package/dist/cjs/src/entities/index.js +6 -0
- package/dist/cjs/src/entities/index.js.map +1 -0
- package/dist/cjs/src/index.d.ts +19 -0
- package/dist/cjs/src/index.js +43 -0
- package/dist/cjs/src/index.js.map +1 -0
- package/dist/cjs/src/kittycorn.d.ts +9 -0
- package/dist/cjs/src/kittycorn.js +26 -0
- package/dist/cjs/src/kittycorn.js.map +1 -0
- package/dist/cjs/src/swapRouter.d.ts +123 -0
- package/dist/cjs/src/swapRouter.js +445 -0
- package/dist/cjs/src/swapRouter.js.map +1 -0
- package/dist/cjs/src/types/encodeSwaps.d.ts +197 -0
- package/dist/cjs/src/types/encodeSwaps.js +3 -0
- package/dist/cjs/src/types/encodeSwaps.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/commandParser.d.ts +36 -34
- package/dist/cjs/src/utils/commandParser.js +154 -0
- package/dist/cjs/src/utils/commandParser.js.map +1 -0
- package/dist/cjs/src/utils/computeEncodeSwapsAmounts.d.ts +8 -0
- package/dist/cjs/src/utils/computeEncodeSwapsAmounts.js +46 -0
- package/dist/cjs/src/utils/computeEncodeSwapsAmounts.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/constants.d.ts +46 -30
- package/dist/cjs/src/utils/constants.js +777 -0
- package/dist/cjs/src/utils/constants.js.map +1 -0
- package/dist/cjs/src/utils/directTransfers.d.ts +15 -0
- package/dist/cjs/src/utils/directTransfers.js +381 -0
- package/dist/cjs/src/utils/directTransfers.js.map +1 -0
- package/dist/cjs/src/utils/eip712.d.ts +17 -0
- package/dist/cjs/src/utils/eip712.js +44 -0
- package/dist/cjs/src/utils/eip712.js.map +1 -0
- package/dist/cjs/src/utils/encodeSwapStep.d.ts +4 -0
- package/dist/cjs/src/utils/encodeSwapStep.js +62 -0
- package/dist/cjs/src/utils/encodeSwapStep.js.map +1 -0
- package/dist/cjs/src/utils/encodeV4Action.d.ts +7 -0
- package/dist/cjs/src/utils/encodeV4Action.js +84 -0
- package/dist/cjs/src/utils/encodeV4Action.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/getCurrencyAddress.d.ts +2 -2
- package/dist/cjs/src/utils/getCurrencyAddress.js +9 -0
- package/dist/cjs/src/utils/getCurrencyAddress.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/inputTokens.d.ts +23 -23
- package/dist/cjs/src/utils/inputTokens.js +58 -0
- package/dist/cjs/src/utils/inputTokens.js.map +1 -0
- package/dist/cjs/src/utils/normalizeEncodeSwapsSpec.d.ts +6 -0
- package/dist/cjs/src/utils/normalizeEncodeSwapsSpec.js +34 -0
- package/dist/cjs/src/utils/normalizeEncodeSwapsSpec.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/numbers.d.ts +7 -6
- package/dist/cjs/src/utils/numbers.js +27 -0
- package/dist/cjs/src/utils/numbers.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/pathCurrency.d.ts +3 -3
- package/dist/cjs/src/utils/pathCurrency.js +27 -0
- package/dist/cjs/src/utils/pathCurrency.js.map +1 -0
- package/dist/cjs/src/utils/portionFees.d.ts +14 -0
- package/dist/cjs/src/utils/portionFees.js +40 -0
- package/dist/cjs/src/utils/portionFees.js.map +1 -0
- package/dist/cjs/src/utils/routerBalanceSteps.d.ts +31 -0
- package/dist/cjs/src/utils/routerBalanceSteps.js +293 -0
- package/dist/cjs/src/utils/routerBalanceSteps.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/routerCommands.d.ts +77 -64
- package/dist/cjs/src/utils/routerCommands.js +331 -0
- package/dist/cjs/src/utils/routerCommands.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/routerTradeAdapter.d.ts +73 -73
- package/dist/cjs/src/utils/routerTradeAdapter.js +139 -0
- package/dist/cjs/src/utils/routerTradeAdapter.js.map +1 -0
- package/dist/cjs/src/utils/toV4URVersion.d.ts +3 -0
- package/dist/cjs/src/utils/toV4URVersion.js +17 -0
- package/dist/cjs/src/utils/toV4URVersion.js.map +1 -0
- package/dist/cjs/src/utils/validateEncodeSwaps.d.ts +2 -0
- package/dist/cjs/src/utils/validateEncodeSwaps.js +344 -0
- package/dist/cjs/src/utils/validateEncodeSwaps.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/src/abis/UniversalRouter.json +740 -0
- package/dist/esm/src/entities/Command.d.ts +12 -0
- package/dist/esm/src/entities/Command.js +6 -0
- package/dist/esm/src/entities/Command.js.map +1 -0
- package/dist/esm/src/entities/actions/across.d.ts +21 -0
- package/dist/esm/src/entities/actions/across.js +3 -0
- package/dist/esm/src/entities/actions/across.js.map +1 -0
- package/dist/esm/src/entities/actions/index.d.ts +3 -0
- package/dist/esm/src/entities/actions/index.js +4 -0
- package/dist/esm/src/entities/actions/index.js.map +1 -0
- package/dist/esm/src/entities/actions/uniswap.d.ts +94 -0
- package/dist/esm/src/entities/actions/uniswap.js +735 -0
- package/dist/esm/src/entities/actions/uniswap.js.map +1 -0
- package/dist/esm/src/entities/actions/unwrapWETH.d.ts +12 -0
- package/dist/esm/src/entities/actions/unwrapWETH.js +28 -0
- package/dist/esm/src/entities/actions/unwrapWETH.js.map +1 -0
- package/dist/esm/src/entities/index.d.ts +2 -0
- package/dist/esm/src/entities/index.js +3 -0
- package/dist/esm/src/entities/index.js.map +1 -0
- package/dist/esm/src/index.d.ts +19 -0
- package/dist/esm/src/index.js +15 -0
- package/dist/esm/src/index.js.map +1 -0
- package/dist/esm/src/kittycorn.d.ts +9 -0
- package/dist/esm/src/kittycorn.js +22 -0
- package/dist/esm/src/kittycorn.js.map +1 -0
- package/dist/esm/src/swapRouter.d.ts +123 -0
- package/dist/esm/src/swapRouter.js +440 -0
- package/dist/esm/src/swapRouter.js.map +1 -0
- package/dist/esm/src/types/encodeSwaps.d.ts +197 -0
- package/dist/esm/src/types/encodeSwaps.js +2 -0
- package/dist/esm/src/types/encodeSwaps.js.map +1 -0
- package/dist/esm/src/utils/commandParser.d.ts +36 -0
- package/dist/esm/src/utils/commandParser.js +146 -0
- package/dist/esm/src/utils/commandParser.js.map +1 -0
- package/dist/esm/src/utils/computeEncodeSwapsAmounts.d.ts +8 -0
- package/dist/esm/src/utils/computeEncodeSwapsAmounts.js +41 -0
- package/dist/esm/src/utils/computeEncodeSwapsAmounts.js.map +1 -0
- package/dist/esm/src/utils/constants.d.ts +46 -0
- package/dist/esm/src/utils/constants.js +768 -0
- package/dist/esm/src/utils/constants.js.map +1 -0
- package/dist/esm/src/utils/directTransfers.d.ts +15 -0
- package/dist/esm/src/utils/directTransfers.js +369 -0
- package/dist/esm/src/utils/directTransfers.js.map +1 -0
- package/dist/esm/src/utils/eip712.d.ts +17 -0
- package/dist/esm/src/utils/eip712.js +39 -0
- package/dist/esm/src/utils/eip712.js.map +1 -0
- package/dist/esm/src/utils/encodeSwapStep.d.ts +4 -0
- package/dist/esm/src/utils/encodeSwapStep.js +58 -0
- package/dist/esm/src/utils/encodeSwapStep.js.map +1 -0
- package/dist/esm/src/utils/encodeV4Action.d.ts +7 -0
- package/dist/esm/src/utils/encodeV4Action.js +80 -0
- package/dist/esm/src/utils/encodeV4Action.js.map +1 -0
- package/dist/esm/src/utils/getCurrencyAddress.d.ts +2 -0
- package/dist/esm/src/utils/getCurrencyAddress.js +5 -0
- package/dist/esm/src/utils/getCurrencyAddress.js.map +1 -0
- package/dist/esm/src/utils/inputTokens.d.ts +23 -0
- package/dist/esm/src/utils/inputTokens.js +51 -0
- package/dist/esm/src/utils/inputTokens.js.map +1 -0
- package/dist/esm/src/utils/normalizeEncodeSwapsSpec.d.ts +6 -0
- package/dist/esm/src/utils/normalizeEncodeSwapsSpec.js +27 -0
- package/dist/esm/src/utils/normalizeEncodeSwapsSpec.js.map +1 -0
- package/dist/esm/src/utils/numbers.d.ts +7 -0
- package/dist/esm/src/utils/numbers.js +19 -0
- package/dist/esm/src/utils/numbers.js.map +1 -0
- package/dist/esm/src/utils/pathCurrency.d.ts +3 -0
- package/dist/esm/src/utils/pathCurrency.js +23 -0
- package/dist/esm/src/utils/pathCurrency.js.map +1 -0
- package/dist/esm/src/utils/portionFees.d.ts +14 -0
- package/dist/esm/src/utils/portionFees.js +35 -0
- package/dist/esm/src/utils/portionFees.js.map +1 -0
- package/dist/esm/src/utils/routerBalanceSteps.d.ts +31 -0
- package/dist/esm/src/utils/routerBalanceSteps.js +282 -0
- package/dist/esm/src/utils/routerBalanceSteps.js.map +1 -0
- package/dist/esm/src/utils/routerCommands.d.ts +77 -0
- package/dist/esm/src/utils/routerCommands.js +326 -0
- package/dist/esm/src/utils/routerCommands.js.map +1 -0
- package/dist/esm/src/utils/routerTradeAdapter.d.ts +73 -0
- package/dist/esm/src/utils/routerTradeAdapter.js +134 -0
- package/dist/esm/src/utils/routerTradeAdapter.js.map +1 -0
- package/dist/esm/src/utils/toV4URVersion.d.ts +3 -0
- package/dist/esm/src/utils/toV4URVersion.js +13 -0
- package/dist/esm/src/utils/toV4URVersion.js.map +1 -0
- package/dist/esm/src/utils/validateEncodeSwaps.d.ts +2 -0
- package/dist/esm/src/utils/validateEncodeSwaps.js +339 -0
- package/dist/esm/src/utils/validateEncodeSwaps.js.map +1 -0
- package/dist/types/src/entities/Command.d.ts +13 -0
- package/dist/types/src/entities/Command.d.ts.map +1 -0
- package/dist/types/src/entities/actions/across.d.ts +22 -0
- package/dist/types/src/entities/actions/across.d.ts.map +1 -0
- package/dist/types/src/entities/actions/index.d.ts +4 -0
- package/dist/types/src/entities/actions/index.d.ts.map +1 -0
- package/dist/types/src/entities/actions/uniswap.d.ts +95 -0
- package/dist/types/src/entities/actions/uniswap.d.ts.map +1 -0
- package/dist/types/src/entities/actions/unwrapWETH.d.ts +13 -0
- package/dist/types/src/entities/actions/unwrapWETH.d.ts.map +1 -0
- package/dist/types/src/entities/index.d.ts +3 -0
- package/dist/types/src/entities/index.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +20 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/kittycorn.d.ts +10 -0
- package/dist/types/src/kittycorn.d.ts.map +1 -0
- package/dist/types/src/swapRouter.d.ts +124 -0
- package/dist/types/src/swapRouter.d.ts.map +1 -0
- package/dist/types/src/types/encodeSwaps.d.ts +198 -0
- package/dist/types/src/types/encodeSwaps.d.ts.map +1 -0
- package/dist/types/src/utils/commandParser.d.ts +37 -0
- package/dist/types/src/utils/commandParser.d.ts.map +1 -0
- package/dist/types/src/utils/computeEncodeSwapsAmounts.d.ts +9 -0
- package/dist/types/src/utils/computeEncodeSwapsAmounts.d.ts.map +1 -0
- package/dist/types/src/utils/constants.d.ts +47 -0
- package/dist/types/src/utils/constants.d.ts.map +1 -0
- package/dist/types/src/utils/directTransfers.d.ts +16 -0
- package/dist/types/src/utils/directTransfers.d.ts.map +1 -0
- package/dist/types/src/utils/eip712.d.ts +18 -0
- package/dist/types/src/utils/eip712.d.ts.map +1 -0
- package/dist/types/src/utils/encodeSwapStep.d.ts +5 -0
- package/dist/types/src/utils/encodeSwapStep.d.ts.map +1 -0
- package/dist/types/src/utils/encodeV4Action.d.ts +8 -0
- package/dist/types/src/utils/encodeV4Action.d.ts.map +1 -0
- package/dist/types/src/utils/getCurrencyAddress.d.ts +3 -0
- package/dist/types/src/utils/getCurrencyAddress.d.ts.map +1 -0
- package/dist/types/src/utils/inputTokens.d.ts +24 -0
- package/dist/types/src/utils/inputTokens.d.ts.map +1 -0
- package/dist/types/src/utils/normalizeEncodeSwapsSpec.d.ts +7 -0
- package/dist/types/src/utils/normalizeEncodeSwapsSpec.d.ts.map +1 -0
- package/dist/types/src/utils/numbers.d.ts +8 -0
- package/dist/types/src/utils/numbers.d.ts.map +1 -0
- package/dist/types/src/utils/pathCurrency.d.ts +4 -0
- package/dist/types/src/utils/pathCurrency.d.ts.map +1 -0
- package/dist/types/src/utils/portionFees.d.ts +15 -0
- package/dist/types/src/utils/portionFees.d.ts.map +1 -0
- package/dist/types/src/utils/routerBalanceSteps.d.ts +32 -0
- package/dist/types/src/utils/routerBalanceSteps.d.ts.map +1 -0
- package/dist/types/src/utils/routerCommands.d.ts +78 -0
- package/dist/types/src/utils/routerCommands.d.ts.map +1 -0
- package/dist/types/src/utils/routerTradeAdapter.d.ts +74 -0
- package/dist/types/src/utils/routerTradeAdapter.d.ts.map +1 -0
- package/dist/types/src/utils/toV4URVersion.d.ts +4 -0
- package/dist/types/src/utils/toV4URVersion.d.ts.map +1 -0
- package/dist/types/src/utils/validateEncodeSwaps.d.ts +3 -0
- package/dist/types/src/utils/validateEncodeSwaps.d.ts.map +1 -0
- package/package.json +33 -52
- package/dist/entities/actions/uniswap.d.ts +0 -30
- package/dist/index.d.ts +0 -10
- package/dist/index.js +0 -8
- package/dist/swapRouter.d.ts +0 -37
- package/dist/test/forge/writeInterop.d.ts +0 -2
- package/dist/test/utils/addresses.d.ts +0 -5
- package/dist/test/utils/hexToDecimalString.d.ts +0 -2
- package/dist/test/utils/permit2.d.ts +0 -7
- package/dist/test/utils/uniswapData.d.ts +0 -25
- package/dist/universal-router-sdk.cjs.development.js +0 -1500
- package/dist/universal-router-sdk.cjs.development.js.map +0 -1
- package/dist/universal-router-sdk.cjs.production.min.js +0 -2
- package/dist/universal-router-sdk.cjs.production.min.js.map +0 -1
- package/dist/universal-router-sdk.esm.js +0 -1493
- package/dist/universal-router-sdk.esm.js.map +0 -1
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { BigNumberish } from '@ethersproject/bignumber';
|
|
2
|
+
import { Currency, CurrencyAmount, Percent, TradeType } from '@kittycorn-labs/sdk-core';
|
|
3
|
+
import { type PathKey, type PoolKey } from '@kittycorn-labs/v4-sdk';
|
|
4
|
+
import { RouterBalanceInput, TokenTransferMode } from '../entities/actions/uniswap';
|
|
5
|
+
import { Permit2Permit } from '../utils/inputTokens';
|
|
6
|
+
import { UniversalRouterVersion } from '../utils/constants';
|
|
7
|
+
export type { PathKey, PoolKey };
|
|
8
|
+
export type Fee = {
|
|
9
|
+
kind: 'portion';
|
|
10
|
+
recipient: string;
|
|
11
|
+
fee: Percent;
|
|
12
|
+
} | {
|
|
13
|
+
kind: 'flat';
|
|
14
|
+
recipient: string;
|
|
15
|
+
amount: BigNumberish;
|
|
16
|
+
};
|
|
17
|
+
export type PortionFee = Extract<Fee, {
|
|
18
|
+
kind: 'portion';
|
|
19
|
+
}>;
|
|
20
|
+
export type FlatFee = Extract<Fee, {
|
|
21
|
+
kind: 'flat';
|
|
22
|
+
}>;
|
|
23
|
+
/** A bare `Fee` encodes as it always has; an array pays one recipient per entry (at most MAX_FEE_RECIPIENTS), each a fraction of GROSS output, all the same `kind`, and >1 portion needs urVersion >= 2.1.1. */
|
|
24
|
+
export type FeeSpecification = Fee | Fee[];
|
|
25
|
+
export type SwapSpecification = {
|
|
26
|
+
tradeType: TradeType;
|
|
27
|
+
routing: {
|
|
28
|
+
inputToken: Currency;
|
|
29
|
+
outputToken: Currency;
|
|
30
|
+
amount: CurrencyAmount<Currency>;
|
|
31
|
+
quote: CurrencyAmount<Currency>;
|
|
32
|
+
};
|
|
33
|
+
slippageTolerance: Percent;
|
|
34
|
+
recipient?: string;
|
|
35
|
+
fee?: FeeSpecification;
|
|
36
|
+
tokenTransferMode?: TokenTransferMode;
|
|
37
|
+
permit?: Permit2Permit;
|
|
38
|
+
chainId?: number;
|
|
39
|
+
deadline?: BigNumberish;
|
|
40
|
+
urVersion?: UniversalRouterVersion;
|
|
41
|
+
safeMode?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* The input Token is the chain's native gas token exposed via an ERC20 predeploy whose balance
|
|
44
|
+
* mirrors the native balance (e.g. USDC on Arc). The swap is funded by attaching
|
|
45
|
+
* msg.value = exactOrMaxAmountIn * 10^(18 - token.decimals) instead of pulling via Permit2:
|
|
46
|
+
* the PERMIT2_TRANSFER_FROM ingress is skipped and no ERC20 approval or permit is ever needed.
|
|
47
|
+
* Incompatible with native input, permit, and TokenTransferMode.ApproveProxy.
|
|
48
|
+
*/
|
|
49
|
+
nativeErc20Input?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Lets steps pull input straight from the user and pay output straight to `recipient`
|
|
52
|
+
* (instead of router custody), still validated so the user never pays more than
|
|
53
|
+
* `exactOrMaxAmountIn` or receives less than the minimum output. Default false.
|
|
54
|
+
* See `SwapRouter.encodeSwaps`.
|
|
55
|
+
*/
|
|
56
|
+
allowDirectTransfers?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Omit the zero-floor settlement SWEEP when no step can leave the output token on the router.
|
|
59
|
+
* Default false. Needs `allowDirectTransfers`. Assumes the router starts with no output balance
|
|
60
|
+
* and token transfers do not credit it; see the README "Direct Transfers" section.
|
|
61
|
+
*/
|
|
62
|
+
omitRedundantSettlementSweep?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Fund the swap from the Universal Router's own balance of the input token: no Permit2
|
|
65
|
+
* ingress is emitted, the first hop spends the CONTRACT_BALANCE sentinel, and an optional
|
|
66
|
+
* `minimumAmount` emits a BALANCE_CHECK_ERC20 (requires `chainId` to resolve the router
|
|
67
|
+
* address). A native input is funded by attaching msg.value to execute() (raw transfers
|
|
68
|
+
* to the router revert): the plan must lead with a WRAP_ETH, which is resized to wrap the
|
|
69
|
+
* whole balance, the floor is asserted post-wrap as WETH, ETH dust is always swept to the
|
|
70
|
+
* recipient, and the encoded value is 0. Same semantics and guards as
|
|
71
|
+
* `SwapOptions.routerBalanceInput`: explicit `recipient`, EXACT_INPUT, exactly one step
|
|
72
|
+
* spending the (wrapped) input token (no splits); incompatible with `permit`,
|
|
73
|
+
* `nativeErc20Input`, `allowDirectTransfers`, and ApproveProxy.
|
|
74
|
+
*/
|
|
75
|
+
routerBalanceInput?: RouterBalanceInput;
|
|
76
|
+
};
|
|
77
|
+
export type NormalizedSwapSpecification = Omit<SwapSpecification, 'recipient' | 'tokenTransferMode' | 'urVersion' | 'safeMode' | 'allowDirectTransfers'> & {
|
|
78
|
+
recipient: string;
|
|
79
|
+
tokenTransferMode: TokenTransferMode;
|
|
80
|
+
urVersion: UniversalRouterVersion;
|
|
81
|
+
safeMode: boolean;
|
|
82
|
+
allowDirectTransfers: boolean;
|
|
83
|
+
};
|
|
84
|
+
export type V2SwapExactIn = {
|
|
85
|
+
type: 'V2_SWAP_EXACT_IN';
|
|
86
|
+
recipient: string;
|
|
87
|
+
amountIn: BigNumberish;
|
|
88
|
+
amountOutMin: BigNumberish;
|
|
89
|
+
path: string[];
|
|
90
|
+
minHopPriceX36?: BigNumberish[];
|
|
91
|
+
payerIsUser?: boolean;
|
|
92
|
+
};
|
|
93
|
+
export type V2SwapExactOut = {
|
|
94
|
+
type: 'V2_SWAP_EXACT_OUT';
|
|
95
|
+
recipient: string;
|
|
96
|
+
amountOut: BigNumberish;
|
|
97
|
+
amountInMax: BigNumberish;
|
|
98
|
+
path: string[];
|
|
99
|
+
minHopPriceX36?: BigNumberish[];
|
|
100
|
+
payerIsUser?: boolean;
|
|
101
|
+
};
|
|
102
|
+
export type V3SwapExactIn = {
|
|
103
|
+
type: 'V3_SWAP_EXACT_IN';
|
|
104
|
+
recipient: string;
|
|
105
|
+
amountIn: BigNumberish;
|
|
106
|
+
amountOutMin: BigNumberish;
|
|
107
|
+
path: string;
|
|
108
|
+
minHopPriceX36?: BigNumberish[];
|
|
109
|
+
payerIsUser?: boolean;
|
|
110
|
+
};
|
|
111
|
+
export type V3SwapExactOut = {
|
|
112
|
+
type: 'V3_SWAP_EXACT_OUT';
|
|
113
|
+
recipient: string;
|
|
114
|
+
amountOut: BigNumberish;
|
|
115
|
+
amountInMax: BigNumberish;
|
|
116
|
+
path: string;
|
|
117
|
+
minHopPriceX36?: BigNumberish[];
|
|
118
|
+
payerIsUser?: boolean;
|
|
119
|
+
};
|
|
120
|
+
export type V4Swap = {
|
|
121
|
+
type: 'V4_SWAP';
|
|
122
|
+
v4Actions: V4Action[];
|
|
123
|
+
};
|
|
124
|
+
export type WrapEth = {
|
|
125
|
+
type: 'WRAP_ETH';
|
|
126
|
+
recipient: string;
|
|
127
|
+
amount: BigNumberish;
|
|
128
|
+
};
|
|
129
|
+
export type UnwrapWeth = {
|
|
130
|
+
type: 'UNWRAP_WETH';
|
|
131
|
+
recipient: string;
|
|
132
|
+
amountMin: BigNumberish;
|
|
133
|
+
};
|
|
134
|
+
export type SwapStep = V2SwapExactIn | V2SwapExactOut | V3SwapExactIn | V3SwapExactOut | V4Swap | WrapEth | UnwrapWeth;
|
|
135
|
+
export type V4SwapExactIn = {
|
|
136
|
+
action: 'SWAP_EXACT_IN';
|
|
137
|
+
currencyIn: string;
|
|
138
|
+
path: PathKey[];
|
|
139
|
+
amountIn: BigNumberish;
|
|
140
|
+
amountOutMinimum: BigNumberish;
|
|
141
|
+
minHopPriceX36?: BigNumberish[];
|
|
142
|
+
};
|
|
143
|
+
export type V4SwapExactInSingle = {
|
|
144
|
+
action: 'SWAP_EXACT_IN_SINGLE';
|
|
145
|
+
poolKey: PoolKey;
|
|
146
|
+
zeroForOne: boolean;
|
|
147
|
+
amountIn: BigNumberish;
|
|
148
|
+
amountOutMinimum: BigNumberish;
|
|
149
|
+
minHopPriceX36?: BigNumberish;
|
|
150
|
+
hookData: string;
|
|
151
|
+
};
|
|
152
|
+
export type V4SwapExactOut = {
|
|
153
|
+
action: 'SWAP_EXACT_OUT';
|
|
154
|
+
currencyOut: string;
|
|
155
|
+
path: PathKey[];
|
|
156
|
+
amountOut: BigNumberish;
|
|
157
|
+
amountInMaximum: BigNumberish;
|
|
158
|
+
minHopPriceX36?: BigNumberish[];
|
|
159
|
+
};
|
|
160
|
+
export type V4SwapExactOutSingle = {
|
|
161
|
+
action: 'SWAP_EXACT_OUT_SINGLE';
|
|
162
|
+
poolKey: PoolKey;
|
|
163
|
+
zeroForOne: boolean;
|
|
164
|
+
amountOut: BigNumberish;
|
|
165
|
+
amountInMaximum: BigNumberish;
|
|
166
|
+
minHopPriceX36?: BigNumberish;
|
|
167
|
+
hookData: string;
|
|
168
|
+
};
|
|
169
|
+
export type V4Settle = {
|
|
170
|
+
action: 'SETTLE';
|
|
171
|
+
currency: string;
|
|
172
|
+
amount: BigNumberish;
|
|
173
|
+
payerIsUser?: boolean;
|
|
174
|
+
};
|
|
175
|
+
export type V4SettleAll = {
|
|
176
|
+
action: 'SETTLE_ALL';
|
|
177
|
+
currency: string;
|
|
178
|
+
maxAmount: BigNumberish;
|
|
179
|
+
};
|
|
180
|
+
export type V4Take = {
|
|
181
|
+
action: 'TAKE';
|
|
182
|
+
currency: string;
|
|
183
|
+
recipient: string;
|
|
184
|
+
amount: BigNumberish;
|
|
185
|
+
};
|
|
186
|
+
export type V4TakeAll = {
|
|
187
|
+
action: 'TAKE_ALL';
|
|
188
|
+
currency: string;
|
|
189
|
+
minAmount: BigNumberish;
|
|
190
|
+
};
|
|
191
|
+
export type V4TakePortion = {
|
|
192
|
+
action: 'TAKE_PORTION';
|
|
193
|
+
currency: string;
|
|
194
|
+
recipient: string;
|
|
195
|
+
bips: BigNumberish;
|
|
196
|
+
};
|
|
197
|
+
export type V4Action = V4SwapExactIn | V4SwapExactInSingle | V4SwapExactOut | V4SwapExactOutSingle | V4Settle | V4SettleAll | V4Take | V4TakeAll | V4TakePortion;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encodeSwaps.js","sourceRoot":"","sources":["../../../../src/types/encodeSwaps.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Interface } from '@ethersproject/abi';
|
|
2
|
+
import { CommandType, CommandDefinition } from '../utils/routerCommands';
|
|
3
|
+
import { UniversalRouterVersion } from '../utils/constants';
|
|
4
|
+
export type Param = {
|
|
5
|
+
readonly name: string;
|
|
6
|
+
readonly value: any;
|
|
7
|
+
};
|
|
8
|
+
export type UniversalRouterCommand = {
|
|
9
|
+
readonly commandName: string;
|
|
10
|
+
readonly commandType: CommandType;
|
|
11
|
+
readonly params: readonly Param[];
|
|
12
|
+
};
|
|
13
|
+
export type UniversalRouterCall = {
|
|
14
|
+
readonly commands: readonly UniversalRouterCommand[];
|
|
15
|
+
};
|
|
16
|
+
export type V3PathItem = {
|
|
17
|
+
readonly tokenIn: string;
|
|
18
|
+
readonly tokenOut: string;
|
|
19
|
+
readonly fee: number;
|
|
20
|
+
};
|
|
21
|
+
export interface CommandsDefinition {
|
|
22
|
+
[key: number]: CommandDefinition;
|
|
23
|
+
}
|
|
24
|
+
export declare abstract class CommandParser {
|
|
25
|
+
static INTERFACE: Interface;
|
|
26
|
+
static parseCalldata(calldata: string, urVersion?: UniversalRouterVersion): UniversalRouterCall;
|
|
27
|
+
}
|
|
28
|
+
export declare class GenericCommandParser {
|
|
29
|
+
private readonly commandDefinition;
|
|
30
|
+
private readonly urVersion;
|
|
31
|
+
constructor(commandDefinition: CommandsDefinition, urVersion?: UniversalRouterVersion);
|
|
32
|
+
parse(commands: string, inputs: string[]): UniversalRouterCall;
|
|
33
|
+
private static getCommands;
|
|
34
|
+
}
|
|
35
|
+
export declare function parseV3PathExactIn(path: string): readonly V3PathItem[];
|
|
36
|
+
export declare function parseV3PathExactOut(path: string): readonly V3PathItem[];
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { defaultAbiCoder } from '@ethersproject/abi';
|
|
2
|
+
import { getAddress } from '@ethersproject/address';
|
|
3
|
+
import UniversalRouter from '../abis/UniversalRouter.json' with { type: "json" };
|
|
4
|
+
import { Interface } from '@ethersproject/abi';
|
|
5
|
+
import { V4BaseActionsParser } from '@kittycorn-labs/v4-sdk';
|
|
6
|
+
import { CommandType, COMMAND_DEFINITION, V2V3_SWAP_COMMANDS_V2_1_1, Subparser, Parser, } from '../utils/routerCommands.js';
|
|
7
|
+
import { UniversalRouterVersion, isAtLeastV2_1_1 } from '../utils/constants.js';
|
|
8
|
+
// Parses UniversalRouter V2 commands
|
|
9
|
+
export class CommandParser {
|
|
10
|
+
static parseCalldata(calldata, urVersion = UniversalRouterVersion.V2_0) {
|
|
11
|
+
// From V2.1.1 onwards the V2/V3 swap commands carry an extra minHopPriceX36 array,
|
|
12
|
+
// so the matching command definitions must be used to decode them correctly.
|
|
13
|
+
const commandDefinition = isAtLeastV2_1_1(urVersion)
|
|
14
|
+
? { ...COMMAND_DEFINITION, ...V2V3_SWAP_COMMANDS_V2_1_1 }
|
|
15
|
+
: COMMAND_DEFINITION;
|
|
16
|
+
const genericParser = new GenericCommandParser(commandDefinition, urVersion);
|
|
17
|
+
const txDescription = CommandParser.INTERFACE.parseTransaction({ data: calldata });
|
|
18
|
+
const { commands, inputs } = txDescription.args;
|
|
19
|
+
return genericParser.parse(commands, inputs);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
CommandParser.INTERFACE = new Interface(UniversalRouter.abi);
|
|
23
|
+
// Parses commands based on given command definition
|
|
24
|
+
export class GenericCommandParser {
|
|
25
|
+
constructor(commandDefinition, urVersion = UniversalRouterVersion.V2_0) {
|
|
26
|
+
this.commandDefinition = commandDefinition;
|
|
27
|
+
this.urVersion = urVersion;
|
|
28
|
+
}
|
|
29
|
+
parse(commands, inputs) {
|
|
30
|
+
const commandTypes = GenericCommandParser.getCommands(commands);
|
|
31
|
+
return {
|
|
32
|
+
commands: commandTypes.map((commandType, i) => {
|
|
33
|
+
const commandDef = this.commandDefinition[commandType];
|
|
34
|
+
if (commandDef.parser === Parser.V4Actions) {
|
|
35
|
+
// UniversalRouterVersion and URVersion share identical string values; cast across the SDK boundary
|
|
36
|
+
const { actions } = V4BaseActionsParser.parseCalldata(inputs[i], this.urVersion);
|
|
37
|
+
return {
|
|
38
|
+
commandName: CommandType[commandType],
|
|
39
|
+
commandType,
|
|
40
|
+
params: v4RouterCallToParams(actions),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
else if (commandDef.parser === Parser.Abi) {
|
|
44
|
+
const abiDef = commandDef.params;
|
|
45
|
+
const rawParams = defaultAbiCoder.decode(abiDef.map((command) => command.type), inputs[i]);
|
|
46
|
+
const params = rawParams.map((param, j) => {
|
|
47
|
+
switch (abiDef[j].subparser) {
|
|
48
|
+
case Subparser.V3PathExactIn:
|
|
49
|
+
return {
|
|
50
|
+
name: abiDef[j].name,
|
|
51
|
+
value: parseV3PathExactIn(param),
|
|
52
|
+
};
|
|
53
|
+
case Subparser.V3PathExactOut:
|
|
54
|
+
return {
|
|
55
|
+
name: abiDef[j].name,
|
|
56
|
+
value: parseV3PathExactOut(param),
|
|
57
|
+
};
|
|
58
|
+
default:
|
|
59
|
+
return {
|
|
60
|
+
name: abiDef[j].name,
|
|
61
|
+
value: param,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
return {
|
|
66
|
+
commandName: CommandType[commandType],
|
|
67
|
+
commandType,
|
|
68
|
+
params,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
else if (commandDef.parser === Parser.V3Actions) {
|
|
72
|
+
// TODO: implement better parsing here
|
|
73
|
+
return {
|
|
74
|
+
commandName: CommandType[commandType],
|
|
75
|
+
commandType,
|
|
76
|
+
params: inputs.map((input) => ({ name: 'command', value: input })),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
throw new Error(`Unsupported parser: ${commandDef}`);
|
|
81
|
+
}
|
|
82
|
+
}),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
// parse command types from bytes string
|
|
86
|
+
static getCommands(commands) {
|
|
87
|
+
const commandTypes = [];
|
|
88
|
+
for (let i = 2; i < commands.length; i += 2) {
|
|
89
|
+
const byte = commands.substring(i, i + 2);
|
|
90
|
+
commandTypes.push(parseInt(byte, 16));
|
|
91
|
+
}
|
|
92
|
+
return commandTypes;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export function parseV3PathExactIn(path) {
|
|
96
|
+
const strippedPath = path.replace('0x', '');
|
|
97
|
+
let tokenIn = getAddress(strippedPath.substring(0, 40));
|
|
98
|
+
let loc = 40;
|
|
99
|
+
const res = [];
|
|
100
|
+
while (loc < strippedPath.length) {
|
|
101
|
+
const feeAndTokenOut = strippedPath.substring(loc, loc + 46);
|
|
102
|
+
const fee = parseInt(feeAndTokenOut.substring(0, 6), 16);
|
|
103
|
+
const tokenOut = getAddress(feeAndTokenOut.substring(6, 46));
|
|
104
|
+
res.push({
|
|
105
|
+
tokenIn,
|
|
106
|
+
tokenOut,
|
|
107
|
+
fee,
|
|
108
|
+
});
|
|
109
|
+
tokenIn = tokenOut;
|
|
110
|
+
loc += 46;
|
|
111
|
+
}
|
|
112
|
+
return res;
|
|
113
|
+
}
|
|
114
|
+
export function parseV3PathExactOut(path) {
|
|
115
|
+
const strippedPath = path.replace('0x', '');
|
|
116
|
+
let tokenIn = getAddress(strippedPath.substring(strippedPath.length - 40));
|
|
117
|
+
let loc = strippedPath.length - 86; // 86 = (20 addr + 3 fee + 20 addr) * 2 (for hex characters)
|
|
118
|
+
const res = [];
|
|
119
|
+
while (loc >= 0) {
|
|
120
|
+
const feeAndTokenOut = strippedPath.substring(loc, loc + 46);
|
|
121
|
+
const tokenOut = getAddress(feeAndTokenOut.substring(0, 40));
|
|
122
|
+
const fee = parseInt(feeAndTokenOut.substring(40, 46), 16);
|
|
123
|
+
res.push({
|
|
124
|
+
tokenIn,
|
|
125
|
+
tokenOut,
|
|
126
|
+
fee,
|
|
127
|
+
});
|
|
128
|
+
tokenIn = tokenOut;
|
|
129
|
+
loc -= 46;
|
|
130
|
+
}
|
|
131
|
+
return res;
|
|
132
|
+
}
|
|
133
|
+
function v4RouterCallToParams(actions) {
|
|
134
|
+
return actions.map((action) => {
|
|
135
|
+
return {
|
|
136
|
+
name: action.actionName,
|
|
137
|
+
value: action.params.map((param) => {
|
|
138
|
+
return {
|
|
139
|
+
name: param.name,
|
|
140
|
+
value: param.value,
|
|
141
|
+
};
|
|
142
|
+
}),
|
|
143
|
+
};
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=commandParser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commandParser.js","sourceRoot":"","sources":["../../../../src/utils/commandParser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACnD,OAAO,eAAe,MAAM,8BAA8B,CAAA;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAC9C,OAAO,EAAa,mBAAmB,EAAkB,MAAM,wBAAwB,CAAA;AACvF,OAAO,EACL,WAAW,EAEX,kBAAkB,EAClB,yBAAyB,EACzB,SAAS,EACT,MAAM,GACP,MAAM,yBAAyB,CAAA;AAChC,OAAO,EAAE,sBAAsB,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AA2B5E,qCAAqC;AACrC,MAAM,OAAgB,aAAa;IAG1B,MAAM,CAAC,aAAa,CACzB,QAAgB,EAChB,YAAoC,sBAAsB,CAAC,IAAI;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AG,CAAC,CAAC,MAAM,EAAE,EAAE;QAC5B,OAAO;YACL,IAAI,EAAE,MAAM,CAAC,UAAU;YACvB,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;gBACjC,OAAO;oBACL,IAAI,EAAE,KAAK,CAAC,IAAI;oBAChB,KAAK,EAAE,KAAK,CAAC,KAAK;iBACnB,CAAA;YACH,CAAC,CAAC;SACH,CAAA;IACH,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { BigNumber } from '@ethersproject/bignumber';
|
|
2
|
+
import { NormalizedSwapSpecification } from '../types/encodeSwaps';
|
|
3
|
+
export type EncodeSwapsAmounts = {
|
|
4
|
+
exactOrMaxAmountIn: BigNumber;
|
|
5
|
+
grossMinOrExactAmountOut: BigNumber;
|
|
6
|
+
netMinOrExactAmountOut: BigNumber;
|
|
7
|
+
};
|
|
8
|
+
export declare function computeEncodeSwapsAmounts(spec: NormalizedSwapSpecification): EncodeSwapsAmounts;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { BigNumber } from '@ethersproject/bignumber';
|
|
2
|
+
import invariant from 'tiny-invariant';
|
|
3
|
+
import { TradeType } from '@kittycorn-labs/sdk-core';
|
|
4
|
+
import { isAtLeastV2_1_1 } from './constants.js';
|
|
5
|
+
import { toFlatFeeList, toPortionFeeList } from './normalizeEncodeSwapsSpec.js';
|
|
6
|
+
import { scalePortionFees, simulatePortionFeeDeduction } from './portionFees.js';
|
|
7
|
+
// Slippage is applied to the quote: scaled down to a floor for exact-input output,
|
|
8
|
+
// scaled up to a ceiling for exact-output input. The unscaled side is the user's exact value.
|
|
9
|
+
// Portion fees are fractions of the gross output; flat fees pair with exact-output.
|
|
10
|
+
export function computeEncodeSwapsAmounts(spec) {
|
|
11
|
+
const routingAmount = BigNumber.from(spec.routing.amount.quotient.toString());
|
|
12
|
+
const routingQuote = BigNumber.from(spec.routing.quote.quotient.toString());
|
|
13
|
+
const slippageNumerator = BigNumber.from(spec.slippageTolerance.numerator.toString());
|
|
14
|
+
const slippageDenominator = BigNumber.from(spec.slippageTolerance.denominator.toString());
|
|
15
|
+
if (spec.tradeType === TradeType.EXACT_INPUT) {
|
|
16
|
+
const grossMinOrExactAmountOut = routingQuote
|
|
17
|
+
.mul(slippageDenominator.sub(slippageNumerator))
|
|
18
|
+
.div(slippageDenominator);
|
|
19
|
+
// Replays the encoded cascade at the gross minimum, so the sweep floor expects exactly what the commands leave behind.
|
|
20
|
+
const feeAmount = simulatePortionFeeDeduction(grossMinOrExactAmountOut, scalePortionFees(toPortionFeeList(spec.fee)), isAtLeastV2_1_1(spec.urVersion));
|
|
21
|
+
return {
|
|
22
|
+
exactOrMaxAmountIn: routingAmount,
|
|
23
|
+
grossMinOrExactAmountOut,
|
|
24
|
+
netMinOrExactAmountOut: grossMinOrExactAmountOut.sub(feeAmount),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
const exactOrMaxAmountIn = routingQuote.mul(slippageDenominator.add(slippageNumerator)).div(slippageDenominator);
|
|
28
|
+
const grossMinOrExactAmountOut = routingAmount;
|
|
29
|
+
// Flat fees are absolute TRANSFERs, so the router must hold every one on top of the settled amount.
|
|
30
|
+
let flatFeeTotal = BigNumber.from(0);
|
|
31
|
+
for (const flatFee of toFlatFeeList(spec.fee)) {
|
|
32
|
+
flatFeeTotal = flatFeeTotal.add(BigNumber.from(flatFee.amount));
|
|
33
|
+
}
|
|
34
|
+
invariant(flatFeeTotal.lte(grossMinOrExactAmountOut), 'FEE_TOTAL_GT_AMOUNT_OUT');
|
|
35
|
+
return {
|
|
36
|
+
exactOrMaxAmountIn,
|
|
37
|
+
grossMinOrExactAmountOut,
|
|
38
|
+
netMinOrExactAmountOut: grossMinOrExactAmountOut.sub(flatFeeTotal),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=computeEncodeSwapsAmounts.js.map
|
|
@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AAM,SAAS,GAAG,2BAA2B,CAC3C,wBAAwB,EACxB,gBAAgB,CAAC,gBAAgB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAC5C,eAAe,CAAC,IAAI,CAAC,SAAS,CAAC,CAChC,CAAA;QAED,OAAO;YACL,kBAAkB,EAAE,aAAa;YACjC,wBAAwB;YACxB,sBAAsB,EAAE,wBAAwB,CAAC,GAAG,CAAC,SAAS,CAAC;SAChE,CAAA;KACF;IAED,MAAM,kBAAkB,GAAG,YAAY,CAAC,GAAG,CAAC,mBAAmB,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAA;IAChH,MAAM,wBAAwB,GAAG,aAAa,CAAA;IAE9C,oGAAoG;IACpG,IAAI,YAAY,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IACpC,KAAK,MAAM,OAAO,IAAI,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;QAC7C,YAAY,GAAG,YAAY,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAA;KAChE;IACD,SAAS,CAAC,YAAY,CAAC,GAAG,CAAC,wBAAwB,CAAC,EAAE,yBAAyB,CAAC,CAAA;IAEhF,OAAO;QACL,kBAAkB;QAClB,wBAAwB;QACxB,sBAAsB,EAAE,wBAAwB,CAAC,GAAG,CAAC,YAAY,CAAC;KACnE,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { BigNumber } from '@ethersproject/bignumber';
|
|
2
|
+
export declare enum UniversalRouterVersion {
|
|
3
|
+
V1_2 = "1.2",
|
|
4
|
+
V2_0 = "2.0",
|
|
5
|
+
V2_1_1 = "2.1.1",
|
|
6
|
+
V2_1_2 = "2.1.2",
|
|
7
|
+
V2_2_0 = "2.2.0"
|
|
8
|
+
}
|
|
9
|
+
/** Version assumed when a caller does not pass `urVersion`; kept at V2_0 for backward compatibility. */
|
|
10
|
+
export declare const DEFAULT_UR_VERSION = UniversalRouterVersion.V2_0;
|
|
11
|
+
/**
|
|
12
|
+
* Check if a UniversalRouterVersion is at least V2_1_1.
|
|
13
|
+
* Duplicated from @kittycorn-labs/v4-sdk's isAtLeastV2_1_1 (which operates on URVersion)
|
|
14
|
+
* to avoid coupling universal-router-sdk's version logic to v4-sdk.
|
|
15
|
+
*/
|
|
16
|
+
export declare function isAtLeastV2_1_1(version?: UniversalRouterVersion): boolean;
|
|
17
|
+
/** Check if a UniversalRouterVersion is at least V2_0, the first router with the position-manager commands. */
|
|
18
|
+
export declare function isAtLeastV2_0(version?: UniversalRouterVersion): boolean;
|
|
19
|
+
export type RouterConfig = {
|
|
20
|
+
address: string;
|
|
21
|
+
creationBlock: number;
|
|
22
|
+
};
|
|
23
|
+
type ChainConfig = {
|
|
24
|
+
weth: string;
|
|
25
|
+
nativeErc20BalanceAlias: boolean;
|
|
26
|
+
routerConfigs: {
|
|
27
|
+
[key in UniversalRouterVersion]?: RouterConfig;
|
|
28
|
+
};
|
|
29
|
+
swapProxy?: string;
|
|
30
|
+
};
|
|
31
|
+
export declare const CHAIN_CONFIGS: {
|
|
32
|
+
[key: number]: ChainConfig;
|
|
33
|
+
};
|
|
34
|
+
export declare const UNIVERSAL_ROUTER_ADDRESS: (version: UniversalRouterVersion, chainId: number) => string;
|
|
35
|
+
export declare const SWAP_PROXY_ADDRESS: (chainId: number) => string;
|
|
36
|
+
export declare const UNIVERSAL_ROUTER_CREATION_BLOCK: (version: UniversalRouterVersion, chainId: number) => number;
|
|
37
|
+
export declare const WETH_ADDRESS: (chainId: number) => string;
|
|
38
|
+
export declare const CONTRACT_BALANCE: BigNumber;
|
|
39
|
+
export declare const ETH_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
40
|
+
export declare const E_ETH_ADDRESS = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
|
|
41
|
+
export declare const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
42
|
+
export declare const MAX_UINT256: BigNumber;
|
|
43
|
+
export declare const MAX_UINT160: BigNumber;
|
|
44
|
+
export declare const SENDER_AS_RECIPIENT = "0x0000000000000000000000000000000000000001";
|
|
45
|
+
export declare const ROUTER_AS_RECIPIENT = "0x0000000000000000000000000000000000000002";
|
|
46
|
+
export {};
|