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