@anywalletkit/protocol-swap2-lifi 0.2.4 → 0.2.6

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/provider.cjs CHANGED
@@ -72,8 +72,257 @@ function describeLifiActivity(tx, ctx, isDiamond) {
72
72
  });
73
73
  }
74
74
 
75
+ // src/calldata.ts
76
+ var GENERIC_SWAPS = {
77
+ "0x4630a0d8": { single: false },
78
+ // swapTokensGeneric
79
+ "0x4666fc80": { single: true },
80
+ // swapTokensSingleV3ERC20ToERC20
81
+ "0x733214a3": { single: true },
82
+ // swapTokensSingleV3ERC20ToNative
83
+ "0xaf7060fd": { single: true },
84
+ // swapTokensSingleV3NativeToERC20
85
+ "0x5fd9ae2e": { single: false },
86
+ // swapTokensMultipleV3ERC20ToERC20
87
+ "0x2c57e884": { single: false },
88
+ // swapTokensMultipleV3ERC20ToNative
89
+ "0x736eac0b": { single: false }
90
+ // swapTokensMultipleV3NativeToERC20
91
+ };
92
+ var BRIDGE_CALLS = {
93
+ "0x1fd8010c": { sourceSwaps: false },
94
+ // startBridgeTokensViaAcross (AcrossFacet)
95
+ "0x3a3f7332": { sourceSwaps: true },
96
+ // swapAndStartBridgeTokensViaAcross (AcrossFacet)
97
+ "0xa1f1ce43": { sourceSwaps: false },
98
+ // startBridgeTokensViaAcrossV4 (AcrossFacetV4)
99
+ "0x1794958f": { sourceSwaps: true },
100
+ // swapAndStartBridgeTokensViaAcrossV4 (AcrossFacetV4)
101
+ "0x6a90d66e": { sourceSwaps: false },
102
+ // startBridgeTokensViaAcrossV4Swap (AcrossV4SwapFacet)
103
+ "0x9b054bc4": { sourceSwaps: true },
104
+ // swapAndStartBridgeTokensViaAcrossV4Swap (AcrossV4SwapFacet)
105
+ "0x6a51e9a9": { sourceSwaps: false },
106
+ // startBridgeTokensViaAllBridge (AllBridgeFacet)
107
+ "0x63267469": { sourceSwaps: true },
108
+ // swapAndStartBridgeTokensViaAllBridge (AllBridgeFacet)
109
+ "0xc9851d0b": { sourceSwaps: false },
110
+ // startBridgeTokensViaArbitrumBridge (ArbitrumBridgeFacet)
111
+ "0x3cc9517b": { sourceSwaps: true },
112
+ // swapAndStartBridgeTokensViaArbitrumBridge (ArbitrumBridgeFacet)
113
+ "0xe0a4201c": { sourceSwaps: false },
114
+ // startBridgeTokensViaCelerCircleBridge (CelerCircleBridgeFacet)
115
+ "0x4f3b0759": { sourceSwaps: true },
116
+ // swapAndStartBridgeTokensViaCelerCircleBridge (CelerCircleBridgeFacet)
117
+ "0x0315138f": { sourceSwaps: false },
118
+ // startBridgeTokensViaCentrifuge (CentrifugeFacet)
119
+ "0x8fd4d4dd": { sourceSwaps: true },
120
+ // swapAndStartBridgeTokensViaCentrifuge (CentrifugeFacet)
121
+ "0x0ad553b3": { sourceSwaps: false },
122
+ // startBridgeTokensViaChainflip (ChainflipFacet)
123
+ "0xee3314a1": { sourceSwaps: true },
124
+ // swapAndStartBridgeTokensViaChainflip (ChainflipFacet)
125
+ "0x4004633e": { sourceSwaps: false },
126
+ // startBridgeTokensViaDeBridgeDln (DeBridgeDlnFacet)
127
+ "0x2c7d2db0": { sourceSwaps: true },
128
+ // swapAndStartBridgeTokensViaDeBridgeDln (DeBridgeDlnFacet)
129
+ "0xbff90b61": { sourceSwaps: false },
130
+ // startBridgeTokensViaEco (EcoFacet)
131
+ "0x762aea18": { sourceSwaps: true },
132
+ // swapAndStartBridgeTokensViaEco (EcoFacet)
133
+ "0x678786ea": { sourceSwaps: false },
134
+ // startBridgeTokensViaFrax (FraxFacet)
135
+ "0x07b8e4c7": { sourceSwaps: true },
136
+ // swapAndStartBridgeTokensViaFrax (FraxFacet)
137
+ "0x5f9af35d": { sourceSwaps: false },
138
+ // startBridgeTokensViaGarden (GardenFacet)
139
+ "0x76ad76fe": { sourceSwaps: true },
140
+ // swapAndStartBridgeTokensViaGarden (GardenFacet)
141
+ "0xfc5f1003": { sourceSwaps: false },
142
+ // startBridgeTokensViaGasZip (GasZipFacet)
143
+ "0x606326ff": { sourceSwaps: true },
144
+ // swapAndStartBridgeTokensViaGasZip (GasZipFacet)
145
+ "0x6f9206ba": { sourceSwaps: false },
146
+ // startBridgeTokensViaGlacis (GlacisFacet)
147
+ "0x9c4b6dd9": { sourceSwaps: true },
148
+ // swapAndStartBridgeTokensViaGlacis (GlacisFacet)
149
+ "0xf66fe519": { sourceSwaps: false },
150
+ // startBridgeTokensViaGnosisBridge (GnosisBridgeFacet)
151
+ "0x7bf96e0a": { sourceSwaps: true },
152
+ // swapAndStartBridgeTokensViaGnosisBridge (GnosisBridgeFacet)
153
+ "0xee9e98e0": { sourceSwaps: false },
154
+ // startBridgeTokensViaLayerSwap (LayerSwapFacet)
155
+ "0x4c279d6b": { sourceSwaps: true },
156
+ // swapAndStartBridgeTokensViaLayerSwap (LayerSwapFacet)
157
+ "0x7dbcf1d9": { sourceSwaps: false },
158
+ // startBridgeTokensViaLiFiIntentEscrowV2 (LiFiIntentEscrowFacetV2)
159
+ "0x6d21c5df": { sourceSwaps: true },
160
+ // swapAndStartBridgeTokensViaLiFiIntentEscrowV2 (LiFiIntentEscrowFacetV2)
161
+ "0x65782c4b": { sourceSwaps: false },
162
+ // startBridgeTokensViaM0 (M0Facet)
163
+ "0xb9eaf86d": { sourceSwaps: true },
164
+ // swapAndStartBridgeTokensViaM0 (M0Facet)
165
+ "0xb4875292": { sourceSwaps: false },
166
+ // startBridgeTokensViaMayan (MayanFacet)
167
+ "0x80c65808": { sourceSwaps: true },
168
+ // swapAndStartBridgeTokensViaMayan (MayanFacet)
169
+ "0x4213dfff": { sourceSwaps: false },
170
+ // startBridgeTokensViaMegaETHBridge (MegaETHBridgeFacet)
171
+ "0x22256e89": { sourceSwaps: true },
172
+ // swapAndStartBridgeTokensViaMegaETHBridge (MegaETHBridgeFacet)
173
+ "0x4698f032": { sourceSwaps: false },
174
+ // startBridgeTokensViaNEARIntents (NEARIntentsFacet)
175
+ "0x02631b09": { sourceSwaps: true },
176
+ // swapAndStartBridgeTokensViaNEARIntents (NEARIntentsFacet)
177
+ "0x782621d8": { sourceSwaps: false },
178
+ // startBridgeTokensViaOmniBridge (OmniBridgeFacet)
179
+ "0x95726782": { sourceSwaps: true },
180
+ // swapAndStartBridgeTokensViaOmniBridge (OmniBridgeFacet)
181
+ "0xce8a97a5": { sourceSwaps: false },
182
+ // startBridgeTokensViaOptimismBridge (OptimismBridgeFacet)
183
+ "0x5bb5d448": { sourceSwaps: true },
184
+ // swapAndStartBridgeTokensViaOptimismBridge (OptimismBridgeFacet)
185
+ "0xaf62c7d6": { sourceSwaps: false },
186
+ // startBridgeTokensViaPolygonBridge (PolygonBridgeFacet)
187
+ "0xb4f37581": { sourceSwaps: true },
188
+ // swapAndStartBridgeTokensViaPolygonBridge (PolygonBridgeFacet)
189
+ "0xf434d6ca": { sourceSwaps: false },
190
+ // startBridgeTokensViaPolymerCCTP (PolymerCCTPFacet)
191
+ "0x17917a4e": { sourceSwaps: true },
192
+ // swapAndStartBridgeTokensViaPolymerCCTP (PolymerCCTPFacet)
193
+ "0x092e8fa4": { sourceSwaps: false },
194
+ // startBridgeTokensViaRelayDepository (RelayDepositoryFacet)
195
+ "0xa3443faa": { sourceSwaps: true },
196
+ // swapAndStartBridgeTokensViaRelayDepository (RelayDepositoryFacet)
197
+ "0x3f313808": { sourceSwaps: false },
198
+ // startBridgeTokensViaSquid (SquidFacet)
199
+ "0xa8f66666": { sourceSwaps: true },
200
+ // swapAndStartBridgeTokensViaSquid (SquidFacet)
201
+ "0x14d53077": { sourceSwaps: false },
202
+ // startBridgeTokensViaStargate (StargateFacetV2)
203
+ "0xa6010a66": { sourceSwaps: true },
204
+ // swapAndStartBridgeTokensViaStargate (StargateFacetV2)
205
+ "0x0691ff78": { sourceSwaps: false },
206
+ // startBridgeTokensViaSuperset (SupersetFacet)
207
+ "0xf26e657f": { sourceSwaps: true },
208
+ // swapAndStartBridgeTokensViaSuperset (SupersetFacet)
209
+ "0xe23b7a08": { sourceSwaps: false },
210
+ // startBridgeTokensViaSymbiosis (SymbiosisFacet)
211
+ "0xc46059b2": { sourceSwaps: true },
212
+ // swapAndStartBridgeTokensViaSymbiosis (SymbiosisFacet)
213
+ "0x2541ec57": { sourceSwaps: false },
214
+ // startBridgeTokensViaThorSwap (ThorSwapFacet)
215
+ "0xad673d88": { sourceSwaps: true },
216
+ // swapAndStartBridgeTokensViaThorSwap (ThorSwapFacet)
217
+ "0x64261d58": { sourceSwaps: false },
218
+ // startBridgeTokensViaUnit (UnitFacet)
219
+ "0x21a3af52": { sourceSwaps: true }
220
+ // swapAndStartBridgeTokensViaUnit (UnitFacet)
221
+ };
222
+ var Reader = class {
223
+ b;
224
+ constructor(hex) {
225
+ if (!/^0x([0-9a-fA-F]{2})*$/.test(hex)) throw new Error("not hex");
226
+ this.b = Uint8Array.from(hex.slice(2).match(/../g) ?? [], (h) => Number.parseInt(h, 16));
227
+ }
228
+ get length() {
229
+ return this.b.length;
230
+ }
231
+ word(at) {
232
+ if (at < 0 || at + 32 > this.b.length) throw new Error("out of bounds");
233
+ let v = 0n;
234
+ for (let i = at; i < at + 32; i++) v = v << 8n | BigInt(this.b[i]);
235
+ return v;
236
+ }
237
+ /** A word that must fit a JS offset / length (bounded by the calldata). */
238
+ small(at) {
239
+ const v = this.word(at);
240
+ if (v > BigInt(this.b.length)) throw new Error("offset out of bounds");
241
+ return Number(v.toString());
242
+ }
243
+ address(at) {
244
+ const v = this.word(at);
245
+ if (v >> 160n !== 0n) throw new Error("not an address");
246
+ return `0x${v.toString(16).padStart(40, "0")}`;
247
+ }
248
+ bool(at) {
249
+ const v = this.word(at);
250
+ if (v > 1n) throw new Error("not a bool");
251
+ return v === 1n;
252
+ }
253
+ };
254
+ var same = (a, b) => a.toLowerCase() === b.toLowerCase();
255
+ function swapAt(r, at) {
256
+ r.address(at);
257
+ r.address(at + 32);
258
+ const step = { sendingAssetId: r.address(at + 64), receivingAssetId: r.address(at + 96), fromAmount: r.word(at + 128) };
259
+ const bytesAt = at + r.small(at + 160);
260
+ r.small(bytesAt);
261
+ r.bool(at + 192);
262
+ return step;
263
+ }
264
+ function swapsAt(r, base, head) {
265
+ const array = base + r.small(head);
266
+ const n = r.small(array);
267
+ if (n === 0 || n > 16) throw new Error("bad swap count");
268
+ const items = array + 32;
269
+ return Array.from({ length: n }, (_, i) => swapAt(r, items + r.small(items + 32 * i)));
270
+ }
271
+ function checkSteps(steps, e, finalAsset) {
272
+ const first = steps[0];
273
+ const last = steps[steps.length - 1];
274
+ if (!same(first.sendingAssetId, e.fromToken)) return `spends ${first.sendingAssetId}, not ${e.fromToken}`;
275
+ if (first.fromAmount === 0n || first.fromAmount > e.amountIn) return `swaps ${first.fromAmount}, not the ${e.amountIn} spent`;
276
+ if (!same(last.receivingAssetId, finalAsset)) return `delivers ${last.receivingAssetId}, not ${finalAsset}`;
277
+ return null;
278
+ }
279
+ function diamondCallMismatch(data, e) {
280
+ let r;
281
+ try {
282
+ r = new Reader(data);
283
+ } catch {
284
+ return "the calldata is not hex";
285
+ }
286
+ if (r.length < 4 + 32 * 2) return "the calldata is too short";
287
+ const selector = data.slice(0, 10).toLowerCase();
288
+ const generic = GENERIC_SWAPS[selector];
289
+ try {
290
+ if (generic) {
291
+ if (!e.sameChain) return "a same-chain swap call on a cross-chain route";
292
+ const receiver2 = r.address(4 + 96);
293
+ const minAmount2 = r.word(4 + 128);
294
+ const steps = generic.single ? [swapAt(r, 4 + r.small(4 + 160))] : swapsAt(r, 4, 4 + 160);
295
+ if (!e.evmDestination || !same(receiver2, e.receiver)) return `pays ${receiver2}, not ${e.receiver}`;
296
+ if (minAmount2 < e.minOut) return `accepts ${minAmount2}, below the ${e.minOut} shown`;
297
+ return checkSteps(steps, e, e.toToken);
298
+ }
299
+ const bridge = BRIDGE_CALLS[selector];
300
+ if (!bridge) return `unknown Diamond call ${selector}`;
301
+ const head = r.small(4);
302
+ if (head < (bridge.sourceSwaps ? 96 : 64) || head % 32 !== 0 || head > 32 * 32) return `unknown Diamond call ${selector}`;
303
+ const at = 4 + head;
304
+ if (r.small(at + 32) !== 320) return `unknown Diamond call ${selector}`;
305
+ const sendingAssetId = r.address(at + 128);
306
+ const receiver = r.address(at + 160);
307
+ const minAmount = r.word(at + 192);
308
+ const destinationChainId = r.word(at + 224);
309
+ const hasSourceSwaps = r.bool(at + 256);
310
+ const hasDestinationCall = r.bool(at + 288);
311
+ if (e.sameChain) return "a bridge call on a same-chain route";
312
+ if (destinationChainId !== e.destChainId) return `bridges to chain ${destinationChainId}, not ${e.destChainId}`;
313
+ if (hasDestinationCall) return "the route runs a call on the destination chain, whose recipient cannot be checked here";
314
+ if (!e.evmDestination) return "the recipient on a non-EVM destination sits in bridge-specific data and cannot be checked here";
315
+ if (!same(receiver, e.receiver)) return `pays ${receiver}, not ${e.receiver}`;
316
+ if (hasSourceSwaps !== bridge.sourceSwaps) return `unknown Diamond call ${selector}`;
317
+ if (hasSourceSwaps) return checkSteps(swapsAt(r, 4, 4 + 32), e, sendingAssetId);
318
+ if (minAmount === 0n || minAmount > e.amountIn) return `bridges ${minAmount}, not the ${e.amountIn} spent`;
319
+ return same(sendingAssetId, e.fromToken) ? null : `bridges ${sendingAssetId}, not ${e.fromToken}`;
320
+ } catch (err) {
321
+ return `the Diamond calldata does not decode (${err instanceof Error ? err.message : String(err)})`;
322
+ }
323
+ }
324
+
75
325
  // src/provider.ts
