@nradko/metric-omm-sdk-v1 0.4.2 → 0.4.4
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 +134 -19
- package/dist/abis/DepositAllowlistExtension.d.ts +43 -0
- package/dist/abis/DepositAllowlistExtension.d.ts.map +1 -1
- package/dist/abis/DepositAllowlistExtension.js +56 -0
- package/dist/abis/DepositAllowlistExtension.js.map +1 -1
- package/dist/abis/MetricOmmPoolFactory.d.ts +12 -0
- package/dist/abis/MetricOmmPoolFactory.d.ts.map +1 -1
- package/dist/abis/MetricOmmPoolFactory.js +16 -0
- package/dist/abis/MetricOmmPoolFactory.js.map +1 -1
- package/dist/abis/MetricOmmPoolLiquidityAdder.d.ts +116 -5
- package/dist/abis/MetricOmmPoolLiquidityAdder.d.ts.map +1 -1
- package/dist/abis/MetricOmmPoolLiquidityAdder.js +150 -5
- package/dist/abis/MetricOmmPoolLiquidityAdder.js.map +1 -1
- package/dist/abis/MetricOmmSimpleRouter.d.ts +32 -0
- package/dist/abis/MetricOmmSimpleRouter.d.ts.map +1 -1
- package/dist/abis/MetricOmmSimpleRouter.js +42 -0
- package/dist/abis/MetricOmmSimpleRouter.js.map +1 -1
- package/dist/abis/MetricOmmSwapQuoter.d.ts +14 -2
- package/dist/abis/MetricOmmSwapQuoter.d.ts.map +1 -1
- package/dist/abis/MetricOmmSwapQuoter.js +18 -2
- package/dist/abis/MetricOmmSwapQuoter.js.map +1 -1
- package/dist/abis/SwapAllowlistExtension.d.ts +43 -0
- package/dist/abis/SwapAllowlistExtension.d.ts.map +1 -1
- package/dist/abis/SwapAllowlistExtension.js +56 -0
- package/dist/abis/SwapAllowlistExtension.js.map +1 -1
- package/dist/abis/index.d.ts +2 -3
- package/dist/abis/index.d.ts.map +1 -1
- package/dist/abis/index.js +2 -3
- package/dist/abis/index.js.map +1 -1
- package/dist/addresses.d.ts +2 -4
- package/dist/addresses.d.ts.map +1 -1
- package/dist/addresses.js +11 -12
- package/dist/addresses.js.map +1 -1
- package/dist/dataProvider/index.d.ts +1 -1
- package/dist/dataProvider/index.d.ts.map +1 -1
- package/dist/dataProvider/index.js +1 -1
- package/dist/dataProvider/index.js.map +1 -1
- package/dist/dataProvider/read.d.ts +1 -3
- package/dist/dataProvider/read.d.ts.map +1 -1
- package/dist/dataProvider/read.js +1 -3
- package/dist/dataProvider/read.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -3
- package/dist/index.js.map +1 -1
- package/dist/peripheryPayments/index.d.ts +2 -0
- package/dist/peripheryPayments/index.d.ts.map +1 -0
- package/dist/peripheryPayments/index.js +2 -0
- package/dist/peripheryPayments/index.js.map +1 -0
- package/dist/peripheryPayments/simpleRouter.d.ts +9 -0
- package/dist/peripheryPayments/simpleRouter.d.ts.map +1 -0
- package/dist/peripheryPayments/simpleRouter.js +27 -0
- package/dist/peripheryPayments/simpleRouter.js.map +1 -0
- package/dist/pool/cursorBounds.d.ts +29 -0
- package/dist/pool/cursorBounds.d.ts.map +1 -0
- package/dist/pool/cursorBounds.js +76 -0
- package/dist/pool/cursorBounds.js.map +1 -0
- package/dist/pool/index.d.ts +3 -1
- package/dist/pool/index.d.ts.map +1 -1
- package/dist/pool/index.js +3 -1
- package/dist/pool/index.js.map +1 -1
- package/dist/pool/liquidity.d.ts +114 -7
- package/dist/pool/liquidity.d.ts.map +1 -1
- package/dist/pool/liquidity.js +96 -11
- package/dist/pool/liquidity.js.map +1 -1
- package/dist/pool/nativeLiquidity.d.ts +32 -0
- package/dist/pool/nativeLiquidity.d.ts.map +1 -0
- package/dist/pool/nativeLiquidity.js +47 -0
- package/dist/pool/nativeLiquidity.js.map +1 -0
- package/dist/router/fromRoute.d.ts +93 -0
- package/dist/router/fromRoute.d.ts.map +1 -0
- package/dist/router/fromRoute.js +187 -0
- package/dist/router/fromRoute.js.map +1 -0
- package/dist/router/index.d.ts +8 -1
- package/dist/router/index.d.ts.map +1 -1
- package/dist/router/index.js +8 -1
- package/dist/router/index.js.map +1 -1
- package/dist/router/nativeSwap.d.ts +47 -0
- package/dist/router/nativeSwap.d.ts.map +1 -0
- package/dist/router/nativeSwap.js +89 -0
- package/dist/router/nativeSwap.js.map +1 -0
- package/dist/router/swap.d.ts +82 -1
- package/dist/router/swap.d.ts.map +1 -1
- package/dist/router/swap.js +51 -1
- package/dist/router/swap.js.map +1 -1
- package/dist/router/swapPath.d.ts +46 -1
- package/dist/router/swapPath.d.ts.map +1 -1
- package/dist/router/swapPath.js +34 -0
- package/dist/router/swapPath.js.map +1 -1
- package/package.json +2 -2
- package/src/abis/DepositAllowlistExtension.ts +56 -0
- package/src/abis/MetricOmmPoolFactory.ts +16 -0
- package/src/abis/MetricOmmPoolLiquidityAdder.ts +150 -5
- package/src/abis/MetricOmmSimpleRouter.ts +42 -0
- package/src/abis/MetricOmmSwapQuoter.ts +18 -2
- package/src/abis/SwapAllowlistExtension.ts +56 -0
- package/src/abis/index.ts +2 -3
- package/src/addresses.ts +12 -15
- package/src/dataProvider/index.ts +0 -1
- package/src/dataProvider/read.ts +1 -4
- package/src/index.ts +38 -2
- package/src/peripheryPayments/index.ts +5 -0
- package/src/peripheryPayments/simpleRouter.ts +31 -0
- package/src/pool/cursorBounds.ts +112 -0
- package/src/pool/index.ts +15 -0
- package/src/pool/liquidity.ts +124 -15
- package/src/pool/nativeLiquidity.ts +71 -0
- package/src/router/fromRoute.ts +318 -0
- package/src/router/index.ts +36 -1
- package/src/router/nativeSwap.ts +123 -0
- package/src/router/swap.ts +82 -1
- package/src/router/swapPath.ts +47 -1
- package/dist/abis/MetricOmmPoolStateView.d.ts +0 -238
- package/dist/abis/MetricOmmPoolStateView.d.ts.map +0 -1
- package/dist/abis/MetricOmmPoolStateView.js +0 -315
- package/dist/abis/MetricOmmPoolStateView.js.map +0 -1
- package/src/abis/MetricOmmPoolStateView.ts +0 -314
package/src/router/index.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* SimpleRouter swap API — quotes, calldata encoding, and native ETH multicall helpers.
|
|
3
|
+
*
|
|
4
|
+
* **ERC-20 swap:** `buildExactInputSingleParams` → `encodeExactInputSingleCalldata` → send to `simpleRouter`.
|
|
5
|
+
|
|
6
|
+
* Approve `tokenIn` on the router for ERC-20 input (not required for ETH input).
|
|
3
7
|
*/
|
|
4
8
|
|
|
5
9
|
export {
|
|
@@ -28,3 +32,34 @@ export {
|
|
|
28
32
|
type BuildExactInputSingleParams,
|
|
29
33
|
type BuildExactOutputSingleParams,
|
|
30
34
|
} from "./swap.js";
|
|
35
|
+
|
|
36
|
+
export {
|
|
37
|
+
routeToExactInputParams as prepareExactInputFromRoute,
|
|
38
|
+
routeToExactOutputParams as prepareExactOutputFromRoute,
|
|
39
|
+
type TradeType,
|
|
40
|
+
type RouteType,
|
|
41
|
+
type RouteLegResponse,
|
|
42
|
+
type RouteOptionResponse,
|
|
43
|
+
type RouteResponse,
|
|
44
|
+
type PrepareExactInputFromRouteParams,
|
|
45
|
+
type PrepareExactOutputFromRouteParams,
|
|
46
|
+
type PreparedExactInputFromRoute,
|
|
47
|
+
type PreparedExactOutputFromRoute,
|
|
48
|
+
} from "./fromRoute.js";
|
|
49
|
+
|
|
50
|
+
export {
|
|
51
|
+
encodeMulticallCalldata,
|
|
52
|
+
encodeRefundETHCalldata,
|
|
53
|
+
encodeUnwrapWETH9Calldata,
|
|
54
|
+
} from "../peripheryPayments/index.js";
|
|
55
|
+
|
|
56
|
+
export {
|
|
57
|
+
prepareExactInputSingleAndUnwrapCalldata,
|
|
58
|
+
prepareExactInputAndUnwrapCalldata,
|
|
59
|
+
prepareExactInputSingleSupportingNativePaymentCalldata,
|
|
60
|
+
prepareExactInputSupportingNativePaymentCalldata,
|
|
61
|
+
prepareExactOutputSupportingNativePaymentCalldata,
|
|
62
|
+
prepareExactOutputSingleSupportingNativePaymentCalldata,
|
|
63
|
+
prepareExactOutputSingleAndUnwrapCalldata,
|
|
64
|
+
prepareExactOutputAndUnwrapCalldata,
|
|
65
|
+
} from "./nativeSwap.js";
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { Address, Hex } from "viem";
|
|
2
|
+
import type { ExactInputParams, ExactOutputParams } from "./swapPath.js";
|
|
3
|
+
import type { ExactInputSingleParams, ExactOutputSingleParams } from "./swap.js";
|
|
4
|
+
import { encodeExactInputSingleCalldata, encodeExactOutputSingleCalldata } from "./swap.js";
|
|
5
|
+
import { encodeExactInputCalldata, encodeExactOutputCalldata } from "./swapPath.js";
|
|
6
|
+
import {
|
|
7
|
+
encodeMulticallCalldata,
|
|
8
|
+
encodeRefundETHCalldata,
|
|
9
|
+
encodeUnwrapWETH9Calldata,
|
|
10
|
+
} from "../peripheryPayments/index.js";
|
|
11
|
+
|
|
12
|
+
function withRouterRecipient<T extends { recipient: Address }>(
|
|
13
|
+
params: T,
|
|
14
|
+
simpleRouter: Address,
|
|
15
|
+
): T {
|
|
16
|
+
return { ...params, recipient: simpleRouter };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Single-hop exact input when `tokenIn` is wrappedNative - `tokenIn` MUST be wrappedNative.
|
|
20
|
+
* Set `msg.value` to a value that you want to pay with native ETH on the transaction.
|
|
21
|
+
* The remaining amount will be paid with wrappedNative (if it is approved, otherwise it will revert).
|
|
22
|
+
* Any unused native ETH will be refunded to the caller.
|
|
23
|
+
*/
|
|
24
|
+
export function prepareExactInputSingleSupportingNativePaymentCalldata(
|
|
25
|
+
params: ExactInputSingleParams,
|
|
26
|
+
): Hex {
|
|
27
|
+
return encodeMulticallCalldata([
|
|
28
|
+
encodeExactInputSingleCalldata(params),
|
|
29
|
+
// Refund is needed because it is possible (vary rare) that the router will not consume whole amountIn while still output the required amountOutMinimum.
|
|
30
|
+
encodeRefundETHCalldata(),
|
|
31
|
+
]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Multihop exact input when the first token is wrappedNative - `tokenIn` MUST be wrappedNative.
|
|
35
|
+
* Set `msg.value` to a value that you want to pay with native ETH on the transaction.
|
|
36
|
+
* The remaining amount will be paid with wrappedNative (if it is approved, otherwise it will revert).
|
|
37
|
+
* Any unused native ETH will be refunded to the caller.
|
|
38
|
+
*/
|
|
39
|
+
export function prepareExactInputSupportingNativePaymentCalldata(params: ExactInputParams): Hex {
|
|
40
|
+
return encodeMulticallCalldata([
|
|
41
|
+
encodeExactInputCalldata(params),
|
|
42
|
+
// Refund is needed because it is possible (vary rare) that the router will not consume whole amountIn while still output the required amountOutMinimum.
|
|
43
|
+
encodeRefundETHCalldata(),
|
|
44
|
+
]);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Single-hop exact output when `tokenIn` is wrappedNative - `tokenIn` MUST be wrappedNative.
|
|
48
|
+
* Set `msg.value` to a value that you want to pay with native ETH on the transaction.
|
|
49
|
+
* The remaining amount will be paid with wrappedNative (if it is approved, otherwise it will revert)
|
|
50
|
+
* Any unused native ETH will be refunded to the caller.
|
|
51
|
+
* */
|
|
52
|
+
export function prepareExactOutputSingleSupportingNativePaymentCalldata(
|
|
53
|
+
params: ExactOutputSingleParams,
|
|
54
|
+
): Hex {
|
|
55
|
+
return encodeMulticallCalldata([
|
|
56
|
+
encodeExactOutputSingleCalldata(params),
|
|
57
|
+
encodeRefundETHCalldata(),
|
|
58
|
+
]);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Multihop exact output when the first token is wrappedNative - `tokenIn` MUST be wrappedNative.
|
|
62
|
+
* Set `msg.value` to a value that you want to pay with native ETH on the transaction.
|
|
63
|
+
* The remaining amount will be paid with wrappedNative (if it is approved, otherwise it will revert)
|
|
64
|
+
* Any unused native ETH will be refunded to the caller.
|
|
65
|
+
* */
|
|
66
|
+
export function prepareExactOutputSupportingNativePaymentCalldata(params: ExactOutputParams): Hex {
|
|
67
|
+
return encodeMulticallCalldata([encodeExactOutputCalldata(params), encodeRefundETHCalldata()]);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Single-hop exact input with ETH output — `tokenOut` MUST be wrappedNative.
|
|
72
|
+
* Unwraps the whole amount of wrappedNative to native ETH and sends it to the recipient.
|
|
73
|
+
*/
|
|
74
|
+
export function prepareExactInputSingleAndUnwrapCalldata(
|
|
75
|
+
params: ExactInputSingleParams,
|
|
76
|
+
simpleRouter: Address,
|
|
77
|
+
): Hex {
|
|
78
|
+
return encodeMulticallCalldata([
|
|
79
|
+
encodeExactInputSingleCalldata(withRouterRecipient(params, simpleRouter)),
|
|
80
|
+
encodeUnwrapWETH9Calldata(params.amountOutMinimum, params.recipient),
|
|
81
|
+
]);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Multihop exact input with ETH output - `tokenOut` MUST be wrappedNative.
|
|
85
|
+
* Unwraps the whole amount of wrappedNative to native ETH and sends it to the recipient.
|
|
86
|
+
* */
|
|
87
|
+
export function prepareExactInputAndUnwrapCalldata(
|
|
88
|
+
params: ExactInputParams,
|
|
89
|
+
simpleRouter: Address,
|
|
90
|
+
): Hex {
|
|
91
|
+
return encodeMulticallCalldata([
|
|
92
|
+
encodeExactInputCalldata(withRouterRecipient(params, simpleRouter)),
|
|
93
|
+
encodeUnwrapWETH9Calldata(params.amountOutMinimum, params.recipient),
|
|
94
|
+
]);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Single-hop exact output with ETH output — `tokenOut` MUST be wrappedNative.
|
|
99
|
+
* Unwraps the whole amount of wrappedNative to native ETH and sends it to the recipient.
|
|
100
|
+
*/
|
|
101
|
+
export function prepareExactOutputSingleAndUnwrapCalldata(
|
|
102
|
+
params: ExactOutputSingleParams,
|
|
103
|
+
simpleRouter: Address,
|
|
104
|
+
): Hex {
|
|
105
|
+
return encodeMulticallCalldata([
|
|
106
|
+
encodeExactOutputSingleCalldata(withRouterRecipient(params, simpleRouter)),
|
|
107
|
+
encodeUnwrapWETH9Calldata(params.amountOut, params.recipient),
|
|
108
|
+
]);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Multihop exact output with ETH output — `tokenOut` MUST be wrappedNative.
|
|
113
|
+
* Unwraps the whole amount of wrappedNative to native ETH and sends it to the recipient.
|
|
114
|
+
*/
|
|
115
|
+
export function prepareExactOutputAndUnwrapCalldata(
|
|
116
|
+
params: ExactOutputParams,
|
|
117
|
+
simpleRouter: Address,
|
|
118
|
+
): Hex {
|
|
119
|
+
return encodeMulticallCalldata([
|
|
120
|
+
encodeExactOutputCalldata(withRouterRecipient(params, simpleRouter)),
|
|
121
|
+
encodeUnwrapWETH9Calldata(params.amountOut, params.recipient),
|
|
122
|
+
]);
|
|
123
|
+
}
|
package/src/router/swap.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* SimpleRouter single-hop swap helpers.
|
|
3
|
+
*
|
|
4
|
+
* Typical ERC-20 flow:
|
|
5
|
+
* 1. `buildExactInputSingleParams` / `buildExactOutputSingleParams` — quote + slippage
|
|
6
|
+
* 2. `encodeExactInputSingleCalldata` — calldata for `walletClient.writeContract` or `sendTransaction`
|
|
7
|
+
* 3. Approve `tokenIn` on the SimpleRouter before sending (not needed for native ETH; see `nativeSwap.ts`)
|
|
3
8
|
*/
|
|
4
9
|
|
|
5
10
|
import type { Address, Hex } from "viem";
|
|
@@ -13,25 +18,44 @@ import type { PublicClient } from "viem";
|
|
|
13
18
|
|
|
14
19
|
const BPS_BASE = 10_000n;
|
|
15
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Arguments for `MetricOmmSimpleRouter.exactInputSingle`.
|
|
23
|
+
* Pass to `encodeExactInputSingleCalldata` or native prepare helpers when `tokenIn` is WETH.
|
|
24
|
+
*/
|
|
16
25
|
export type ExactInputSingleParams = {
|
|
26
|
+
/** Pool to swap through. */
|
|
17
27
|
pool: Address;
|
|
28
|
+
/** Input token (`addresses.wrappedNative` when swapping from native ETH). */
|
|
18
29
|
tokenIn: Address;
|
|
30
|
+
/** Output token (`addresses.wrappedNative` when unwrapping to ETH in a follow-up call). */
|
|
19
31
|
tokenOut: Address;
|
|
32
|
+
/** `true` when swapping token0 → token1 for this pool. */
|
|
20
33
|
zeroForOne: boolean;
|
|
34
|
+
/** Exact input amount in `tokenIn` native decimals. */
|
|
21
35
|
amountIn: bigint;
|
|
36
|
+
/** Minimum acceptable output; reverts if less is received. */
|
|
22
37
|
amountOutMinimum: bigint;
|
|
38
|
+
/** Address that receives output tokens. */
|
|
23
39
|
recipient: Address;
|
|
40
|
+
/** Unix timestamp after which the swap reverts. */
|
|
24
41
|
deadline: bigint;
|
|
42
|
+
/** Q64.64 price bound; use `0` / `MAX_UINT128` defaults from builders when unsure. */
|
|
25
43
|
priceLimitX64: bigint;
|
|
44
|
+
/** Per-pool extension calldata; omit for pools without extensions. */
|
|
26
45
|
extensionData?: Hex;
|
|
27
46
|
};
|
|
28
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Arguments for `MetricOmmSimpleRouter.exactOutputSingle`.
|
|
50
|
+
*/
|
|
29
51
|
export type ExactOutputSingleParams = {
|
|
30
52
|
pool: Address;
|
|
31
53
|
tokenIn: Address;
|
|
32
54
|
tokenOut: Address;
|
|
33
55
|
zeroForOne: boolean;
|
|
56
|
+
/** Exact output amount in `tokenOut` native decimals. */
|
|
34
57
|
amountOut: bigint;
|
|
58
|
+
/** Maximum input willing to spend (slippage cap). */
|
|
35
59
|
amountInMaximum: bigint;
|
|
36
60
|
recipient: Address;
|
|
37
61
|
deadline: bigint;
|
|
@@ -39,20 +63,31 @@ export type ExactOutputSingleParams = {
|
|
|
39
63
|
extensionData?: Hex;
|
|
40
64
|
};
|
|
41
65
|
|
|
66
|
+
/**
|
|
67
|
+
* Inputs for {@link buildExactInputSingleParams}.
|
|
68
|
+
* Quotes live on-chain via the SwapQuoter and applies `slippagePercent` to derive `amountOutMinimum`.
|
|
69
|
+
*/
|
|
42
70
|
export interface BuildExactInputSingleParams {
|
|
43
71
|
publicClient: PublicClient;
|
|
44
72
|
pool: Address;
|
|
45
73
|
tokenIn: Address;
|
|
46
74
|
tokenOut: Address;
|
|
47
75
|
recipient: Address;
|
|
76
|
+
/** Input amount in `tokenIn` decimals (wei for ETH / WETH). */
|
|
48
77
|
amountIn: bigint;
|
|
78
|
+
/** Human-readable slippage tolerance, e.g. `0.5` = 0.5%. */
|
|
49
79
|
slippagePercent: number;
|
|
80
|
+
/** Optional price bound; defaults from quoter direction when omitted. */
|
|
50
81
|
priceLimitX64?: bigint;
|
|
51
82
|
deadline: bigint;
|
|
52
83
|
extensionData?: Hex;
|
|
84
|
+
/** Override when `addresses.quoter` is unset. */
|
|
53
85
|
quoterAddress?: Address;
|
|
86
|
+
/** `addresses.wrappedNative` — required when `tokenIn` or `tokenOut` is native (zero address). */
|
|
87
|
+
wrappedNative?: Address;
|
|
54
88
|
}
|
|
55
89
|
|
|
90
|
+
/** Inputs for {@link buildExactOutputSingleParams}. */
|
|
56
91
|
export interface BuildExactOutputSingleParams {
|
|
57
92
|
publicClient: PublicClient;
|
|
58
93
|
pool: Address;
|
|
@@ -65,8 +100,13 @@ export interface BuildExactOutputSingleParams {
|
|
|
65
100
|
deadline: bigint;
|
|
66
101
|
extensionData?: Hex;
|
|
67
102
|
quoterAddress?: Address;
|
|
103
|
+
wrappedNative?: Address;
|
|
68
104
|
}
|
|
69
105
|
|
|
106
|
+
/**
|
|
107
|
+
* Convert a human slippage percent to basis points (1 bp = 0.01%).
|
|
108
|
+
* @example toSlippageBps(0.5) // 50n
|
|
109
|
+
*/
|
|
70
110
|
export function toSlippageBps(slippagePercent: number): bigint {
|
|
71
111
|
if (!Number.isFinite(slippagePercent) || slippagePercent < 0 || slippagePercent > 100) {
|
|
72
112
|
throw new Error("slippagePercent must be a finite number between 0 and 100");
|
|
@@ -74,6 +114,7 @@ export function toSlippageBps(slippagePercent: number): bigint {
|
|
|
74
114
|
return BigInt(Math.round(slippagePercent * 100));
|
|
75
115
|
}
|
|
76
116
|
|
|
117
|
+
/** Ceiling division for bigint values. Used when rounding slippage up on exact-output swaps. */
|
|
77
118
|
export function ceilDiv(numerator: bigint, denominator: bigint): bigint {
|
|
78
119
|
if (denominator <= 0n) {
|
|
79
120
|
throw new Error("denominator must be > 0");
|
|
@@ -115,6 +156,35 @@ function toExactOutputSingleStruct(params: ExactOutputSingleParams) {
|
|
|
115
156
|
};
|
|
116
157
|
}
|
|
117
158
|
|
|
159
|
+
/**
|
|
160
|
+
* Build single-hop exact-input swap params with live quote and slippage.
|
|
161
|
+
*
|
|
162
|
+
* Resolves `zeroForOne`, quotes `amountOut`, and sets `amountOutMinimum`.
|
|
163
|
+
* For native ETH input, use `prepareExactInputSingleSupportingNativePaymentCalldata` and set `msg.value` on the transaction.
|
|
164
|
+
* (params will contain WETH as `tokenIn`).
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* const params = await buildExactInputSingleParams({
|
|
169
|
+
* publicClient,
|
|
170
|
+
* pool,
|
|
171
|
+
* tokenIn: usdc,
|
|
172
|
+
* tokenOut: weth,
|
|
173
|
+
* recipient: account,
|
|
174
|
+
* amountIn: parseUnits("100", 6),
|
|
175
|
+
* slippagePercent: 0.5,
|
|
176
|
+
* deadline: BigInt(Math.floor(Date.now() / 1000) + 1200),
|
|
177
|
+
* quoterAddress: addresses.quoter,
|
|
178
|
+
* });
|
|
179
|
+
* await walletClient.writeContract({
|
|
180
|
+
* address: addresses.simpleRouter,
|
|
181
|
+
* abi: MetricOmmSimpleRouterAbi,
|
|
182
|
+
* functionName: "exactInputSingle",
|
|
183
|
+
* args: [params],
|
|
184
|
+
* account,
|
|
185
|
+
* });
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
118
188
|
export async function buildExactInputSingleParams(
|
|
119
189
|
params: BuildExactInputSingleParams,
|
|
120
190
|
): Promise<ExactInputSingleParams> {
|
|
@@ -157,6 +227,10 @@ export async function buildExactInputSingleParams(
|
|
|
157
227
|
};
|
|
158
228
|
}
|
|
159
229
|
|
|
230
|
+
/**
|
|
231
|
+
* Build single-hop exact-output swap params with live quote and slippage.
|
|
232
|
+
* Sets `amountInMaximum` from quoted input plus `slippagePercent`.
|
|
233
|
+
*/
|
|
160
234
|
export async function buildExactOutputSingleParams(
|
|
161
235
|
params: BuildExactOutputSingleParams,
|
|
162
236
|
): Promise<ExactOutputSingleParams> {
|
|
@@ -199,6 +273,10 @@ export async function buildExactOutputSingleParams(
|
|
|
199
273
|
};
|
|
200
274
|
}
|
|
201
275
|
|
|
276
|
+
/**
|
|
277
|
+
* Encode `exactInputSingle` calldata for `addresses.simpleRouter`.
|
|
278
|
+
* Approve `tokenIn` on the router before sending (unless using native ETH prepare helpers).
|
|
279
|
+
*/
|
|
202
280
|
export function encodeExactInputSingleCalldata(params: ExactInputSingleParams): Hex {
|
|
203
281
|
return encodeFunctionData({
|
|
204
282
|
abi: SIMPLE_ROUTER_ABI,
|
|
@@ -207,8 +285,10 @@ export function encodeExactInputSingleCalldata(params: ExactInputSingleParams):
|
|
|
207
285
|
});
|
|
208
286
|
}
|
|
209
287
|
|
|
288
|
+
/** Alias for {@link encodeExactInputSingleCalldata}. */
|
|
210
289
|
export const prepareExactInputSingleCalldata = encodeExactInputSingleCalldata;
|
|
211
290
|
|
|
291
|
+
/** Encode `exactOutputSingle` calldata for `addresses.simpleRouter`. */
|
|
212
292
|
export function encodeExactOutputSingleCalldata(params: ExactOutputSingleParams): Hex {
|
|
213
293
|
return encodeFunctionData({
|
|
214
294
|
abi: SIMPLE_ROUTER_ABI,
|
|
@@ -217,4 +297,5 @@ export function encodeExactOutputSingleCalldata(params: ExactOutputSingleParams)
|
|
|
217
297
|
});
|
|
218
298
|
}
|
|
219
299
|
|
|
300
|
+
/** Alias for {@link encodeExactOutputSingleCalldata}. */
|
|
220
301
|
export const prepareExactOutputSingleCalldata = encodeExactOutputSingleCalldata;
|
package/src/router/swapPath.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* SimpleRouter multihop swap helpers.
|
|
3
|
+
*
|
|
4
|
+
* Multihop swaps walk `pools` in order; intermediate tokens remain on the router between hops.
|
|
5
|
+
* For ERC-20 paths, approve the router for `tokenIn` once before sending the transaction.
|
|
3
6
|
*/
|
|
7
|
+
|
|
4
8
|
import type { Address, Hex, PublicClient } from "viem";
|
|
5
9
|
import { encodeFunctionData } from "viem";
|
|
6
10
|
import { quoteLiveExactIn, quoteLiveExactOut } from "../quoter/quotePath.js";
|
|
@@ -10,10 +14,18 @@ import { ceilDiv, toSlippageBps } from "./swap.js";
|
|
|
10
14
|
|
|
11
15
|
const BPS_BASE = 10_000n;
|
|
12
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Arguments for `MetricOmmSimpleRouter.exactInput` (multihop exact input).
|
|
19
|
+
* `tokens` and `zeroForOneBitMap` are usually produced by {@link buildExactInputParams}.
|
|
20
|
+
*/
|
|
13
21
|
export type ExactInputParams = {
|
|
22
|
+
/** Path tokens, length `pools.length + 1`. */
|
|
14
23
|
tokens: Address[];
|
|
24
|
+
/** One pool per hop, in swap order. */
|
|
15
25
|
pools: Address[];
|
|
26
|
+
/** Extension calldata per hop; use `0x` when a pool has no extension. */
|
|
16
27
|
extensionDatas: Hex[];
|
|
28
|
+
/** Bit `i` set when hop `i` is token0 → token1. */
|
|
17
29
|
zeroForOneBitMap: bigint;
|
|
18
30
|
amountIn: bigint;
|
|
19
31
|
amountOutMinimum: bigint;
|
|
@@ -21,6 +33,7 @@ export type ExactInputParams = {
|
|
|
21
33
|
deadline: bigint;
|
|
22
34
|
};
|
|
23
35
|
|
|
36
|
+
/** Arguments for `MetricOmmSimpleRouter.exactOutput` (multihop exact output). */
|
|
24
37
|
export type ExactOutputParams = {
|
|
25
38
|
tokens: Address[];
|
|
26
39
|
pools: Address[];
|
|
@@ -32,10 +45,12 @@ export type ExactOutputParams = {
|
|
|
32
45
|
deadline: bigint;
|
|
33
46
|
};
|
|
34
47
|
|
|
48
|
+
/** Inputs for {@link buildExactInputParams}. */
|
|
35
49
|
export interface BuildExactInputParams {
|
|
36
50
|
publicClient: PublicClient;
|
|
37
51
|
tokenIn: Address;
|
|
38
52
|
tokenOut: Address;
|
|
53
|
+
/** Ordered pool addresses connecting `tokenIn` → `tokenOut`. */
|
|
39
54
|
pools: readonly Address[];
|
|
40
55
|
extensionDatas?: readonly Hex[];
|
|
41
56
|
recipient: Address;
|
|
@@ -43,8 +58,10 @@ export interface BuildExactInputParams {
|
|
|
43
58
|
slippagePercent: number;
|
|
44
59
|
deadline: bigint;
|
|
45
60
|
quoterAddress?: Address;
|
|
61
|
+
wrappedNative?: Address;
|
|
46
62
|
}
|
|
47
63
|
|
|
64
|
+
/** Inputs for {@link buildExactOutputParams}. */
|
|
48
65
|
export interface BuildExactOutputParams {
|
|
49
66
|
publicClient: PublicClient;
|
|
50
67
|
tokenIn: Address;
|
|
@@ -56,6 +73,7 @@ export interface BuildExactOutputParams {
|
|
|
56
73
|
slippagePercent: number;
|
|
57
74
|
deadline: bigint;
|
|
58
75
|
quoterAddress?: Address;
|
|
76
|
+
wrappedNative?: Address;
|
|
59
77
|
}
|
|
60
78
|
|
|
61
79
|
function toExactInputStruct(params: ExactInputParams) {
|
|
@@ -84,6 +102,29 @@ function toExactOutputStruct(params: ExactOutputParams) {
|
|
|
84
102
|
};
|
|
85
103
|
}
|
|
86
104
|
|
|
105
|
+
/**
|
|
106
|
+
* Build multihop exact-input params: resolves path, quotes output, applies slippage.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* const pathParams = await buildExactInputParams({
|
|
111
|
+
* publicClient,
|
|
112
|
+
* tokenIn: tokenA,
|
|
113
|
+
* tokenOut: tokenC,
|
|
114
|
+
* pools: [poolAB, poolBC],
|
|
115
|
+
* recipient: account,
|
|
116
|
+
* amountIn: parseUnits("1", 18),
|
|
117
|
+
* slippagePercent: 0.5,
|
|
118
|
+
* deadline,
|
|
119
|
+
* quoterAddress: addresses.quoter,
|
|
120
|
+
* });
|
|
121
|
+
* await walletClient.sendTransaction({
|
|
122
|
+
* to: addresses.simpleRouter,
|
|
123
|
+
* data: encodeExactInputCalldata(pathParams),
|
|
124
|
+
* account,
|
|
125
|
+
* });
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
87
128
|
export async function buildExactInputParams(
|
|
88
129
|
params: BuildExactInputParams,
|
|
89
130
|
): Promise<ExactInputParams> {
|
|
@@ -121,6 +162,7 @@ export async function buildExactInputParams(
|
|
|
121
162
|
};
|
|
122
163
|
}
|
|
123
164
|
|
|
165
|
+
/** Build multihop exact-output params with live quote and slippage on `amountInMaximum`. */
|
|
124
166
|
export async function buildExactOutputParams(
|
|
125
167
|
params: BuildExactOutputParams,
|
|
126
168
|
): Promise<ExactOutputParams> {
|
|
@@ -158,6 +200,7 @@ export async function buildExactOutputParams(
|
|
|
158
200
|
};
|
|
159
201
|
}
|
|
160
202
|
|
|
203
|
+
/** Encode `exactInput` multihop calldata. */
|
|
161
204
|
export function encodeExactInputCalldata(params: ExactInputParams): Hex {
|
|
162
205
|
return encodeFunctionData({
|
|
163
206
|
abi: SIMPLE_ROUTER_ABI,
|
|
@@ -166,8 +209,10 @@ export function encodeExactInputCalldata(params: ExactInputParams): Hex {
|
|
|
166
209
|
});
|
|
167
210
|
}
|
|
168
211
|
|
|
212
|
+
/** Alias for {@link encodeExactInputCalldata}. */
|
|
169
213
|
export const prepareExactInputCalldata = encodeExactInputCalldata;
|
|
170
214
|
|
|
215
|
+
/** Encode `exactOutput` multihop calldata. */
|
|
171
216
|
export function encodeExactOutputCalldata(params: ExactOutputParams): Hex {
|
|
172
217
|
return encodeFunctionData({
|
|
173
218
|
abi: SIMPLE_ROUTER_ABI,
|
|
@@ -176,6 +221,7 @@ export function encodeExactOutputCalldata(params: ExactOutputParams): Hex {
|
|
|
176
221
|
});
|
|
177
222
|
}
|
|
178
223
|
|
|
224
|
+
/** Alias for {@link encodeExactOutputCalldata}. */
|
|
179
225
|
export const prepareExactOutputCalldata = encodeExactOutputCalldata;
|
|
180
226
|
|
|
181
227
|
export { encodeZeroForOneBitMap } from "../swap/path.js";
|