@seer-pm/lens 1.3.0 → 1.5.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-STF3B72X.js} +22 -2
- package/dist/chunk-STF3B72X.js.map +1 -0
- package/dist/client.cjs +263 -55
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +25 -3
- package/dist/client.d.ts +25 -3
- package/dist/client.js +252 -54
- package/dist/client.js.map +1 -1
- package/dist/index.cjs +25 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +27 -2
- package/dist/index.d.ts +27 -2
- package/dist/index.js +5 -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,21 @@
|
|
|
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
|
+
var SwapRevertsError = class extends Error {
|
|
10
|
+
code = "SWAP_REVERTS";
|
|
11
|
+
constructor(options, message = "Swap simulation reverted; the router rejected the built calldata.") {
|
|
12
|
+
super(options?.detail ? `${message} (${options.detail})` : message, {
|
|
13
|
+
cause: options?.cause
|
|
14
|
+
});
|
|
15
|
+
this.name = "SwapRevertsError";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
1
19
|
// src/index.ts
|
|
2
20
|
var AMM = /* @__PURE__ */ ((AMM2) => {
|
|
3
21
|
AMM2[AMM2["UNI_V3"] = 0] = "UNI_V3";
|
|
@@ -37,7 +55,7 @@ var CHAINS = {
|
|
|
37
55
|
wnative: "0x4200000000000000000000000000000000000006"
|
|
38
56
|
},
|
|
39
57
|
100: {
|
|
40
|
-
lensQuoter: "
|
|
58
|
+
lensQuoter: "0x82150d38288e53AfcF20a5eEee47682c5Ef796d8",
|
|
41
59
|
v3SwapRouter: "0x0000000000000000000000000000000000000000",
|
|
42
60
|
v4Router: "0x0000000000000000000000000000000000000000",
|
|
43
61
|
swaprSwapRouter: "0xfFB643E73f280B97809A8b41f7232AB401a04ee1",
|
|
@@ -58,6 +76,8 @@ function defaultDeadline() {
|
|
|
58
76
|
}
|
|
59
77
|
|
|
60
78
|
export {
|
|
79
|
+
InsufficientLiquidityError,
|
|
80
|
+
SwapRevertsError,
|
|
61
81
|
AMM,
|
|
62
82
|
AMM_NAMES,
|
|
63
83
|
LENS_QUOTER_ABI,
|
|
@@ -66,4 +86,4 @@ export {
|
|
|
66
86
|
GNOSIS_sDAI,
|
|
67
87
|
defaultDeadline
|
|
68
88
|
};
|
|
69
|
-
//# sourceMappingURL=chunk-
|
|
89
|
+
//# sourceMappingURL=chunk-STF3B72X.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\n/**\n * Thrown when the pre-flight simulation shows the built calldata reverts, and the\n * state overrides that make it executable are confirmed to have applied — so the\n * revert is the router's, not an artifact of the simulation.\n *\n * Sending such a swap would waste gas and surface as an opaque wallet failure.\n */\nexport class SwapRevertsError extends Error {\n readonly code = \"SWAP_REVERTS\" as const;\n\n constructor(\n options?: { cause?: unknown; detail?: string },\n message = \"Swap simulation reverted; the router rejected the built calldata.\",\n ) {\n super(options?.detail ? `${message} (${options.detail})` : message, {\n cause: options?.cause,\n });\n this.name = \"SwapRevertsError\";\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: \"0x82150d38288e53AfcF20a5eEee47682c5Ef796d8\",\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, SwapRevertsError } 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;AASO,IAAM,mBAAN,cAA+B,MAAM;AAAA,EACjC,OAAO;AAAA,EAEhB,YACE,SACA,UAAU,qEACV;AACA,UAAM,SAAS,SAAS,GAAG,OAAO,KAAK,QAAQ,MAAM,MAAM,SAAS;AAAA,MAClE,OAAO,SAAS;AAAA,IAClB,CAAC;AACD,SAAK,OAAO;AAAA,EACd;AACF;;;AC9BO,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,33 @@ __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
|
+
SwapRevertsError: () => SwapRevertsError,
|
|
32
|
+
assertSwapExecutable: () => assertSwapExecutable,
|
|
30
33
|
defaultDeadline: () => defaultDeadline
|
|
31
34
|
});
|
|
32
35
|
module.exports = __toCommonJS(client_exports);
|
|
33
|
-
var
|
|
36
|
+
var import_viem2 = require("viem");
|
|
37
|
+
|
|
38
|
+
// src/errors.ts
|
|
39
|
+
var InsufficientLiquidityError = class extends Error {
|
|
40
|
+
code = "INSUFFICIENT_LIQUIDITY";
|
|
41
|
+
constructor(message = "Swap too large for available liquidity (would exceed block gas limit). Try a smaller amount.") {
|
|
42
|
+
super(message);
|
|
43
|
+
this.name = "InsufficientLiquidityError";
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
var SwapRevertsError = class extends Error {
|
|
47
|
+
code = "SWAP_REVERTS";
|
|
48
|
+
constructor(options, message = "Swap simulation reverted; the router rejected the built calldata.") {
|
|
49
|
+
super(options?.detail ? `${message} (${options.detail})` : message, {
|
|
50
|
+
cause: options?.cause
|
|
51
|
+
});
|
|
52
|
+
this.name = "SwapRevertsError";
|
|
53
|
+
}
|
|
54
|
+
};
|
|
34
55
|
|
|
35
56
|
// src/index.ts
|
|
36
57
|
var AMM = /* @__PURE__ */ ((AMM2) => {
|
|
@@ -71,7 +92,7 @@ var CHAINS = {
|
|
|
71
92
|
wnative: "0x4200000000000000000000000000000000000006"
|
|
72
93
|
},
|
|
73
94
|
100: {
|
|
74
|
-
lensQuoter: "
|
|
95
|
+
lensQuoter: "0x82150d38288e53AfcF20a5eEee47682c5Ef796d8",
|
|
75
96
|
v3SwapRouter: "0x0000000000000000000000000000000000000000",
|
|
76
97
|
v4Router: "0x0000000000000000000000000000000000000000",
|
|
77
98
|
swaprSwapRouter: "0xfFB643E73f280B97809A8b41f7232AB401a04ee1",
|
|
@@ -91,10 +112,179 @@ function defaultDeadline() {
|
|
|
91
112
|
return BigInt(Math.trunc(Date.now() / 1e3) + 300);
|
|
92
113
|
}
|
|
93
114
|
|
|
115
|
+
// src/gasGuard.ts
|
|
116
|
+
var import_viem = require("viem");
|
|
117
|
+
var GAS_LIMIT_BPS = 9000n;
|
|
118
|
+
function erc20BalanceSlot(owner) {
|
|
119
|
+
return (0, import_viem.keccak256)(
|
|
120
|
+
(0, import_viem.encodeAbiParameters)(
|
|
121
|
+
[{ type: "address" }, { type: "uint256" }],
|
|
122
|
+
[owner, 0n]
|
|
123
|
+
)
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
function erc20AllowanceSlot(owner, spender) {
|
|
127
|
+
const inner = (0, import_viem.keccak256)(
|
|
128
|
+
(0, import_viem.encodeAbiParameters)(
|
|
129
|
+
[{ type: "address" }, { type: "uint256" }],
|
|
130
|
+
[owner, 1n]
|
|
131
|
+
)
|
|
132
|
+
);
|
|
133
|
+
return (0, import_viem.keccak256)(
|
|
134
|
+
(0, import_viem.encodeAbiParameters)(
|
|
135
|
+
[{ type: "address" }, { type: "bytes32" }],
|
|
136
|
+
[spender, inner]
|
|
137
|
+
)
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
function erc20SpendOverride(token, owner, spender, amount) {
|
|
141
|
+
const bal = amount > 0n ? amount : 1n;
|
|
142
|
+
return {
|
|
143
|
+
address: token,
|
|
144
|
+
stateDiff: [
|
|
145
|
+
{ slot: erc20BalanceSlot(owner), value: (0, import_viem.numberToHex)(bal, { size: 32 }) },
|
|
146
|
+
{
|
|
147
|
+
slot: erc20AllowanceSlot(owner, spender),
|
|
148
|
+
value: (0, import_viem.numberToHex)(import_viem.maxUint256, { size: 32 })
|
|
149
|
+
}
|
|
150
|
+
]
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function isNative(token) {
|
|
154
|
+
return (0, import_viem.isAddressEqual)(token, import_viem.zeroAddress);
|
|
155
|
+
}
|
|
156
|
+
function buildSwapStateOverride(params) {
|
|
157
|
+
const { account, tokenIn, amountIn, target, msgValue } = params;
|
|
158
|
+
const overrides = [
|
|
159
|
+
{
|
|
160
|
+
address: account,
|
|
161
|
+
balance: msgValue + 10n ** 18n
|
|
162
|
+
}
|
|
163
|
+
];
|
|
164
|
+
if (!isNative(tokenIn)) {
|
|
165
|
+
overrides.push(
|
|
166
|
+
erc20SpendOverride(tokenIn, account, target, amountIn)
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
return overrides;
|
|
170
|
+
}
|
|
171
|
+
function maxGasForBlock(gasLimit) {
|
|
172
|
+
return gasLimit * GAS_LIMIT_BPS / 10000n;
|
|
173
|
+
}
|
|
174
|
+
var PROBE_ADDRESS = "0x0000000000000000000000000000000000f0f0f0";
|
|
175
|
+
var PROBE_CODE = "0x600160005260206000f3";
|
|
176
|
+
var ONE_WORD = (0, import_viem.numberToHex)(1n, { size: 32 });
|
|
177
|
+
var BALANCE_OF_SELECTOR = "0x70a08231";
|
|
178
|
+
var ALLOWANCE_SELECTOR = "0xdd62ed3e";
|
|
179
|
+
function encodeAddressArgs(...args) {
|
|
180
|
+
return (0, import_viem.encodeAbiParameters)(
|
|
181
|
+
args.map(() => ({ type: "address" })),
|
|
182
|
+
args
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
async function nodeAppliesStateOverrides(client) {
|
|
186
|
+
try {
|
|
187
|
+
const res = await client.call({
|
|
188
|
+
to: PROBE_ADDRESS,
|
|
189
|
+
data: "0x",
|
|
190
|
+
stateOverride: [{ address: PROBE_ADDRESS, code: PROBE_CODE }]
|
|
191
|
+
});
|
|
192
|
+
return res.data === ONE_WORD;
|
|
193
|
+
} catch {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
async function readWord(client, to, data, stateOverride) {
|
|
198
|
+
try {
|
|
199
|
+
const res = await client.call({ to, data, stateOverride });
|
|
200
|
+
return res.data;
|
|
201
|
+
} catch {
|
|
202
|
+
return void 0;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
async function swapOverridesApplied(params) {
|
|
206
|
+
const { client, account, tokenIn, amountIn, target, stateOverride } = params;
|
|
207
|
+
if (!await nodeAppliesStateOverrides(client)) return false;
|
|
208
|
+
if (isNative(tokenIn)) return true;
|
|
209
|
+
const token = tokenIn;
|
|
210
|
+
const expectedBalance = (0, import_viem.numberToHex)(amountIn > 0n ? amountIn : 1n, { size: 32 });
|
|
211
|
+
const balance = await readWord(
|
|
212
|
+
client,
|
|
213
|
+
token,
|
|
214
|
+
`${BALANCE_OF_SELECTOR}${encodeAddressArgs(account).slice(2)}`,
|
|
215
|
+
stateOverride
|
|
216
|
+
);
|
|
217
|
+
if (balance !== expectedBalance) return false;
|
|
218
|
+
const allowance = await readWord(
|
|
219
|
+
client,
|
|
220
|
+
token,
|
|
221
|
+
`${ALLOWANCE_SELECTOR}${encodeAddressArgs(account, target).slice(2)}`,
|
|
222
|
+
stateOverride
|
|
223
|
+
);
|
|
224
|
+
return allowance === (0, import_viem.numberToHex)(import_viem.maxUint256, { size: 32 });
|
|
225
|
+
}
|
|
226
|
+
function revertDetail(error) {
|
|
227
|
+
if (!(error instanceof Error)) return void 0;
|
|
228
|
+
const short = error.shortMessage;
|
|
229
|
+
return (short ?? error.message).split("\n")[0]?.trim() || void 0;
|
|
230
|
+
}
|
|
231
|
+
async function assertSwapExecutable(client, params) {
|
|
232
|
+
const { account, tokenIn, amountIn, result } = params;
|
|
233
|
+
const target = result.target;
|
|
234
|
+
const data = result.data;
|
|
235
|
+
let block;
|
|
236
|
+
try {
|
|
237
|
+
block = await client.getBlock({ blockTag: "latest" });
|
|
238
|
+
} catch {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const maxGas = maxGasForBlock(block.gasLimit);
|
|
242
|
+
if (maxGas === 0n) return;
|
|
243
|
+
const stateOverride = buildSwapStateOverride({
|
|
244
|
+
account,
|
|
245
|
+
tokenIn,
|
|
246
|
+
amountIn,
|
|
247
|
+
target,
|
|
248
|
+
msgValue: result.msgValue
|
|
249
|
+
});
|
|
250
|
+
const base = {
|
|
251
|
+
account,
|
|
252
|
+
to: target,
|
|
253
|
+
data,
|
|
254
|
+
value: result.msgValue,
|
|
255
|
+
stateOverride
|
|
256
|
+
};
|
|
257
|
+
try {
|
|
258
|
+
await client.call(base);
|
|
259
|
+
} catch (e) {
|
|
260
|
+
const applied = await swapOverridesApplied({
|
|
261
|
+
client,
|
|
262
|
+
account,
|
|
263
|
+
tokenIn,
|
|
264
|
+
amountIn,
|
|
265
|
+
target,
|
|
266
|
+
stateOverride
|
|
267
|
+
});
|
|
268
|
+
if (!applied) return;
|
|
269
|
+
throw new SwapRevertsError({ cause: e, detail: revertDetail(e) });
|
|
270
|
+
}
|
|
271
|
+
try {
|
|
272
|
+
await client.call({ ...base, gas: maxGas });
|
|
273
|
+
} catch {
|
|
274
|
+
throw new InsufficientLiquidityError();
|
|
275
|
+
}
|
|
276
|
+
try {
|
|
277
|
+
const gas = await client.estimateGas(base);
|
|
278
|
+
if (gas > maxGas) throw new InsufficientLiquidityError();
|
|
279
|
+
} catch (e) {
|
|
280
|
+
if (e instanceof InsufficientLiquidityError) throw e;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
94
284
|
// src/client.ts
|
|
95
285
|
var QUOTE_TUPLE = "tuple(uint8 source, uint256 feeBps, uint256 amountIn, uint256 amountOut)";
|
|
96
286
|
var QUOTE_STRUCT = "struct Quote { uint8 source; uint256 feeBps; uint256 amountIn; uint256 amountOut; }";
|
|
97
|
-
var toQuoterAbi = (lines) => (0,
|
|
287
|
+
var toQuoterAbi = (lines) => (0, import_viem2.parseAbi)([QUOTE_STRUCT, ...lines.map((l) => l.split(QUOTE_TUPLE).join("Quote"))]);
|
|
98
288
|
var quoterAbi = toQuoterAbi(LENS_QUOTER_ABI);
|
|
99
289
|
var DEFAULT_SLIPPAGE = 50;
|
|
100
290
|
function ammName(source) {
|
|
@@ -115,7 +305,7 @@ function mapViaResult(value) {
|
|
|
115
305
|
};
|
|
116
306
|
}
|
|
117
307
|
function involvesNativeOrWnative(tokenIn, tokenOut, wnative) {
|
|
118
|
-
return (0,
|
|
308
|
+
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
309
|
}
|
|
120
310
|
var Lens = class {
|
|
121
311
|
client;
|
|
@@ -170,6 +360,10 @@ var Lens = class {
|
|
|
170
360
|
* - native / WNATIVE ↔ ERC20 → on Gnosis, `ViaToken(sDAI)` directly;
|
|
171
361
|
* elsewhere `buildBestSwapViaETH`
|
|
172
362
|
* - ERC20 ↔ ERC20 → `buildBestSwap` only (optional V4 hooked candidate)
|
|
363
|
+
*
|
|
364
|
+
* After building calldata, simulates the swap with a gas cap of 90% of
|
|
365
|
+
* `block.gasLimit`. Thin pools that quote but cannot fit in a block throw
|
|
366
|
+
* {@link InsufficientLiquidityError}.
|
|
173
367
|
*/
|
|
174
368
|
async quote(params) {
|
|
175
369
|
const {
|
|
@@ -182,7 +376,7 @@ var Lens = class {
|
|
|
182
376
|
exactOut = false,
|
|
183
377
|
hookPoolFee = 0,
|
|
184
378
|
hookTickSpacing = 0,
|
|
185
|
-
hookAddress =
|
|
379
|
+
hookAddress = import_viem2.zeroAddress,
|
|
186
380
|
intermediate
|
|
187
381
|
} = params;
|
|
188
382
|
const routeArgs = {
|
|
@@ -197,60 +391,71 @@ var Lens = class {
|
|
|
197
391
|
hookTickSpacing,
|
|
198
392
|
hookAddress
|
|
199
393
|
};
|
|
394
|
+
let result;
|
|
200
395
|
if (intermediate) {
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
396
|
+
result = mapViaResult(await this.viaToken({ ...routeArgs, mid: intermediate }));
|
|
397
|
+
} else {
|
|
398
|
+
const useViaEth = involvesNativeOrWnative(tokenIn, tokenOut, this.wnative);
|
|
399
|
+
if (useViaEth) {
|
|
400
|
+
if (this.chainId === 100) {
|
|
401
|
+
result = mapViaResult(await this.viaToken({ ...routeArgs, mid: GNOSIS_sDAI }));
|
|
402
|
+
} else {
|
|
403
|
+
const value = await this.client.readContract({
|
|
404
|
+
address: this.lensQuoter,
|
|
405
|
+
abi: this.quoterAbi,
|
|
406
|
+
functionName: "buildBestSwapViaETH",
|
|
407
|
+
args: [
|
|
408
|
+
recipient,
|
|
409
|
+
exactOut,
|
|
410
|
+
tokenIn,
|
|
411
|
+
tokenOut,
|
|
412
|
+
amount,
|
|
413
|
+
BigInt(slippageBps),
|
|
414
|
+
deadline,
|
|
415
|
+
hookPoolFee,
|
|
416
|
+
hookTickSpacing,
|
|
417
|
+
hookAddress
|
|
418
|
+
]
|
|
419
|
+
});
|
|
420
|
+
result = mapViaResult(value);
|
|
421
|
+
}
|
|
422
|
+
} else {
|
|
423
|
+
const [best, target, callData, _amountLimit, msgValue] = await this.client.readContract({
|
|
424
|
+
address: this.lensQuoter,
|
|
425
|
+
abi: this.quoterAbi,
|
|
426
|
+
functionName: "buildBestSwap",
|
|
427
|
+
args: [
|
|
428
|
+
recipient,
|
|
429
|
+
exactOut,
|
|
430
|
+
tokenIn,
|
|
431
|
+
tokenOut,
|
|
432
|
+
amount,
|
|
433
|
+
BigInt(slippageBps),
|
|
434
|
+
deadline,
|
|
435
|
+
hookPoolFee,
|
|
436
|
+
hookTickSpacing,
|
|
437
|
+
hookAddress
|
|
438
|
+
]
|
|
439
|
+
});
|
|
440
|
+
result = {
|
|
441
|
+
amountIn: best.amountIn,
|
|
442
|
+
amountOut: best.amountOut,
|
|
443
|
+
target,
|
|
444
|
+
data: callData,
|
|
445
|
+
msgValue: msgValue ?? 0n,
|
|
446
|
+
isTwoHop: false,
|
|
447
|
+
sourceA: ammName(best.source),
|
|
448
|
+
sourceB: null
|
|
449
|
+
};
|
|
207
450
|
}
|
|
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
451
|
}
|
|
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
|
-
]
|
|
452
|
+
await assertSwapExecutable(this.client, {
|
|
453
|
+
account: recipient,
|
|
454
|
+
tokenIn,
|
|
455
|
+
amountIn: result.amountIn,
|
|
456
|
+
result
|
|
243
457
|
});
|
|
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
|
-
};
|
|
458
|
+
return result;
|
|
254
459
|
}
|
|
255
460
|
};
|
|
256
461
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -260,8 +465,11 @@ var Lens = class {
|
|
|
260
465
|
CHAINS,
|
|
261
466
|
ETH,
|
|
262
467
|
GNOSIS_sDAI,
|
|
468
|
+
InsufficientLiquidityError,
|
|
263
469
|
LENS_QUOTER_ABI,
|
|
264
470
|
Lens,
|
|
471
|
+
SwapRevertsError,
|
|
472
|
+
assertSwapExecutable,
|
|
265
473
|
defaultDeadline
|
|
266
474
|
});
|
|
267
475
|
//# 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,IACj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AAO,aAAa;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 SwapRevertsError,\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\n/**\n * Thrown when the pre-flight simulation shows the built calldata reverts, and the\n * state overrides that make it executable are confirmed to have applied — so the\n * revert is the router's, not an artifact of the simulation.\n *\n * Sending such a swap would waste gas and surface as an opaque wallet failure.\n */\nexport class SwapRevertsError extends Error {\n readonly code = \"SWAP_REVERTS\" as const;\n\n constructor(\n options?: { cause?: unknown; detail?: string },\n message = \"Swap simulation reverted; the router rejected the built calldata.\",\n ) {\n super(options?.detail ? `${message} (${options.detail})` : message, {\n cause: options?.cause,\n });\n this.name = \"SwapRevertsError\";\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: \"0x82150d38288e53AfcF20a5eEee47682c5Ef796d8\",\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, SwapRevertsError } 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, SwapRevertsError } 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/** Scratch address for the override-support probe; holds no real code on any chain. */\nconst PROBE_ADDRESS = \"0x0000000000000000000000000000000000f0f0f0\" as const;\n/** Runtime that returns uint256(1): PUSH1 01, PUSH1 00, MSTORE, PUSH1 20, PUSH1 00, RETURN. */\nconst PROBE_CODE = \"0x600160005260206000f3\" as const;\nconst ONE_WORD = numberToHex(1n, { size: 32 });\n\nconst BALANCE_OF_SELECTOR = \"0x70a08231\";\nconst ALLOWANCE_SELECTOR = \"0xdd62ed3e\";\n\nfunction encodeAddressArgs(...args: Address[]): Hex {\n return encodeAbiParameters(\n args.map(() => ({ type: \"address\" }) as const),\n args,\n );\n}\n\n/** `true` when the node applied our `code` override — i.e. it honours state overrides at all. */\nasync function nodeAppliesStateOverrides(client: PublicClient): Promise<boolean> {\n try {\n const res = await client.call({\n to: PROBE_ADDRESS,\n data: \"0x\",\n stateOverride: [{ address: PROBE_ADDRESS, code: PROBE_CODE }],\n });\n return res.data === ONE_WORD;\n } catch {\n return false;\n }\n}\n\nasync function readWord(\n client: PublicClient,\n to: Address,\n data: Hex,\n stateOverride: StateOverride,\n): Promise<Hex | undefined> {\n try {\n const res = await client.call({ to, data, stateOverride });\n return res.data;\n } catch {\n return undefined;\n }\n}\n\n/**\n * Confirms the overrides that make the swap payable actually took effect.\n *\n * `erc20SpendOverride` guesses the OpenZeppelin storage layout (balances in slot 0,\n * allowances in slot 1). For a token that lays its storage out differently the\n * injected balance/allowance never lands, and the swap then reverts for reasons that\n * have nothing to do with the router — so we must not blame the calldata.\n */\nasync function swapOverridesApplied(params: {\n client: PublicClient;\n account: Address;\n tokenIn: string;\n amountIn: bigint;\n target: Address;\n stateOverride: StateOverride;\n}): Promise<boolean> {\n const { client, account, tokenIn, amountIn, target, stateOverride } = params;\n\n if (!(await nodeAppliesStateOverrides(client))) return false;\n if (isNative(tokenIn)) return true; // only the plain `balance` override is involved\n\n const token = tokenIn as Address;\n const expectedBalance = numberToHex(amountIn > 0n ? amountIn : 1n, { size: 32 });\n\n const balance = await readWord(\n client,\n token,\n `${BALANCE_OF_SELECTOR}${encodeAddressArgs(account).slice(2)}`,\n stateOverride,\n );\n if (balance !== expectedBalance) return false;\n\n const allowance = await readWord(\n client,\n token,\n `${ALLOWANCE_SELECTOR}${encodeAddressArgs(account, target).slice(2)}`,\n stateOverride,\n );\n return allowance === numberToHex(maxUint256, { size: 32 });\n}\n\nfunction revertDetail(error: unknown): string | undefined {\n if (!(error instanceof Error)) return undefined;\n const short = (error as { shortMessage?: string }).shortMessage;\n return (short ?? error.message).split(\"\\n\")[0]?.trim() || undefined;\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 reverts, re-check that the overrides applied. If they\n * did, the router really rejects the calldata → {@link SwapRevertsError}. If\n * they did not (non-standard token storage, RPC without state overrides), 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 (e) {\n const applied = await swapOverridesApplied({\n client,\n account,\n tokenIn,\n amountIn,\n target,\n stateOverride,\n });\n if (!applied) return;\n throw new SwapRevertsError({ cause: e, detail: revertDetail(e) });\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;AAAA;AAEA,IAAAA,eA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AM,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,24 @@
|
|
|
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, SwapRevertsError, 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 reverts, re-check that the overrides applied. If they
|
|
12
|
+
* did, the router really rejects the calldata → {@link SwapRevertsError}. If
|
|
13
|
+
* they did not (non-standard token storage, RPC without state overrides), skip
|
|
14
|
+
* quietly — never false-positive as liquidity.
|
|
15
|
+
*/
|
|
16
|
+
declare function assertSwapExecutable(client: PublicClient, params: {
|
|
17
|
+
account: Address;
|
|
18
|
+
tokenIn: string;
|
|
19
|
+
amountIn: bigint;
|
|
20
|
+
result: QuoteResult;
|
|
21
|
+
}): Promise<void>;
|
|
4
22
|
|
|
5
23
|
/**
|
|
6
24
|
* Lens SDK client: quotes best routes via the unified on-chain LensQuoter
|
|
@@ -33,8 +51,12 @@ declare class Lens {
|
|
|
33
51
|
* - native / WNATIVE ↔ ERC20 → on Gnosis, `ViaToken(sDAI)` directly;
|
|
34
52
|
* elsewhere `buildBestSwapViaETH`
|
|
35
53
|
* - ERC20 ↔ ERC20 → `buildBestSwap` only (optional V4 hooked candidate)
|
|
54
|
+
*
|
|
55
|
+
* After building calldata, simulates the swap with a gas cap of 90% of
|
|
56
|
+
* `block.gasLimit`. Thin pools that quote but cannot fit in a block throw
|
|
57
|
+
* {@link InsufficientLiquidityError}.
|
|
36
58
|
*/
|
|
37
59
|
quote(params: QuoteParams): Promise<QuoteResult>;
|
|
38
60
|
}
|
|
39
61
|
|
|
40
|
-
export { ChainId, Lens, QuoteParams, QuoteResult };
|
|
62
|
+
export { ChainId, Lens, QuoteParams, QuoteResult, assertSwapExecutable };
|