@anywalletkit/protocol-swap2-core 0.4.0 → 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.
@@ -195,6 +195,10 @@ function normalizeTypedDomain(domain) {
195
195
  return chainId === void 0 || !Number.isSafeInteger(chainId) ? domain : { ...domain, chainId };
196
196
  }
197
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
+ }
198
202
  var SWAP2_ID_PREFIX = "awk1";
199
203
  function encodeId(provider, intentId, sourceTxHash) {
200
204
  const enc = (v) => encodeURIComponent(v).replace(/\./g, "%2E");
@@ -706,7 +710,8 @@ var BaseSwap2Protocol = class extends import_core.Swap2Protocol {
706
710
  approvalAmounts = current === approvalAmount ? [] : [...usdtMainnet && current !== 0n ? [0n] : [], approvalAmount];
707
711
  }
708
712
  const review = this._config.reviewExecution;
709
- if (review && !carried) {
713
+ let reviewed;
714
+ if (review && (!carried || carried.reviewed)) {
710
715
  const chainId = unsigned.chainId !== void 0 ? { chainId: unsigned.chainId } : {};
711
716
  const approvals = approvalAmounts.map((amount) => ({ token: source.contract, spender: unsigned.approvalAddress, amount, ...chainId }));
712
717
  const plan = {
@@ -734,12 +739,28 @@ var BaseSwap2Protocol = class extends import_core.Swap2Protocol {
734
739
  ...cellBody !== void 0 ? { cellBody: true } : {}
735
740
  }
736
741
  };
737
- await review(deepFreeze(structuredCloneSafe(plan)));
738
- if (q.validUntilMs > 0 && q.validUntilMs <= Date.now()) {
739
- 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
+ }
740
755
  }
741
756
  }
742
- 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
+ }
743
764
  if (approvalAmounts.length) {
744
765
  const approve = async (amount) => {
745
766
  const sent2 = await signer.sendTransaction({
@@ -768,7 +789,7 @@ var BaseSwap2Protocol = class extends import_core.Swap2Protocol {
768
789
  if (q.validUntilMs > 0 && q.validUntilMs <= Date.now()) {
769
790
  if (!carried && approvalAmounts.length && revokeApproval) {
770
791
  this.emitPhase("requoting");
771
- 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 } : {} };
772
793
  try {
773
794
  return await this.executeSwap2(signer, snapshot, config, handoff);
774
795
  } catch (error) {