@anywalletkit/protocol-swap2-core 0.3.2 → 0.4.1

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.
@@ -183,17 +183,22 @@ function declaredNativeFee(q, chain) {
183
183
  if (fee > cap) throw new Error(`swap2: quoted native fee ${fee} exceeds the cap ${cap} for ${chain}`);
184
184
  return fee;
185
185
  }
186
+ var safeChainId = (v) => v >= 0n && v <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(v.toString()) : void 0;
186
187
  function normalizeTypedDomain(domain) {
187
188
  if (!domain || typeof domain !== "object") return domain;
188
189
  const raw = domain.chainId;
189
190
  let chainId;
190
191
  if (typeof raw === "number") chainId = raw;
191
- else if (typeof raw === "bigint") chainId = Number(raw);
192
+ else if (typeof raw === "bigint") chainId = safeChainId(raw);
192
193
  else if (typeof raw === "string" && /^\d+$/.test(raw)) chainId = Number(raw);
193
- else if (typeof raw === "string" && /^0x[0-9a-fA-F]+$/.test(raw)) chainId = Number(BigInt(raw));
194
+ else if (typeof raw === "string" && /^0x[0-9a-fA-F]+$/.test(raw)) chainId = safeChainId(BigInt(raw));
194
195
  return chainId === void 0 || !Number.isSafeInteger(chainId) ? domain : { ...domain, chainId };
195
196
  }
196
197
  var DEFAULT_SERIALIZED_NATIVE_ALLOWANCE = 20000000n;
198
+ function reviewedIdentity(plan) {
199
+ const { feeUsd: _fee, etaSeconds: _eta, validUntilMs: _valid, approvals: _approvals, destAmount: _dest, requoted: _requoted, ...pinned } = plan;
200
+ return JSON.stringify(pinned, (_k, v) => typeof v === "bigint" ? v.toString() : v);
201
+ }
197
202
  var SWAP2_ID_PREFIX = "awk1";
198
203
  function encodeId(provider, intentId, sourceTxHash) {
199
204
  const enc = (v) => encodeURIComponent(v).replace(/\./g, "%2E");
@@ -336,7 +341,7 @@ function assertQuoteConstraints(q, options) {
336
341
  }
337
342
  if (options.slippage !== void 0) {
338
343
  const scale = 1000000000n;
339
- const permitted = BigInt(floorScaled(options.slippage, Number(scale)));
344
+ const permitted = BigInt(floorScaled(options.slippage, 1e9));
340
345
  const required = nominal * (scale - permitted) / scale;
341
346
  if (guaranteed < required) {
342
347
  throw new Error(`swap2: quote exceeds caller slippage (${options.slippage})`);
@@ -705,7 +710,8 @@ var BaseSwap2Protocol = class extends import_core.Swap2Protocol {
705
710
  approvalAmounts = current === approvalAmount ? [] : [...usdtMainnet && current !== 0n ? [0n] : [], approvalAmount];
706
711
  }
707
712
  const review = this._config.reviewExecution;
708
- if (review && !carried) {
713
+ let reviewed;
714
+ if (review && (!carried || carried.reviewed)) {
709
715
  const chainId = unsigned.chainId !== void 0 ? { chainId: unsigned.chainId } : {};
710
716
  const approvals = approvalAmounts.map((amount) => ({ token: source.contract, spender: unsigned.approvalAddress, amount, ...chainId }));
711
717
  const plan = {
@@ -733,12 +739,28 @@ var BaseSwap2Protocol = class extends import_core.Swap2Protocol {
733
739
  ...cellBody !== void 0 ? { cellBody: true } : {}
734
740
  }
735
741
  };
736
- await review(deepFreeze(structuredCloneSafe(plan)));
737
- if (q.validUntilMs > 0 && q.validUntilMs <= Date.now()) {
738
- throw new Swap2QuoteError("swap2: quote expired during review \u2014 nothing was signed");
742
+ const previous = carried?.reviewed;
743
+ if (!previous || reviewedIdentity(previous) !== reviewedIdentity(plan) || plan.destAmount < previous.destAmount) {
744
+ try {
745
+ reviewed = deepFreeze(structuredCloneSafe(previous ? { ...plan, requoted: true } : plan));
746
+ await review(reviewed);
747
+ if (q.validUntilMs > 0 && q.validUntilMs <= Date.now()) {
748
+ throw new Swap2QuoteError(`swap2: quote expired during review \u2014 ${previous ? "the allowance was revoked" : "nothing was signed"}`);
749
+ }
750
+ } catch (error) {
751
+ if (previous) await revokeApproval?.().catch(() => {
752
+ });
753
+ throw error;
754
+ }
739
755
  }
740
756
  }
741
- await this.assertNativeGas(signer, unsigned, approvalAmounts, source.contract ?? void 0, !!unsigned.typedData || serialized || !!tokenId);
757
+ try {
758
+ await this.assertNativeGas(signer, unsigned, approvalAmounts, source.contract ?? void 0, !!unsigned.typedData || serialized || !!tokenId);
759
+ } catch (error) {
760
+ if (carried) await revokeApproval?.().catch(() => {
761
+ });
762
+ throw error;
763
+ }
742
764
  if (approvalAmounts.length) {
743
765
  const approve = async (amount) => {
744
766
  const sent2 = await signer.sendTransaction({
@@ -767,7 +789,7 @@ var BaseSwap2Protocol = class extends import_core.Swap2Protocol {
767
789
  if (q.validUntilMs > 0 && q.validUntilMs <= Date.now()) {
768
790
  if (!carried && approvalAmounts.length && revokeApproval) {
769
791
  this.emitPhase("requoting");
770
- const handoff = { token: source.contract, spender: unsigned.approvalAddress, amount: approvalAmount, revoke: revokeApproval, adopted: false };
792
+ const handoff = { token: source.contract, spender: unsigned.approvalAddress, amount: approvalAmount, revoke: revokeApproval, adopted: false, ...reviewed ? { reviewed } : {} };
771
793
  try {
772
794
  return await this.executeSwap2(signer, snapshot, config, handoff);
773
795
  } catch (error) {