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