@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
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Map route API POST /public/v1/evm/:chain_id/route responses to SimpleRouter swap params.
|
|
3
|
+
*/
|
|
4
|
+
import { getAddress } from "viem";
|
|
5
|
+
import { resolveSwapPath } from "../swap/path.js";
|
|
6
|
+
import { ceilDiv, toSlippageBps } from "./swap.js";
|
|
7
|
+
const BPS_BASE = 10000n;
|
|
8
|
+
function parseAddress(value, field) {
|
|
9
|
+
try {
|
|
10
|
+
return getAddress(value);
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
throw new Error(`invalid ${field}: ${value}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function parseAmount(value, field) {
|
|
17
|
+
try {
|
|
18
|
+
const amount = BigInt(value);
|
|
19
|
+
if (amount < 0n) {
|
|
20
|
+
throw new Error(`${field} must be non-negative`);
|
|
21
|
+
}
|
|
22
|
+
return amount;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
if (error instanceof Error && error.message.includes("must be non-negative")) {
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
throw new Error(`invalid ${field}: ${value}`, { cause: error });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function selectRouteOption(route) {
|
|
32
|
+
if (route.routes.length === 0) {
|
|
33
|
+
throw new Error("route.routes must not be empty");
|
|
34
|
+
}
|
|
35
|
+
if (route.routes.length > 1) {
|
|
36
|
+
throw new Error("route was split across multiple routes; re-request with maxRoutes=1");
|
|
37
|
+
}
|
|
38
|
+
return route.routes[0];
|
|
39
|
+
}
|
|
40
|
+
function assertTradeType(route, option, tradeType) {
|
|
41
|
+
if (route.tradeType !== tradeType) {
|
|
42
|
+
throw new Error(`route tradeType must be ${tradeType}; use ${tradeType === "exact_in" ? "prepareExactOutputFromRoute" : "prepareExactInputFromRoute"} for ${tradeType === "exact_in" ? "exact_out" : "exact_in"}`);
|
|
43
|
+
}
|
|
44
|
+
if (option.tradeType !== tradeType) {
|
|
45
|
+
throw new Error(`routes[0].tradeType must be ${tradeType}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function validateRouteLegs(legs, tokenIn, tokenOut) {
|
|
49
|
+
if (legs.length === 0) {
|
|
50
|
+
throw new Error("routes[0].legs must not be empty");
|
|
51
|
+
}
|
|
52
|
+
const pools = [];
|
|
53
|
+
for (let i = 0; i < legs.length; i++) {
|
|
54
|
+
const leg = legs[i];
|
|
55
|
+
const legTokenIn = parseAddress(leg.tokenIn, `legs[${i}].tokenIn`);
|
|
56
|
+
const legTokenOut = parseAddress(leg.tokenOut, `legs[${i}].tokenOut`);
|
|
57
|
+
pools.push(parseAddress(leg.poolAddress, `legs[${i}].poolAddress`));
|
|
58
|
+
if (i === 0 && legTokenIn !== tokenIn) {
|
|
59
|
+
throw new Error(`legs[0].tokenIn (${legTokenIn}) does not match route.tokenIn (${tokenIn})`);
|
|
60
|
+
}
|
|
61
|
+
if (i === legs.length - 1 && legTokenOut !== tokenOut) {
|
|
62
|
+
throw new Error(`legs[${i}].tokenOut (${legTokenOut}) does not match route.tokenOut (${tokenOut})`);
|
|
63
|
+
}
|
|
64
|
+
if (i > 0) {
|
|
65
|
+
const prevLegTokenOut = parseAddress(legs[i - 1].tokenOut, `legs[${i - 1}].tokenOut`);
|
|
66
|
+
if (legTokenIn !== prevLegTokenOut) {
|
|
67
|
+
throw new Error(`legs[${i}].tokenIn (${legTokenIn}) does not connect to legs[${i - 1}].tokenOut (${prevLegTokenOut})`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return pools;
|
|
72
|
+
}
|
|
73
|
+
function amountOutMinimumFromRoute(amountOut, slippagePercent) {
|
|
74
|
+
const slippageBps = toSlippageBps(slippagePercent);
|
|
75
|
+
return (amountOut * (BPS_BASE - slippageBps)) / BPS_BASE;
|
|
76
|
+
}
|
|
77
|
+
function amountInMaximumFromRoute(amountIn, slippagePercent) {
|
|
78
|
+
const slippageBps = toSlippageBps(slippagePercent);
|
|
79
|
+
return ceilDiv(amountIn * (BPS_BASE + slippageBps), BPS_BASE);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Convert a route API exact-in response into SimpleRouter exact-input swap params.
|
|
83
|
+
*
|
|
84
|
+
* Resolves swap direction on-chain, applies slippage to the quoted `amountOut`, and
|
|
85
|
+
* returns params ready for `encodeExactInputSingleCalldata` or `encodeExactInputCalldata`.
|
|
86
|
+
*/
|
|
87
|
+
export async function routeToExactInputParams(params) {
|
|
88
|
+
const { route, publicClient, recipient, deadline, slippagePercent, extensionDatas } = params;
|
|
89
|
+
const tokenIn = parseAddress(route.tokenIn, "tokenIn");
|
|
90
|
+
const tokenOut = parseAddress(route.tokenOut, "tokenOut");
|
|
91
|
+
const option = selectRouteOption(route);
|
|
92
|
+
assertTradeType(route, option, "exact_in");
|
|
93
|
+
const requestedAmountIn = parseAmount(route.amountIn, "amountIn");
|
|
94
|
+
const amountIn = parseAmount(option.amountIn, "routes[0].amountIn");
|
|
95
|
+
if (amountIn !== requestedAmountIn) {
|
|
96
|
+
throw new Error("routes[0].amountIn does not match request amountIn; partial fill is not supported");
|
|
97
|
+
}
|
|
98
|
+
const quotedAmountOut = parseAmount(option.amountOut, "routes[0].amountOut");
|
|
99
|
+
const amountOutMinimum = amountOutMinimumFromRoute(quotedAmountOut, slippagePercent);
|
|
100
|
+
const pools = validateRouteLegs(option.legs, tokenIn, tokenOut);
|
|
101
|
+
const { tokens, zeroForOnes, extensionDatas: resolvedExtensionDatas, zeroForOneBitMap, } = await resolveSwapPath(publicClient, tokenIn, tokenOut, pools, extensionDatas);
|
|
102
|
+
if (pools.length === 1) {
|
|
103
|
+
const leg = option.legs[0];
|
|
104
|
+
return {
|
|
105
|
+
kind: "single",
|
|
106
|
+
params: {
|
|
107
|
+
pool: pools[0],
|
|
108
|
+
tokenIn,
|
|
109
|
+
tokenOut,
|
|
110
|
+
zeroForOne: zeroForOnes[0],
|
|
111
|
+
amountIn,
|
|
112
|
+
amountOutMinimum,
|
|
113
|
+
recipient,
|
|
114
|
+
deadline,
|
|
115
|
+
priceLimitX64: parseAmount(leg.priceLimitX64, "legs[0].priceLimitX64"),
|
|
116
|
+
extensionData: resolvedExtensionDatas[0],
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
kind: "multihop",
|
|
122
|
+
params: {
|
|
123
|
+
tokens,
|
|
124
|
+
pools,
|
|
125
|
+
extensionDatas: resolvedExtensionDatas,
|
|
126
|
+
zeroForOneBitMap,
|
|
127
|
+
amountIn,
|
|
128
|
+
amountOutMinimum,
|
|
129
|
+
recipient,
|
|
130
|
+
deadline,
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Convert a route API exact-out response into SimpleRouter exact-output swap params.
|
|
136
|
+
*
|
|
137
|
+
* Resolves swap direction on-chain, applies slippage to the quoted `amountIn`, and
|
|
138
|
+
* returns params ready for `encodeExactOutputSingleCalldata` or `encodeExactOutputCalldata`.
|
|
139
|
+
*/
|
|
140
|
+
export async function routeToExactOutputParams(params) {
|
|
141
|
+
const { route, publicClient, recipient, deadline, slippagePercent, extensionDatas } = params;
|
|
142
|
+
const tokenIn = parseAddress(route.tokenIn, "tokenIn");
|
|
143
|
+
const tokenOut = parseAddress(route.tokenOut, "tokenOut");
|
|
144
|
+
const option = selectRouteOption(route);
|
|
145
|
+
assertTradeType(route, option, "exact_out");
|
|
146
|
+
const requestedAmountOut = parseAmount(route.amountOut, "amountOut");
|
|
147
|
+
const amountOut = parseAmount(option.amountOut, "routes[0].amountOut");
|
|
148
|
+
if (amountOut !== requestedAmountOut) {
|
|
149
|
+
throw new Error("routes[0].amountOut does not match request amountOut; partial fill is not supported");
|
|
150
|
+
}
|
|
151
|
+
const quotedAmountIn = parseAmount(option.amountIn, "routes[0].amountIn");
|
|
152
|
+
const amountInMaximum = amountInMaximumFromRoute(quotedAmountIn, slippagePercent);
|
|
153
|
+
const pools = validateRouteLegs(option.legs, tokenIn, tokenOut);
|
|
154
|
+
const { tokens, zeroForOnes, extensionDatas: resolvedExtensionDatas, zeroForOneBitMap, } = await resolveSwapPath(publicClient, tokenIn, tokenOut, pools, extensionDatas);
|
|
155
|
+
if (pools.length === 1) {
|
|
156
|
+
const leg = option.legs[0];
|
|
157
|
+
return {
|
|
158
|
+
kind: "single",
|
|
159
|
+
params: {
|
|
160
|
+
pool: pools[0],
|
|
161
|
+
tokenIn,
|
|
162
|
+
tokenOut,
|
|
163
|
+
zeroForOne: zeroForOnes[0],
|
|
164
|
+
amountOut,
|
|
165
|
+
amountInMaximum,
|
|
166
|
+
recipient,
|
|
167
|
+
deadline,
|
|
168
|
+
priceLimitX64: parseAmount(leg.priceLimitX64, "legs[0].priceLimitX64"),
|
|
169
|
+
extensionData: resolvedExtensionDatas[0],
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
kind: "multihop",
|
|
175
|
+
params: {
|
|
176
|
+
tokens,
|
|
177
|
+
pools,
|
|
178
|
+
extensionDatas: resolvedExtensionDatas,
|
|
179
|
+
zeroForOneBitMap,
|
|
180
|
+
amountOut,
|
|
181
|
+
amountInMaximum,
|
|
182
|
+
recipient,
|
|
183
|
+
deadline,
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
//# sourceMappingURL=fromRoute.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fromRoute.js","sourceRoot":"","sources":["../../src/router/fromRoute.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAInD,MAAM,QAAQ,GAAG,MAAO,CAAC;AA2EzB,SAAS,YAAY,CAAC,KAAa,EAAE,KAAa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AAQ,CAAC,CAAC;IAChE,MAAM,EACJ,MAAM,EACN,WAAW,EACX,cAAc,EAAE,sBAAsB,EACtC,gBAAgB,GACjB,GAAG,MAAM,eAAe,CAAC,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,cAAc,CAAC,CAAC;IAElF,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAE,CAAC;QAC5B,OAAO;YACL,IAAI,EAAE,QAAQ;YACd,MAAM,EAAE;gBACN,IAAI,EAAE,KAAK,CAAC,CAAC,CAAE;gBACf,OAAO;gBACP,QAAQ;gBACR,UAAU,EAAE,WAAW,CAAC,CAAC,CAAE;gBAC3B,SAAS;gBACT,eAAe;gBACf,SAAS;gBACT,QAAQ;gBACR,aAAa,EAAE,WAAW,CAAC,GAAG,CAAC,aAAa,EAAE,uBAAuB,CAAC;gBACtE,aAAa,EAAE,sBAAsB,CAAC,CAAC,CAAC;aACzC;SACF,CAAC;IACJ,CAAC;IAED,OAAO;QACL,IAAI,EAAE,UAAU;QAChB,MAAM,EAAE;YACN,MAAM;YACN,KAAK;YACL,cAAc,EAAE,sBAAsB;YACtC,gBAAgB;YAChB,SAAS;YACT,eAAe;YACf,SAAS;YACT,QAAQ;SACT;KACF,CAAC;AACJ,CAAC"}
|
package/dist/router/index.d.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
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
|
export { buildExactInputParams, buildExactOutputParams, encodeExactInputCalldata, encodeExactOutputCalldata, encodeZeroForOneBitMap, prepareExactInputCalldata, prepareExactOutputCalldata, type ExactInputParams, type ExactOutputParams, type BuildExactInputParams, type BuildExactOutputParams, } from "./swapPath.js";
|
|
5
9
|
export { buildExactInputSingleParams, buildExactOutputSingleParams, encodeExactInputSingleCalldata, encodeExactOutputSingleCalldata, prepareExactInputSingleCalldata, prepareExactOutputSingleCalldata, type ExactInputSingleParams, type ExactOutputSingleParams, type BuildExactInputSingleParams, type BuildExactOutputSingleParams, } from "./swap.js";
|
|
10
|
+
export { routeToExactInputParams as prepareExactInputFromRoute, routeToExactOutputParams as prepareExactOutputFromRoute, type TradeType, type RouteType, type RouteLegResponse, type RouteOptionResponse, type RouteResponse, type PrepareExactInputFromRouteParams, type PrepareExactOutputFromRouteParams, type PreparedExactInputFromRoute, type PreparedExactOutputFromRoute, } from "./fromRoute.js";
|
|
11
|
+
export { encodeMulticallCalldata, encodeRefundETHCalldata, encodeUnwrapWETH9Calldata, } from "../peripheryPayments/index.js";
|
|
12
|
+
export { prepareExactInputSingleAndUnwrapCalldata, prepareExactInputAndUnwrapCalldata, prepareExactInputSingleSupportingNativePaymentCalldata, prepareExactInputSupportingNativePaymentCalldata, prepareExactOutputSupportingNativePaymentCalldata, prepareExactOutputSingleSupportingNativePaymentCalldata, prepareExactOutputSingleAndUnwrapCalldata, prepareExactOutputAndUnwrapCalldata, } from "./nativeSwap.js";
|
|
6
13
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/router/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/router/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,wBAAwB,EACxB,yBAAyB,EACzB,sBAAsB,EACtB,yBAAyB,EACzB,0BAA0B,EAC1B,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACtB,KAAK,qBAAqB,EAC1B,KAAK,sBAAsB,GAC5B,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,2BAA2B,EAC3B,4BAA4B,EAC5B,8BAA8B,EAC9B,+BAA+B,EAC/B,+BAA+B,EAC/B,gCAAgC,EAChC,KAAK,sBAAsB,EAC3B,KAAK,uBAAuB,EAC5B,KAAK,2BAA2B,EAChC,KAAK,4BAA4B,GAClC,MAAM,WAAW,CAAC;AAEnB,OAAO,EACL,uBAAuB,IAAI,0BAA0B,EACrD,wBAAwB,IAAI,2BAA2B,EACvD,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,gBAAgB,EACrB,KAAK,mBAAmB,EACxB,KAAK,aAAa,EAClB,KAAK,gCAAgC,EACrC,KAAK,iCAAiC,EACtC,KAAK,2BAA2B,EAChC,KAAK,4BAA4B,GAClC,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACL,uBAAuB,EACvB,uBAAuB,EACvB,yBAAyB,GAC1B,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EACL,wCAAwC,EACxC,kCAAkC,EAClC,sDAAsD,EACtD,gDAAgD,EAChD,iDAAiD,EACjD,uDAAuD,EACvD,yCAAyC,EACzC,mCAAmC,GACpC,MAAM,iBAAiB,CAAC"}
|
package/dist/router/index.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
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
|
export { buildExactInputParams, buildExactOutputParams, encodeExactInputCalldata, encodeExactOutputCalldata, encodeZeroForOneBitMap, prepareExactInputCalldata, prepareExactOutputCalldata, } from "./swapPath.js";
|
|
5
9
|
export { buildExactInputSingleParams, buildExactOutputSingleParams, encodeExactInputSingleCalldata, encodeExactOutputSingleCalldata, prepareExactInputSingleCalldata, prepareExactOutputSingleCalldata, } from "./swap.js";
|
|
10
|
+
export { routeToExactInputParams as prepareExactInputFromRoute, routeToExactOutputParams as prepareExactOutputFromRoute, } from "./fromRoute.js";
|
|
11
|
+
export { encodeMulticallCalldata, encodeRefundETHCalldata, encodeUnwrapWETH9Calldata, } from "../peripheryPayments/index.js";
|
|
12
|
+
export { prepareExactInputSingleAndUnwrapCalldata, prepareExactInputAndUnwrapCalldata, prepareExactInputSingleSupportingNativePaymentCalldata, prepareExactInputSupportingNativePaymentCalldata, prepareExactOutputSupportingNativePaymentCalldata, prepareExactOutputSingleSupportingNativePaymentCalldata, prepareExactOutputSingleAndUnwrapCalldata, prepareExactOutputAndUnwrapCalldata, } from "./nativeSwap.js";
|
|
6
13
|
//# sourceMappingURL=index.js.map
|
package/dist/router/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/router/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/router/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,wBAAwB,EACxB,yBAAyB,EACzB,sBAAsB,EACtB,yBAAyB,EACzB,0BAA0B,GAK3B,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,2BAA2B,EAC3B,4BAA4B,EAC5B,8BAA8B,EAC9B,+BAA+B,EAC/B,+BAA+B,EAC/B,gCAAgC,GAKjC,MAAM,WAAW,CAAC;AAEnB,OAAO,EACL,uBAAuB,IAAI,0BAA0B,EACrD,wBAAwB,IAAI,2BAA2B,GAUxD,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACL,uBAAuB,EACvB,uBAAuB,EACvB,yBAAyB,GAC1B,MAAM,+BAA+B,CAAC;AAEvC,OAAO,EACL,wCAAwC,EACxC,kCAAkC,EAClC,sDAAsD,EACtD,gDAAgD,EAChD,iDAAiD,EACjD,uDAAuD,EACvD,yCAAyC,EACzC,mCAAmC,GACpC,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Address, Hex } from "viem";
|
|
2
|
+
import type { ExactInputParams, ExactOutputParams } from "./swapPath.js";
|
|
3
|
+
import type { ExactInputSingleParams, ExactOutputSingleParams } from "./swap.js";
|
|
4
|
+
/** Single-hop exact input when `tokenIn` is wrappedNative - `tokenIn` MUST be wrappedNative.
|
|
5
|
+
* Set `msg.value` to a value that you want to pay with native ETH on the transaction.
|
|
6
|
+
* The remaining amount will be paid with wrappedNative (if it is approved, otherwise it will revert).
|
|
7
|
+
* Any unused native ETH will be refunded to the caller.
|
|
8
|
+
*/
|
|
9
|
+
export declare function prepareExactInputSingleSupportingNativePaymentCalldata(params: ExactInputSingleParams): Hex;
|
|
10
|
+
/** Multihop exact input when the first token is wrappedNative - `tokenIn` MUST be wrappedNative.
|
|
11
|
+
* Set `msg.value` to a value that you want to pay with native ETH on the transaction.
|
|
12
|
+
* The remaining amount will be paid with wrappedNative (if it is approved, otherwise it will revert).
|
|
13
|
+
* Any unused native ETH will be refunded to the caller.
|
|
14
|
+
*/
|
|
15
|
+
export declare function prepareExactInputSupportingNativePaymentCalldata(params: ExactInputParams): Hex;
|
|
16
|
+
/** Single-hop exact output when `tokenIn` is wrappedNative - `tokenIn` MUST be wrappedNative.
|
|
17
|
+
* Set `msg.value` to a value that you want to pay with native ETH on the transaction.
|
|
18
|
+
* The remaining amount will be paid with wrappedNative (if it is approved, otherwise it will revert)
|
|
19
|
+
* Any unused native ETH will be refunded to the caller.
|
|
20
|
+
* */
|
|
21
|
+
export declare function prepareExactOutputSingleSupportingNativePaymentCalldata(params: ExactOutputSingleParams): Hex;
|
|
22
|
+
/** Multihop exact output when the first token is wrappedNative - `tokenIn` MUST be wrappedNative.
|
|
23
|
+
* Set `msg.value` to a value that you want to pay with native ETH on the transaction.
|
|
24
|
+
* The remaining amount will be paid with wrappedNative (if it is approved, otherwise it will revert)
|
|
25
|
+
* Any unused native ETH will be refunded to the caller.
|
|
26
|
+
* */
|
|
27
|
+
export declare function prepareExactOutputSupportingNativePaymentCalldata(params: ExactOutputParams): Hex;
|
|
28
|
+
/**
|
|
29
|
+
* Single-hop exact input with ETH output — `tokenOut` MUST be wrappedNative.
|
|
30
|
+
* Unwraps the whole amount of wrappedNative to native ETH and sends it to the recipient.
|
|
31
|
+
*/
|
|
32
|
+
export declare function prepareExactInputSingleAndUnwrapCalldata(params: ExactInputSingleParams, simpleRouter: Address): Hex;
|
|
33
|
+
/** Multihop exact input with ETH output - `tokenOut` MUST be wrappedNative.
|
|
34
|
+
* Unwraps the whole amount of wrappedNative to native ETH and sends it to the recipient.
|
|
35
|
+
* */
|
|
36
|
+
export declare function prepareExactInputAndUnwrapCalldata(params: ExactInputParams, simpleRouter: Address): Hex;
|
|
37
|
+
/**
|
|
38
|
+
* Single-hop exact output with ETH output — `tokenOut` MUST be wrappedNative.
|
|
39
|
+
* Unwraps the whole amount of wrappedNative to native ETH and sends it to the recipient.
|
|
40
|
+
*/
|
|
41
|
+
export declare function prepareExactOutputSingleAndUnwrapCalldata(params: ExactOutputSingleParams, simpleRouter: Address): Hex;
|
|
42
|
+
/**
|
|
43
|
+
* Multihop exact output with ETH output — `tokenOut` MUST be wrappedNative.
|
|
44
|
+
* Unwraps the whole amount of wrappedNative to native ETH and sends it to the recipient.
|
|
45
|
+
*/
|
|
46
|
+
export declare function prepareExactOutputAndUnwrapCalldata(params: ExactOutputParams, simpleRouter: Address): Hex;
|
|
47
|
+
//# sourceMappingURL=nativeSwap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nativeSwap.d.ts","sourceRoot":"","sources":["../../src/router/nativeSwap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AACzC,OAAO,KAAK,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AACzE,OAAO,KAAK,EAAE,sBAAsB,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAC;AAgBjF;;;;GAIG;AACH,wBAAgB,sDAAsD,CACpE,MAAM,EAAE,sBAAsB,GAC7B,GAAG,CAML;AAED;;;;GAIG;AACH,wBAAgB,gDAAgD,CAAC,MAAM,EAAE,gBAAgB,GAAG,GAAG,CAM9F;AAED;;;;MAIM;AACN,wBAAgB,uDAAuD,CACrE,MAAM,EAAE,uBAAuB,GAC9B,GAAG,CAKL;AAED;;;;MAIM;AACN,wBAAgB,iDAAiD,CAAC,MAAM,EAAE,iBAAiB,GAAG,GAAG,CAEhG;AAED;;;GAGG;AACH,wBAAgB,wCAAwC,CACtD,MAAM,EAAE,sBAAsB,EAC9B,YAAY,EAAE,OAAO,GACpB,GAAG,CAKL;AAED;;MAEM;AACN,wBAAgB,kCAAkC,CAChD,MAAM,EAAE,gBAAgB,EACxB,YAAY,EAAE,OAAO,GACpB,GAAG,CAKL;AAED;;;GAGG;AACH,wBAAgB,yCAAyC,CACvD,MAAM,EAAE,uBAAuB,EAC/B,YAAY,EAAE,OAAO,GACpB,GAAG,CAKL;AAED;;;GAGG;AACH,wBAAgB,mCAAmC,CACjD,MAAM,EAAE,iBAAiB,EACzB,YAAY,EAAE,OAAO,GACpB,GAAG,CAKL"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { encodeExactInputSingleCalldata, encodeExactOutputSingleCalldata } from "./swap.js";
|
|
2
|
+
import { encodeExactInputCalldata, encodeExactOutputCalldata } from "./swapPath.js";
|
|
3
|
+
import { encodeMulticallCalldata, encodeRefundETHCalldata, encodeUnwrapWETH9Calldata, } from "../peripheryPayments/index.js";
|
|
4
|
+
function withRouterRecipient(params, simpleRouter) {
|
|
5
|
+
return { ...params, recipient: simpleRouter };
|
|
6
|
+
}
|
|
7
|
+
/** Single-hop exact input when `tokenIn` is wrappedNative - `tokenIn` MUST be wrappedNative.
|
|
8
|
+
* Set `msg.value` to a value that you want to pay with native ETH on the transaction.
|
|
9
|
+
* The remaining amount will be paid with wrappedNative (if it is approved, otherwise it will revert).
|
|
10
|
+
* Any unused native ETH will be refunded to the caller.
|
|
11
|
+
*/
|
|
12
|
+
export function prepareExactInputSingleSupportingNativePaymentCalldata(params) {
|
|
13
|
+
return encodeMulticallCalldata([
|
|
14
|
+
encodeExactInputSingleCalldata(params),
|
|
15
|
+
// Refund is needed because it is possible (vary rare) that the router will not consume whole amountIn while still output the required amountOutMinimum.
|
|
16
|
+
encodeRefundETHCalldata(),
|
|
17
|
+
]);
|
|
18
|
+
}
|
|
19
|
+
/** Multihop exact input when the first token 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 prepareExactInputSupportingNativePaymentCalldata(params) {
|
|
25
|
+
return encodeMulticallCalldata([
|
|
26
|
+
encodeExactInputCalldata(params),
|
|
27
|
+
// Refund is needed because it is possible (vary rare) that the router will not consume whole amountIn while still output the required amountOutMinimum.
|
|
28
|
+
encodeRefundETHCalldata(),
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
/** Single-hop exact output when `tokenIn` is wrappedNative - `tokenIn` MUST be wrappedNative.
|
|
32
|
+
* Set `msg.value` to a value that you want to pay with native ETH on the transaction.
|
|
33
|
+
* The remaining amount will be paid with wrappedNative (if it is approved, otherwise it will revert)
|
|
34
|
+
* Any unused native ETH will be refunded to the caller.
|
|
35
|
+
* */
|
|
36
|
+
export function prepareExactOutputSingleSupportingNativePaymentCalldata(params) {
|
|
37
|
+
return encodeMulticallCalldata([
|
|
38
|
+
encodeExactOutputSingleCalldata(params),
|
|
39
|
+
encodeRefundETHCalldata(),
|
|
40
|
+
]);
|
|
41
|
+
}
|
|
42
|
+
/** Multihop exact output when the first token is wrappedNative - `tokenIn` MUST be wrappedNative.
|
|
43
|
+
* Set `msg.value` to a value that you want to pay with native ETH on the transaction.
|
|
44
|
+
* The remaining amount will be paid with wrappedNative (if it is approved, otherwise it will revert)
|
|
45
|
+
* Any unused native ETH will be refunded to the caller.
|
|
46
|
+
* */
|
|
47
|
+
export function prepareExactOutputSupportingNativePaymentCalldata(params) {
|
|
48
|
+
return encodeMulticallCalldata([encodeExactOutputCalldata(params), encodeRefundETHCalldata()]);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Single-hop exact input with ETH output — `tokenOut` MUST be wrappedNative.
|
|
52
|
+
* Unwraps the whole amount of wrappedNative to native ETH and sends it to the recipient.
|
|
53
|
+
*/
|
|
54
|
+
export function prepareExactInputSingleAndUnwrapCalldata(params, simpleRouter) {
|
|
55
|
+
return encodeMulticallCalldata([
|
|
56
|
+
encodeExactInputSingleCalldata(withRouterRecipient(params, simpleRouter)),
|
|
57
|
+
encodeUnwrapWETH9Calldata(params.amountOutMinimum, params.recipient),
|
|
58
|
+
]);
|
|
59
|
+
}
|
|
60
|
+
/** Multihop exact input with ETH output - `tokenOut` MUST be wrappedNative.
|
|
61
|
+
* Unwraps the whole amount of wrappedNative to native ETH and sends it to the recipient.
|
|
62
|
+
* */
|
|
63
|
+
export function prepareExactInputAndUnwrapCalldata(params, simpleRouter) {
|
|
64
|
+
return encodeMulticallCalldata([
|
|
65
|
+
encodeExactInputCalldata(withRouterRecipient(params, simpleRouter)),
|
|
66
|
+
encodeUnwrapWETH9Calldata(params.amountOutMinimum, params.recipient),
|
|
67
|
+
]);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Single-hop exact output with ETH output — `tokenOut` MUST be wrappedNative.
|
|
71
|
+
* Unwraps the whole amount of wrappedNative to native ETH and sends it to the recipient.
|
|
72
|
+
*/
|
|
73
|
+
export function prepareExactOutputSingleAndUnwrapCalldata(params, simpleRouter) {
|
|
74
|
+
return encodeMulticallCalldata([
|
|
75
|
+
encodeExactOutputSingleCalldata(withRouterRecipient(params, simpleRouter)),
|
|
76
|
+
encodeUnwrapWETH9Calldata(params.amountOut, params.recipient),
|
|
77
|
+
]);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Multihop exact output with ETH output — `tokenOut` MUST be wrappedNative.
|
|
81
|
+
* Unwraps the whole amount of wrappedNative to native ETH and sends it to the recipient.
|
|
82
|
+
*/
|
|
83
|
+
export function prepareExactOutputAndUnwrapCalldata(params, simpleRouter) {
|
|
84
|
+
return encodeMulticallCalldata([
|
|
85
|
+
encodeExactOutputCalldata(withRouterRecipient(params, simpleRouter)),
|
|
86
|
+
encodeUnwrapWETH9Calldata(params.amountOut, params.recipient),
|
|
87
|
+
]);
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=nativeSwap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nativeSwap.js","sourceRoot":"","sources":["../../src/router/nativeSwap.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,8BAA8B,EAAE,+BAA+B,EAAE,MAAM,WAAW,CAAC;AAC5F,OAAO,EAAE,wBAAwB,EAAE,yBAAyB,EAAE,MAAM,eAAe,CAAC;AACpF,OAAO,EACL,uBAAuB,EACvB,uBAAuB,EACvB,yBAAyB,GAC1B,MAAM,+BAA+B,CAAC;AAEvC,SAAS,mBAAmB,CAC1B,MAAS,EACT,YAAqB;IAErB,OAAO,EAAE,GAAG,MAAM,EAAE,SAAS,EAAE,YAAY,EAAE,CAAC;AAChD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,sDAAsD,CACpE,MAA8B;IAE9B,OAAO,uBAAuB,CAAC;QAC7B,8BAA8B,CAAC,MAAM,CAAC;QACtC,wJAAwJ;QACxJ,uBAAuB,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,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,SAAS,CAAC;KAC9D,CAAC,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,mCAAmC,CACjD,MAAyB,EACzB,YAAqB;IAErB,OAAO,uBAAuB,CAAC;QAC7B,yBAAyB,CAAC,mBAAmB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;QACpE,yBAAyB,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,SAAS,CAAC;KAC9D,CAAC,CAAC;AACL,CAAC"}
|
package/dist/router/swap.d.ts
CHANGED
|
@@ -1,45 +1,80 @@
|
|
|
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
|
import type { Address, Hex } from "viem";
|
|
5
10
|
import type { PublicClient } from "viem";
|
|
11
|
+
/**
|
|
12
|
+
* Arguments for `MetricOmmSimpleRouter.exactInputSingle`.
|
|
13
|
+
* Pass to `encodeExactInputSingleCalldata` or native prepare helpers when `tokenIn` is WETH.
|
|
14
|
+
*/
|
|
6
15
|
export type ExactInputSingleParams = {
|
|
16
|
+
/** Pool to swap through. */
|
|
7
17
|
pool: Address;
|
|
18
|
+
/** Input token (`addresses.wrappedNative` when swapping from native ETH). */
|
|
8
19
|
tokenIn: Address;
|
|
20
|
+
/** Output token (`addresses.wrappedNative` when unwrapping to ETH in a follow-up call). */
|
|
9
21
|
tokenOut: Address;
|
|
22
|
+
/** `true` when swapping token0 → token1 for this pool. */
|
|
10
23
|
zeroForOne: boolean;
|
|
24
|
+
/** Exact input amount in `tokenIn` native decimals. */
|
|
11
25
|
amountIn: bigint;
|
|
26
|
+
/** Minimum acceptable output; reverts if less is received. */
|
|
12
27
|
amountOutMinimum: bigint;
|
|
28
|
+
/** Address that receives output tokens. */
|
|
13
29
|
recipient: Address;
|
|
30
|
+
/** Unix timestamp after which the swap reverts. */
|
|
14
31
|
deadline: bigint;
|
|
32
|
+
/** Q64.64 price bound; use `0` / `MAX_UINT128` defaults from builders when unsure. */
|
|
15
33
|
priceLimitX64: bigint;
|
|
34
|
+
/** Per-pool extension calldata; omit for pools without extensions. */
|
|
16
35
|
extensionData?: Hex;
|
|
17
36
|
};
|
|
37
|
+
/**
|
|
38
|
+
* Arguments for `MetricOmmSimpleRouter.exactOutputSingle`.
|
|
39
|
+
*/
|
|
18
40
|
export type ExactOutputSingleParams = {
|
|
19
41
|
pool: Address;
|
|
20
42
|
tokenIn: Address;
|
|
21
43
|
tokenOut: Address;
|
|
22
44
|
zeroForOne: boolean;
|
|
45
|
+
/** Exact output amount in `tokenOut` native decimals. */
|
|
23
46
|
amountOut: bigint;
|
|
47
|
+
/** Maximum input willing to spend (slippage cap). */
|
|
24
48
|
amountInMaximum: bigint;
|
|
25
49
|
recipient: Address;
|
|
26
50
|
deadline: bigint;
|
|
27
51
|
priceLimitX64: bigint;
|
|
28
52
|
extensionData?: Hex;
|
|
29
53
|
};
|
|
54
|
+
/**
|
|
55
|
+
* Inputs for {@link buildExactInputSingleParams}.
|
|
56
|
+
* Quotes live on-chain via the SwapQuoter and applies `slippagePercent` to derive `amountOutMinimum`.
|
|
57
|
+
*/
|
|
30
58
|
export interface BuildExactInputSingleParams {
|
|
31
59
|
publicClient: PublicClient;
|
|
32
60
|
pool: Address;
|
|
33
61
|
tokenIn: Address;
|
|
34
62
|
tokenOut: Address;
|
|
35
63
|
recipient: Address;
|
|
64
|
+
/** Input amount in `tokenIn` decimals (wei for ETH / WETH). */
|
|
36
65
|
amountIn: bigint;
|
|
66
|
+
/** Human-readable slippage tolerance, e.g. `0.5` = 0.5%. */
|
|
37
67
|
slippagePercent: number;
|
|
68
|
+
/** Optional price bound; defaults from quoter direction when omitted. */
|
|
38
69
|
priceLimitX64?: bigint;
|
|
39
70
|
deadline: bigint;
|
|
40
71
|
extensionData?: Hex;
|
|
72
|
+
/** Override when `addresses.quoter` is unset. */
|
|
41
73
|
quoterAddress?: Address;
|
|
74
|
+
/** `addresses.wrappedNative` — required when `tokenIn` or `tokenOut` is native (zero address). */
|
|
75
|
+
wrappedNative?: Address;
|
|
42
76
|
}
|
|
77
|
+
/** Inputs for {@link buildExactOutputSingleParams}. */
|
|
43
78
|
export interface BuildExactOutputSingleParams {
|
|
44
79
|
publicClient: PublicClient;
|
|
45
80
|
pool: Address;
|
|
@@ -52,13 +87,59 @@ export interface BuildExactOutputSingleParams {
|
|
|
52
87
|
deadline: bigint;
|
|
53
88
|
extensionData?: Hex;
|
|
54
89
|
quoterAddress?: Address;
|
|
90
|
+
wrappedNative?: Address;
|
|
55
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Convert a human slippage percent to basis points (1 bp = 0.01%).
|
|
94
|
+
* @example toSlippageBps(0.5) // 50n
|
|
95
|
+
*/
|
|
56
96
|
export declare function toSlippageBps(slippagePercent: number): bigint;
|
|
97
|
+
/** Ceiling division for bigint values. Used when rounding slippage up on exact-output swaps. */
|
|
57
98
|
export declare function ceilDiv(numerator: bigint, denominator: bigint): bigint;
|
|
99
|
+
/**
|
|
100
|
+
* Build single-hop exact-input swap params with live quote and slippage.
|
|
101
|
+
*
|
|
102
|
+
* Resolves `zeroForOne`, quotes `amountOut`, and sets `amountOutMinimum`.
|
|
103
|
+
* For native ETH input, use `prepareExactInputSingleSupportingNativePaymentCalldata` and set `msg.value` on the transaction.
|
|
104
|
+
* (params will contain WETH as `tokenIn`).
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* const params = await buildExactInputSingleParams({
|
|
109
|
+
* publicClient,
|
|
110
|
+
* pool,
|
|
111
|
+
* tokenIn: usdc,
|
|
112
|
+
* tokenOut: weth,
|
|
113
|
+
* recipient: account,
|
|
114
|
+
* amountIn: parseUnits("100", 6),
|
|
115
|
+
* slippagePercent: 0.5,
|
|
116
|
+
* deadline: BigInt(Math.floor(Date.now() / 1000) + 1200),
|
|
117
|
+
* quoterAddress: addresses.quoter,
|
|
118
|
+
* });
|
|
119
|
+
* await walletClient.writeContract({
|
|
120
|
+
* address: addresses.simpleRouter,
|
|
121
|
+
* abi: MetricOmmSimpleRouterAbi,
|
|
122
|
+
* functionName: "exactInputSingle",
|
|
123
|
+
* args: [params],
|
|
124
|
+
* account,
|
|
125
|
+
* });
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
58
128
|
export declare function buildExactInputSingleParams(params: BuildExactInputSingleParams): Promise<ExactInputSingleParams>;
|
|
129
|
+
/**
|
|
130
|
+
* Build single-hop exact-output swap params with live quote and slippage.
|
|
131
|
+
* Sets `amountInMaximum` from quoted input plus `slippagePercent`.
|
|
132
|
+
*/
|
|
59
133
|
export declare function buildExactOutputSingleParams(params: BuildExactOutputSingleParams): Promise<ExactOutputSingleParams>;
|
|
134
|
+
/**
|
|
135
|
+
* Encode `exactInputSingle` calldata for `addresses.simpleRouter`.
|
|
136
|
+
* Approve `tokenIn` on the router before sending (unless using native ETH prepare helpers).
|
|
137
|
+
*/
|
|
60
138
|
export declare function encodeExactInputSingleCalldata(params: ExactInputSingleParams): Hex;
|
|
139
|
+
/** Alias for {@link encodeExactInputSingleCalldata}. */
|
|
61
140
|
export declare const prepareExactInputSingleCalldata: typeof encodeExactInputSingleCalldata;
|
|
141
|
+
/** Encode `exactOutputSingle` calldata for `addresses.simpleRouter`. */
|
|
62
142
|
export declare function encodeExactOutputSingleCalldata(params: ExactOutputSingleParams): Hex;
|
|
143
|
+
/** Alias for {@link encodeExactOutputSingleCalldata}. */
|
|
63
144
|
export declare const prepareExactOutputSingleCalldata: typeof encodeExactOutputSingleCalldata;
|
|
64
145
|
//# sourceMappingURL=swap.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"swap.d.ts","sourceRoot":"","sources":["../../src/router/swap.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"swap.d.ts","sourceRoot":"","sources":["../../src/router/swap.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAOzC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AAIzC;;;GAGG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,4BAA4B;IAC5B,IAAI,EAAE,OAAO,CAAC;IACd,6EAA6E;IAC7E,OAAO,EAAE,OAAO,CAAC;IACjB,2FAA2F;IAC3F,QAAQ,EAAE,OAAO,CAAC;IAClB,0DAA0D;IAC1D,UAAU,EAAE,OAAO,CAAC;IACpB,uDAAuD;IACvD,QAAQ,EAAE,MAAM,CAAC;IACjB,8DAA8D;IAC9D,gBAAgB,EAAE,MAAM,CAAC;IACzB,2CAA2C;IAC3C,SAAS,EAAE,OAAO,CAAC;IACnB,mDAAmD;IACnD,QAAQ,EAAE,MAAM,CAAC;IACjB,sFAAsF;IACtF,aAAa,EAAE,MAAM,CAAC;IACtB,sEAAsE;IACtE,aAAa,CAAC,EAAE,GAAG,CAAC;CACrB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,uBAAuB,GAAG;IACpC,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,OAAO,CAAC;IAClB,UAAU,EAAE,OAAO,CAAC;IACpB,yDAAyD;IACzD,SAAS,EAAE,MAAM,CAAC;IAClB,qDAAqD;IACrD,eAAe,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE,OAAO,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IAC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2B,CAC/C,MAAM,EAAE,2BAA2B,GAClC,OAAO,CAAC,sBAAsB,CAAC,CAsCjC;AAED;;;GAGG;AACH,wBAAsB,4BAA4B,CAChD,MAAM,EAAE,4BAA4B,GACnC,OAAO,CAAC,uBAAuB,CAAC,CAsClC;AAED;;;GAGG;AACH,wBAAgB,8BAA8B,CAAC,MAAM,EAAE,sBAAsB,GAAG,GAAG,CAMlF;AAED,wDAAwD;AACxD,eAAO,MAAM,+BAA+B,uCAAiC,CAAC;AAE9E,wEAAwE;AACxE,wBAAgB,+BAA+B,CAAC,MAAM,EAAE,uBAAuB,GAAG,GAAG,CAMpF;AAED,yDAAyD;AACzD,eAAO,MAAM,gCAAgC,wCAAkC,CAAC"}
|
package/dist/router/swap.js
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
|
import { encodeFunctionData } from "viem";
|
|
5
10
|
import { defaultPriceLimitX64 } from "../quoter/quotePath.js";
|
|
@@ -8,12 +13,17 @@ import { resolveSwapPath } from "../swap/path.js";
|
|
|
8
13
|
import { SIMPLE_ROUTER_ABI } from "../constants.js";
|
|
9
14
|
import { EMPTY_EXTENSION_DATA } from "../extensions/types.js";
|
|
10
15
|
const BPS_BASE = 10000n;
|
|
16
|
+
/**
|
|
17
|
+
* Convert a human slippage percent to basis points (1 bp = 0.01%).
|
|
18
|
+
* @example toSlippageBps(0.5) // 50n
|
|
19
|
+
*/
|
|
11
20
|
export function toSlippageBps(slippagePercent) {
|
|
12
21
|
if (!Number.isFinite(slippagePercent) || slippagePercent < 0 || slippagePercent > 100) {
|
|
13
22
|
throw new Error("slippagePercent must be a finite number between 0 and 100");
|
|
14
23
|
}
|
|
15
24
|
return BigInt(Math.round(slippagePercent * 100));
|
|
16
25
|
}
|
|
26
|
+
/** Ceiling division for bigint values. Used when rounding slippage up on exact-output swaps. */
|
|
17
27
|
export function ceilDiv(numerator, denominator) {
|
|
18
28
|
if (denominator <= 0n) {
|
|
19
29
|
throw new Error("denominator must be > 0");
|
|
@@ -51,6 +61,35 @@ function toExactOutputSingleStruct(params) {
|
|
|
51
61
|
extensionData: withExtensionData(params.extensionData),
|
|
52
62
|
};
|
|
53
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Build single-hop exact-input swap params with live quote and slippage.
|
|
66
|
+
*
|
|
67
|
+
* Resolves `zeroForOne`, quotes `amountOut`, and sets `amountOutMinimum`.
|
|
68
|
+
* For native ETH input, use `prepareExactInputSingleSupportingNativePaymentCalldata` and set `msg.value` on the transaction.
|
|
69
|
+
* (params will contain WETH as `tokenIn`).
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* const params = await buildExactInputSingleParams({
|
|
74
|
+
* publicClient,
|
|
75
|
+
* pool,
|
|
76
|
+
* tokenIn: usdc,
|
|
77
|
+
* tokenOut: weth,
|
|
78
|
+
* recipient: account,
|
|
79
|
+
* amountIn: parseUnits("100", 6),
|
|
80
|
+
* slippagePercent: 0.5,
|
|
81
|
+
* deadline: BigInt(Math.floor(Date.now() / 1000) + 1200),
|
|
82
|
+
* quoterAddress: addresses.quoter,
|
|
83
|
+
* });
|
|
84
|
+
* await walletClient.writeContract({
|
|
85
|
+
* address: addresses.simpleRouter,
|
|
86
|
+
* abi: MetricOmmSimpleRouterAbi,
|
|
87
|
+
* functionName: "exactInputSingle",
|
|
88
|
+
* args: [params],
|
|
89
|
+
* account,
|
|
90
|
+
* });
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
54
93
|
export async function buildExactInputSingleParams(params) {
|
|
55
94
|
const slippageBps = toSlippageBps(params.slippagePercent);
|
|
56
95
|
const { zeroForOnes } = await resolveSwapPath(params.publicClient, params.tokenIn, params.tokenOut, [params.pool], params.extensionData ? [params.extensionData] : undefined);
|
|
@@ -81,6 +120,10 @@ export async function buildExactInputSingleParams(params) {
|
|
|
81
120
|
extensionData: params.extensionData,
|
|
82
121
|
};
|
|
83
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Build single-hop exact-output swap params with live quote and slippage.
|
|
125
|
+
* Sets `amountInMaximum` from quoted input plus `slippagePercent`.
|
|
126
|
+
*/
|
|
84
127
|
export async function buildExactOutputSingleParams(params) {
|
|
85
128
|
const slippageBps = toSlippageBps(params.slippagePercent);
|
|
86
129
|
const { zeroForOnes } = await resolveSwapPath(params.publicClient, params.tokenIn, params.tokenOut, [params.pool], params.extensionData ? [params.extensionData] : undefined);
|
|
@@ -111,6 +154,10 @@ export async function buildExactOutputSingleParams(params) {
|
|
|
111
154
|
extensionData: params.extensionData,
|
|
112
155
|
};
|
|
113
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* Encode `exactInputSingle` calldata for `addresses.simpleRouter`.
|
|
159
|
+
* Approve `tokenIn` on the router before sending (unless using native ETH prepare helpers).
|
|
160
|
+
*/
|
|
114
161
|
export function encodeExactInputSingleCalldata(params) {
|
|
115
162
|
return encodeFunctionData({
|
|
116
163
|
abi: SIMPLE_ROUTER_ABI,
|
|
@@ -118,7 +165,9 @@ export function encodeExactInputSingleCalldata(params) {
|
|
|
118
165
|
args: [toExactInputSingleStruct(params)],
|
|
119
166
|
});
|
|
120
167
|
}
|
|
168
|
+
/** Alias for {@link encodeExactInputSingleCalldata}. */
|
|
121
169
|
export const prepareExactInputSingleCalldata = encodeExactInputSingleCalldata;
|
|
170
|
+
/** Encode `exactOutputSingle` calldata for `addresses.simpleRouter`. */
|
|
122
171
|
export function encodeExactOutputSingleCalldata(params) {
|
|
123
172
|
return encodeFunctionData({
|
|
124
173
|
abi: SIMPLE_ROUTER_ABI,
|
|
@@ -126,5 +175,6 @@ export function encodeExactOutputSingleCalldata(params) {
|
|
|
126
175
|
args: [toExactOutputSingleStruct(params)],
|
|
127
176
|
});
|
|
128
177
|
}
|
|
178
|
+
/** Alias for {@link encodeExactOutputSingleCalldata}. */
|
|
129
179
|
export const prepareExactOutputSingleCalldata = encodeExactOutputSingleCalldata;
|
|
130
180
|
//# sourceMappingURL=swap.js.map
|