@seer-pm/lens 1.3.0 → 1.4.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 +20 -1
- package/dist/{chunk-W6IFHZ77.js → chunk-5UNFCMSF.js} +11 -1
- package/dist/chunk-5UNFCMSF.js.map +1 -0
- package/dist/client.cjs +185 -54
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +23 -3
- package/dist/client.d.ts +23 -3
- package/dist/client.js +184 -54
- package/dist/client.js.map +1 -1
- package/dist/index.cjs +13 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -1
- package/dist/index.d.ts +12 -1
- package/dist/index.js +3 -1
- package/package.json +1 -1
- package/dist/chunk-W6IFHZ77.js.map +0 -1
package/README.md
CHANGED
|
@@ -113,6 +113,25 @@ await walletClient.sendTransaction({
|
|
|
113
113
|
});
|
|
114
114
|
```
|
|
115
115
|
|
|
116
|
+
### Errors
|
|
117
|
+
|
|
118
|
+
After building calldata, `quote` simulates the swap with a gas cap of **90% of `block.gasLimit`** (using ERC20 allowance/balance state overrides so a prior `approve` is not required).
|
|
119
|
+
|
|
120
|
+
If the AMM quotes successfully but the swap would exceed that cap — typical of a very thin concentrated-liquidity pool — it throws **`InsufficientLiquidityError`** (`code: "INSUFFICIENT_LIQUIDITY"`). Try a smaller `amount`.
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import { Lens, InsufficientLiquidityError } from "@seer-pm/lens/client";
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
const q = await lens.quote({ /* ... */ });
|
|
127
|
+
} catch (e) {
|
|
128
|
+
if (e instanceof InsufficientLiquidityError) {
|
|
129
|
+
// Pool too thin for this size — reduce amount and retry
|
|
130
|
+
}
|
|
131
|
+
throw e;
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
116
135
|
## Supported venues
|
|
117
136
|
|
|
118
137
|
Venues match Seer’s liquidity per chain: Swapr on Gnosis; Uniswap V3/V4 on Ethereum, Optimism, and Base.
|
|
@@ -126,7 +145,7 @@ Venues match Seer’s liquidity per chain: Swapr on Gnosis; Uniswap V3/V4 on Eth
|
|
|
126
145
|
|
|
127
146
|
| Import | Contents |
|
|
128
147
|
|---|---|
|
|
129
|
-
| `@seer-pm/lens` | Constants, ABIs, types, `CHAINS`, `ETH`, `GNOSIS_sDAI` |
|
|
148
|
+
| `@seer-pm/lens` | Constants, ABIs, types, `CHAINS`, `ETH`, `GNOSIS_sDAI`, `InsufficientLiquidityError` |
|
|
130
149
|
| `@seer-pm/lens/client` | `Lens` + re-exports (peer: viem ^2) |
|
|
131
150
|
|
|
132
151
|
## Contracts
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var InsufficientLiquidityError = class extends Error {
|
|
3
|
+
code = "INSUFFICIENT_LIQUIDITY";
|
|
4
|
+
constructor(message = "Swap too large for available liquidity (would exceed block gas limit). Try a smaller amount.") {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "InsufficientLiquidityError";
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
|
|
1
10
|
// src/index.ts
|
|
2
11
|
var AMM = /* @__PURE__ */ ((AMM2) => {
|
|
3
12
|
AMM2[AMM2["UNI_V3"] = 0] = "UNI_V3";
|
|
@@ -58,6 +67,7 @@ function defaultDeadline() {
|
|
|
58
67
|
}
|
|
59
68
|
|
|
60
69
|
export {
|
|
70
|
+
InsufficientLiquidityError,
|
|
61
71
|
AMM,
|
|
62
72
|
AMM_NAMES,
|
|
63
73
|
LENS_QUOTER_ABI,
|
|
@@ -66,4 +76,4 @@ export {
|
|
|
66
76
|
GNOSIS_sDAI,
|
|
67
77
|
defaultDeadline
|
|
68
78
|
};
|
|
69
|
-
//# sourceMappingURL=chunk-
|
|
79
|
+
//# sourceMappingURL=chunk-5UNFCMSF.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts","../src/index.ts"],"sourcesContent":["/**\n * Thrown when a route quotes successfully but the swap cannot execute within\n * the chain's block gas limit — typically a thin concentrated-liquidity pool\n * that would cross too many ticks for the requested size.\n */\nexport class InsufficientLiquidityError extends Error {\n readonly code = \"INSUFFICIENT_LIQUIDITY\" as const;\n\n constructor(\n message = \"Swap too large for available liquidity (would exceed block gas limit). Try a smaller amount.\",\n ) {\n super(message);\n this.name = \"InsufficientLiquidityError\";\n }\n}\n","// Lens SDK — Core constants, ABIs, types, token list (zero dependencies)\n\n// ---- Chain config ----\n\n/** Unified AMM enum (same ordinals on every chain). */\nexport enum AMM {\n UNI_V3 = 0,\n UNI_V4 = 1,\n UNI_V4_HOOKED = 2,\n SWAPR_V3 = 3,\n}\n\nexport const AMM_NAMES: Record<AMM, string> = {\n [AMM.UNI_V3]: \"Uniswap V3\",\n [AMM.UNI_V4]: \"Uniswap V4\",\n [AMM.UNI_V4_HOOKED]: \"Uniswap V4 Hooked\",\n [AMM.SWAPR_V3]: \"Swapr V3\",\n};\n\n/** Shared LensQuoter ABI for the unified contract on all chains. */\nexport const LENS_QUOTER_ABI = [\n \"function buildBestSwapViaETH(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) a, tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) b, address target, bytes data, uint256 msgValue)\",\n \"function buildBestSwapViaToken(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress, address mid) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) a, tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) b, address target, bytes data, uint256 msgValue)\",\n \"function buildBestSwap(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) best, address target, bytes callData, uint256 amountLimit, uint256 msgValue)\",\n \"function V3_SWAP_ROUTER() view returns (address)\",\n \"function V4_ROUTER() view returns (address)\",\n \"function SWAPR_SWAP_ROUTER() view returns (address)\",\n \"function V4_QUOTER() view returns (address)\",\n] as const;\n\n/** Per-chain LensQuoter, official DEX routers, and wrapped native. */\nexport const CHAINS = {\n 1: {\n lensQuoter: \"0xaCE2C61E250c2d019b9Ed7FEf6A441022D6Ec7d2\",\n v3SwapRouter: \"0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2\",\n },\n 10: {\n lensQuoter: \"0x591aF101cAf2b7351C74c25F5E64bC2E062D2843\",\n v3SwapRouter: \"0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0x4200000000000000000000000000000000000006\",\n },\n 100: {\n lensQuoter: \"0xE8E3d230C1c17C7249126036F170027a664d1FbA\",\n v3SwapRouter: \"0x0000000000000000000000000000000000000000\",\n v4Router: \"0x0000000000000000000000000000000000000000\",\n swaprSwapRouter: \"0xfFB643E73f280B97809A8b41f7232AB401a04ee1\",\n wnative: \"0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d\",\n },\n 8453: {\n lensQuoter: \"0x591aF101cAf2b7351C74c25F5E64bC2E062D2843\",\n v3SwapRouter: \"0x2626664c2603336E57B271c5C0b26F421741e481\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0x4200000000000000000000000000000000000006\",\n },\n} as const;\n\nexport type ChainId = keyof typeof CHAINS;\n\nexport const ETH = \"0x0000000000000000000000000000000000000000\" as const;\n\n/** Gnosis sDAI — default ViaToken mid for native/WXDAI↔ERC20 on Gnosis (Seer). */\nexport const GNOSIS_sDAI = \"0xaf204776c7245bF4147c2612BF6e5972Ee483701\" as const;\n\n// ---- Types ----\n\nexport interface QuoteResult {\n amountIn: bigint;\n amountOut: bigint;\n /** Official DEX router to call. */\n target: string;\n /** Ready-to-send calldata for `target`. */\n data: string;\n msgValue: bigint;\n isTwoHop: boolean;\n sourceA: string;\n sourceB: string | null;\n}\n\nexport interface BaseQuoteParams {\n tokenIn: string;\n tokenOut: string;\n amount: bigint;\n}\n\nexport interface QuoteParams extends BaseQuoteParams {\n recipient: string;\n slippageBps?: number;\n deadline?: bigint;\n /** false = exactIn (default); true = exactOut. `amount` is amountIn or desired amountOut accordingly. */\n exactOut?: boolean;\n /** Optional V4 hooked-pool candidate (exactIn/exactOut; can beat the primary route). */\n hookPoolFee?: number;\n hookTickSpacing?: number;\n hookAddress?: string;\n /**\n * Intermediate token for two-hop routes (`buildBestSwapViaToken`).\n * When set, skips the native/ERC20 cascade. On Gnosis, native/WXDAI↔ERC20\n * already defaults to ViaToken(sDAI); use this only to force a different mid.\n */\n intermediate?: string;\n}\n\n// ---- Errors ----\n\nexport { InsufficientLiquidityError } from \"./errors.js\";\n\n// ---- Helpers ----\n\nexport function defaultDeadline(): bigint {\n return BigInt(Math.trunc(Date.now() / 1000) + 300);\n}\n"],"mappings":";AAKO,IAAM,6BAAN,cAAyC,MAAM;AAAA,EAC3C,OAAO;AAAA,EAEhB,YACE,UAAU,gGACV;AACA,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;;;ACTO,IAAK,MAAL,kBAAKA,SAAL;AACL,EAAAA,UAAA,YAAS,KAAT;AACA,EAAAA,UAAA,YAAS,KAAT;AACA,EAAAA,UAAA,mBAAgB,KAAhB;AACA,EAAAA,UAAA,cAAW,KAAX;AAJU,SAAAA;AAAA,GAAA;AAOL,IAAM,YAAiC;AAAA,EAC5C,CAAC,cAAU,GAAG;AAAA,EACd,CAAC,cAAU,GAAG;AAAA,EACd,CAAC,qBAAiB,GAAG;AAAA,EACrB,CAAC,gBAAY,GAAG;AAClB;AAGO,IAAM,kBAAkB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAGO,IAAM,SAAS;AAAA,EACpB,GAAG;AAAA,IACD,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AAAA,EACA,IAAI;AAAA,IACF,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AAAA,EACA,KAAK;AAAA,IACH,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AAAA,EACA,MAAM;AAAA,IACJ,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AACF;AAIO,IAAM,MAAM;AAGZ,IAAM,cAAc;AA+CpB,SAAS,kBAA0B;AACxC,SAAO,OAAO,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI,IAAI,GAAG;AACnD;","names":["AMM"]}
|
package/dist/client.cjs
CHANGED
|
@@ -25,12 +25,23 @@ __export(client_exports, {
|
|
|
25
25
|
CHAINS: () => CHAINS,
|
|
26
26
|
ETH: () => ETH,
|
|
27
27
|
GNOSIS_sDAI: () => GNOSIS_sDAI,
|
|
28
|
+
InsufficientLiquidityError: () => InsufficientLiquidityError,
|
|
28
29
|
LENS_QUOTER_ABI: () => LENS_QUOTER_ABI,
|
|
29
30
|
Lens: () => Lens,
|
|
31
|
+
assertSwapExecutable: () => assertSwapExecutable,
|
|
30
32
|
defaultDeadline: () => defaultDeadline
|
|
31
33
|
});
|
|
32
34
|
module.exports = __toCommonJS(client_exports);
|
|
33
|
-
var
|
|
35
|
+
var import_viem2 = require("viem");
|
|
36
|
+
|
|
37
|
+
// src/errors.ts
|
|
38
|
+
var InsufficientLiquidityError = class extends Error {
|
|
39
|
+
code = "INSUFFICIENT_LIQUIDITY";
|
|
40
|
+
constructor(message = "Swap too large for available liquidity (would exceed block gas limit). Try a smaller amount.") {
|
|
41
|
+
super(message);
|
|
42
|
+
this.name = "InsufficientLiquidityError";
|
|
43
|
+
}
|
|
44
|
+
};
|
|
34
45
|
|
|
35
46
|
// src/index.ts
|
|
36
47
|
var AMM = /* @__PURE__ */ ((AMM2) => {
|
|
@@ -91,10 +102,113 @@ function defaultDeadline() {
|
|
|
91
102
|
return BigInt(Math.trunc(Date.now() / 1e3) + 300);
|
|
92
103
|
}
|
|
93
104
|
|
|
105
|
+
// src/gasGuard.ts
|
|
106
|
+
var import_viem = require("viem");
|
|
107
|
+
var GAS_LIMIT_BPS = 9000n;
|
|
108
|
+
function erc20BalanceSlot(owner) {
|
|
109
|
+
return (0, import_viem.keccak256)(
|
|
110
|
+
(0, import_viem.encodeAbiParameters)(
|
|
111
|
+
[{ type: "address" }, { type: "uint256" }],
|
|
112
|
+
[owner, 0n]
|
|
113
|
+
)
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
function erc20AllowanceSlot(owner, spender) {
|
|
117
|
+
const inner = (0, import_viem.keccak256)(
|
|
118
|
+
(0, import_viem.encodeAbiParameters)(
|
|
119
|
+
[{ type: "address" }, { type: "uint256" }],
|
|
120
|
+
[owner, 1n]
|
|
121
|
+
)
|
|
122
|
+
);
|
|
123
|
+
return (0, import_viem.keccak256)(
|
|
124
|
+
(0, import_viem.encodeAbiParameters)(
|
|
125
|
+
[{ type: "address" }, { type: "bytes32" }],
|
|
126
|
+
[spender, inner]
|
|
127
|
+
)
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
function erc20SpendOverride(token, owner, spender, amount) {
|
|
131
|
+
const bal = amount > 0n ? amount : 1n;
|
|
132
|
+
return {
|
|
133
|
+
address: token,
|
|
134
|
+
stateDiff: [
|
|
135
|
+
{ slot: erc20BalanceSlot(owner), value: (0, import_viem.numberToHex)(bal, { size: 32 }) },
|
|
136
|
+
{
|
|
137
|
+
slot: erc20AllowanceSlot(owner, spender),
|
|
138
|
+
value: (0, import_viem.numberToHex)(import_viem.maxUint256, { size: 32 })
|
|
139
|
+
}
|
|
140
|
+
]
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function isNative(token) {
|
|
144
|
+
return (0, import_viem.isAddressEqual)(token, import_viem.zeroAddress);
|
|
145
|
+
}
|
|
146
|
+
function buildSwapStateOverride(params) {
|
|
147
|
+
const { account, tokenIn, amountIn, target, msgValue } = params;
|
|
148
|
+
const overrides = [
|
|
149
|
+
{
|
|
150
|
+
address: account,
|
|
151
|
+
balance: msgValue + 10n ** 18n
|
|
152
|
+
}
|
|
153
|
+
];
|
|
154
|
+
if (!isNative(tokenIn)) {
|
|
155
|
+
overrides.push(
|
|
156
|
+
erc20SpendOverride(tokenIn, account, target, amountIn)
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
return overrides;
|
|
160
|
+
}
|
|
161
|
+
function maxGasForBlock(gasLimit) {
|
|
162
|
+
return gasLimit * GAS_LIMIT_BPS / 10000n;
|
|
163
|
+
}
|
|
164
|
+
async function assertSwapExecutable(client, params) {
|
|
165
|
+
const { account, tokenIn, amountIn, result } = params;
|
|
166
|
+
const target = result.target;
|
|
167
|
+
const data = result.data;
|
|
168
|
+
let block;
|
|
169
|
+
try {
|
|
170
|
+
block = await client.getBlock({ blockTag: "latest" });
|
|
171
|
+
} catch {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const maxGas = maxGasForBlock(block.gasLimit);
|
|
175
|
+
if (maxGas === 0n) return;
|
|
176
|
+
const stateOverride = buildSwapStateOverride({
|
|
177
|
+
account,
|
|
178
|
+
tokenIn,
|
|
179
|
+
amountIn,
|
|
180
|
+
target,
|
|
181
|
+
msgValue: result.msgValue
|
|
182
|
+
});
|
|
183
|
+
const base = {
|
|
184
|
+
account,
|
|
185
|
+
to: target,
|
|
186
|
+
data,
|
|
187
|
+
value: result.msgValue,
|
|
188
|
+
stateOverride
|
|
189
|
+
};
|
|
190
|
+
try {
|
|
191
|
+
await client.call(base);
|
|
192
|
+
} catch {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
try {
|
|
196
|
+
await client.call({ ...base, gas: maxGas });
|
|
197
|
+
} catch {
|
|
198
|
+
throw new InsufficientLiquidityError();
|
|
199
|
+
}
|
|
200
|
+
try {
|
|
201
|
+
const gas = await client.estimateGas(base);
|
|
202
|
+
if (gas > maxGas) throw new InsufficientLiquidityError();
|
|
203
|
+
} catch (e) {
|
|
204
|
+
if (e instanceof InsufficientLiquidityError) throw e;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
94
208
|
// src/client.ts
|
|
95
209
|
var QUOTE_TUPLE = "tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut)";
|
|
96
210
|
var QUOTE_STRUCT = "struct Quote { uint8 source; uint256 feeBps; uint256 amountIn; uint256 amountOut; }";
|
|
97
|
-
var toQuoterAbi = (lines) => (0,
|
|
211
|
+
var toQuoterAbi = (lines) => (0, import_viem2.parseAbi)([QUOTE_STRUCT, ...lines.map((l) => l.split(QUOTE_TUPLE).join("Quote"))]);
|
|
98
212
|
var quoterAbi = toQuoterAbi(LENS_QUOTER_ABI);
|
|
99
213
|
var DEFAULT_SLIPPAGE = 50;
|
|
100
214
|
function ammName(source) {
|
|
@@ -115,7 +229,7 @@ function mapViaResult(value) {
|
|
|
115
229
|
};
|
|
116
230
|
}
|
|
117
231
|
function involvesNativeOrWnative(tokenIn, tokenOut, wnative) {
|
|
118
|
-
return (0,
|
|
232
|
+
return (0, import_viem2.isAddressEqual)(tokenIn, import_viem2.zeroAddress) || (0, import_viem2.isAddressEqual)(tokenOut, import_viem2.zeroAddress) || (0, import_viem2.isAddressEqual)(tokenIn, wnative) || (0, import_viem2.isAddressEqual)(tokenOut, wnative);
|
|
119
233
|
}
|
|
120
234
|
var Lens = class {
|
|
121
235
|
client;
|
|
@@ -170,6 +284,10 @@ var Lens = class {
|
|
|
170
284
|
* - native / WNATIVE ↔ ERC20 → on Gnosis, `ViaToken(sDAI)` directly;
|
|
171
285
|
* elsewhere `buildBestSwapViaETH`
|
|
172
286
|
* - ERC20 ↔ ERC20 → `buildBestSwap` only (optional V4 hooked candidate)
|
|
287
|
+
*
|
|
288
|
+
* After building calldata, simulates the swap with a gas cap of 90% of
|
|
289
|
+
* `block.gasLimit`. Thin pools that quote but cannot fit in a block throw
|
|
290
|
+
* {@link InsufficientLiquidityError}.
|
|
173
291
|
*/
|
|
174
292
|
async quote(params) {
|
|
175
293
|
const {
|
|
@@ -182,7 +300,7 @@ var Lens = class {
|
|
|
182
300
|
exactOut = false,
|
|
183
301
|
hookPoolFee = 0,
|
|
184
302
|
hookTickSpacing = 0,
|
|
185
|
-
hookAddress =
|
|
303
|
+
hookAddress = import_viem2.zeroAddress,
|
|
186
304
|
intermediate
|
|
187
305
|
} = params;
|
|
188
306
|
const routeArgs = {
|
|
@@ -197,60 +315,71 @@ var Lens = class {
|
|
|
197
315
|
hookTickSpacing,
|
|
198
316
|
hookAddress
|
|
199
317
|
};
|
|
318
|
+
let result;
|
|
200
319
|
if (intermediate) {
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
320
|
+
result = mapViaResult(await this.viaToken({ ...routeArgs, mid: intermediate }));
|
|
321
|
+
} else {
|
|
322
|
+
const useViaEth = involvesNativeOrWnative(tokenIn, tokenOut, this.wnative);
|
|
323
|
+
if (useViaEth) {
|
|
324
|
+
if (this.chainId === 100) {
|
|
325
|
+
result = mapViaResult(await this.viaToken({ ...routeArgs, mid: GNOSIS_sDAI }));
|
|
326
|
+
} else {
|
|
327
|
+
const value = await this.client.readContract({
|
|
328
|
+
address: this.lensQuoter,
|
|
329
|
+
abi: this.quoterAbi,
|
|
330
|
+
functionName: "buildBestSwapViaETH",
|
|
331
|
+
args: [
|
|
332
|
+
recipient,
|
|
333
|
+
exactOut,
|
|
334
|
+
tokenIn,
|
|
335
|
+
tokenOut,
|
|
336
|
+
amount,
|
|
337
|
+
BigInt(slippageBps),
|
|
338
|
+
deadline,
|
|
339
|
+
hookPoolFee,
|
|
340
|
+
hookTickSpacing,
|
|
341
|
+
hookAddress
|
|
342
|
+
]
|
|
343
|
+
});
|
|
344
|
+
result = mapViaResult(value);
|
|
345
|
+
}
|
|
346
|
+
} else {
|
|
347
|
+
const [best, target, callData, _amountLimit, msgValue] = await this.client.readContract({
|
|
348
|
+
address: this.lensQuoter,
|
|
349
|
+
abi: this.quoterAbi,
|
|
350
|
+
functionName: "buildBestSwap",
|
|
351
|
+
args: [
|
|
352
|
+
recipient,
|
|
353
|
+
exactOut,
|
|
354
|
+
tokenIn,
|
|
355
|
+
tokenOut,
|
|
356
|
+
amount,
|
|
357
|
+
BigInt(slippageBps),
|
|
358
|
+
deadline,
|
|
359
|
+
hookPoolFee,
|
|
360
|
+
hookTickSpacing,
|
|
361
|
+
hookAddress
|
|
362
|
+
]
|
|
363
|
+
});
|
|
364
|
+
result = {
|
|
365
|
+
amountIn: best.amountIn,
|
|
366
|
+
amountOut: best.amountOut,
|
|
367
|
+
target,
|
|
368
|
+
data: callData,
|
|
369
|
+
msgValue: msgValue ?? 0n,
|
|
370
|
+
isTwoHop: false,
|
|
371
|
+
sourceA: ammName(best.source),
|
|
372
|
+
sourceB: null
|
|
373
|
+
};
|
|
207
374
|
}
|
|
208
|
-
const value = await this.client.readContract({
|
|
209
|
-
address: this.lensQuoter,
|
|
210
|
-
abi: this.quoterAbi,
|
|
211
|
-
functionName: "buildBestSwapViaETH",
|
|
212
|
-
args: [
|
|
213
|
-
recipient,
|
|
214
|
-
exactOut,
|
|
215
|
-
tokenIn,
|
|
216
|
-
tokenOut,
|
|
217
|
-
amount,
|
|
218
|
-
BigInt(slippageBps),
|
|
219
|
-
deadline,
|
|
220
|
-
hookPoolFee,
|
|
221
|
-
hookTickSpacing,
|
|
222
|
-
hookAddress
|
|
223
|
-
]
|
|
224
|
-
});
|
|
225
|
-
return mapViaResult(value);
|
|
226
375
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
recipient,
|
|
233
|
-
exactOut,
|
|
234
|
-
tokenIn,
|
|
235
|
-
tokenOut,
|
|
236
|
-
amount,
|
|
237
|
-
BigInt(slippageBps),
|
|
238
|
-
deadline,
|
|
239
|
-
hookPoolFee,
|
|
240
|
-
hookTickSpacing,
|
|
241
|
-
hookAddress
|
|
242
|
-
]
|
|
376
|
+
await assertSwapExecutable(this.client, {
|
|
377
|
+
account: recipient,
|
|
378
|
+
tokenIn,
|
|
379
|
+
amountIn: result.amountIn,
|
|
380
|
+
result
|
|
243
381
|
});
|
|
244
|
-
return
|
|
245
|
-
amountIn: best.amountIn,
|
|
246
|
-
amountOut: best.amountOut,
|
|
247
|
-
target,
|
|
248
|
-
data: callData,
|
|
249
|
-
msgValue: msgValue ?? 0n,
|
|
250
|
-
isTwoHop: false,
|
|
251
|
-
sourceA: ammName(best.source),
|
|
252
|
-
sourceB: null
|
|
253
|
-
};
|
|
382
|
+
return result;
|
|
254
383
|
}
|
|
255
384
|
};
|
|
256
385
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -260,8 +389,10 @@ var Lens = class {
|
|
|
260
389
|
CHAINS,
|
|
261
390
|
ETH,
|
|
262
391
|
GNOSIS_sDAI,
|
|
392
|
+
InsufficientLiquidityError,
|
|
263
393
|
LENS_QUOTER_ABI,
|
|
264
394
|
Lens,
|
|
395
|
+
assertSwapExecutable,
|
|
265
396
|
defaultDeadline
|
|
266
397
|
});
|
|
267
398
|
//# sourceMappingURL=client.cjs.map
|
package/dist/client.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client.ts","../src/index.ts"],"sourcesContent":["// Lens SDK — client\n\nimport { parseAbi, isAddressEqual, zeroAddress } from \"viem\";\nimport type { PublicClient, Address, Abi } from \"viem\";\nimport {\n CHAINS, GNOSIS_sDAI,\n LENS_QUOTER_ABI,\n AMM_NAMES,\n defaultDeadline,\n type ChainId,\n type QuoteParams,\n type QuoteResult,\n} from \"./index.js\";\n\n// viem's parseAbi (abitype) doesn't support inline `tuple(...)` syntax,\n// so rewrite the shared quoter ABI strings to use a named struct.\nconst QUOTE_TUPLE = \"tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut)\";\nconst QUOTE_STRUCT = \"struct Quote { uint8 source; uint256 feeBps; uint256 amountIn; uint256 amountOut; }\";\n\n/** Rewrites inline `tuple(...)` quoter ABI lines to a named `Quote` struct for `parseAbi`. */\nconst toQuoterAbi = (lines: readonly string[]): Abi =>\n parseAbi([QUOTE_STRUCT, ...lines.map((l) => l.split(QUOTE_TUPLE).join(\"Quote\"))]);\n\nconst quoterAbi = toQuoterAbi(LENS_QUOTER_ABI);\n\nexport {\n ETH, GNOSIS_sDAI, AMM, AMM_NAMES,\n CHAINS,\n LENS_QUOTER_ABI, defaultDeadline,\n} from \"./index.js\";\nexport type { QuoteResult, BaseQuoteParams, QuoteParams, ChainId } from \"./index.js\";\n\nconst DEFAULT_SLIPPAGE = 50; // 0.5%\n\ntype QuoteStruct = { source: number; feeBps: bigint; amountIn: bigint; amountOut: bigint };\ntype BestSwapResult = readonly [\n QuoteStruct,\n QuoteStruct,\n Address,\n `0x${string}`,\n bigint,\n];\ntype BuildBestSwapResult = readonly [QuoteStruct, Address, `0x${string}`, bigint, bigint];\n\n/** Resolves a human-readable AMM name from an on-chain `source` id. */\nfunction ammName(source: number): string {\n return (AMM_NAMES as Record<number, string>)[source] || `AMM #${source}`;\n}\n\n/**\n * Maps a `buildBestSwapViaETH` / `buildBestSwapViaToken` return value to `QuoteResult`.\n * Hop B is zeroed for single-hop; `b.amountOut > 0` means a real two-hop\n * route, so final `amountOut` comes from hop B.\n */\nfunction mapViaResult(value: BestSwapResult): QuoteResult {\n const [a, b, target, data, msgValue] = value;\n const isTwoHop = b.amountOut > 0n;\n return {\n amountIn: a.amountIn,\n amountOut: isTwoHop ? b.amountOut : a.amountOut,\n target: target as string,\n data: data as string,\n msgValue: msgValue ?? 0n,\n isTwoHop,\n sourceA: ammName(a.source),\n sourceB: isTwoHop ? ammName(b.source) : null,\n };\n}\n\n/**\n * True when either endpoint is native (`address(0)`) or the chain's wrapped native.\n * On non-Gnosis those pairs use ViaETH; on Gnosis they use ViaToken(sDAI).\n * Pure ERC20↔ERC20 uses `buildBestSwap`.\n */\nfunction involvesNativeOrWnative(\n tokenIn: string,\n tokenOut: string,\n wnative: Address,\n): boolean {\n return (\n isAddressEqual(tokenIn as Address, zeroAddress)\n || isAddressEqual(tokenOut as Address, zeroAddress)\n || isAddressEqual(tokenIn as Address, wnative)\n || isAddressEqual(tokenOut as Address, wnative)\n );\n}\n\n/**\n * Lens SDK client: quotes best routes via the unified on-chain LensQuoter\n * and builds ready-to-send txs for official DEX routers.\n */\nexport class Lens {\n private client: PublicClient;\n private chainId: ChainId;\n private lensQuoter: Address;\n private wnative: Address;\n private quoterAbi: Abi;\n\n /**\n * @param client - viem public client for the target chain\n * @param chainId - chain id (`1` | `10` | `100` | `8453`)\n * @param lensQuoter - optional LensQuoter address; defaults to `CHAINS[chainId].lensQuoter`\n */\n constructor(client: PublicClient, chainId: ChainId, lensQuoter?: Address) {\n this.client = client;\n this.chainId = chainId;\n const chain = CHAINS[chainId];\n this.lensQuoter = lensQuoter ?? (chain.lensQuoter as Address);\n this.wnative = chain.wnative as Address;\n this.quoterAbi = quoterAbi;\n }\n\n /**\n * Builds calldata for a two-hop (or one-hop if mid is unused) route via a\n * mid token — used when the caller sets `intermediate`, or as the default\n * Gnosis path for native/WXDAI↔ERC20 (`mid = GNOSIS_sDAI`).\n */\n private async viaToken(\n params: {\n recipient: string;\n exactOut: boolean;\n tokenIn: string;\n tokenOut: string;\n amount: bigint;\n slippageBps: number;\n deadline: bigint;\n hookPoolFee: number;\n hookTickSpacing: number;\n hookAddress: string;\n mid: string;\n },\n ): Promise<BestSwapResult> {\n return this.client.readContract({\n address: this.lensQuoter,\n abi: this.quoterAbi,\n functionName: \"buildBestSwapViaToken\",\n args: [\n params.recipient as Address,\n params.exactOut,\n params.tokenIn as Address,\n params.tokenOut as Address,\n params.amount,\n BigInt(params.slippageBps),\n params.deadline,\n params.hookPoolFee,\n params.hookTickSpacing,\n params.hookAddress as Address,\n params.mid as Address,\n ],\n }) as Promise<BestSwapResult>;\n }\n\n /**\n * Finds a swap route via LensQuoter and returns amounts plus ready-to-send\n * calldata for an official DEX router.\n *\n * Route selection by request shape:\n * - `intermediate` set → `buildBestSwapViaToken(mid)` only\n * - native / WNATIVE ↔ ERC20 → on Gnosis, `ViaToken(sDAI)` directly;\n * elsewhere `buildBestSwapViaETH`\n * - ERC20 ↔ ERC20 → `buildBestSwap` only (optional V4 hooked candidate)\n */\n async quote(params: QuoteParams): Promise<QuoteResult> {\n const {\n tokenIn, tokenOut, amount, recipient,\n slippageBps = DEFAULT_SLIPPAGE, deadline = defaultDeadline(),\n exactOut = false,\n hookPoolFee = 0, hookTickSpacing = 0, hookAddress = zeroAddress,\n intermediate,\n } = params;\n\n const routeArgs = {\n recipient, exactOut, tokenIn, tokenOut, amount,\n slippageBps, deadline, hookPoolFee, hookTickSpacing, hookAddress,\n };\n\n // Caller forced a mid token: skip native/ERC20 cascade.\n if (intermediate) {\n return mapViaResult(await this.viaToken({ ...routeArgs, mid: intermediate }));\n }\n\n const useViaEth = involvesNativeOrWnative(tokenIn, tokenOut, this.wnative);\n\n if (useViaEth) {\n // Gnosis (Seer): outcomes trade vs sDAI — skip ViaETH entirely.\n if (this.chainId === 100) {\n return mapViaResult(await this.viaToken({ ...routeArgs, mid: GNOSIS_sDAI }));\n }\n\n // Other chains: single-hop ViaETH (router wrap/unwrap).\n const value = await this.client.readContract({\n address: this.lensQuoter,\n abi: this.quoterAbi,\n functionName: \"buildBestSwapViaETH\",\n args: [\n recipient as Address, exactOut,\n tokenIn as Address, tokenOut as Address,\n amount, BigInt(slippageBps), deadline,\n hookPoolFee, hookTickSpacing, hookAddress as Address,\n ],\n }) as BestSwapResult;\n return mapViaResult(value);\n }\n\n // Pure ERC20 ↔ ERC20: direct single-hop only (optional V4 hooked candidate).\n const [best, target, callData, _amountLimit, msgValue] = await this.client.readContract({\n address: this.lensQuoter,\n abi: this.quoterAbi,\n functionName: \"buildBestSwap\",\n args: [\n recipient as Address, exactOut,\n tokenIn as Address, tokenOut as Address,\n amount, BigInt(slippageBps), deadline,\n hookPoolFee, hookTickSpacing, hookAddress as Address,\n ],\n }) as BuildBestSwapResult;\n\n return {\n amountIn: best.amountIn,\n amountOut: best.amountOut,\n target: target as string,\n data: callData as string,\n msgValue: msgValue ?? 0n,\n isTwoHop: false,\n sourceA: ammName(best.source),\n sourceB: null,\n };\n }\n}\n","// Lens SDK — Core constants, ABIs, types, token list (zero dependencies)\n\n// ---- Chain config ----\n\n/** Unified AMM enum (same ordinals on every chain). */\nexport enum AMM {\n UNI_V3 = 0,\n UNI_V4 = 1,\n UNI_V4_HOOKED = 2,\n SWAPR_V3 = 3,\n}\n\nexport const AMM_NAMES: Record<AMM, string> = {\n [AMM.UNI_V3]: \"Uniswap V3\",\n [AMM.UNI_V4]: \"Uniswap V4\",\n [AMM.UNI_V4_HOOKED]: \"Uniswap V4 Hooked\",\n [AMM.SWAPR_V3]: \"Swapr V3\",\n};\n\n/** Shared LensQuoter ABI for the unified contract on all chains. */\nexport const LENS_QUOTER_ABI = [\n \"function buildBestSwapViaETH(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) a, tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) b, address target, bytes data, uint256 msgValue)\",\n \"function buildBestSwapViaToken(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress, address mid) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) a, tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) b, address target, bytes data, uint256 msgValue)\",\n \"function buildBestSwap(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) best, address target, bytes callData, uint256 amountLimit, uint256 msgValue)\",\n \"function V3_SWAP_ROUTER() view returns (address)\",\n \"function V4_ROUTER() view returns (address)\",\n \"function SWAPR_SWAP_ROUTER() view returns (address)\",\n \"function V4_QUOTER() view returns (address)\",\n] as const;\n\n/** Per-chain LensQuoter, official DEX routers, and wrapped native. */\nexport const CHAINS = {\n 1: {\n lensQuoter: \"0xaCE2C61E250c2d019b9Ed7FEf6A441022D6Ec7d2\",\n v3SwapRouter: \"0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2\",\n },\n 10: {\n lensQuoter: \"0x591aF101cAf2b7351C74c25F5E64bC2E062D2843\",\n v3SwapRouter: \"0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0x4200000000000000000000000000000000000006\",\n },\n 100: {\n lensQuoter: \"0xE8E3d230C1c17C7249126036F170027a664d1FbA\",\n v3SwapRouter: \"0x0000000000000000000000000000000000000000\",\n v4Router: \"0x0000000000000000000000000000000000000000\",\n swaprSwapRouter: \"0xfFB643E73f280B97809A8b41f7232AB401a04ee1\",\n wnative: \"0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d\",\n },\n 8453: {\n lensQuoter: \"0x591aF101cAf2b7351C74c25F5E64bC2E062D2843\",\n v3SwapRouter: \"0x2626664c2603336E57B271c5C0b26F421741e481\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0x4200000000000000000000000000000000000006\",\n },\n} as const;\n\nexport type ChainId = keyof typeof CHAINS;\n\nexport const ETH = \"0x0000000000000000000000000000000000000000\" as const;\n\n/** Gnosis sDAI — default ViaToken mid for native/WXDAI↔ERC20 on Gnosis (Seer). */\nexport const GNOSIS_sDAI = \"0xaf204776c7245bF4147c2612BF6e5972Ee483701\" as const;\n\n// ---- Types ----\n\nexport interface QuoteResult {\n amountIn: bigint;\n amountOut: bigint;\n /** Official DEX router to call. */\n target: string;\n /** Ready-to-send calldata for `target`. */\n data: string;\n msgValue: bigint;\n isTwoHop: boolean;\n sourceA: string;\n sourceB: string | null;\n}\n\nexport interface BaseQuoteParams {\n tokenIn: string;\n tokenOut: string;\n amount: bigint;\n}\n\nexport interface QuoteParams extends BaseQuoteParams {\n recipient: string;\n slippageBps?: number;\n deadline?: bigint;\n /** false = exactIn (default); true = exactOut. `amount` is amountIn or desired amountOut accordingly. */\n exactOut?: boolean;\n /** Optional V4 hooked-pool candidate (exactIn/exactOut; can beat the primary route). */\n hookPoolFee?: number;\n hookTickSpacing?: number;\n hookAddress?: string;\n /**\n * Intermediate token for two-hop routes (`buildBestSwapViaToken`).\n * When set, skips the native/ERC20 cascade. On Gnosis, native/WXDAI↔ERC20\n * already defaults to ViaToken(sDAI); use this only to force a different mid.\n */\n intermediate?: string;\n}\n\n// ---- Helpers ----\n\nexport function defaultDeadline(): bigint {\n return BigInt(Math.trunc(Date.now() / 1000) + 300);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,kBAAsD;;;ACG/C,IAAK,MAAL,kBAAKA,SAAL;AACL,EAAAA,UAAA,YAAS,KAAT;AACA,EAAAA,UAAA,YAAS,KAAT;AACA,EAAAA,UAAA,mBAAgB,KAAhB;AACA,EAAAA,UAAA,cAAW,KAAX;AAJU,SAAAA;AAAA,GAAA;AAOL,IAAM,YAAiC;AAAA,EAC5C,CAAC,cAAU,GAAG;AAAA,EACd,CAAC,cAAU,GAAG;AAAA,EACd,CAAC,qBAAiB,GAAG;AAAA,EACrB,CAAC,gBAAY,GAAG;AAClB;AAGO,IAAM,kBAAkB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAGO,IAAM,SAAS;AAAA,EACpB,GAAG;AAAA,IACD,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AAAA,EACA,IAAI;AAAA,IACF,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AAAA,EACA,KAAK;AAAA,IACH,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AAAA,EACA,MAAM;AAAA,IACJ,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;A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;AAAA,QAC3C,SAAS,KAAK;AAAA,QACd,KAAK,KAAK;AAAA,QACV,cAAc;AAAA,QACd,MAAM;AAAA,UACJ;AAAA,UAAsB;AAAA,UACtB;AAAA,UAAoB;AAAA,UACpB;AAAA,UAAQ,OAAO,WAAW;AAAA,UAAG;AAAA,UAC7B;AAAA,UAAa;AAAA,UAAiB;AAAA,QAChC;AAAA,MACF,CAAC;AACD,aAAO,aAAa,KAAK;AAAA,IAC3B;AAGA,UAAM,CAAC,MAAM,QAAQ,UAAU,cAAc,QAAQ,IAAI,MAAM,KAAK,OAAO,aAAa;AAAA,MACtF,SAAS,KAAK;AAAA,MACd,KAAK,KAAK;AAAA,MACV,cAAc;AAAA,MACd,MAAM;AAAA,QACJ;AAAA,QAAsB;AAAA,QACtB;AAAA,QAAoB;AAAA,QACpB;AAAA,QAAQ,OAAO,WAAW;AAAA,QAAG;AAAA,QAC7B;AAAA,QAAa;AAAA,QAAiB;AAAA,MAChC;AAAA,IACF,CAAC;AAED,WAAO;AAAA,MACL,UAAU,KAAK;AAAA,MACf,WAAW,KAAK;AAAA,MAChB;AAAA,MACA,MAAM;AAAA,MACN,UAAU,YAAY;AAAA,MACtB,UAAU;AAAA,MACV,SAAS,QAAQ,KAAK,MAAM;AAAA,MAC5B,SAAS;AAAA,IACX;AAAA,EACF;AACF;","names":["AMM"]}
|
|
1
|
+
{"version":3,"sources":["../src/client.ts","../src/errors.ts","../src/index.ts","../src/gasGuard.ts"],"sourcesContent":["// Lens SDK — client\n\nimport { parseAbi, isAddressEqual, zeroAddress } from \"viem\";\nimport type { PublicClient, Address, Abi } from \"viem\";\nimport {\n CHAINS, GNOSIS_sDAI,\n LENS_QUOTER_ABI,\n AMM_NAMES,\n defaultDeadline,\n type ChainId,\n type QuoteParams,\n type QuoteResult,\n} from \"./index.js\";\nimport { assertSwapExecutable } from \"./gasGuard.js\";\nimport { InsufficientLiquidityError } from \"./errors.js\";\n\n// viem's parseAbi (abitype) doesn't support inline `tuple(...)` syntax,\n// so rewrite the shared quoter ABI strings to use a named struct.\nconst QUOTE_TUPLE = \"tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut)\";\nconst QUOTE_STRUCT = \"struct Quote { uint8 source; uint256 feeBps; uint256 amountIn; uint256 amountOut; }\";\n\n/** Rewrites inline `tuple(...)` quoter ABI lines to a named `Quote` struct for `parseAbi`. */\nconst toQuoterAbi = (lines: readonly string[]): Abi =>\n parseAbi([QUOTE_STRUCT, ...lines.map((l) => l.split(QUOTE_TUPLE).join(\"Quote\"))]);\n\nconst quoterAbi = toQuoterAbi(LENS_QUOTER_ABI);\n\nexport {\n ETH, GNOSIS_sDAI, AMM, AMM_NAMES,\n CHAINS,\n LENS_QUOTER_ABI, defaultDeadline,\n InsufficientLiquidityError,\n} from \"./index.js\";\nexport type { QuoteResult, BaseQuoteParams, QuoteParams, ChainId } from \"./index.js\";\nexport { assertSwapExecutable } from \"./gasGuard.js\";\n\nconst DEFAULT_SLIPPAGE = 50; // 0.5%\n\ntype QuoteStruct = { source: number; feeBps: bigint; amountIn: bigint; amountOut: bigint };\ntype BestSwapResult = readonly [\n QuoteStruct,\n QuoteStruct,\n Address,\n `0x${string}`,\n bigint,\n];\ntype BuildBestSwapResult = readonly [QuoteStruct, Address, `0x${string}`, bigint, bigint];\n\n/** Resolves a human-readable AMM name from an on-chain `source` id. */\nfunction ammName(source: number): string {\n return (AMM_NAMES as Record<number, string>)[source] || `AMM #${source}`;\n}\n\n/**\n * Maps a `buildBestSwapViaETH` / `buildBestSwapViaToken` return value to `QuoteResult`.\n * Hop B is zeroed for single-hop; `b.amountOut > 0` means a real two-hop\n * route, so final `amountOut` comes from hop B.\n */\nfunction mapViaResult(value: BestSwapResult): QuoteResult {\n const [a, b, target, data, msgValue] = value;\n const isTwoHop = b.amountOut > 0n;\n return {\n amountIn: a.amountIn,\n amountOut: isTwoHop ? b.amountOut : a.amountOut,\n target: target as string,\n data: data as string,\n msgValue: msgValue ?? 0n,\n isTwoHop,\n sourceA: ammName(a.source),\n sourceB: isTwoHop ? ammName(b.source) : null,\n };\n}\n\n/**\n * True when either endpoint is native (`address(0)`) or the chain's wrapped native.\n * On non-Gnosis those pairs use ViaETH; on Gnosis they use ViaToken(sDAI).\n * Pure ERC20↔ERC20 uses `buildBestSwap`.\n */\nfunction involvesNativeOrWnative(\n tokenIn: string,\n tokenOut: string,\n wnative: Address,\n): boolean {\n return (\n isAddressEqual(tokenIn as Address, zeroAddress)\n || isAddressEqual(tokenOut as Address, zeroAddress)\n || isAddressEqual(tokenIn as Address, wnative)\n || isAddressEqual(tokenOut as Address, wnative)\n );\n}\n\n/**\n * Lens SDK client: quotes best routes via the unified on-chain LensQuoter\n * and builds ready-to-send txs for official DEX routers.\n */\nexport class Lens {\n private client: PublicClient;\n private chainId: ChainId;\n private lensQuoter: Address;\n private wnative: Address;\n private quoterAbi: Abi;\n\n /**\n * @param client - viem public client for the target chain\n * @param chainId - chain id (`1` | `10` | `100` | `8453`)\n * @param lensQuoter - optional LensQuoter address; defaults to `CHAINS[chainId].lensQuoter`\n */\n constructor(client: PublicClient, chainId: ChainId, lensQuoter?: Address) {\n this.client = client;\n this.chainId = chainId;\n const chain = CHAINS[chainId];\n this.lensQuoter = lensQuoter ?? (chain.lensQuoter as Address);\n this.wnative = chain.wnative as Address;\n this.quoterAbi = quoterAbi;\n }\n\n /**\n * Builds calldata for a two-hop (or one-hop if mid is unused) route via a\n * mid token — used when the caller sets `intermediate`, or as the default\n * Gnosis path for native/WXDAI↔ERC20 (`mid = GNOSIS_sDAI`).\n */\n private async viaToken(\n params: {\n recipient: string;\n exactOut: boolean;\n tokenIn: string;\n tokenOut: string;\n amount: bigint;\n slippageBps: number;\n deadline: bigint;\n hookPoolFee: number;\n hookTickSpacing: number;\n hookAddress: string;\n mid: string;\n },\n ): Promise<BestSwapResult> {\n return this.client.readContract({\n address: this.lensQuoter,\n abi: this.quoterAbi,\n functionName: \"buildBestSwapViaToken\",\n args: [\n params.recipient as Address,\n params.exactOut,\n params.tokenIn as Address,\n params.tokenOut as Address,\n params.amount,\n BigInt(params.slippageBps),\n params.deadline,\n params.hookPoolFee,\n params.hookTickSpacing,\n params.hookAddress as Address,\n params.mid as Address,\n ],\n }) as Promise<BestSwapResult>;\n }\n\n /**\n * Finds a swap route via LensQuoter and returns amounts plus ready-to-send\n * calldata for an official DEX router.\n *\n * Route selection by request shape:\n * - `intermediate` set → `buildBestSwapViaToken(mid)` only\n * - native / WNATIVE ↔ ERC20 → on Gnosis, `ViaToken(sDAI)` directly;\n * elsewhere `buildBestSwapViaETH`\n * - ERC20 ↔ ERC20 → `buildBestSwap` only (optional V4 hooked candidate)\n *\n * After building calldata, simulates the swap with a gas cap of 90% of\n * `block.gasLimit`. Thin pools that quote but cannot fit in a block throw\n * {@link InsufficientLiquidityError}.\n */\n async quote(params: QuoteParams): Promise<QuoteResult> {\n const {\n tokenIn, tokenOut, amount, recipient,\n slippageBps = DEFAULT_SLIPPAGE, deadline = defaultDeadline(),\n exactOut = false,\n hookPoolFee = 0, hookTickSpacing = 0, hookAddress = zeroAddress,\n intermediate,\n } = params;\n\n const routeArgs = {\n recipient, exactOut, tokenIn, tokenOut, amount,\n slippageBps, deadline, hookPoolFee, hookTickSpacing, hookAddress,\n };\n\n let result: QuoteResult;\n\n // Caller forced a mid token: skip native/ERC20 cascade.\n if (intermediate) {\n result = mapViaResult(await this.viaToken({ ...routeArgs, mid: intermediate }));\n } else {\n const useViaEth = involvesNativeOrWnative(tokenIn, tokenOut, this.wnative);\n\n if (useViaEth) {\n // Gnosis (Seer): outcomes trade vs sDAI — skip ViaETH entirely.\n if (this.chainId === 100) {\n result = mapViaResult(await this.viaToken({ ...routeArgs, mid: GNOSIS_sDAI }));\n } else {\n // Other chains: single-hop ViaETH (router wrap/unwrap).\n const value = await this.client.readContract({\n address: this.lensQuoter,\n abi: this.quoterAbi,\n functionName: \"buildBestSwapViaETH\",\n args: [\n recipient as Address, exactOut,\n tokenIn as Address, tokenOut as Address,\n amount, BigInt(slippageBps), deadline,\n hookPoolFee, hookTickSpacing, hookAddress as Address,\n ],\n }) as BestSwapResult;\n result = mapViaResult(value);\n }\n } else {\n // Pure ERC20 ↔ ERC20: direct single-hop only (optional V4 hooked candidate).\n const [best, target, callData, _amountLimit, msgValue] = await this.client.readContract({\n address: this.lensQuoter,\n abi: this.quoterAbi,\n functionName: \"buildBestSwap\",\n args: [\n recipient as Address, exactOut,\n tokenIn as Address, tokenOut as Address,\n amount, BigInt(slippageBps), deadline,\n hookPoolFee, hookTickSpacing, hookAddress as Address,\n ],\n }) as BuildBestSwapResult;\n\n result = {\n amountIn: best.amountIn,\n amountOut: best.amountOut,\n target: target as string,\n data: callData as string,\n msgValue: msgValue ?? 0n,\n isTwoHop: false,\n sourceA: ammName(best.source),\n sourceB: null,\n };\n }\n }\n\n await assertSwapExecutable(this.client, {\n account: recipient as Address,\n tokenIn,\n amountIn: result.amountIn,\n result,\n });\n\n return result;\n }\n}\n","/**\n * Thrown when a route quotes successfully but the swap cannot execute within\n * the chain's block gas limit — typically a thin concentrated-liquidity pool\n * that would cross too many ticks for the requested size.\n */\nexport class InsufficientLiquidityError extends Error {\n readonly code = \"INSUFFICIENT_LIQUIDITY\" as const;\n\n constructor(\n message = \"Swap too large for available liquidity (would exceed block gas limit). Try a smaller amount.\",\n ) {\n super(message);\n this.name = \"InsufficientLiquidityError\";\n }\n}\n","// Lens SDK — Core constants, ABIs, types, token list (zero dependencies)\n\n// ---- Chain config ----\n\n/** Unified AMM enum (same ordinals on every chain). */\nexport enum AMM {\n UNI_V3 = 0,\n UNI_V4 = 1,\n UNI_V4_HOOKED = 2,\n SWAPR_V3 = 3,\n}\n\nexport const AMM_NAMES: Record<AMM, string> = {\n [AMM.UNI_V3]: \"Uniswap V3\",\n [AMM.UNI_V4]: \"Uniswap V4\",\n [AMM.UNI_V4_HOOKED]: \"Uniswap V4 Hooked\",\n [AMM.SWAPR_V3]: \"Swapr V3\",\n};\n\n/** Shared LensQuoter ABI for the unified contract on all chains. */\nexport const LENS_QUOTER_ABI = [\n \"function buildBestSwapViaETH(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) a, tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) b, address target, bytes data, uint256 msgValue)\",\n \"function buildBestSwapViaToken(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress, address mid) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) a, tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) b, address target, bytes data, uint256 msgValue)\",\n \"function buildBestSwap(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) best, address target, bytes callData, uint256 amountLimit, uint256 msgValue)\",\n \"function V3_SWAP_ROUTER() view returns (address)\",\n \"function V4_ROUTER() view returns (address)\",\n \"function SWAPR_SWAP_ROUTER() view returns (address)\",\n \"function V4_QUOTER() view returns (address)\",\n] as const;\n\n/** Per-chain LensQuoter, official DEX routers, and wrapped native. */\nexport const CHAINS = {\n 1: {\n lensQuoter: \"0xaCE2C61E250c2d019b9Ed7FEf6A441022D6Ec7d2\",\n v3SwapRouter: \"0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2\",\n },\n 10: {\n lensQuoter: \"0x591aF101cAf2b7351C74c25F5E64bC2E062D2843\",\n v3SwapRouter: \"0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0x4200000000000000000000000000000000000006\",\n },\n 100: {\n lensQuoter: \"0xE8E3d230C1c17C7249126036F170027a664d1FbA\",\n v3SwapRouter: \"0x0000000000000000000000000000000000000000\",\n v4Router: \"0x0000000000000000000000000000000000000000\",\n swaprSwapRouter: \"0xfFB643E73f280B97809A8b41f7232AB401a04ee1\",\n wnative: \"0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d\",\n },\n 8453: {\n lensQuoter: \"0x591aF101cAf2b7351C74c25F5E64bC2E062D2843\",\n v3SwapRouter: \"0x2626664c2603336E57B271c5C0b26F421741e481\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0x4200000000000000000000000000000000000006\",\n },\n} as const;\n\nexport type ChainId = keyof typeof CHAINS;\n\nexport const ETH = \"0x0000000000000000000000000000000000000000\" as const;\n\n/** Gnosis sDAI — default ViaToken mid for native/WXDAI↔ERC20 on Gnosis (Seer). */\nexport const GNOSIS_sDAI = \"0xaf204776c7245bF4147c2612BF6e5972Ee483701\" as const;\n\n// ---- Types ----\n\nexport interface QuoteResult {\n amountIn: bigint;\n amountOut: bigint;\n /** Official DEX router to call. */\n target: string;\n /** Ready-to-send calldata for `target`. */\n data: string;\n msgValue: bigint;\n isTwoHop: boolean;\n sourceA: string;\n sourceB: string | null;\n}\n\nexport interface BaseQuoteParams {\n tokenIn: string;\n tokenOut: string;\n amount: bigint;\n}\n\nexport interface QuoteParams extends BaseQuoteParams {\n recipient: string;\n slippageBps?: number;\n deadline?: bigint;\n /** false = exactIn (default); true = exactOut. `amount` is amountIn or desired amountOut accordingly. */\n exactOut?: boolean;\n /** Optional V4 hooked-pool candidate (exactIn/exactOut; can beat the primary route). */\n hookPoolFee?: number;\n hookTickSpacing?: number;\n hookAddress?: string;\n /**\n * Intermediate token for two-hop routes (`buildBestSwapViaToken`).\n * When set, skips the native/ERC20 cascade. On Gnosis, native/WXDAI↔ERC20\n * already defaults to ViaToken(sDAI); use this only to force a different mid.\n */\n intermediate?: string;\n}\n\n// ---- Errors ----\n\nexport { InsufficientLiquidityError } from \"./errors.js\";\n\n// ---- Helpers ----\n\nexport function defaultDeadline(): bigint {\n return BigInt(Math.trunc(Date.now() / 1000) + 300);\n}\n","// Post-quote gas executability check (block gas limit / thin pools)\n\nimport {\n encodeAbiParameters,\n isAddressEqual,\n keccak256,\n maxUint256,\n numberToHex,\n zeroAddress,\n type Address,\n type Hex,\n type PublicClient,\n type StateOverride,\n} from \"viem\";\nimport { InsufficientLiquidityError } from \"./errors.js\";\nimport type { QuoteResult } from \"./index.js\";\n\n/** Fraction of block.gasLimit the swap is allowed to consume (90%). */\nexport const GAS_LIMIT_BPS = 9000n;\n\n/**\n * OpenZeppelin ERC20 layout: `balanceOf` at mapping slot 0, `allowance` at 1.\n * Covers sDAI and most Seer collateral / outcome tokens.\n */\nexport function erc20BalanceSlot(owner: Address): Hex {\n return keccak256(\n encodeAbiParameters(\n [{ type: \"address\" }, { type: \"uint256\" }],\n [owner, 0n],\n ),\n );\n}\n\nexport function erc20AllowanceSlot(owner: Address, spender: Address): Hex {\n const inner = keccak256(\n encodeAbiParameters(\n [{ type: \"address\" }, { type: \"uint256\" }],\n [owner, 1n],\n ),\n );\n return keccak256(\n encodeAbiParameters(\n [{ type: \"address\" }, { type: \"bytes32\" }],\n [spender, inner],\n ),\n );\n}\n\n/** stateDiff so `owner` can pay `spender` at least `amount` without a prior approve. */\nexport function erc20SpendOverride(\n token: Address,\n owner: Address,\n spender: Address,\n amount: bigint,\n): StateOverride[number] {\n const bal = amount > 0n ? amount : 1n;\n return {\n address: token,\n stateDiff: [\n { slot: erc20BalanceSlot(owner), value: numberToHex(bal, { size: 32 }) },\n {\n slot: erc20AllowanceSlot(owner, spender),\n value: numberToHex(maxUint256, { size: 32 }),\n },\n ],\n };\n}\n\nfunction isNative(token: string): boolean {\n return isAddressEqual(token as Address, zeroAddress);\n}\n\n/**\n * Builds state overrides so the router sim does not depend on a prior approve.\n * Native `tokenIn` only needs a funded account for `msg.value` + gas.\n */\nexport function buildSwapStateOverride(params: {\n account: Address;\n tokenIn: string;\n amountIn: bigint;\n target: Address;\n msgValue: bigint;\n}): StateOverride {\n const { account, tokenIn, amountIn, target, msgValue } = params;\n const overrides: StateOverride = [\n {\n address: account,\n balance: msgValue + 10n ** 18n,\n },\n ];\n if (!isNative(tokenIn)) {\n overrides.push(\n erc20SpendOverride(tokenIn as Address, account, target, amountIn),\n );\n }\n return overrides;\n}\n\nfunction maxGasForBlock(gasLimit: bigint): bigint {\n return (gasLimit * GAS_LIMIT_BPS) / 10000n;\n}\n\n/**\n * After LensQuoter builds calldata, verify the swap fits in a block.\n *\n * 1. `eth_call` with ERC20 allowance/balance overrides (no prior approve needed).\n * 2. If that succeeds, repeat with `gas = 90% of block.gasLimit`.\n * 3. Unlimited OK + capped fail → {@link InsufficientLiquidityError}.\n * 4. If the unlimited sim fails (non-standard token storage, flaky RPC), skip\n * quietly — never false-positive as liquidity.\n */\nexport async function assertSwapExecutable(\n client: PublicClient,\n params: {\n account: Address;\n tokenIn: string;\n amountIn: bigint;\n result: QuoteResult;\n },\n): Promise<void> {\n const { account, tokenIn, amountIn, result } = params;\n const target = result.target as Address;\n const data = result.data as Hex;\n\n let block: { gasLimit: bigint };\n try {\n block = await client.getBlock({ blockTag: \"latest\" });\n } catch {\n return;\n }\n const maxGas = maxGasForBlock(block.gasLimit);\n if (maxGas === 0n) return;\n\n const stateOverride = buildSwapStateOverride({\n account,\n tokenIn,\n amountIn,\n target,\n msgValue: result.msgValue,\n });\n\n const base = {\n account,\n to: target,\n data,\n value: result.msgValue,\n stateOverride,\n } as const;\n\n try {\n await client.call(base);\n } catch {\n return;\n }\n\n try {\n await client.call({ ...base, gas: maxGas });\n } catch {\n throw new InsufficientLiquidityError();\n }\n\n try {\n const gas = await client.estimateGas(base);\n if (gas > maxGas) throw new InsufficientLiquidityError();\n } catch (e) {\n if (e instanceof InsufficientLiquidityError) throw e;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,IAAAA,eAAsD;;;ACG/C,IAAM,6BAAN,cAAyC,MAAM;AAAA,EAC3C,OAAO;AAAA,EAEhB,YACE,UAAU,gGACV;AACA,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;;;ACTO,IAAK,MAAL,kBAAKC,SAAL;AACL,EAAAA,UAAA,YAAS,KAAT;AACA,EAAAA,UAAA,YAAS,KAAT;AACA,EAAAA,UAAA,mBAAgB,KAAhB;AACA,EAAAA,UAAA,cAAW,KAAX;AAJU,SAAAA;AAAA,GAAA;AAOL,IAAM,YAAiC;AAAA,EAC5C,CAAC,cAAU,GAAG;AAAA,EACd,CAAC,cAAU,GAAG;AAAA,EACd,CAAC,qBAAiB,GAAG;AAAA,EACrB,CAAC,gBAAY,GAAG;AAClB;AAGO,IAAM,kBAAkB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAGO,IAAM,SAAS;AAAA,EACpB,GAAG;AAAA,IACD,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AAAA,EACA,IAAI;AAAA,IACF,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AAAA,EACA,KAAK;AAAA,IACH,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAA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Ac;AAAA,YACd,MAAM;AAAA,cACJ;AAAA,cAAsB;AAAA,cACtB;AAAA,cAAoB;AAAA,cACpB;AAAA,cAAQ,OAAO,WAAW;AAAA,cAAG;AAAA,cAC7B;AAAA,cAAa;AAAA,cAAiB;AAAA,YAChC;AAAA,UACF,CAAC;AACD,mBAAS,aAAa,KAAK;AAAA,QAC7B;AAAA,MACF,OAAO;AAEL,cAAM,CAAC,MAAM,QAAQ,UAAU,cAAc,QAAQ,IAAI,MAAM,KAAK,OAAO,aAAa;AAAA,UACtF,SAAS,KAAK;AAAA,UACd,KAAK,KAAK;AAAA,UACV,cAAc;AAAA,UACd,MAAM;AAAA,YACJ;AAAA,YAAsB;AAAA,YACtB;AAAA,YAAoB;AAAA,YACpB;AAAA,YAAQ,OAAO,WAAW;AAAA,YAAG;AAAA,YAC7B;AAAA,YAAa;AAAA,YAAiB;AAAA,UAChC;AAAA,QACF,CAAC;AAED,iBAAS;AAAA,UACP,UAAU,KAAK;AAAA,UACf,WAAW,KAAK;AAAA,UAChB;AAAA,UACA,MAAM;AAAA,UACN,UAAU,YAAY;AAAA,UACtB,UAAU;AAAA,UACV,SAAS,QAAQ,KAAK,MAAM;AAAA,UAC5B,SAAS;AAAA,QACX;AAAA,MACF;AAAA,IACF;AAEA,UAAM,qBAAqB,KAAK,QAAQ;AAAA,MACtC,SAAS;AAAA,MACT;AAAA,MACA,UAAU,OAAO;AAAA,MACjB;AAAA,IACF,CAAC;AAED,WAAO;AAAA,EACT;AACF;","names":["import_viem","AMM"]}
|
package/dist/client.d.cts
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
import { PublicClient, Address } from 'viem';
|
|
2
|
-
import { ChainId, QuoteParams
|
|
3
|
-
export { AMM, AMM_NAMES, BaseQuoteParams, CHAINS, ETH, GNOSIS_sDAI, LENS_QUOTER_ABI, defaultDeadline } from './index.cjs';
|
|
2
|
+
import { QuoteResult, ChainId, QuoteParams } from './index.cjs';
|
|
3
|
+
export { AMM, AMM_NAMES, BaseQuoteParams, CHAINS, ETH, GNOSIS_sDAI, InsufficientLiquidityError, LENS_QUOTER_ABI, defaultDeadline } from './index.cjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* After LensQuoter builds calldata, verify the swap fits in a block.
|
|
7
|
+
*
|
|
8
|
+
* 1. `eth_call` with ERC20 allowance/balance overrides (no prior approve needed).
|
|
9
|
+
* 2. If that succeeds, repeat with `gas = 90% of block.gasLimit`.
|
|
10
|
+
* 3. Unlimited OK + capped fail → {@link InsufficientLiquidityError}.
|
|
11
|
+
* 4. If the unlimited sim fails (non-standard token storage, flaky RPC), skip
|
|
12
|
+
* quietly — never false-positive as liquidity.
|
|
13
|
+
*/
|
|
14
|
+
declare function assertSwapExecutable(client: PublicClient, params: {
|
|
15
|
+
account: Address;
|
|
16
|
+
tokenIn: string;
|
|
17
|
+
amountIn: bigint;
|
|
18
|
+
result: QuoteResult;
|
|
19
|
+
}): Promise<void>;
|
|
4
20
|
|
|
5
21
|
/**
|
|
6
22
|
* Lens SDK client: quotes best routes via the unified on-chain LensQuoter
|
|
@@ -33,8 +49,12 @@ declare class Lens {
|
|
|
33
49
|
* - native / WNATIVE ↔ ERC20 → on Gnosis, `ViaToken(sDAI)` directly;
|
|
34
50
|
* elsewhere `buildBestSwapViaETH`
|
|
35
51
|
* - ERC20 ↔ ERC20 → `buildBestSwap` only (optional V4 hooked candidate)
|
|
52
|
+
*
|
|
53
|
+
* After building calldata, simulates the swap with a gas cap of 90% of
|
|
54
|
+
* `block.gasLimit`. Thin pools that quote but cannot fit in a block throw
|
|
55
|
+
* {@link InsufficientLiquidityError}.
|
|
36
56
|
*/
|
|
37
57
|
quote(params: QuoteParams): Promise<QuoteResult>;
|
|
38
58
|
}
|
|
39
59
|
|
|
40
|
-
export { ChainId, Lens, QuoteParams, QuoteResult };
|
|
60
|
+
export { ChainId, Lens, QuoteParams, QuoteResult, assertSwapExecutable };
|
package/dist/client.d.ts
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
import { PublicClient, Address } from 'viem';
|
|
2
|
-
import { ChainId, QuoteParams
|
|
3
|
-
export { AMM, AMM_NAMES, BaseQuoteParams, CHAINS, ETH, GNOSIS_sDAI, LENS_QUOTER_ABI, defaultDeadline } from './index.js';
|
|
2
|
+
import { QuoteResult, ChainId, QuoteParams } from './index.js';
|
|
3
|
+
export { AMM, AMM_NAMES, BaseQuoteParams, CHAINS, ETH, GNOSIS_sDAI, InsufficientLiquidityError, LENS_QUOTER_ABI, defaultDeadline } from './index.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* After LensQuoter builds calldata, verify the swap fits in a block.
|
|
7
|
+
*
|
|
8
|
+
* 1. `eth_call` with ERC20 allowance/balance overrides (no prior approve needed).
|
|
9
|
+
* 2. If that succeeds, repeat with `gas = 90% of block.gasLimit`.
|
|
10
|
+
* 3. Unlimited OK + capped fail → {@link InsufficientLiquidityError}.
|
|
11
|
+
* 4. If the unlimited sim fails (non-standard token storage, flaky RPC), skip
|
|
12
|
+
* quietly — never false-positive as liquidity.
|
|
13
|
+
*/
|
|
14
|
+
declare function assertSwapExecutable(client: PublicClient, params: {
|
|
15
|
+
account: Address;
|
|
16
|
+
tokenIn: string;
|
|
17
|
+
amountIn: bigint;
|
|
18
|
+
result: QuoteResult;
|
|
19
|
+
}): Promise<void>;
|
|
4
20
|
|
|
5
21
|
/**
|
|
6
22
|
* Lens SDK client: quotes best routes via the unified on-chain LensQuoter
|
|
@@ -33,8 +49,12 @@ declare class Lens {
|
|
|
33
49
|
* - native / WNATIVE ↔ ERC20 → on Gnosis, `ViaToken(sDAI)` directly;
|
|
34
50
|
* elsewhere `buildBestSwapViaETH`
|
|
35
51
|
* - ERC20 ↔ ERC20 → `buildBestSwap` only (optional V4 hooked candidate)
|
|
52
|
+
*
|
|
53
|
+
* After building calldata, simulates the swap with a gas cap of 90% of
|
|
54
|
+
* `block.gasLimit`. Thin pools that quote but cannot fit in a block throw
|
|
55
|
+
* {@link InsufficientLiquidityError}.
|
|
36
56
|
*/
|
|
37
57
|
quote(params: QuoteParams): Promise<QuoteResult>;
|
|
38
58
|
}
|
|
39
59
|
|
|
40
|
-
export { ChainId, Lens, QuoteParams, QuoteResult };
|
|
60
|
+
export { ChainId, Lens, QuoteParams, QuoteResult, assertSwapExecutable };
|
package/dist/client.js
CHANGED
|
@@ -4,12 +4,125 @@ import {
|
|
|
4
4
|
CHAINS,
|
|
5
5
|
ETH,
|
|
6
6
|
GNOSIS_sDAI,
|
|
7
|
+
InsufficientLiquidityError,
|
|
7
8
|
LENS_QUOTER_ABI,
|
|
8
9
|
defaultDeadline
|
|
9
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-5UNFCMSF.js";
|
|
11
|
+
|
|
12
|
+
// src/client.ts
|
|
13
|
+
import { parseAbi, isAddressEqual as isAddressEqual2, zeroAddress as zeroAddress2 } from "viem";
|
|
14
|
+
|
|
15
|
+
// src/gasGuard.ts
|
|
16
|
+
import {
|
|
17
|
+
encodeAbiParameters,
|
|
18
|
+
isAddressEqual,
|
|
19
|
+
keccak256,
|
|
20
|
+
maxUint256,
|
|
21
|
+
numberToHex,
|
|
22
|
+
zeroAddress
|
|
23
|
+
} from "viem";
|
|
24
|
+
var GAS_LIMIT_BPS = 9000n;
|
|
25
|
+
function erc20BalanceSlot(owner) {
|
|
26
|
+
return keccak256(
|
|
27
|
+
encodeAbiParameters(
|
|
28
|
+
[{ type: "address" }, { type: "uint256" }],
|
|
29
|
+
[owner, 0n]
|
|
30
|
+
)
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
function erc20AllowanceSlot(owner, spender) {
|
|
34
|
+
const inner = keccak256(
|
|
35
|
+
encodeAbiParameters(
|
|
36
|
+
[{ type: "address" }, { type: "uint256" }],
|
|
37
|
+
[owner, 1n]
|
|
38
|
+
)
|
|
39
|
+
);
|
|
40
|
+
return keccak256(
|
|
41
|
+
encodeAbiParameters(
|
|
42
|
+
[{ type: "address" }, { type: "bytes32" }],
|
|
43
|
+
[spender, inner]
|
|
44
|
+
)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
function erc20SpendOverride(token, owner, spender, amount) {
|
|
48
|
+
const bal = amount > 0n ? amount : 1n;
|
|
49
|
+
return {
|
|
50
|
+
address: token,
|
|
51
|
+
stateDiff: [
|
|
52
|
+
{ slot: erc20BalanceSlot(owner), value: numberToHex(bal, { size: 32 }) },
|
|
53
|
+
{
|
|
54
|
+
slot: erc20AllowanceSlot(owner, spender),
|
|
55
|
+
value: numberToHex(maxUint256, { size: 32 })
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function isNative(token) {
|
|
61
|
+
return isAddressEqual(token, zeroAddress);
|
|
62
|
+
}
|
|
63
|
+
function buildSwapStateOverride(params) {
|
|
64
|
+
const { account, tokenIn, amountIn, target, msgValue } = params;
|
|
65
|
+
const overrides = [
|
|
66
|
+
{
|
|
67
|
+
address: account,
|
|
68
|
+
balance: msgValue + 10n ** 18n
|
|
69
|
+
}
|
|
70
|
+
];
|
|
71
|
+
if (!isNative(tokenIn)) {
|
|
72
|
+
overrides.push(
|
|
73
|
+
erc20SpendOverride(tokenIn, account, target, amountIn)
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
return overrides;
|
|
77
|
+
}
|
|
78
|
+
function maxGasForBlock(gasLimit) {
|
|
79
|
+
return gasLimit * GAS_LIMIT_BPS / 10000n;
|
|
80
|
+
}
|
|
81
|
+
async function assertSwapExecutable(client, params) {
|
|
82
|
+
const { account, tokenIn, amountIn, result } = params;
|
|
83
|
+
const target = result.target;
|
|
84
|
+
const data = result.data;
|
|
85
|
+
let block;
|
|
86
|
+
try {
|
|
87
|
+
block = await client.getBlock({ blockTag: "latest" });
|
|
88
|
+
} catch {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const maxGas = maxGasForBlock(block.gasLimit);
|
|
92
|
+
if (maxGas === 0n) return;
|
|
93
|
+
const stateOverride = buildSwapStateOverride({
|
|
94
|
+
account,
|
|
95
|
+
tokenIn,
|
|
96
|
+
amountIn,
|
|
97
|
+
target,
|
|
98
|
+
msgValue: result.msgValue
|
|
99
|
+
});
|
|
100
|
+
const base = {
|
|
101
|
+
account,
|
|
102
|
+
to: target,
|
|
103
|
+
data,
|
|
104
|
+
value: result.msgValue,
|
|
105
|
+
stateOverride
|
|
106
|
+
};
|
|
107
|
+
try {
|
|
108
|
+
await client.call(base);
|
|
109
|
+
} catch {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
await client.call({ ...base, gas: maxGas });
|
|
114
|
+
} catch {
|
|
115
|
+
throw new InsufficientLiquidityError();
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
const gas = await client.estimateGas(base);
|
|
119
|
+
if (gas > maxGas) throw new InsufficientLiquidityError();
|
|
120
|
+
} catch (e) {
|
|
121
|
+
if (e instanceof InsufficientLiquidityError) throw e;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
10
124
|
|
|
11
125
|
// src/client.ts
|
|
12
|
-
import { parseAbi, isAddressEqual, zeroAddress } from "viem";
|
|
13
126
|
var QUOTE_TUPLE = "tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut)";
|
|
14
127
|
var QUOTE_STRUCT = "struct Quote { uint8 source; uint256 feeBps; uint256 amountIn; uint256 amountOut; }";
|
|
15
128
|
var toQuoterAbi = (lines) => parseAbi([QUOTE_STRUCT, ...lines.map((l) => l.split(QUOTE_TUPLE).join("Quote"))]);
|
|
@@ -33,7 +146,7 @@ function mapViaResult(value) {
|
|
|
33
146
|
};
|
|
34
147
|
}
|
|
35
148
|
function involvesNativeOrWnative(tokenIn, tokenOut, wnative) {
|
|
36
|
-
return
|
|
149
|
+
return isAddressEqual2(tokenIn, zeroAddress2) || isAddressEqual2(tokenOut, zeroAddress2) || isAddressEqual2(tokenIn, wnative) || isAddressEqual2(tokenOut, wnative);
|
|
37
150
|
}
|
|
38
151
|
var Lens = class {
|
|
39
152
|
client;
|
|
@@ -88,6 +201,10 @@ var Lens = class {
|
|
|
88
201
|
* - native / WNATIVE ↔ ERC20 → on Gnosis, `ViaToken(sDAI)` directly;
|
|
89
202
|
* elsewhere `buildBestSwapViaETH`
|
|
90
203
|
* - ERC20 ↔ ERC20 → `buildBestSwap` only (optional V4 hooked candidate)
|
|
204
|
+
*
|
|
205
|
+
* After building calldata, simulates the swap with a gas cap of 90% of
|
|
206
|
+
* `block.gasLimit`. Thin pools that quote but cannot fit in a block throw
|
|
207
|
+
* {@link InsufficientLiquidityError}.
|
|
91
208
|
*/
|
|
92
209
|
async quote(params) {
|
|
93
210
|
const {
|
|
@@ -100,7 +217,7 @@ var Lens = class {
|
|
|
100
217
|
exactOut = false,
|
|
101
218
|
hookPoolFee = 0,
|
|
102
219
|
hookTickSpacing = 0,
|
|
103
|
-
hookAddress =
|
|
220
|
+
hookAddress = zeroAddress2,
|
|
104
221
|
intermediate
|
|
105
222
|
} = params;
|
|
106
223
|
const routeArgs = {
|
|
@@ -115,60 +232,71 @@ var Lens = class {
|
|
|
115
232
|
hookTickSpacing,
|
|
116
233
|
hookAddress
|
|
117
234
|
};
|
|
235
|
+
let result;
|
|
118
236
|
if (intermediate) {
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
237
|
+
result = mapViaResult(await this.viaToken({ ...routeArgs, mid: intermediate }));
|
|
238
|
+
} else {
|
|
239
|
+
const useViaEth = involvesNativeOrWnative(tokenIn, tokenOut, this.wnative);
|
|
240
|
+
if (useViaEth) {
|
|
241
|
+
if (this.chainId === 100) {
|
|
242
|
+
result = mapViaResult(await this.viaToken({ ...routeArgs, mid: GNOSIS_sDAI }));
|
|
243
|
+
} else {
|
|
244
|
+
const value = await this.client.readContract({
|
|
245
|
+
address: this.lensQuoter,
|
|
246
|
+
abi: this.quoterAbi,
|
|
247
|
+
functionName: "buildBestSwapViaETH",
|
|
248
|
+
args: [
|
|
249
|
+
recipient,
|
|
250
|
+
exactOut,
|
|
251
|
+
tokenIn,
|
|
252
|
+
tokenOut,
|
|
253
|
+
amount,
|
|
254
|
+
BigInt(slippageBps),
|
|
255
|
+
deadline,
|
|
256
|
+
hookPoolFee,
|
|
257
|
+
hookTickSpacing,
|
|
258
|
+
hookAddress
|
|
259
|
+
]
|
|
260
|
+
});
|
|
261
|
+
result = mapViaResult(value);
|
|
262
|
+
}
|
|
263
|
+
} else {
|
|
264
|
+
const [best, target, callData, _amountLimit, msgValue] = await this.client.readContract({
|
|
265
|
+
address: this.lensQuoter,
|
|
266
|
+
abi: this.quoterAbi,
|
|
267
|
+
functionName: "buildBestSwap",
|
|
268
|
+
args: [
|
|
269
|
+
recipient,
|
|
270
|
+
exactOut,
|
|
271
|
+
tokenIn,
|
|
272
|
+
tokenOut,
|
|
273
|
+
amount,
|
|
274
|
+
BigInt(slippageBps),
|
|
275
|
+
deadline,
|
|
276
|
+
hookPoolFee,
|
|
277
|
+
hookTickSpacing,
|
|
278
|
+
hookAddress
|
|
279
|
+
]
|
|
280
|
+
});
|
|
281
|
+
result = {
|
|
282
|
+
amountIn: best.amountIn,
|
|
283
|
+
amountOut: best.amountOut,
|
|
284
|
+
target,
|
|
285
|
+
data: callData,
|
|
286
|
+
msgValue: msgValue ?? 0n,
|
|
287
|
+
isTwoHop: false,
|
|
288
|
+
sourceA: ammName(best.source),
|
|
289
|
+
sourceB: null
|
|
290
|
+
};
|
|
125
291
|
}
|
|
126
|
-
const value = await this.client.readContract({
|
|
127
|
-
address: this.lensQuoter,
|
|
128
|
-
abi: this.quoterAbi,
|
|
129
|
-
functionName: "buildBestSwapViaETH",
|
|
130
|
-
args: [
|
|
131
|
-
recipient,
|
|
132
|
-
exactOut,
|
|
133
|
-
tokenIn,
|
|
134
|
-
tokenOut,
|
|
135
|
-
amount,
|
|
136
|
-
BigInt(slippageBps),
|
|
137
|
-
deadline,
|
|
138
|
-
hookPoolFee,
|
|
139
|
-
hookTickSpacing,
|
|
140
|
-
hookAddress
|
|
141
|
-
]
|
|
142
|
-
});
|
|
143
|
-
return mapViaResult(value);
|
|
144
292
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
recipient,
|
|
151
|
-
exactOut,
|
|
152
|
-
tokenIn,
|
|
153
|
-
tokenOut,
|
|
154
|
-
amount,
|
|
155
|
-
BigInt(slippageBps),
|
|
156
|
-
deadline,
|
|
157
|
-
hookPoolFee,
|
|
158
|
-
hookTickSpacing,
|
|
159
|
-
hookAddress
|
|
160
|
-
]
|
|
293
|
+
await assertSwapExecutable(this.client, {
|
|
294
|
+
account: recipient,
|
|
295
|
+
tokenIn,
|
|
296
|
+
amountIn: result.amountIn,
|
|
297
|
+
result
|
|
161
298
|
});
|
|
162
|
-
return
|
|
163
|
-
amountIn: best.amountIn,
|
|
164
|
-
amountOut: best.amountOut,
|
|
165
|
-
target,
|
|
166
|
-
data: callData,
|
|
167
|
-
msgValue: msgValue ?? 0n,
|
|
168
|
-
isTwoHop: false,
|
|
169
|
-
sourceA: ammName(best.source),
|
|
170
|
-
sourceB: null
|
|
171
|
-
};
|
|
299
|
+
return result;
|
|
172
300
|
}
|
|
173
301
|
};
|
|
174
302
|
export {
|
|
@@ -177,8 +305,10 @@ export {
|
|
|
177
305
|
CHAINS,
|
|
178
306
|
ETH,
|
|
179
307
|
GNOSIS_sDAI,
|
|
308
|
+
InsufficientLiquidityError,
|
|
180
309
|
LENS_QUOTER_ABI,
|
|
181
310
|
Lens,
|
|
311
|
+
assertSwapExecutable,
|
|
182
312
|
defaultDeadline
|
|
183
313
|
};
|
|
184
314
|
//# sourceMappingURL=client.js.map
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client.ts"],"sourcesContent":["// Lens SDK — client\n\nimport { parseAbi, isAddressEqual, zeroAddress } from \"viem\";\nimport type { PublicClient, Address, Abi } from \"viem\";\nimport {\n CHAINS, GNOSIS_sDAI,\n LENS_QUOTER_ABI,\n AMM_NAMES,\n defaultDeadline,\n type ChainId,\n type QuoteParams,\n type QuoteResult,\n} from \"./index.js\";\n\n// viem's parseAbi (abitype) doesn't support inline `tuple(...)` syntax,\n// so rewrite the shared quoter ABI strings to use a named struct.\nconst QUOTE_TUPLE = \"tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut)\";\nconst QUOTE_STRUCT = \"struct Quote { uint8 source; uint256 feeBps; uint256 amountIn; uint256 amountOut; }\";\n\n/** Rewrites inline `tuple(...)` quoter ABI lines to a named `Quote` struct for `parseAbi`. */\nconst toQuoterAbi = (lines: readonly string[]): Abi =>\n parseAbi([QUOTE_STRUCT, ...lines.map((l) => l.split(QUOTE_TUPLE).join(\"Quote\"))]);\n\nconst quoterAbi = toQuoterAbi(LENS_QUOTER_ABI);\n\nexport {\n ETH, GNOSIS_sDAI, AMM, AMM_NAMES,\n CHAINS,\n LENS_QUOTER_ABI, defaultDeadline,\n} from \"./index.js\";\nexport type { QuoteResult, BaseQuoteParams, QuoteParams, ChainId } from \"./index.js\";\n\nconst DEFAULT_SLIPPAGE = 50; // 0.5%\n\ntype QuoteStruct = { source: number; feeBps: bigint; amountIn: bigint; amountOut: bigint };\ntype BestSwapResult = readonly [\n QuoteStruct,\n QuoteStruct,\n Address,\n `0x${string}`,\n bigint,\n];\ntype BuildBestSwapResult = readonly [QuoteStruct, Address, `0x${string}`, bigint, bigint];\n\n/** Resolves a human-readable AMM name from an on-chain `source` id. */\nfunction ammName(source: number): string {\n return (AMM_NAMES as Record<number, string>)[source] || `AMM #${source}`;\n}\n\n/**\n * Maps a `buildBestSwapViaETH` / `buildBestSwapViaToken` return value to `QuoteResult`.\n * Hop B is zeroed for single-hop; `b.amountOut > 0` means a real two-hop\n * route, so final `amountOut` comes from hop B.\n */\nfunction mapViaResult(value: BestSwapResult): QuoteResult {\n const [a, b, target, data, msgValue] = value;\n const isTwoHop = b.amountOut > 0n;\n return {\n amountIn: a.amountIn,\n amountOut: isTwoHop ? b.amountOut : a.amountOut,\n target: target as string,\n data: data as string,\n msgValue: msgValue ?? 0n,\n isTwoHop,\n sourceA: ammName(a.source),\n sourceB: isTwoHop ? ammName(b.source) : null,\n };\n}\n\n/**\n * True when either endpoint is native (`address(0)`) or the chain's wrapped native.\n * On non-Gnosis those pairs use ViaETH; on Gnosis they use ViaToken(sDAI).\n * Pure ERC20↔ERC20 uses `buildBestSwap`.\n */\nfunction involvesNativeOrWnative(\n tokenIn: string,\n tokenOut: string,\n wnative: Address,\n): boolean {\n return (\n isAddressEqual(tokenIn as Address, zeroAddress)\n || isAddressEqual(tokenOut as Address, zeroAddress)\n || isAddressEqual(tokenIn as Address, wnative)\n || isAddressEqual(tokenOut as Address, wnative)\n );\n}\n\n/**\n * Lens SDK client: quotes best routes via the unified on-chain LensQuoter\n * and builds ready-to-send txs for official DEX routers.\n */\nexport class Lens {\n private client: PublicClient;\n private chainId: ChainId;\n private lensQuoter: Address;\n private wnative: Address;\n private quoterAbi: Abi;\n\n /**\n * @param client - viem public client for the target chain\n * @param chainId - chain id (`1` | `10` | `100` | `8453`)\n * @param lensQuoter - optional LensQuoter address; defaults to `CHAINS[chainId].lensQuoter`\n */\n constructor(client: PublicClient, chainId: ChainId, lensQuoter?: Address) {\n this.client = client;\n this.chainId = chainId;\n const chain = CHAINS[chainId];\n this.lensQuoter = lensQuoter ?? (chain.lensQuoter as Address);\n this.wnative = chain.wnative as Address;\n this.quoterAbi = quoterAbi;\n }\n\n /**\n * Builds calldata for a two-hop (or one-hop if mid is unused) route via a\n * mid token — used when the caller sets `intermediate`, or as the default\n * Gnosis path for native/WXDAI↔ERC20 (`mid = GNOSIS_sDAI`).\n */\n private async viaToken(\n params: {\n recipient: string;\n exactOut: boolean;\n tokenIn: string;\n tokenOut: string;\n amount: bigint;\n slippageBps: number;\n deadline: bigint;\n hookPoolFee: number;\n hookTickSpacing: number;\n hookAddress: string;\n mid: string;\n },\n ): Promise<BestSwapResult> {\n return this.client.readContract({\n address: this.lensQuoter,\n abi: this.quoterAbi,\n functionName: \"buildBestSwapViaToken\",\n args: [\n params.recipient as Address,\n params.exactOut,\n params.tokenIn as Address,\n params.tokenOut as Address,\n params.amount,\n BigInt(params.slippageBps),\n params.deadline,\n params.hookPoolFee,\n params.hookTickSpacing,\n params.hookAddress as Address,\n params.mid as Address,\n ],\n }) as Promise<BestSwapResult>;\n }\n\n /**\n * Finds a swap route via LensQuoter and returns amounts plus ready-to-send\n * calldata for an official DEX router.\n *\n * Route selection by request shape:\n * - `intermediate` set → `buildBestSwapViaToken(mid)` only\n * - native / WNATIVE ↔ ERC20 → on Gnosis, `ViaToken(sDAI)` directly;\n * elsewhere `buildBestSwapViaETH`\n * - ERC20 ↔ ERC20 → `buildBestSwap` only (optional V4 hooked candidate)\n */\n async quote(params: QuoteParams): Promise<QuoteResult> {\n const {\n tokenIn, tokenOut, amount, recipient,\n slippageBps = DEFAULT_SLIPPAGE, deadline = defaultDeadline(),\n exactOut = false,\n hookPoolFee = 0, hookTickSpacing = 0, hookAddress = zeroAddress,\n intermediate,\n } = params;\n\n const routeArgs = {\n recipient, exactOut, tokenIn, tokenOut, amount,\n slippageBps, deadline, hookPoolFee, hookTickSpacing, hookAddress,\n };\n\n // Caller forced a mid token: skip native/ERC20 cascade.\n if (intermediate) {\n return mapViaResult(await this.viaToken({ ...routeArgs, mid: intermediate }));\n }\n\n const useViaEth = involvesNativeOrWnative(tokenIn, tokenOut, this.wnative);\n\n if (useViaEth) {\n // Gnosis (Seer): outcomes trade vs sDAI — skip ViaETH entirely.\n if (this.chainId === 100) {\n return mapViaResult(await this.viaToken({ ...routeArgs, mid: GNOSIS_sDAI }));\n }\n\n // Other chains: single-hop ViaETH (router wrap/unwrap).\n const value = await this.client.readContract({\n address: this.lensQuoter,\n abi: this.quoterAbi,\n functionName: \"buildBestSwapViaETH\",\n args: [\n recipient as Address, exactOut,\n tokenIn as Address, tokenOut as Address,\n amount, BigInt(slippageBps), deadline,\n hookPoolFee, hookTickSpacing, hookAddress as Address,\n ],\n }) as BestSwapResult;\n return mapViaResult(value);\n }\n\n // Pure ERC20 ↔ ERC20: direct single-hop only (optional V4 hooked candidate).\n const [best, target, callData, _amountLimit, msgValue] = await this.client.readContract({\n address: this.lensQuoter,\n abi: this.quoterAbi,\n functionName: \"buildBestSwap\",\n args: [\n recipient as Address, exactOut,\n tokenIn as Address, tokenOut as Address,\n amount, BigInt(slippageBps), deadline,\n hookPoolFee, hookTickSpacing, hookAddress as Address,\n ],\n }) as BuildBestSwapResult;\n\n return {\n amountIn: best.amountIn,\n amountOut: best.amountOut,\n target: target as string,\n data: callData as string,\n msgValue: msgValue ?? 0n,\n isTwoHop: false,\n sourceA: ammName(best.source),\n sourceB: null,\n };\n }\n}\n"],"mappings":";;;;;;;;;;;AAEA,SAAS,UAAU,gBAAgB,mBAAmB;AActD,IAAM,cAAc;AACpB,IAAM,eAAe;AAGrB,IAAM,cAAc,CAAC,UACnB,SAAS,CAAC,cAAc,GAAG,MAAM,IAAI,CAAC,MAAM,EAAE,MAAM,WAAW,EAAE,KAAK,OAAO,CAAC,CAAC,CAAC;AAElF,IAAM,YAAY,YAAY,eAAe;AAS7C,IAAM,mBAAmB;AAazB,SAAS,QAAQ,QAAwB;AACvC,SAAQ,UAAqC,MAAM,KAAK,QAAQ,MAAM;AACxE;AAOA,SAAS,aAAa,OAAoC;AACxD,QAAM,CAAC,GAAG,GAAG,QAAQ,MAAM,QAAQ,IAAI;AACvC,QAAM,WAAW,EAAE,YAAY;AAC/B,SAAO;AAAA,IACL,UAAU,EAAE;AAAA,IACZ,WAAW,WAAW,EAAE,YAAY,EAAE;AAAA,IACtC;AAAA,IACA;AAAA,IACA,UAAU,YAAY;AAAA,IACtB;AAAA,IACA,SAAS,QAAQ,EAAE,MAAM;AAAA,IACzB,SAAS,WAAW,QAAQ,EAAE,MAAM,IAAI;AAA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;AAAA,IACX;AAAA,EACF;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/client.ts","../src/gasGuard.ts"],"sourcesContent":["// Lens SDK — client\n\nimport { parseAbi, isAddressEqual, zeroAddress } from \"viem\";\nimport type { PublicClient, Address, Abi } from \"viem\";\nimport {\n CHAINS, GNOSIS_sDAI,\n LENS_QUOTER_ABI,\n AMM_NAMES,\n defaultDeadline,\n type ChainId,\n type QuoteParams,\n type QuoteResult,\n} from \"./index.js\";\nimport { assertSwapExecutable } from \"./gasGuard.js\";\nimport { InsufficientLiquidityError } from \"./errors.js\";\n\n// viem's parseAbi (abitype) doesn't support inline `tuple(...)` syntax,\n// so rewrite the shared quoter ABI strings to use a named struct.\nconst QUOTE_TUPLE = \"tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut)\";\nconst QUOTE_STRUCT = \"struct Quote { uint8 source; uint256 feeBps; uint256 amountIn; uint256 amountOut; }\";\n\n/** Rewrites inline `tuple(...)` quoter ABI lines to a named `Quote` struct for `parseAbi`. */\nconst toQuoterAbi = (lines: readonly string[]): Abi =>\n parseAbi([QUOTE_STRUCT, ...lines.map((l) => l.split(QUOTE_TUPLE).join(\"Quote\"))]);\n\nconst quoterAbi = toQuoterAbi(LENS_QUOTER_ABI);\n\nexport {\n ETH, GNOSIS_sDAI, AMM, AMM_NAMES,\n CHAINS,\n LENS_QUOTER_ABI, defaultDeadline,\n InsufficientLiquidityError,\n} from \"./index.js\";\nexport type { QuoteResult, BaseQuoteParams, QuoteParams, ChainId } from \"./index.js\";\nexport { assertSwapExecutable } from \"./gasGuard.js\";\n\nconst DEFAULT_SLIPPAGE = 50; // 0.5%\n\ntype QuoteStruct = { source: number; feeBps: bigint; amountIn: bigint; amountOut: bigint };\ntype BestSwapResult = readonly [\n QuoteStruct,\n QuoteStruct,\n Address,\n `0x${string}`,\n bigint,\n];\ntype BuildBestSwapResult = readonly [QuoteStruct, Address, `0x${string}`, bigint, bigint];\n\n/** Resolves a human-readable AMM name from an on-chain `source` id. */\nfunction ammName(source: number): string {\n return (AMM_NAMES as Record<number, string>)[source] || `AMM #${source}`;\n}\n\n/**\n * Maps a `buildBestSwapViaETH` / `buildBestSwapViaToken` return value to `QuoteResult`.\n * Hop B is zeroed for single-hop; `b.amountOut > 0` means a real two-hop\n * route, so final `amountOut` comes from hop B.\n */\nfunction mapViaResult(value: BestSwapResult): QuoteResult {\n const [a, b, target, data, msgValue] = value;\n const isTwoHop = b.amountOut > 0n;\n return {\n amountIn: a.amountIn,\n amountOut: isTwoHop ? b.amountOut : a.amountOut,\n target: target as string,\n data: data as string,\n msgValue: msgValue ?? 0n,\n isTwoHop,\n sourceA: ammName(a.source),\n sourceB: isTwoHop ? ammName(b.source) : null,\n };\n}\n\n/**\n * True when either endpoint is native (`address(0)`) or the chain's wrapped native.\n * On non-Gnosis those pairs use ViaETH; on Gnosis they use ViaToken(sDAI).\n * Pure ERC20↔ERC20 uses `buildBestSwap`.\n */\nfunction involvesNativeOrWnative(\n tokenIn: string,\n tokenOut: string,\n wnative: Address,\n): boolean {\n return (\n isAddressEqual(tokenIn as Address, zeroAddress)\n || isAddressEqual(tokenOut as Address, zeroAddress)\n || isAddressEqual(tokenIn as Address, wnative)\n || isAddressEqual(tokenOut as Address, wnative)\n );\n}\n\n/**\n * Lens SDK client: quotes best routes via the unified on-chain LensQuoter\n * and builds ready-to-send txs for official DEX routers.\n */\nexport class Lens {\n private client: PublicClient;\n private chainId: ChainId;\n private lensQuoter: Address;\n private wnative: Address;\n private quoterAbi: Abi;\n\n /**\n * @param client - viem public client for the target chain\n * @param chainId - chain id (`1` | `10` | `100` | `8453`)\n * @param lensQuoter - optional LensQuoter address; defaults to `CHAINS[chainId].lensQuoter`\n */\n constructor(client: PublicClient, chainId: ChainId, lensQuoter?: Address) {\n this.client = client;\n this.chainId = chainId;\n const chain = CHAINS[chainId];\n this.lensQuoter = lensQuoter ?? (chain.lensQuoter as Address);\n this.wnative = chain.wnative as Address;\n this.quoterAbi = quoterAbi;\n }\n\n /**\n * Builds calldata for a two-hop (or one-hop if mid is unused) route via a\n * mid token — used when the caller sets `intermediate`, or as the default\n * Gnosis path for native/WXDAI↔ERC20 (`mid = GNOSIS_sDAI`).\n */\n private async viaToken(\n params: {\n recipient: string;\n exactOut: boolean;\n tokenIn: string;\n tokenOut: string;\n amount: bigint;\n slippageBps: number;\n deadline: bigint;\n hookPoolFee: number;\n hookTickSpacing: number;\n hookAddress: string;\n mid: string;\n },\n ): Promise<BestSwapResult> {\n return this.client.readContract({\n address: this.lensQuoter,\n abi: this.quoterAbi,\n functionName: \"buildBestSwapViaToken\",\n args: [\n params.recipient as Address,\n params.exactOut,\n params.tokenIn as Address,\n params.tokenOut as Address,\n params.amount,\n BigInt(params.slippageBps),\n params.deadline,\n params.hookPoolFee,\n params.hookTickSpacing,\n params.hookAddress as Address,\n params.mid as Address,\n ],\n }) as Promise<BestSwapResult>;\n }\n\n /**\n * Finds a swap route via LensQuoter and returns amounts plus ready-to-send\n * calldata for an official DEX router.\n *\n * Route selection by request shape:\n * - `intermediate` set → `buildBestSwapViaToken(mid)` only\n * - native / WNATIVE ↔ ERC20 → on Gnosis, `ViaToken(sDAI)` directly;\n * elsewhere `buildBestSwapViaETH`\n * - ERC20 ↔ ERC20 → `buildBestSwap` only (optional V4 hooked candidate)\n *\n * After building calldata, simulates the swap with a gas cap of 90% of\n * `block.gasLimit`. Thin pools that quote but cannot fit in a block throw\n * {@link InsufficientLiquidityError}.\n */\n async quote(params: QuoteParams): Promise<QuoteResult> {\n const {\n tokenIn, tokenOut, amount, recipient,\n slippageBps = DEFAULT_SLIPPAGE, deadline = defaultDeadline(),\n exactOut = false,\n hookPoolFee = 0, hookTickSpacing = 0, hookAddress = zeroAddress,\n intermediate,\n } = params;\n\n const routeArgs = {\n recipient, exactOut, tokenIn, tokenOut, amount,\n slippageBps, deadline, hookPoolFee, hookTickSpacing, hookAddress,\n };\n\n let result: QuoteResult;\n\n // Caller forced a mid token: skip native/ERC20 cascade.\n if (intermediate) {\n result = mapViaResult(await this.viaToken({ ...routeArgs, mid: intermediate }));\n } else {\n const useViaEth = involvesNativeOrWnative(tokenIn, tokenOut, this.wnative);\n\n if (useViaEth) {\n // Gnosis (Seer): outcomes trade vs sDAI — skip ViaETH entirely.\n if (this.chainId === 100) {\n result = mapViaResult(await this.viaToken({ ...routeArgs, mid: GNOSIS_sDAI }));\n } else {\n // Other chains: single-hop ViaETH (router wrap/unwrap).\n const value = await this.client.readContract({\n address: this.lensQuoter,\n abi: this.quoterAbi,\n functionName: \"buildBestSwapViaETH\",\n args: [\n recipient as Address, exactOut,\n tokenIn as Address, tokenOut as Address,\n amount, BigInt(slippageBps), deadline,\n hookPoolFee, hookTickSpacing, hookAddress as Address,\n ],\n }) as BestSwapResult;\n result = mapViaResult(value);\n }\n } else {\n // Pure ERC20 ↔ ERC20: direct single-hop only (optional V4 hooked candidate).\n const [best, target, callData, _amountLimit, msgValue] = await this.client.readContract({\n address: this.lensQuoter,\n abi: this.quoterAbi,\n functionName: \"buildBestSwap\",\n args: [\n recipient as Address, exactOut,\n tokenIn as Address, tokenOut as Address,\n amount, BigInt(slippageBps), deadline,\n hookPoolFee, hookTickSpacing, hookAddress as Address,\n ],\n }) as BuildBestSwapResult;\n\n result = {\n amountIn: best.amountIn,\n amountOut: best.amountOut,\n target: target as string,\n data: callData as string,\n msgValue: msgValue ?? 0n,\n isTwoHop: false,\n sourceA: ammName(best.source),\n sourceB: null,\n };\n }\n }\n\n await assertSwapExecutable(this.client, {\n account: recipient as Address,\n tokenIn,\n amountIn: result.amountIn,\n result,\n });\n\n return result;\n }\n}\n","// Post-quote gas executability check (block gas limit / thin pools)\n\nimport {\n encodeAbiParameters,\n isAddressEqual,\n keccak256,\n maxUint256,\n numberToHex,\n zeroAddress,\n type Address,\n type Hex,\n type PublicClient,\n type StateOverride,\n} from \"viem\";\nimport { InsufficientLiquidityError } from \"./errors.js\";\nimport type { QuoteResult } from \"./index.js\";\n\n/** Fraction of block.gasLimit the swap is allowed to consume (90%). */\nexport const GAS_LIMIT_BPS = 9000n;\n\n/**\n * OpenZeppelin ERC20 layout: `balanceOf` at mapping slot 0, `allowance` at 1.\n * Covers sDAI and most Seer collateral / outcome tokens.\n */\nexport function erc20BalanceSlot(owner: Address): Hex {\n return keccak256(\n encodeAbiParameters(\n [{ type: \"address\" }, { type: \"uint256\" }],\n [owner, 0n],\n ),\n );\n}\n\nexport function erc20AllowanceSlot(owner: Address, spender: Address): Hex {\n const inner = keccak256(\n encodeAbiParameters(\n [{ type: \"address\" }, { type: \"uint256\" }],\n [owner, 1n],\n ),\n );\n return keccak256(\n encodeAbiParameters(\n [{ type: \"address\" }, { type: \"bytes32\" }],\n [spender, inner],\n ),\n );\n}\n\n/** stateDiff so `owner` can pay `spender` at least `amount` without a prior approve. */\nexport function erc20SpendOverride(\n token: Address,\n owner: Address,\n spender: Address,\n amount: bigint,\n): StateOverride[number] {\n const bal = amount > 0n ? amount : 1n;\n return {\n address: token,\n stateDiff: [\n { slot: erc20BalanceSlot(owner), value: numberToHex(bal, { size: 32 }) },\n {\n slot: erc20AllowanceSlot(owner, spender),\n value: numberToHex(maxUint256, { size: 32 }),\n },\n ],\n };\n}\n\nfunction isNative(token: string): boolean {\n return isAddressEqual(token as Address, zeroAddress);\n}\n\n/**\n * Builds state overrides so the router sim does not depend on a prior approve.\n * Native `tokenIn` only needs a funded account for `msg.value` + gas.\n */\nexport function buildSwapStateOverride(params: {\n account: Address;\n tokenIn: string;\n amountIn: bigint;\n target: Address;\n msgValue: bigint;\n}): StateOverride {\n const { account, tokenIn, amountIn, target, msgValue } = params;\n const overrides: StateOverride = [\n {\n address: account,\n balance: msgValue + 10n ** 18n,\n },\n ];\n if (!isNative(tokenIn)) {\n overrides.push(\n erc20SpendOverride(tokenIn as Address, account, target, amountIn),\n );\n }\n return overrides;\n}\n\nfunction maxGasForBlock(gasLimit: bigint): bigint {\n return (gasLimit * GAS_LIMIT_BPS) / 10000n;\n}\n\n/**\n * After LensQuoter builds calldata, verify the swap fits in a block.\n *\n * 1. `eth_call` with ERC20 allowance/balance overrides (no prior approve needed).\n * 2. If that succeeds, repeat with `gas = 90% of block.gasLimit`.\n * 3. Unlimited OK + capped fail → {@link InsufficientLiquidityError}.\n * 4. If the unlimited sim fails (non-standard token storage, flaky RPC), skip\n * quietly — never false-positive as liquidity.\n */\nexport async function assertSwapExecutable(\n client: PublicClient,\n params: {\n account: Address;\n tokenIn: string;\n amountIn: bigint;\n result: QuoteResult;\n },\n): Promise<void> {\n const { account, tokenIn, amountIn, result } = params;\n const target = result.target as Address;\n const data = result.data as Hex;\n\n let block: { gasLimit: bigint };\n try {\n block = await client.getBlock({ blockTag: \"latest\" });\n } catch {\n return;\n }\n const maxGas = maxGasForBlock(block.gasLimit);\n if (maxGas === 0n) return;\n\n const stateOverride = buildSwapStateOverride({\n account,\n tokenIn,\n amountIn,\n target,\n msgValue: result.msgValue,\n });\n\n const base = {\n account,\n to: target,\n data,\n value: result.msgValue,\n stateOverride,\n } as const;\n\n try {\n await client.call(base);\n } catch {\n return;\n }\n\n try {\n await client.call({ ...base, gas: maxGas });\n } catch {\n throw new InsufficientLiquidityError();\n }\n\n try {\n const gas = await client.estimateGas(base);\n if (gas > maxGas) throw new InsufficientLiquidityError();\n } catch (e) {\n if (e instanceof InsufficientLiquidityError) throw e;\n }\n}\n"],"mappings":";;;;;;;;;;;;AAEA,SAAS,UAAU,kBAAAA,iBAAgB,eAAAC,oBAAmB;;;ACAtD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAKK;AAKA,IAAM,gBAAgB;AAMtB,SAAS,iBAAiB,OAAqB;AACpD,SAAO;AAAA,IACL;AAAA,MACE,CAAC,EAAE,MAAM,UAAU,GAAG,EAAE,MAAM,UAAU,CAAC;AAAA,MACzC,CAAC,OAAO,EAAE;AAAA,IACZ;AAAA,EACF;AACF;AAEO,SAAS,mBAAmB,OAAgB,SAAu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A,YAC7B;AAAA,YAAa;AAAA,YAAiB;AAAA,UAChC;AAAA,QACF,CAAC;AAED,iBAAS;AAAA,UACP,UAAU,KAAK;AAAA,UACf,WAAW,KAAK;AAAA,UAChB;AAAA,UACA,MAAM;AAAA,UACN,UAAU,YAAY;AAAA,UACtB,UAAU;AAAA,UACV,SAAS,QAAQ,KAAK,MAAM;AAAA,UAC5B,SAAS;AAAA,QACX;AAAA,MACF;AAAA,IACF;AAEA,UAAM,qBAAqB,KAAK,QAAQ;AAAA,MACtC,SAAS;AAAA,MACT;AAAA,MACA,UAAU,OAAO;AAAA,MACjB;AAAA,IACF,CAAC;AAED,WAAO;AAAA,EACT;AACF;","names":["isAddressEqual","zeroAddress","isAddressEqual","zeroAddress"]}
|
package/dist/index.cjs
CHANGED
|
@@ -25,10 +25,22 @@ __export(index_exports, {
|
|
|
25
25
|
CHAINS: () => CHAINS,
|
|
26
26
|
ETH: () => ETH,
|
|
27
27
|
GNOSIS_sDAI: () => GNOSIS_sDAI,
|
|
28
|
+
InsufficientLiquidityError: () => InsufficientLiquidityError,
|
|
28
29
|
LENS_QUOTER_ABI: () => LENS_QUOTER_ABI,
|
|
29
30
|
defaultDeadline: () => defaultDeadline
|
|
30
31
|
});
|
|
31
32
|
module.exports = __toCommonJS(index_exports);
|
|
33
|
+
|
|
34
|
+
// src/errors.ts
|
|
35
|
+
var InsufficientLiquidityError = class extends Error {
|
|
36
|
+
code = "INSUFFICIENT_LIQUIDITY";
|
|
37
|
+
constructor(message = "Swap too large for available liquidity (would exceed block gas limit). Try a smaller amount.") {
|
|
38
|
+
super(message);
|
|
39
|
+
this.name = "InsufficientLiquidityError";
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// src/index.ts
|
|
32
44
|
var AMM = /* @__PURE__ */ ((AMM2) => {
|
|
33
45
|
AMM2[AMM2["UNI_V3"] = 0] = "UNI_V3";
|
|
34
46
|
AMM2[AMM2["UNI_V4"] = 1] = "UNI_V4";
|
|
@@ -93,6 +105,7 @@ function defaultDeadline() {
|
|
|
93
105
|
CHAINS,
|
|
94
106
|
ETH,
|
|
95
107
|
GNOSIS_sDAI,
|
|
108
|
+
InsufficientLiquidityError,
|
|
96
109
|
LENS_QUOTER_ABI,
|
|
97
110
|
defaultDeadline
|
|
98
111
|
});
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["// Lens SDK — Core constants, ABIs, types, token list (zero dependencies)\n\n// ---- Chain config ----\n\n/** Unified AMM enum (same ordinals on every chain). */\nexport enum AMM {\n UNI_V3 = 0,\n UNI_V4 = 1,\n UNI_V4_HOOKED = 2,\n SWAPR_V3 = 3,\n}\n\nexport const AMM_NAMES: Record<AMM, string> = {\n [AMM.UNI_V3]: \"Uniswap V3\",\n [AMM.UNI_V4]: \"Uniswap V4\",\n [AMM.UNI_V4_HOOKED]: \"Uniswap V4 Hooked\",\n [AMM.SWAPR_V3]: \"Swapr V3\",\n};\n\n/** Shared LensQuoter ABI for the unified contract on all chains. */\nexport const LENS_QUOTER_ABI = [\n \"function buildBestSwapViaETH(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) a, tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) b, address target, bytes data, uint256 msgValue)\",\n \"function buildBestSwapViaToken(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress, address mid) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) a, tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) b, address target, bytes data, uint256 msgValue)\",\n \"function buildBestSwap(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) best, address target, bytes callData, uint256 amountLimit, uint256 msgValue)\",\n \"function V3_SWAP_ROUTER() view returns (address)\",\n \"function V4_ROUTER() view returns (address)\",\n \"function SWAPR_SWAP_ROUTER() view returns (address)\",\n \"function V4_QUOTER() view returns (address)\",\n] as const;\n\n/** Per-chain LensQuoter, official DEX routers, and wrapped native. */\nexport const CHAINS = {\n 1: {\n lensQuoter: \"0xaCE2C61E250c2d019b9Ed7FEf6A441022D6Ec7d2\",\n v3SwapRouter: \"0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2\",\n },\n 10: {\n lensQuoter: \"0x591aF101cAf2b7351C74c25F5E64bC2E062D2843\",\n v3SwapRouter: \"0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0x4200000000000000000000000000000000000006\",\n },\n 100: {\n lensQuoter: \"0xE8E3d230C1c17C7249126036F170027a664d1FbA\",\n v3SwapRouter: \"0x0000000000000000000000000000000000000000\",\n v4Router: \"0x0000000000000000000000000000000000000000\",\n swaprSwapRouter: \"0xfFB643E73f280B97809A8b41f7232AB401a04ee1\",\n wnative: \"0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d\",\n },\n 8453: {\n lensQuoter: \"0x591aF101cAf2b7351C74c25F5E64bC2E062D2843\",\n v3SwapRouter: \"0x2626664c2603336E57B271c5C0b26F421741e481\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0x4200000000000000000000000000000000000006\",\n },\n} as const;\n\nexport type ChainId = keyof typeof CHAINS;\n\nexport const ETH = \"0x0000000000000000000000000000000000000000\" as const;\n\n/** Gnosis sDAI — default ViaToken mid for native/WXDAI↔ERC20 on Gnosis (Seer). */\nexport const GNOSIS_sDAI = \"0xaf204776c7245bF4147c2612BF6e5972Ee483701\" as const;\n\n// ---- Types ----\n\nexport interface QuoteResult {\n amountIn: bigint;\n amountOut: bigint;\n /** Official DEX router to call. */\n target: string;\n /** Ready-to-send calldata for `target`. */\n data: string;\n msgValue: bigint;\n isTwoHop: boolean;\n sourceA: string;\n sourceB: string | null;\n}\n\nexport interface BaseQuoteParams {\n tokenIn: string;\n tokenOut: string;\n amount: bigint;\n}\n\nexport interface QuoteParams extends BaseQuoteParams {\n recipient: string;\n slippageBps?: number;\n deadline?: bigint;\n /** false = exactIn (default); true = exactOut. `amount` is amountIn or desired amountOut accordingly. */\n exactOut?: boolean;\n /** Optional V4 hooked-pool candidate (exactIn/exactOut; can beat the primary route). */\n hookPoolFee?: number;\n hookTickSpacing?: number;\n hookAddress?: string;\n /**\n * Intermediate token for two-hop routes (`buildBestSwapViaToken`).\n * When set, skips the native/ERC20 cascade. On Gnosis, native/WXDAI↔ERC20\n * already defaults to ViaToken(sDAI); use this only to force a different mid.\n */\n intermediate?: string;\n}\n\n// ---- Helpers ----\n\nexport function defaultDeadline(): bigint {\n return BigInt(Math.trunc(Date.now() / 1000) + 300);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/errors.ts"],"sourcesContent":["// Lens SDK — Core constants, ABIs, types, token list (zero dependencies)\n\n// ---- Chain config ----\n\n/** Unified AMM enum (same ordinals on every chain). */\nexport enum AMM {\n UNI_V3 = 0,\n UNI_V4 = 1,\n UNI_V4_HOOKED = 2,\n SWAPR_V3 = 3,\n}\n\nexport const AMM_NAMES: Record<AMM, string> = {\n [AMM.UNI_V3]: \"Uniswap V3\",\n [AMM.UNI_V4]: \"Uniswap V4\",\n [AMM.UNI_V4_HOOKED]: \"Uniswap V4 Hooked\",\n [AMM.SWAPR_V3]: \"Swapr V3\",\n};\n\n/** Shared LensQuoter ABI for the unified contract on all chains. */\nexport const LENS_QUOTER_ABI = [\n \"function buildBestSwapViaETH(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) a, tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) b, address target, bytes data, uint256 msgValue)\",\n \"function buildBestSwapViaToken(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress, address mid) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) a, tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) b, address target, bytes data, uint256 msgValue)\",\n \"function buildBestSwap(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) best, address target, bytes callData, uint256 amountLimit, uint256 msgValue)\",\n \"function V3_SWAP_ROUTER() view returns (address)\",\n \"function V4_ROUTER() view returns (address)\",\n \"function SWAPR_SWAP_ROUTER() view returns (address)\",\n \"function V4_QUOTER() view returns (address)\",\n] as const;\n\n/** Per-chain LensQuoter, official DEX routers, and wrapped native. */\nexport const CHAINS = {\n 1: {\n lensQuoter: \"0xaCE2C61E250c2d019b9Ed7FEf6A441022D6Ec7d2\",\n v3SwapRouter: \"0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2\",\n },\n 10: {\n lensQuoter: \"0x591aF101cAf2b7351C74c25F5E64bC2E062D2843\",\n v3SwapRouter: \"0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0x4200000000000000000000000000000000000006\",\n },\n 100: {\n lensQuoter: \"0xE8E3d230C1c17C7249126036F170027a664d1FbA\",\n v3SwapRouter: \"0x0000000000000000000000000000000000000000\",\n v4Router: \"0x0000000000000000000000000000000000000000\",\n swaprSwapRouter: \"0xfFB643E73f280B97809A8b41f7232AB401a04ee1\",\n wnative: \"0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d\",\n },\n 8453: {\n lensQuoter: \"0x591aF101cAf2b7351C74c25F5E64bC2E062D2843\",\n v3SwapRouter: \"0x2626664c2603336E57B271c5C0b26F421741e481\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0x4200000000000000000000000000000000000006\",\n },\n} as const;\n\nexport type ChainId = keyof typeof CHAINS;\n\nexport const ETH = \"0x0000000000000000000000000000000000000000\" as const;\n\n/** Gnosis sDAI — default ViaToken mid for native/WXDAI↔ERC20 on Gnosis (Seer). */\nexport const GNOSIS_sDAI = \"0xaf204776c7245bF4147c2612BF6e5972Ee483701\" as const;\n\n// ---- Types ----\n\nexport interface QuoteResult {\n amountIn: bigint;\n amountOut: bigint;\n /** Official DEX router to call. */\n target: string;\n /** Ready-to-send calldata for `target`. */\n data: string;\n msgValue: bigint;\n isTwoHop: boolean;\n sourceA: string;\n sourceB: string | null;\n}\n\nexport interface BaseQuoteParams {\n tokenIn: string;\n tokenOut: string;\n amount: bigint;\n}\n\nexport interface QuoteParams extends BaseQuoteParams {\n recipient: string;\n slippageBps?: number;\n deadline?: bigint;\n /** false = exactIn (default); true = exactOut. `amount` is amountIn or desired amountOut accordingly. */\n exactOut?: boolean;\n /** Optional V4 hooked-pool candidate (exactIn/exactOut; can beat the primary route). */\n hookPoolFee?: number;\n hookTickSpacing?: number;\n hookAddress?: string;\n /**\n * Intermediate token for two-hop routes (`buildBestSwapViaToken`).\n * When set, skips the native/ERC20 cascade. On Gnosis, native/WXDAI↔ERC20\n * already defaults to ViaToken(sDAI); use this only to force a different mid.\n */\n intermediate?: string;\n}\n\n// ---- Errors ----\n\nexport { InsufficientLiquidityError } from \"./errors.js\";\n\n// ---- Helpers ----\n\nexport function defaultDeadline(): bigint {\n return BigInt(Math.trunc(Date.now() / 1000) + 300);\n}\n","/**\n * Thrown when a route quotes successfully but the swap cannot execute within\n * the chain's block gas limit — typically a thin concentrated-liquidity pool\n * that would cross too many ticks for the requested size.\n */\nexport class InsufficientLiquidityError extends Error {\n readonly code = \"INSUFFICIENT_LIQUIDITY\" as const;\n\n constructor(\n message = \"Swap too large for available liquidity (would exceed block gas limit). Try a smaller amount.\",\n ) {\n super(message);\n this.name = \"InsufficientLiquidityError\";\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKO,IAAM,6BAAN,cAAyC,MAAM;AAAA,EAC3C,OAAO;AAAA,EAEhB,YACE,UAAU,gGACV;AACA,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;;;ADTO,IAAK,MAAL,kBAAKA,SAAL;AACL,EAAAA,UAAA,YAAS,KAAT;AACA,EAAAA,UAAA,YAAS,KAAT;AACA,EAAAA,UAAA,mBAAgB,KAAhB;AACA,EAAAA,UAAA,cAAW,KAAX;AAJU,SAAAA;AAAA,GAAA;AAOL,IAAM,YAAiC;AAAA,EAC5C,CAAC,cAAU,GAAG;AAAA,EACd,CAAC,cAAU,GAAG;AAAA,EACd,CAAC,qBAAiB,GAAG;AAAA,EACrB,CAAC,gBAAY,GAAG;AAClB;AAGO,IAAM,kBAAkB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAGO,IAAM,SAAS;AAAA,EACpB,GAAG;AAAA,IACD,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AAAA,EACA,IAAI;AAAA,IACF,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AAAA,EACA,KAAK;AAAA,IACH,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AAAA,EACA,MAAM;AAAA,IACJ,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AACF;AAIO,IAAM,MAAM;AAGZ,IAAM,cAAc;AA+CpB,SAAS,kBAA0B;AACxC,SAAO,OAAO,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI,IAAI,GAAG;AACnD;","names":["AMM"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when a route quotes successfully but the swap cannot execute within
|
|
3
|
+
* the chain's block gas limit — typically a thin concentrated-liquidity pool
|
|
4
|
+
* that would cross too many ticks for the requested size.
|
|
5
|
+
*/
|
|
6
|
+
declare class InsufficientLiquidityError extends Error {
|
|
7
|
+
readonly code: "INSUFFICIENT_LIQUIDITY";
|
|
8
|
+
constructor(message?: string);
|
|
9
|
+
}
|
|
10
|
+
|
|
1
11
|
/** Unified AMM enum (same ordinals on every chain). */
|
|
2
12
|
declare enum AMM {
|
|
3
13
|
UNI_V3 = 0,
|
|
@@ -77,6 +87,7 @@ interface QuoteParams extends BaseQuoteParams {
|
|
|
77
87
|
*/
|
|
78
88
|
intermediate?: string;
|
|
79
89
|
}
|
|
90
|
+
|
|
80
91
|
declare function defaultDeadline(): bigint;
|
|
81
92
|
|
|
82
|
-
export { AMM, AMM_NAMES, type BaseQuoteParams, CHAINS, type ChainId, ETH, GNOSIS_sDAI, LENS_QUOTER_ABI, type QuoteParams, type QuoteResult, defaultDeadline };
|
|
93
|
+
export { AMM, AMM_NAMES, type BaseQuoteParams, CHAINS, type ChainId, ETH, GNOSIS_sDAI, InsufficientLiquidityError, LENS_QUOTER_ABI, type QuoteParams, type QuoteResult, defaultDeadline };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when a route quotes successfully but the swap cannot execute within
|
|
3
|
+
* the chain's block gas limit — typically a thin concentrated-liquidity pool
|
|
4
|
+
* that would cross too many ticks for the requested size.
|
|
5
|
+
*/
|
|
6
|
+
declare class InsufficientLiquidityError extends Error {
|
|
7
|
+
readonly code: "INSUFFICIENT_LIQUIDITY";
|
|
8
|
+
constructor(message?: string);
|
|
9
|
+
}
|
|
10
|
+
|
|
1
11
|
/** Unified AMM enum (same ordinals on every chain). */
|
|
2
12
|
declare enum AMM {
|
|
3
13
|
UNI_V3 = 0,
|
|
@@ -77,6 +87,7 @@ interface QuoteParams extends BaseQuoteParams {
|
|
|
77
87
|
*/
|
|
78
88
|
intermediate?: string;
|
|
79
89
|
}
|
|
90
|
+
|
|
80
91
|
declare function defaultDeadline(): bigint;
|
|
81
92
|
|
|
82
|
-
export { AMM, AMM_NAMES, type BaseQuoteParams, CHAINS, type ChainId, ETH, GNOSIS_sDAI, LENS_QUOTER_ABI, type QuoteParams, type QuoteResult, defaultDeadline };
|
|
93
|
+
export { AMM, AMM_NAMES, type BaseQuoteParams, CHAINS, type ChainId, ETH, GNOSIS_sDAI, InsufficientLiquidityError, LENS_QUOTER_ABI, type QuoteParams, type QuoteResult, defaultDeadline };
|
package/dist/index.js
CHANGED
|
@@ -4,15 +4,17 @@ import {
|
|
|
4
4
|
CHAINS,
|
|
5
5
|
ETH,
|
|
6
6
|
GNOSIS_sDAI,
|
|
7
|
+
InsufficientLiquidityError,
|
|
7
8
|
LENS_QUOTER_ABI,
|
|
8
9
|
defaultDeadline
|
|
9
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-5UNFCMSF.js";
|
|
10
11
|
export {
|
|
11
12
|
AMM,
|
|
12
13
|
AMM_NAMES,
|
|
13
14
|
CHAINS,
|
|
14
15
|
ETH,
|
|
15
16
|
GNOSIS_sDAI,
|
|
17
|
+
InsufficientLiquidityError,
|
|
16
18
|
LENS_QUOTER_ABI,
|
|
17
19
|
defaultDeadline
|
|
18
20
|
};
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["// Lens SDK — Core constants, ABIs, types, token list (zero dependencies)\n\n// ---- Chain config ----\n\n/** Unified AMM enum (same ordinals on every chain). */\nexport enum AMM {\n UNI_V3 = 0,\n UNI_V4 = 1,\n UNI_V4_HOOKED = 2,\n SWAPR_V3 = 3,\n}\n\nexport const AMM_NAMES: Record<AMM, string> = {\n [AMM.UNI_V3]: \"Uniswap V3\",\n [AMM.UNI_V4]: \"Uniswap V4\",\n [AMM.UNI_V4_HOOKED]: \"Uniswap V4 Hooked\",\n [AMM.SWAPR_V3]: \"Swapr V3\",\n};\n\n/** Shared LensQuoter ABI for the unified contract on all chains. */\nexport const LENS_QUOTER_ABI = [\n \"function buildBestSwapViaETH(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) a, tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) b, address target, bytes data, uint256 msgValue)\",\n \"function buildBestSwapViaToken(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress, address mid) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) a, tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) b, address target, bytes data, uint256 msgValue)\",\n \"function buildBestSwap(address to, bool exactOut, address tokenIn, address tokenOut, uint256 swapAmount, uint256 slippageBps, uint256 deadline, uint24 hookPoolFee, int24 hookTickSpacing, address hookAddress) returns (tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut) best, address target, bytes callData, uint256 amountLimit, uint256 msgValue)\",\n \"function V3_SWAP_ROUTER() view returns (address)\",\n \"function V4_ROUTER() view returns (address)\",\n \"function SWAPR_SWAP_ROUTER() view returns (address)\",\n \"function V4_QUOTER() view returns (address)\",\n] as const;\n\n/** Per-chain LensQuoter, official DEX routers, and wrapped native. */\nexport const CHAINS = {\n 1: {\n lensQuoter: \"0xaCE2C61E250c2d019b9Ed7FEf6A441022D6Ec7d2\",\n v3SwapRouter: \"0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2\",\n },\n 10: {\n lensQuoter: \"0x591aF101cAf2b7351C74c25F5E64bC2E062D2843\",\n v3SwapRouter: \"0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0x4200000000000000000000000000000000000006\",\n },\n 100: {\n lensQuoter: \"0xE8E3d230C1c17C7249126036F170027a664d1FbA\",\n v3SwapRouter: \"0x0000000000000000000000000000000000000000\",\n v4Router: \"0x0000000000000000000000000000000000000000\",\n swaprSwapRouter: \"0xfFB643E73f280B97809A8b41f7232AB401a04ee1\",\n wnative: \"0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d\",\n },\n 8453: {\n lensQuoter: \"0x591aF101cAf2b7351C74c25F5E64bC2E062D2843\",\n v3SwapRouter: \"0x2626664c2603336E57B271c5C0b26F421741e481\",\n v4Router: \"0x00000000000044a361Ae3cAc094c9D1b14Eece97\",\n swaprSwapRouter: \"0x0000000000000000000000000000000000000000\",\n wnative: \"0x4200000000000000000000000000000000000006\",\n },\n} as const;\n\nexport type ChainId = keyof typeof CHAINS;\n\nexport const ETH = \"0x0000000000000000000000000000000000000000\" as const;\n\n/** Gnosis sDAI — default ViaToken mid for native/WXDAI↔ERC20 on Gnosis (Seer). */\nexport const GNOSIS_sDAI = \"0xaf204776c7245bF4147c2612BF6e5972Ee483701\" as const;\n\n// ---- Types ----\n\nexport interface QuoteResult {\n amountIn: bigint;\n amountOut: bigint;\n /** Official DEX router to call. */\n target: string;\n /** Ready-to-send calldata for `target`. */\n data: string;\n msgValue: bigint;\n isTwoHop: boolean;\n sourceA: string;\n sourceB: string | null;\n}\n\nexport interface BaseQuoteParams {\n tokenIn: string;\n tokenOut: string;\n amount: bigint;\n}\n\nexport interface QuoteParams extends BaseQuoteParams {\n recipient: string;\n slippageBps?: number;\n deadline?: bigint;\n /** false = exactIn (default); true = exactOut. `amount` is amountIn or desired amountOut accordingly. */\n exactOut?: boolean;\n /** Optional V4 hooked-pool candidate (exactIn/exactOut; can beat the primary route). */\n hookPoolFee?: number;\n hookTickSpacing?: number;\n hookAddress?: string;\n /**\n * Intermediate token for two-hop routes (`buildBestSwapViaToken`).\n * When set, skips the native/ERC20 cascade. On Gnosis, native/WXDAI↔ERC20\n * already defaults to ViaToken(sDAI); use this only to force a different mid.\n */\n intermediate?: string;\n}\n\n// ---- Helpers ----\n\nexport function defaultDeadline(): bigint {\n return BigInt(Math.trunc(Date.now() / 1000) + 300);\n}\n"],"mappings":";AAKO,IAAK,MAAL,kBAAKA,SAAL;AACL,EAAAA,UAAA,YAAS,KAAT;AACA,EAAAA,UAAA,YAAS,KAAT;AACA,EAAAA,UAAA,mBAAgB,KAAhB;AACA,EAAAA,UAAA,cAAW,KAAX;AAJU,SAAAA;AAAA,GAAA;AAOL,IAAM,YAAiC;AAAA,EAC5C,CAAC,cAAU,GAAG;AAAA,EACd,CAAC,cAAU,GAAG;AAAA,EACd,CAAC,qBAAiB,GAAG;AAAA,EACrB,CAAC,gBAAY,GAAG;AAClB;AAGO,IAAM,kBAAkB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAGO,IAAM,SAAS;AAAA,EACpB,GAAG;AAAA,IACD,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AAAA,EACA,IAAI;AAAA,IACF,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AAAA,EACA,KAAK;AAAA,IACH,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AAAA,EACA,MAAM;AAAA,IACJ,YAAY;AAAA,IACZ,cAAc;AAAA,IACd,UAAU;AAAA,IACV,iBAAiB;AAAA,IACjB,SAAS;AAAA,EACX;AACF;AAIO,IAAM,MAAM;AAGZ,IAAM,cAAc;AA2CpB,SAAS,kBAA0B;AACxC,SAAO,OAAO,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI,IAAI,GAAG;AACnD;","names":["AMM"]}
|