76
- var import_swap2_assets = require("@anywalletkit/swap2-assets");
77
326
  var import_util2 = require("@anywalletkit/protocol-swap2-core/util");
78
327
  var import_core = require("@anywalletkit/core");
79
328
  var DEFAULT_LIFI_BASE = "https://li.quest/v1";
@@ -198,8 +447,9 @@ function trustMap(input, label) {
198
447
  }
199
448
  return out;
200
449
  }
201
- function lifiToken(sourceChain, symbol) {
202
- const asset = (0, import_swap2_assets.ccAsset)(sourceChain, symbol);
450
+ function lifiToken(params) {
451
+ const sourceChain = params.sourceChain;
452
+ const asset = (0, import_util2.sourceAssetOf)(params);
203
453
  if (!asset) return null;
204
454
  if (asset.contract) return asset.contract;
205
455
  return isSolana(sourceChain) ? SOLANA_NATIVE : EVM_ZERO;
@@ -281,10 +531,14 @@ function lifiProvider(config = {}) {
281
531
  const fromChain = LIFI_CHAIN[params.sourceChain];
282
532
  const toChain = LIFI_CHAIN[params.destChain];
283
533
  if (!fromChain || !toChain) return null;
284
- const fromToken = lifiToken(params.sourceChain, params.sourceToken);
534
+ const fromToken = lifiToken(params);
285
535
  if (!fromToken) return null;
286
536
  const solanaSource = isSolana(params.sourceChain);
287
- const fromAddress = params.refundAddress ?? (solanaSource ? SOLANA_PLACEHOLDER : EVM_PLACEHOLDER);
537
+ const dry = params.dry !== false;
538
+ const fromAddress = params.refundAddress || (dry ? solanaSource ? SOLANA_PLACEHOLDER : EVM_PLACEHOLDER : "");
539
+ const toAddress = params.destAddress || (dry ? fromAddress : "");
540
+ if (!fromAddress) return reject("a live quote needs the sender's address");
541
+ if (!toAddress) return reject("a live quote needs a recipient");
288
542
  const slippageBps = params.slippageBps ?? defaultSlippageBps;
289
543
  let sentSlippage = slippageBps / 1e4;
290
544
  let deadline;
@@ -301,7 +555,7 @@ function lifiProvider(config = {}) {
301
555
  toTokenAddress: nativeFor(params.destChain, params.destToken),
302
556
  fromAmount: params.amountIn,
303
557
  fromAddress,
304
- toAddress: params.destAddress || fromAddress,
558
+ toAddress,
305
559
  options: { slippage: slippageBps / 1e4, ...integrator().trim() ? { integrator: integrator().trim() } : {} }
306
560
  }),
307
561
  signal: signal ?? (0, import_util2.timeoutSignal)(8e3)
@@ -321,7 +575,6 @@ function lifiProvider(config = {}) {
321
575
  url.searchParams.set("toToken", nativeFor(params.destChain, params.destToken));
322
576
  url.searchParams.set("fromAmount", params.amountIn);
323
577
  url.searchParams.set("fromAddress", fromAddress);
324
- const toAddress = params.destAddress || fromAddress;
325
578
  url.searchParams.set("toAddress", toAddress);
326
579
  const integratorId = integrator().trim();
327
580
  if (integratorId) url.searchParams.set("integrator", integratorId);
@@ -433,6 +686,19 @@ function lifiProvider(config = {}) {
433
686
  params.onRefusal?.(`lifi: same-chain route settles asynchronously (${duration} s via ${q.tool ?? "unknown tool"}) \u2014 a same-chain swap must deliver in the same transaction`);
434
687
  return null;
435
688
  }
689
+ if (!solanaSource) {
690
+ const why = diamondCallMismatch(tr.data, {
691
+ fromToken,
692
+ toToken: expectedToToken,
693
+ amountIn: BigInt(params.amountIn),
694
+ receiver: toAddress,
695
+ minOut: BigInt(minDestAmount),
696
+ destChainId: BigInt(toChain),
697
+ sameChain: fromChain === toChain,
698
+ evmDestination: !isSolana(params.destChain)
699
+ });
700
+ if (why) return reject(`the transaction does not match the quote: ${why}`);
701
+ }
436
702
  return {
437
703
  provider: "lifi",
438
704
  // Status is tracked by SOURCE TX HASH + the chain pair.