@anywalletkit/protocol-swap2-lifi 0.3.1 → 0.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/dist/protocol.cjs CHANGED
@@ -93,144 +93,162 @@ var GENERIC_SWAPS = {
93
93
  var BRIDGE_INPUT_FEE_MAX_BPS = 100n;
94
94
  var kindOf = (asset) => /^0x0{40}$/i.test(asset) ? "native" : "erc20";
95
95
  var BRIDGE_CALLS = {
96
- "0x1fd8010c": { sourceSwaps: false },
96
+ "0x1fd8010c": { sourceSwaps: false, facet: "Across" },
97
97
  // startBridgeTokensViaAcross (AcrossFacet)
98
- "0x3a3f7332": { sourceSwaps: true },
98
+ "0x3a3f7332": { sourceSwaps: true, facet: "Across" },
99
99
  // swapAndStartBridgeTokensViaAcross (AcrossFacet)
100
- "0xa1f1ce43": { sourceSwaps: false },
100
+ "0xa1f1ce43": { sourceSwaps: false, facet: "AcrossV4" },
101
101
  // startBridgeTokensViaAcrossV4 (AcrossFacetV4)
102
- "0x1794958f": { sourceSwaps: true },
102
+ "0x1794958f": { sourceSwaps: true, facet: "AcrossV4" },
103
103
  // swapAndStartBridgeTokensViaAcrossV4 (AcrossFacetV4)
104
- "0x6a90d66e": { sourceSwaps: false },
104
+ "0x6a90d66e": { sourceSwaps: false, facet: "AcrossV4Swap" },
105
105
  // startBridgeTokensViaAcrossV4Swap (AcrossV4SwapFacet)
106
- "0x9b054bc4": { sourceSwaps: true },
106
+ "0x9b054bc4": { sourceSwaps: true, facet: "AcrossV4Swap" },
107
107
  // swapAndStartBridgeTokensViaAcrossV4Swap (AcrossV4SwapFacet)
108
- "0x6a51e9a9": { sourceSwaps: false },
108
+ "0x6a51e9a9": { sourceSwaps: false, facet: "AllBridge" },
109
109
  // startBridgeTokensViaAllBridge (AllBridgeFacet)
110
- "0x63267469": { sourceSwaps: true },
110
+ "0x63267469": { sourceSwaps: true, facet: "AllBridge" },
111
111
  // swapAndStartBridgeTokensViaAllBridge (AllBridgeFacet)
112
- "0xc9851d0b": { sourceSwaps: false },
112
+ "0xc9851d0b": { sourceSwaps: false, facet: "ArbitrumBridge" },
113
113
  // startBridgeTokensViaArbitrumBridge (ArbitrumBridgeFacet)
114
- "0x3cc9517b": { sourceSwaps: true },
114
+ "0x3cc9517b": { sourceSwaps: true, facet: "ArbitrumBridge" },
115
115
  // swapAndStartBridgeTokensViaArbitrumBridge (ArbitrumBridgeFacet)
116
- "0xe0a4201c": { sourceSwaps: false },
116
+ "0xe0a4201c": { sourceSwaps: false, facet: "CelerCircleBridge" },
117
117
  // startBridgeTokensViaCelerCircleBridge (CelerCircleBridgeFacet)
118
- "0x4f3b0759": { sourceSwaps: true },
118
+ "0x4f3b0759": { sourceSwaps: true, facet: "CelerCircleBridge" },
119
119
  // swapAndStartBridgeTokensViaCelerCircleBridge (CelerCircleBridgeFacet)
120
- "0x0315138f": { sourceSwaps: false },
120
+ "0x0315138f": { sourceSwaps: false, facet: "Centrifuge" },
121
121
  // startBridgeTokensViaCentrifuge (CentrifugeFacet)
122
- "0x8fd4d4dd": { sourceSwaps: true },
122
+ "0x8fd4d4dd": { sourceSwaps: true, facet: "Centrifuge" },
123
123
  // swapAndStartBridgeTokensViaCentrifuge (CentrifugeFacet)
124
- "0x0ad553b3": { sourceSwaps: false },
124
+ "0x0ad553b3": { sourceSwaps: false, facet: "Chainflip" },
125
125
  // startBridgeTokensViaChainflip (ChainflipFacet)
126
- "0xee3314a1": { sourceSwaps: true },
126
+ "0xee3314a1": { sourceSwaps: true, facet: "Chainflip" },
127
127
  // swapAndStartBridgeTokensViaChainflip (ChainflipFacet)
128
- "0x4004633e": { sourceSwaps: false },
129
- // startBridgeTokensViaDeBridgeDln (DeBridgeDlnFacet)
130
- "0x2c7d2db0": { sourceSwaps: true },
131
- // swapAndStartBridgeTokensViaDeBridgeDln (DeBridgeDlnFacet)
132
- "0xbff90b61": { sourceSwaps: false },
128
+ "0xbff90b61": { sourceSwaps: false, facet: "Eco" },
133
129
  // startBridgeTokensViaEco (EcoFacet)
134
- "0x762aea18": { sourceSwaps: true },
130
+ "0x762aea18": { sourceSwaps: true, facet: "Eco" },
135
131
  // swapAndStartBridgeTokensViaEco (EcoFacet)
136
- "0x678786ea": { sourceSwaps: false },
132
+ "0x678786ea": { sourceSwaps: false, facet: "Frax" },
137
133
  // startBridgeTokensViaFrax (FraxFacet)
138
- "0x07b8e4c7": { sourceSwaps: true },
134
+ "0x07b8e4c7": { sourceSwaps: true, facet: "Frax" },
139
135
  // swapAndStartBridgeTokensViaFrax (FraxFacet)
140
- "0x5f9af35d": { sourceSwaps: false },
141
- // startBridgeTokensViaGarden (GardenFacet)
142
- "0x76ad76fe": { sourceSwaps: true },
143
- // swapAndStartBridgeTokensViaGarden (GardenFacet)
144
- "0xfc5f1003": { sourceSwaps: false },
136
+ "0xfc5f1003": { sourceSwaps: false, facet: "GasZip" },
145
137
  // startBridgeTokensViaGasZip (GasZipFacet)
146
- "0x606326ff": { sourceSwaps: true },
138
+ "0x606326ff": { sourceSwaps: true, facet: "GasZip" },
147
139
  // swapAndStartBridgeTokensViaGasZip (GasZipFacet)
148
- "0x6f9206ba": { sourceSwaps: false },
140
+ "0x6f9206ba": { sourceSwaps: false, facet: "Glacis" },
149
141
  // startBridgeTokensViaGlacis (GlacisFacet)
150
- "0x9c4b6dd9": { sourceSwaps: true },
142
+ "0x9c4b6dd9": { sourceSwaps: true, facet: "Glacis" },
151
143
  // swapAndStartBridgeTokensViaGlacis (GlacisFacet)
152
- "0xf66fe519": { sourceSwaps: false },
144
+ "0xf66fe519": { sourceSwaps: false, facet: "GnosisBridge" },
153
145
  // startBridgeTokensViaGnosisBridge (GnosisBridgeFacet)
154
- "0x7bf96e0a": { sourceSwaps: true },
146
+ "0x7bf96e0a": { sourceSwaps: true, facet: "GnosisBridge" },
155
147
  // swapAndStartBridgeTokensViaGnosisBridge (GnosisBridgeFacet)
156
- "0xee9e98e0": { sourceSwaps: false },
148
+ "0xee9e98e0": { sourceSwaps: false, facet: "LayerSwap" },
157
149
  // startBridgeTokensViaLayerSwap (LayerSwapFacet)
158
- "0x4c279d6b": { sourceSwaps: true },
150
+ "0x4c279d6b": { sourceSwaps: true, facet: "LayerSwap" },
159
151
  // swapAndStartBridgeTokensViaLayerSwap (LayerSwapFacet)
160
- "0x7dbcf1d9": { sourceSwaps: false },
152
+ "0x7dbcf1d9": { sourceSwaps: false, facet: "LiFiIntentEscrowV2" },
161
153
  // startBridgeTokensViaLiFiIntentEscrowV2 (LiFiIntentEscrowFacetV2)
162
- "0x6d21c5df": { sourceSwaps: true },
154
+ "0x6d21c5df": { sourceSwaps: true, facet: "LiFiIntentEscrowV2" },
163
155
  // swapAndStartBridgeTokensViaLiFiIntentEscrowV2 (LiFiIntentEscrowFacetV2)
164
- "0x65782c4b": { sourceSwaps: false },
156
+ "0x65782c4b": { sourceSwaps: false, facet: "M0" },
165
157
  // startBridgeTokensViaM0 (M0Facet)
166
- "0xb9eaf86d": { sourceSwaps: true },
158
+ "0xb9eaf86d": { sourceSwaps: true, facet: "M0" },
167
159
  // swapAndStartBridgeTokensViaM0 (M0Facet)
168
- "0xb4875292": { sourceSwaps: false },
160
+ "0xb4875292": { sourceSwaps: false, facet: "Mayan" },
169
161
  // startBridgeTokensViaMayan (MayanFacet)
170
- "0x80c65808": { sourceSwaps: true },
162
+ "0x80c65808": { sourceSwaps: true, facet: "Mayan" },
171
163
  // swapAndStartBridgeTokensViaMayan (MayanFacet)
172
- "0x4213dfff": { sourceSwaps: false },
164
+ "0x4213dfff": { sourceSwaps: false, facet: "MegaETHBridge" },
173
165
  // startBridgeTokensViaMegaETHBridge (MegaETHBridgeFacet)
174
- "0x22256e89": { sourceSwaps: true },
166
+ "0x22256e89": { sourceSwaps: true, facet: "MegaETHBridge" },
175
167
  // swapAndStartBridgeTokensViaMegaETHBridge (MegaETHBridgeFacet)
176
- "0x4698f032": { sourceSwaps: false },
168
+ "0x4698f032": { sourceSwaps: false, facet: "NEARIntents" },
177
169
  // startBridgeTokensViaNEARIntents (NEARIntentsFacet)
178
- "0x02631b09": { sourceSwaps: true },
170
+ "0x02631b09": { sourceSwaps: true, facet: "NEARIntents" },
179
171
  // swapAndStartBridgeTokensViaNEARIntents (NEARIntentsFacet)
180
- "0x782621d8": { sourceSwaps: false },
172
+ "0x782621d8": { sourceSwaps: false, facet: "OmniBridge" },
181
173
  // startBridgeTokensViaOmniBridge (OmniBridgeFacet)
182
- "0x95726782": { sourceSwaps: true },
174
+ "0x95726782": { sourceSwaps: true, facet: "OmniBridge" },
183
175
  // swapAndStartBridgeTokensViaOmniBridge (OmniBridgeFacet)
184
- "0xce8a97a5": { sourceSwaps: false },
176
+ "0xce8a97a5": { sourceSwaps: false, facet: "OptimismBridge" },
185
177
  // startBridgeTokensViaOptimismBridge (OptimismBridgeFacet)
186
- "0x5bb5d448": { sourceSwaps: true },
178
+ "0x5bb5d448": { sourceSwaps: true, facet: "OptimismBridge" },
187
179
  // swapAndStartBridgeTokensViaOptimismBridge (OptimismBridgeFacet)
188
- "0xaf62c7d6": { sourceSwaps: false },
180
+ "0xaf62c7d6": { sourceSwaps: false, facet: "PolygonBridge" },
189
181
  // startBridgeTokensViaPolygonBridge (PolygonBridgeFacet)
190
- "0xb4f37581": { sourceSwaps: true },
182
+ "0xb4f37581": { sourceSwaps: true, facet: "PolygonBridge" },
191
183
  // swapAndStartBridgeTokensViaPolygonBridge (PolygonBridgeFacet)
192
- "0xf434d6ca": { sourceSwaps: false },
184
+ "0xf434d6ca": { sourceSwaps: false, facet: "PolymerCCTP" },
193
185
  // startBridgeTokensViaPolymerCCTP (PolymerCCTPFacet)
194
- "0x17917a4e": { sourceSwaps: true },
186
+ "0x17917a4e": { sourceSwaps: true, facet: "PolymerCCTP" },
195
187
  // swapAndStartBridgeTokensViaPolymerCCTP (PolymerCCTPFacet)
196
- "0x092e8fa4": { sourceSwaps: false },
197
- // startBridgeTokensViaRelayDepository (RelayDepositoryFacet)
198
- "0xa3443faa": { sourceSwaps: true },
199
- // swapAndStartBridgeTokensViaRelayDepository (RelayDepositoryFacet)
200
- "0x3f313808": { sourceSwaps: false },
201
- // startBridgeTokensViaSquid (SquidFacet)
202
- "0xa8f66666": { sourceSwaps: true },
203
- // swapAndStartBridgeTokensViaSquid (SquidFacet)
204
- "0x14d53077": { sourceSwaps: false },
188
+ "0x14d53077": { sourceSwaps: false, facet: "Stargate" },
205
189
  // startBridgeTokensViaStargate (StargateFacetV2)
206
- "0xa6010a66": { sourceSwaps: true },
190
+ "0xa6010a66": { sourceSwaps: true, facet: "Stargate" },
207
191
  // swapAndStartBridgeTokensViaStargate (StargateFacetV2)
208
- "0x0691ff78": { sourceSwaps: false },
192
+ "0x0691ff78": { sourceSwaps: false, facet: "Superset" },
209
193
  // startBridgeTokensViaSuperset (SupersetFacet)
210
- "0xf26e657f": { sourceSwaps: true },
194
+ "0xf26e657f": { sourceSwaps: true, facet: "Superset" },
211
195
  // swapAndStartBridgeTokensViaSuperset (SupersetFacet)
212
- "0xe23b7a08": { sourceSwaps: false },
213
- // startBridgeTokensViaSymbiosis (SymbiosisFacet)
214
- "0xc46059b2": { sourceSwaps: true },
215
- // swapAndStartBridgeTokensViaSymbiosis (SymbiosisFacet)
216
- "0x2541ec57": { sourceSwaps: false },
217
- // startBridgeTokensViaThorSwap (ThorSwapFacet)
218
- "0xad673d88": { sourceSwaps: true },
219
- // swapAndStartBridgeTokensViaThorSwap (ThorSwapFacet)
220
- "0x64261d58": { sourceSwaps: false },
196
+ "0x64261d58": { sourceSwaps: false, facet: "Unit" },
221
197
  // startBridgeTokensViaUnit (UnitFacet)
222
- "0x21a3af52": { sourceSwaps: true }
198
+ "0x21a3af52": { sourceSwaps: true, facet: "Unit" }
223
199
  // swapAndStartBridgeTokensViaUnit (UnitFacet)
224
200
  };
225
- var Reader = class {
201
+ var UNBOUND_BRIDGES = {
202
+ "0x092e8fa4": "Relay",
203
+ // startBridgeTokensViaRelayDepository
204
+ "0xa3443faa": "Relay",
205
+ // swapAndStartBridgeTokensViaRelayDepository
206
+ "0x5f9af35d": "Garden",
207
+ // startBridgeTokensViaGarden
208
+ "0x76ad76fe": "Garden",
209
+ // swapAndStartBridgeTokensViaGarden
210
+ "0x4004633e": "deBridge DLN",
211
+ // startBridgeTokensViaDeBridgeDln
212
+ "0x2c7d2db0": "deBridge DLN",
213
+ // swapAndStartBridgeTokensViaDeBridgeDln
214
+ "0x2541ec57": "ThorSwap",
215
+ // startBridgeTokensViaThorSwap
216
+ "0xad673d88": "ThorSwap",
217
+ // swapAndStartBridgeTokensViaThorSwap
218
+ "0x3f313808": "Squid",
219
+ // startBridgeTokensViaSquid
220
+ "0xa8f66666": "Squid",
221
+ // swapAndStartBridgeTokensViaSquid
222
+ "0xe23b7a08": "Symbiosis",
223
+ // startBridgeTokensViaSymbiosis
224
+ "0xc46059b2": "Symbiosis"
225
+ // swapAndStartBridgeTokensViaSymbiosis
226
+ };
227
+ var Reader = class _Reader {
226
228
  b;
227
- constructor(hex) {
228
- if (!/^0x([0-9a-fA-F]{2})*$/.test(hex)) throw new Error("not hex");
229
- this.b = Uint8Array.from(hex.slice(2).match(/../g) ?? [], (h) => Number.parseInt(h, 16));
229
+ constructor(src) {
230
+ if (typeof src !== "string") {
231
+ this.b = src;
232
+ return;
233
+ }
234
+ if (!/^0x([0-9a-fA-F]{2})*$/.test(src)) throw new Error("not hex");
235
+ this.b = Uint8Array.from(src.slice(2).match(/../g) ?? [], (h) => Number.parseInt(h, 16));
230
236
  }
231
237
  get length() {
232
238
  return this.b.length;
233
239
  }
240
+ /** `n` raw bytes at `at`, as 0x-hex. */
241
+ raw(at, n) {
242
+ if (at < 0 || at + n > this.b.length) throw new Error("out of bounds");
243
+ return `0x${Array.from(this.b.subarray(at, at + n), (x) => x.toString(16).padStart(2, "0")).join("")}`;
244
+ }
245
+ /** The `bytes` whose offset word sits at `head` (relative to `base`), as a reader over its contents only. */
246
+ bytes(base, head) {
247
+ const at = base + this.small(head);
248
+ const n = this.small(at);
249
+ if (at + 32 + n > this.b.length) throw new Error("bytes out of bounds");
250
+ return new _Reader(this.b.subarray(at + 32, at + 32 + n));
251
+ }
234
252
  word(at) {
235
253
  if (at < 0 || at + 32 > this.b.length) throw new Error("out of bounds");
236
254
  let v = 0n;
@@ -288,6 +306,79 @@ function checkSteps(steps, e, finalAsset) {
288
306
  if (!same(last.receivingAssetId, finalAsset)) return `delivers ${last.receivingAssetId}, not ${finalAsset}`;
289
307
  return null;
290
308
  }
309
+ var MAYAN_REFUND_AT = {
310
+ "0xa3a30834": 4 + 32 * 3,
311
+ // Swift v2 createOrderWithToken(tokenIn, amountIn, (payloadType, trader, destAddr, …), customPayload)
312
+ "0x6147435b": 4 + 32 * 3,
313
+ // Swift v2 createOrderWithSig: the same order tuple
314
+ "0xb866e173": 4,
315
+ // Swift createOrderWithEth((trader, tokenOut, …))
316
+ "0x8e8d142b": 4 + 32 * 2,
317
+ // Swift createOrderWithToken(tokenIn, amountIn, (trader, tokenOut, …))
318
+ "0x6111ad25": 4 + 32 * 9,
319
+ // MayanSwap swap(relayerFees, (mayanAddr, mayanChainId, auctionAddr, destAddr, destChainId, referrer, refundAddr), …)
320
+ "0x1eb1cff0": 4 + 32 * 9
321
+ // MayanSwap wrapAndSwapETH: the same recipient tuple
322
+ };
323
+ function mayanRefunds(r, at) {
324
+ const p = r.bytes(at, at + 32 * 2);
325
+ const pos = MAYAN_REFUND_AT[p.raw(0, 4)];
326
+ return pos === void 0 ? [] : [["the Mayan order's refund address", p.address(pos)]];
327
+ }
328
+ function chainflipRefunds(r, at) {
329
+ const p = r.bytes(at, at + 32 * 5);
330
+ if (p.length < 25 || p.raw(0, 1) !== "0x01") throw new Error("unknown Chainflip parameters");
331
+ return [["the Chainflip refund address", p.raw(5, 20)]];
332
+ }
333
+ function acrossSwapRefunds(r, at) {
334
+ const target = r.word(at);
335
+ const c = r.bytes(at, at + 32);
336
+ if (target === 0n) return [["the Across deposit's depositor", c.address(c.small(0)), true]];
337
+ if (target === 1n) {
338
+ const swap = c.small(0);
339
+ return [["the Across deposit's depositor", c.address(swap + c.small(swap + 64) + 96), true]];
340
+ }
341
+ if (target === 2n) return [["the sponsored OFT refund recipient", c.address(c.small(0) + 32), true]];
342
+ if (target === 3n) return [["the sponsored CCTP refund recipient", c.address(64), true]];
343
+ throw new Error("unknown Across Swap API target");
344
+ }
345
+ var FACET_DATA = {
346
+ AcrossV4: { refunds: [["refundAddress", 1]], zeroRefused: true },
347
+ // the SpokePool depositor: an expired deposit refunds the input there
348
+ AcrossV4Swap: { nested: acrossSwapRefunds },
349
+ Centrifuge: { inline: 2, refunds: [["refundRecipient", 1]], zeroRefused: true },
350
+ Chainflip: { nested: chainflipRefunds },
351
+ Eco: { refunds: [["refundRecipient", 5]], zeroRefused: true },
352
+ // the intent's creator, refunded its reward (the input) on expiry
353
+ Frax: { inline: 5, refunds: [["refundRecipient", 3]], zeroRefused: true },
354
+ Glacis: { inline: 4, refunds: [["refundAddress", 1]], zeroRefused: true },
355
+ LayerSwap: { refunds: [["refundRecipient", 2]], zeroRefused: true },
356
+ LiFiIntentEscrowV2: { refunds: [["depositAndRefundAddress", 2]], zeroRefused: true },
357
+ // the escrow order's user
358
+ M0: { inline: 7, refunds: [["refundRecipient", 1], ["orderOwner", 2]], zeroRefused: true },
359
+ // orderOwner gets a cancelled order back
360
+ Mayan: { refunds: [["refundRecipient", 7]], nested: mayanRefunds, zeroRefused: true },
361
+ NEARIntents: { refunds: [["refundRecipient", 6]], zeroRefused: true },
362
+ // also the 1Click quote's refundTo
363
+ PolymerCCTP: { refunds: [["refundRecipient", 5]], zeroRefused: true },
364
+ Stargate: { refunds: [["refundAddress", 4]] },
365
+ // (assetId, sendParams, fee.nativeFee, fee.lzTokenFee, refundAddress): LayerZero's fee refund
366
+ Superset: { refunds: [["refundAddress", 2], ["fallbackEoA", 3]], zeroRefused: true }
367
+ };
368
+ var NO_REFUND_FIELDS = /* @__PURE__ */ new Set(["Across", "AllBridge", "ArbitrumBridge", "CelerCircleBridge", "GasZip", "GnosisBridge", "MegaETHBridge", "OmniBridge", "OptimismBridge", "PolygonBridge", "Unit"]);
369
+ function refundMismatch(r, bridge, e) {
370
+ const spec = FACET_DATA[bridge.facet];
371
+ if (!spec) return NO_REFUND_FIELDS.has(bridge.facet) ? null : `no known refund layout for ${bridge.facet}`;
372
+ const slot = 4 + (bridge.sourceSwaps ? 64 : 32);
373
+ const at = spec.inline ? slot : 4 + r.small(slot);
374
+ const paid = (spec.refunds ?? []).map(([field, word]) => [`${bridge.facet} ${field}`, r.address(at + 32 * word), spec.zeroRefused === true]);
375
+ paid.push(...spec.nested?.(r, at) ?? []);
376
+ for (const [what, to, zeroRefused] of paid) {
377
+ if (zeroRefused && /^0x0{40}$/.test(to)) continue;
378
+ if (!same(to, e.sender) && !same(to, e.receiver)) return `${what} pays ${to}, neither the sender ${e.sender} nor the recipient`;
379
+ }
380
+ return null;
381
+ }
291
382
  function diamondCallMismatch(data, e) {
292
383
  let r;
293
384
  try {
@@ -312,6 +403,8 @@ function diamondCallMismatch(data, e) {
312
403
  }
313
404
  return checkSteps(steps, e, e.toToken);
314
405
  }
406
+ const unbound = UNBOUND_BRIDGES[selector];
407
+ if (unbound) return `${unbound} delivers to a recipient the transaction does not bind (an off-chain order or unchecked facet data)`;
315
408
  const bridge = BRIDGE_CALLS[selector];
316
409
  if (!bridge) return `unknown Diamond call ${selector}`;
317
410
  const head = r.small(4);
@@ -330,6 +423,8 @@ function diamondCallMismatch(data, e) {
330
423
  if (!e.evmDestination) return "the recipient on a non-EVM destination sits in bridge-specific data and cannot be checked here";
331
424
  if (!same(receiver, e.receiver)) return `pays ${receiver}, not ${e.receiver}`;
332
425
  if (hasSourceSwaps !== bridge.sourceSwaps) return `unknown Diamond call ${selector}`;
426
+ const refund = refundMismatch(r, bridge, e);
427
+ if (refund) return refund;
333
428
  if (hasSourceSwaps) {
334
429
  if (minAmount === 0n) return "bridges nothing after the source swaps";
335
430
  return checkSteps(swapsAt(r, 4, 4 + 32), e, sendingAssetId);
@@ -341,8 +436,98 @@ function diamondCallMismatch(data, e) {
341
436
  }
342
437
  }
343
438
 
344
- // src/provider.ts
439
+ // src/solana.ts
345
440
  var import_util2 = require("@anywalletkit/protocol-swap2-core/util");
441
+ var ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
442
+ var MAYAN_ENTRY_PROGRAMS = /* @__PURE__ */ new Set([
443
+ "D8C8iW6zmoKg5TRr8nQ7h14TMWqQX8FiBdj2ju5MF3wa",
444
+ // CPI_PROXY_PROGRAM_ID
445
+ "BLZRi6frs4X4DNLw56V4EXai1b6QVESN1BhHBTYM9VcY",
446
+ // SWIFT_PROGRAM_ID
447
+ "mayan34VedncxdK2XobtvWFDXQASUTBXhUVzt2kKgny",
448
+ // SWIFT_V2_PROGRAM_ID
449
+ "dkpZqrxHFrhziEMQ931GLtfy11nFkCsfMftH9u6QwBU",
450
+ // MCTP_PROGRAM_ID
451
+ "Gx9rivpS3YR8pBFwMuP6omYqVxunpLvLkNn7ubNyuZZ5"
452
+ // FAST_MCTP_PROGRAM_ID
453
+ ]);
454
+ var SYSTEM_PROGRAM = "11111111111111111111111111111111";
455
+ var LIFI_SOLANA_FEE_WALLET = "34FKjAdVcTax2DHqV2XnbXa9J3zmyKcFuFKWbcmgxjgm";
456
+ function base58(bytes) {
457
+ let n = 0n;
458
+ for (const b of bytes) n = n << 8n | BigInt(b);
459
+ let out = "";
460
+ while (n > 0n) {
461
+ out = ALPHABET[Number(n % 58n)] + out;
462
+ n /= 58n;
463
+ }
464
+ for (const b of bytes) {
465
+ if (b !== 0) break;
466
+ out = `1${out}`;
467
+ }
468
+ return out;
469
+ }
470
+ var Cursor = class {
471
+ constructor(b) {
472
+ this.b = b;
473
+ }
474
+ b;
475
+ at = 0;
476
+ byte() {
477
+ if (this.at >= this.b.length) throw new Error("truncated");
478
+ return this.b[this.at++];
479
+ }
480
+ bytes(n) {
481
+ if (n < 0 || this.at + n > this.b.length) throw new Error("truncated");
482
+ const out = this.b.subarray(this.at, this.at + n);
483
+ this.at += n;
484
+ return out;
485
+ }
486
+ /** compact-u16 */
487
+ short() {
488
+ let v = 0;
489
+ for (let shift = 0; shift < 21; shift += 7) {
490
+ const b = this.byte();
491
+ v |= (b & 127) << shift;
492
+ if (!(b & 128)) return v;
493
+ }
494
+ throw new Error("bad length");
495
+ }
496
+ };
497
+ function solanaBridgeTxMismatch(base64, sender) {
498
+ const bytes = (0, import_util2.base64Bytes)(base64);
499
+ if (!bytes) return "the Solana transaction is not base64";
500
+ const c = new Cursor(bytes);
501
+ try {
502
+ c.bytes(64 * c.short());
503
+ const first = c.byte();
504
+ if ((first & 128) === 0) c.at--;
505
+ c.bytes(3);
506
+ const keys = Array.from({ length: c.short() }, () => base58(c.bytes(32)));
507
+ if (keys[0] !== sender) return `the Solana transaction is paid by ${keys[0] ?? "nobody"}, not the sender ${sender}`;
508
+ c.bytes(32);
509
+ let mayan = false;
510
+ const payees = [];
511
+ const n = c.short();
512
+ for (let i = 0; i < n; i++) {
513
+ const program = keys[c.byte()];
514
+ const accounts = Array.from(c.bytes(c.short()));
515
+ const data = c.bytes(c.short());
516
+ if (program !== void 0 && MAYAN_ENTRY_PROGRAMS.has(program)) mayan = true;
517
+ if (program === SYSTEM_PROGRAM && data.length === 12 && data[0] === 2 && data[1] === 0 && data[2] === 0 && data[3] === 0) {
518
+ const to = keys[accounts[1] ?? -1];
519
+ if (to !== void 0 && to !== sender && to !== LIFI_SOLANA_FEE_WALLET) payees.push(to);
520
+ }
521
+ }
522
+ return mayan && payees.length ? `the Solana transaction transfers SOL to ${payees[0]}, which is neither the sender nor LI.FI's fee wallet` : null;
523
+ } catch (err) {
524
+ return `the Solana transaction does not decode (${err instanceof Error ? err.message : String(err)})`;
525
+ }
526
+ }
527
+
528
+ // src/provider.ts
529
+ var import_util3 = require("@anywalletkit/protocol-swap2-core/util");
530
+ var import_validate = require("@anywalletkit/protocol-swap2-core/validate");
346
531
  var import_core = require("@anywalletkit/core");
347
532
  var DEFAULT_LIFI_BASE = "https://li.quest/v1";
348
533
  var LIFI_CHAIN = {
@@ -468,7 +653,7 @@ function trustMap(input, label) {
468
653
  }
469
654
  function lifiToken(params) {
470
655
  const sourceChain = params.sourceChain;
471
- const asset = (0, import_util2.sourceAssetOf)(params);
656
+ const asset = (0, import_util3.sourceAssetOf)(params);
472
657
  if (!asset) return null;
473
658
  if (asset.contract) return asset.contract;
474
659
  return isSolana(sourceChain) ? SOLANA_NATIVE : EVM_ZERO;
@@ -479,7 +664,8 @@ function headers(key) {
479
664
  return h;
480
665
  }
481
666
  var TOOL_KEY = /^[A-Za-z0-9_-]{1,64}$/;
482
- function routeOptions(body) {
667
+ var UNBOUND_TOOLS = Object.freeze(["relaydepository", "garden", "squid", "symbiosis"]);
668
+ function routeOptions(body, excluded) {
483
669
  const routes = body?.routes;
484
670
  if (!Array.isArray(routes)) return [];
485
671
  const out = [];
@@ -489,7 +675,7 @@ function routeOptions(body) {
489
675
  const cross = Array.isArray(step.includedSteps) ? step.includedSteps.find((i) => i?.type === "cross")?.tool : void 0;
490
676
  const tool = typeof cross === "string" ? cross : step.tool;
491
677
  const eta = step.estimate?.executionDuration;
492
- if (typeof tool !== "string" || !TOOL_KEY.test(tool) || typeof eta !== "number" || !Number.isFinite(eta) || eta < 0) continue;
678
+ if (typeof tool !== "string" || !TOOL_KEY.test(tool) || excluded.includes(tool) || typeof eta !== "number" || !Number.isFinite(eta) || eta < 0) continue;
493
679
  if (typeof r.toAmount !== "string" || typeof r.toAmountMin !== "string" || !/^\d{1,60}$/.test(r.toAmount) || !/^\d{1,60}$/.test(r.toAmountMin)) continue;
494
680
  out.push({
495
681
  tool,
@@ -575,12 +761,12 @@ function lifiProvider(config = {}) {
575
761
  fromAmount: params.amountIn,
576
762
  fromAddress,
577
763
  toAddress,
578
- options: { slippage: slippageBps / 1e4, ...integrator().trim() ? { integrator: integrator().trim() } : {} }
764
+ options: { slippage: slippageBps / 1e4, ...solanaSource ? {} : { bridges: { deny: UNBOUND_TOOLS } }, ...integrator().trim() ? { integrator: integrator().trim() } : {} }
579
765
  }),
580
- signal: signal ?? (0, import_util2.timeoutSignal)(8e3)
766
+ signal: signal ?? (0, import_util3.timeoutSignal)(8e3)
581
767
  });
582
768
  if (res.ok) {
583
- const options = routeOptions(await res.json());
769
+ const options = routeOptions(await res.json(), solanaSource ? [] : UNBOUND_TOOLS);
584
770
  if (options.length) choice = cleanChoice(config.chooseRoute(options, params));
585
771
  }
586
772
  } catch {
@@ -600,10 +786,11 @@ function lifiProvider(config = {}) {
600
786
  if (sentSlippage !== void 0) {
601
787
  url.searchParams.set("slippage", String(sentSlippage));
602
788
  }
603
- for (const t of choice?.allowBridges ?? []) url.searchParams.append("allowBridges", t);
604
- for (const t of choice?.denyBridges ?? []) url.searchParams.append("denyBridges", t);
789
+ const unbound = params.sourceChain !== params.destChain && !solanaSource ? UNBOUND_TOOLS : [];
790
+ for (const t of (choice?.allowBridges ?? []).filter((b) => solanaSource || !UNBOUND_TOOLS.includes(b))) url.searchParams.append("allowBridges", t);
791
+ for (const t of /* @__PURE__ */ new Set([...choice?.denyBridges ?? [], ...unbound])) url.searchParams.append("denyBridges", t);
605
792
  if (choice?.order) url.searchParams.set("order", choice.order);
606
- deadline = signal ? void 0 : (0, import_util2.timeoutSignal)(12e3);
793
+ deadline = signal ? void 0 : (0, import_util3.timeoutSignal)(12e3);
607
794
  const res = await fetch(url, { headers: headers(apiKey()), signal: signal ?? deadline });
608
795
  if (!res.ok && choice) {
609
796
  choice = null;
@@ -645,6 +832,10 @@ function lifiProvider(config = {}) {
645
832
  if (solanaSource) {
646
833
  if (!tr.data || tr.data.length < 64 || tr.data.length % 4 !== 0 || !BASE64.test(tr.data)) return reject("the Solana transaction is not base64");
647
834
  if (tr.to !== void 0 || tr.value !== void 0 || tr.gasLimit !== void 0) return reject("the Solana transaction carries EVM fields");
835
+ if (fromChain !== toChain) {
836
+ const why = solanaBridgeTxMismatch(tr.data, fromAddress);
837
+ if (why) return reject(why);
838
+ }
648
839
  } else if (!isHexData(tr.data) || !normalizeAddress(tr.to) || !normalizeAddress(tr.from) || tr.chainId !== expectedFromChain || !tr.value || !tr.gasLimit || !trustedAddress(params.sourceChain, tr.to, trustedTransactionTargets) || !sameAddress(tr.from, fromAddress)) return reject("the transaction is malformed or targets an untrusted contract");
649
840
  if (sentSlippage !== void 0 && (typeof action.slippage !== "number" || Math.abs(action.slippage - sentSlippage) > 1e-12)) return reject("the quote ignored the requested slippage");
650
841
  const sourceIsToken = fromToken !== EVM_ZERO && fromToken !== SOLANA_NATIVE;
@@ -682,7 +873,7 @@ function lifiProvider(config = {}) {
682
873
  if (sentSlippage !== void 0) {
683
874
  const dest = BigInt(est.toAmount);
684
875
  const min = BigInt(minDestAmount);
685
- if (dest > 0n && min < dest * BigInt(1e4 - slippageBps) / 10000n) {
876
+ if (dest > 0n && min < (0, import_validate.slippageFloor)(dest, slippageBps)) {
686
877
  const effective = (0, import_core.bigintToNumber)((dest - min) * 1000000n / dest) / 1e6;
687
878
  const requested = slippageBps / 1e4;
688
879
  const scaled = Math.floor(sentSlippage * requested / effective * 0.995 * 1e6) / 1e6;
@@ -711,6 +902,7 @@ function lifiProvider(config = {}) {
711
902
  toToken: expectedToToken,
712
903
  amountIn: BigInt(params.amountIn),
713
904
  receiver: toAddress,
905
+ sender: fromAddress,
714
906
  minOut: BigInt(minDestAmount),
715
907
  destChainId: BigInt(toChain),
716
908
  sameChain: fromChain === toChain,
@@ -729,6 +921,7 @@ function lifiProvider(config = {}) {
729
921
  sourceAmount: params.amountIn,
730
922
  destAmount: est.toAmount,
731
923
  minDestAmount,
924
+ ...Number.isInteger(action.toToken?.decimals) && action.toToken.decimals >= 0 && action.toToken.decimals <= 36 ? { destDecimals: action.toToken.decimals } : {},
732
925
  feeUsd,
733
926
  etaSeconds: est.executionDuration ?? 120,
734
927
  reliability: 0.97,
@@ -765,7 +958,7 @@ function lifiProvider(config = {}) {
765
958
  url.searchParams.set("txHash", ref.sourceTxHash);
766
959
  if (fromChain) url.searchParams.set("fromChain", fromChain);
767
960
  if (toChain) url.searchParams.set("toChain", toChain);
768
- const res = await fetch(url, { headers: headers(apiKey()), signal: (0, import_util2.timeoutSignal)(12e3) });
961
+ const res = await fetch(url, { headers: headers(apiKey()), signal: (0, import_util3.timeoutSignal)(12e3) });
769
962
  if (!res.ok) return { state: "pending" };
770
963
  const s = await res.json();
771
964
  const map = {
@@ -790,9 +983,62 @@ function lifiProvider(config = {}) {
790
983
  }
791
984
  };
792
985
  }
986
+ function lifiVerifyQuote(params, quote) {
987
+ const fromChain = LIFI_CHAIN[params.sourceChain];
988
+ const toChain = LIFI_CHAIN[params.destChain];
989
+ if (!fromChain || !toChain) return "not a LI.FI chain pair";
990
+ const tx = quote.txRequest;
991
+ if (!tx) return "the quote carries no transaction";
992
+ if (isSolana(params.sourceChain)) {
993
+ if (!tx.serializedTx || tx.data !== void 0 || tx.to !== void 0) return "a Solana source carries a serialized transaction only";
994
+ return fromChain === toChain ? null : solanaBridgeTxMismatch(tx.serializedTx, params.refundAddress || (params.dry !== false ? SOLANA_PLACEHOLDER : ""));
995
+ }
996
+ if (!isHexData(tx.data) || !tx.to || tx.serializedTx !== void 0) return "the quote carries no Diamond call";
997
+ if (!sameAddress(tx.to, diamondFor(params.sourceChain))) return `the call targets ${tx.to}, not the LI.FI Diamond`;
998
+ if (tx.chainId !== void 0 && String(tx.chainId) !== fromChain) return `the call is for chain ${tx.chainId}, not ${fromChain}`;
999
+ const fromToken = lifiToken(params);
1000
+ if (!fromToken) return `unknown source token ${params.sourceToken}`;
1001
+ const sourceIsToken = fromToken !== EVM_ZERO;
1002
+ const dry = params.dry !== false;
1003
+ const fromAddress = params.refundAddress || (dry ? EVM_PLACEHOLDER : "");
1004
+ const toAddress = params.destAddress || (dry ? fromAddress : "");
1005
+ if (!fromAddress || !toAddress) return "a live quote needs the sender and the recipient";
1006
+ let amountIn, minOut, value, nativeFee;
1007
+ try {
1008
+ amountIn = BigInt(params.amountIn);
1009
+ minOut = BigInt(quote.minDestAmount);
1010
+ value = BigInt(tx.value ?? "0");
1011
+ nativeFee = BigInt(tx.nativeFee ?? "0");
1012
+ } catch {
1013
+ return "the quote's amounts are not integers";
1014
+ }
1015
+ if (BigInt(quote.sourceAmount) !== amountIn) return `the quote spends ${quote.sourceAmount}, not the ${amountIn} input`;
1016
+ if (params.minAmountOut !== void 0 && minOut < BigInt(params.minAmountOut)) return `the quoted minimum ${minOut} is below the caller's floor ${params.minAmountOut}`;
1017
+ if (sourceIsToken) {
1018
+ if (value !== nativeFee) return "a token input attaches only its declared bridge fee";
1019
+ if (!tx.approvalAddress || !sameAddress(tx.approvalAddress, diamondFor(params.sourceChain))) return "a token input approves the LI.FI Diamond";
1020
+ } else {
1021
+ if (value < amountIn || value - amountIn !== nativeFee) return "a native input is paid as the input plus its declared bridge fee";
1022
+ if (tx.approvalAddress !== void 0) return "a native input approves nothing";
1023
+ }
1024
+ if (quote.intentId.replace(/~[0-9a-f]{32}$/, "") !== `lifi:${fromChain}:${toChain}`) return "the intent id does not name this route";
1025
+ const why = diamondCallMismatch(tx.data, {
1026
+ fromToken,
1027
+ toToken: nativeFor(params.destChain, params.destToken),
1028
+ amountIn,
1029
+ receiver: toAddress,
1030
+ sender: fromAddress,
1031
+ minOut,
1032
+ destChainId: BigInt(toChain),
1033
+ sameChain: fromChain === toChain,
1034
+ evmDestination: !isSolana(params.destChain)
1035
+ });
1036
+ return why ? `the transaction does not match the request: ${why}` : null;
1037
+ }
793
1038
  var lifiPins = Object.freeze({
794
1039
  callTargets: (chain) => /^eip155:[0-9]+$/.test(chain) ? [diamondFor(chain)] : [],
795
- spenders: (chain) => /^eip155:[0-9]+$/.test(chain) ? [diamondFor(chain)] : []
1040
+ spenders: (chain) => /^eip155:[0-9]+$/.test(chain) ? [diamondFor(chain)] : [],
1041
+ verifyQuote: lifiVerifyQuote
796
1042
  });
797
1043
 
798
1044
  // src/protocol.ts