@trustware/sdk-staging 1.1.17-staging.7 → 1.1.18-staging.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.
package/dist/index.cjs CHANGED
@@ -633,7 +633,7 @@ var init_constants = __esm({
633
633
  "src/constants.ts"() {
634
634
  "use strict";
635
635
  SDK_NAME = "@trustware/sdk";
636
- SDK_VERSION = "1.1.17-staging.7";
636
+ SDK_VERSION = "1.1.18-staging.1";
637
637
  API_ROOT = "https://bv-staging-api.trustware.io";
638
638
  GTM_ID = "GTM-TZDGNCXB";
639
639
  API_PREFIX = "/api";
@@ -4960,6 +4960,32 @@ var init_routeError = __esm({
4960
4960
  }
4961
4961
  });
4962
4962
 
4963
+ // src/core/routeApprovals.ts
4964
+ function approvalAmountWei(approval, stepIndex) {
4965
+ const { amount } = approval;
4966
+ if (amount === void 0 || amount === "") {
4967
+ throw new Error(
4968
+ `Route approval ${stepIndex} (spender ${String(approval.spender)}) has no amount.`
4969
+ );
4970
+ }
4971
+ if (!DECIMAL_INTEGER.test(amount)) {
4972
+ throw new Error(
4973
+ `Route approval ${stepIndex} (spender ${String(approval.spender)}) has an invalid amount "${amount}".`
4974
+ );
4975
+ }
4976
+ return BigInt(amount);
4977
+ }
4978
+ function assertValidRouteApprovals(approvals) {
4979
+ (approvals ?? []).forEach(approvalAmountWei);
4980
+ }
4981
+ var DECIMAL_INTEGER;
4982
+ var init_routeApprovals = __esm({
4983
+ "src/core/routeApprovals.ts"() {
4984
+ "use strict";
4985
+ DECIMAL_INTEGER = /^\d+$/;
4986
+ }
4987
+ });
4988
+
4963
4989
  // src/core/routeValue.ts
4964
4990
  function parseUsd(value) {
4965
4991
  if (value == null) return null;
@@ -5120,6 +5146,7 @@ async function buildRoute(body, signal) {
5120
5146
  if (!txReq?.data) {
5121
5147
  throw new Error("Invalid route: missing transaction data");
5122
5148
  }
5149
+ assertValidRouteApprovals(route?.execution?.approvals);
5123
5150
  assertRouteDeliversValue(route);
5124
5151
  return { intentId, txReq, actions, finalExchangeRate, route, sponsorship };
5125
5152
  }
@@ -5240,6 +5267,7 @@ var init_routes = __esm({
5240
5267
  "use strict";
5241
5268
  init_http();
5242
5269
  init_routeError();
5270
+ init_routeApprovals();
5243
5271
  init_routeValue();
5244
5272
  init_store();
5245
5273
  init_address();
@@ -6382,11 +6410,13 @@ async function ensureApprovals(w, approvals, fallbackChainId, intentId, routeAmo
6382
6410
  for (const [stepIndex, approval] of approvals.entries()) {
6383
6411
  const spender = approval.spender;
6384
6412
  const tokenAddress = approval.tokenAddress;
6385
- const amount = approval.amount;
6386
- if (!spender || !tokenAddress || !amount) continue;
6413
+ if (!spender || !tokenAddress) continue;
6387
6414
  const chainId = approval.chainId || String(fallbackChainId ?? "");
6388
6415
  if (!chainId) continue;
6389
- const amountWei = requiredApprovalAmount(BigInt(amount), routeAmountWei);
6416
+ const amountWei = requiredApprovalAmount(
6417
+ approvalAmountWei(approval, stepIndex),
6418
+ routeAmountWei
6419
+ );
6390
6420
  let currentAllowance = 0n;
6391
6421
  try {
6392
6422
  const { allowance } = await getEVMAllowance({
@@ -6472,7 +6502,6 @@ async function sendRouteTransactionReporting(b, fallbackChainId, options, onAcce
6472
6502
  )}).`
6473
6503
  );
6474
6504
  }
6475
- await ensureWalletOnChain(w, target);
6476
6505
  if (!validatedSponsorship && !options?.approvalsEnsured) {
6477
6506
  await ensureApprovals(
6478
6507
  w,
@@ -6482,6 +6511,7 @@ async function sendRouteTransactionReporting(b, fallbackChainId, options, onAcce
6482
6511
  BigInt(b.route?.estimate?.fromAmount ?? "0")
6483
6512
  );
6484
6513
  }
6514
+ await ensureWalletOnChain(w, target);
6485
6515
  if (w.type === "eip1193") {
6486
6516
  const from = await w.getAddress();
6487
6517
  const hexValue = value ? `0x${value.toString(16)}` : "0x0";
@@ -6648,6 +6678,7 @@ var import_viem2, erc20ApproveAbi, NOT_FOUND_GRACE_MS, UNLIMITED_APPROVAL_FLOOR;
6648
6678
  var init_tx = __esm({
6649
6679
  "src/core/tx.ts"() {
6650
6680
  "use strict";
6681
+ init_routeApprovals();
6651
6682
  init_routeValue();
6652
6683
  init_wallets();
6653
6684
  init_routes();
@@ -9948,11 +9979,7 @@ function mapRouteFacts(facts) {
9948
9979
  return INVALID_ADDRESS;
9949
9980
  }
9950
9981
  if (!facts.allDeclined) {
9951
- return {
9952
- category: "route_error",
9953
- title: "Route Unavailable",
9954
- message: "A routing provider did not respond. Please try again in a moment."
9955
- };
9982
+ return ROUTING_BUSY;
9956
9983
  }
9957
9984
  const has = (code) => facts.codes.includes(code);
9958
9985
  if (has(RouteDeclineCode.FeesExceedOutput)) {
@@ -9972,12 +9999,8 @@ function mapRouteFacts(facts) {
9972
9999
  message: minimum ? `This route needs at least ${minimum}. Try a larger amount.` : "This amount is below the minimum for this route. Try a larger amount."
9973
10000
  };
9974
10001
  }
9975
- if (lowLiquidity && !tooLow) {
9976
- return {
9977
- category: "route_error",
9978
- title: "Insufficient Liquidity",
9979
- message: "Not enough liquidity for this swap. Try a smaller amount or different token."
9980
- };
10002
+ if (facts.codes.every((code) => code === RouteDeclineCode.InsufficientLiquidity)) {
10003
+ return INSUFFICIENT_LIQUIDITY;
9981
10004
  }
9982
10005
  if (facts.codes.every((code) => code === RouteDeclineCode.DestinationCallFailed)) {
9983
10006
  return DESTINATION_CALL_FAILED;
@@ -10029,11 +10052,7 @@ function classifyError(raw, msg) {
10029
10052
  };
10030
10053
  }
10031
10054
  if (bm.includes("liquidity") || bm.includes("slippage") || bm.includes("price impact")) {
10032
- return {
10033
- category: "route_error",
10034
- title: "Insufficient Liquidity",
10035
- message: "Not enough liquidity for this swap. Try a smaller amount or different token."
10036
- };
10055
+ return INSUFFICIENT_LIQUIDITY;
10037
10056
  }
10038
10057
  if (bm.includes("insufficient funds") || bm.includes("insufficient balance")) {
10039
10058
  return {
@@ -10069,12 +10088,8 @@ function classifyError(raw, msg) {
10069
10088
  message: "Could not get a quote right now. Please try again."
10070
10089
  };
10071
10090
  }
10072
- if (lower.includes("not enough liquidity") || lower.includes("insufficient liquidity") || lower.includes("price impact too high")) {
10073
- return {
10074
- category: "route_error",
10075
- title: "Insufficient Liquidity",
10076
- message: "Not enough liquidity for this swap. Try a smaller amount or different token."
10077
- };
10091
+ if (lower.includes("enough liquidity") || lower.includes("insufficient liquidity") || lower.includes("price impact too high")) {
10092
+ return INSUFFICIENT_LIQUIDITY;
10078
10093
  }
10079
10094
  if (lower.includes("destination contract call")) {
10080
10095
  return DESTINATION_CALL_FAILED;
@@ -10082,6 +10097,9 @@ function classifyError(raw, msg) {
10082
10097
  if (lower.includes("invalid address") || lower.includes("address is not valid")) {
10083
10098
  return INVALID_ADDRESS;
10084
10099
  }
10100
+ if (lower.includes("routing is busy")) {
10101
+ return ROUTING_BUSY;
10102
+ }
10085
10103
  if (lower.includes("did not respond") || lower.includes("routing provider")) {
10086
10104
  return {
10087
10105
  category: "route_error",
@@ -10160,7 +10178,7 @@ function classifyError(raw, msg) {
10160
10178
  message: "An unexpected error occurred. Please try again."
10161
10179
  };
10162
10180
  }
10163
- var SELF_MAPPED, INVALID_ADDRESS, DESTINATION_CALL_FAILED;
10181
+ var SELF_MAPPED, INVALID_ADDRESS, DESTINATION_CALL_FAILED, INSUFFICIENT_LIQUIDITY, ROUTING_BUSY;
10164
10182
  var init_mapError = __esm({
10165
10183
  "src/widget/lib/mapError.ts"() {
10166
10184
  "use strict";
@@ -10181,6 +10199,16 @@ var init_mapError = __esm({
10181
10199
  title: "Destination Call Failed",
10182
10200
  message: "The destination contract call could not be simulated. Check the call data and target."
10183
10201
  };
10202
+ INSUFFICIENT_LIQUIDITY = {
10203
+ category: "route_error",
10204
+ title: "Insufficient Liquidity",
10205
+ message: "There isn't enough liquidity to route this swap right now. Try a different token or network, or a smaller amount."
10206
+ };
10207
+ ROUTING_BUSY = {
10208
+ category: "route_error",
10209
+ title: "Route Unavailable",
10210
+ message: "Routing is busy right now. Please wait a moment and try again."
10211
+ };
10184
10212
  }
10185
10213
  });
10186
10214
 
@@ -20092,6 +20120,37 @@ var init_SuccessSummaryCard = __esm({
20092
20120
  }
20093
20121
  });
20094
20122
 
20123
+ // src/widget/helpers/plannedApprovals.ts
20124
+ function plannedErc20Approvals(approvals, route) {
20125
+ return (approvals ?? []).flatMap((approval, stepIndex) => {
20126
+ const token = approval.tokenAddress ?? route.fromTokenAddress;
20127
+ const spender = approval.spender;
20128
+ if (!isEvmAddress2(token) || !isEvmAddress2(spender)) return [];
20129
+ return [
20130
+ {
20131
+ stepIndex,
20132
+ chainId: approval.chainId || route.chainId,
20133
+ token,
20134
+ spender,
20135
+ // A planned 0 is the reset-to-zero step USDT-style tokens need
20136
+ // before an existing allowance can change; it is kept as 0.
20137
+ amountWei: requiredApprovalAmount(
20138
+ approvalAmountWei(approval, stepIndex),
20139
+ route.amountWei
20140
+ )
20141
+ }
20142
+ ];
20143
+ });
20144
+ }
20145
+ var init_plannedApprovals = __esm({
20146
+ "src/widget/helpers/plannedApprovals.ts"() {
20147
+ "use strict";
20148
+ init_routeApprovals();
20149
+ init_tx();
20150
+ init_chainHelpers();
20151
+ }
20152
+ });
20153
+
20095
20154
  // src/widget/lib/routeChange.ts
20096
20155
  function describeRouteChange(change) {
20097
20156
  switch (change.kind) {
@@ -33389,9 +33448,6 @@ var init_smart_account2 = __esm({
33389
33448
  });
33390
33449
 
33391
33450
  // src/widget/features/transaction/hooks/useTransactionActionModel.ts
33392
- function sleep3(ms) {
33393
- return new Promise((resolve) => setTimeout(resolve, ms));
33394
- }
33395
33451
  function useTransactionActionModel({
33396
33452
  actionErrorMessage,
33397
33453
  amountWei,
@@ -33429,92 +33485,72 @@ function useTransactionActionModel({
33429
33485
  selectedChain?.id,
33430
33486
  selectedChain?.networkIdentifier
33431
33487
  ]);
33432
- const selectedTokenOnBackendChain = (0, import_react38.useMemo)(() => {
33433
- if (!backendChainId2) return false;
33434
- const target = normalizeChainKey(backendChainId2);
33435
- return [
33436
- selectedChain?.chainId,
33437
- selectedChain?.id,
33438
- selectedChain?.networkIdentifier
33439
- ].some((c) => {
33440
- const k = normalizeChainKey(c ?? null);
33441
- return k !== "" && k === target;
33442
- });
33443
- }, [
33444
- backendChainId2,
33445
- selectedChain?.chainId,
33446
- selectedChain?.id,
33447
- selectedChain?.networkIdentifier
33448
- ]);
33449
33488
  const isNativeSelected = (0, import_react38.useMemo)(() => {
33450
33489
  const address = selectedToken?.address;
33451
33490
  return !address || isNativeTokenAddress(address, chainType);
33452
33491
  }, [chainType, selectedToken?.address]);
33453
- const spender = (0, import_react38.useMemo)(() => {
33454
- const txReq = routeResult?.txReq;
33455
- const addr = txReq?.to ?? txReq?.target;
33456
- return addr ?? null;
33457
- }, [routeResult?.txReq]);
33458
- const [allowanceWei, setAllowanceWei] = (0, import_react38.useState)(0n);
33492
+ const requiredApprovals = (0, import_react38.useMemo)(
33493
+ () => isEvm && !isNativeSelected && !routeResult?.sponsorship ? plannedErc20Approvals(routeResult?.route?.execution?.approvals, {
33494
+ fromTokenAddress: selectedToken?.address,
33495
+ chainId: backendChainId2 ?? "",
33496
+ amountWei
33497
+ }) : [],
33498
+ [
33499
+ amountWei,
33500
+ backendChainId2,
33501
+ isEvm,
33502
+ isNativeSelected,
33503
+ routeResult?.route?.execution?.approvals,
33504
+ routeResult?.sponsorship,
33505
+ selectedToken?.address
33506
+ ]
33507
+ );
33508
+ const [unmetApprovals, setUnmetApprovals] = (0, import_react38.useState)(
33509
+ []
33510
+ );
33459
33511
  const [isReadingAllowance, setIsReadingAllowance] = (0, import_react38.useState)(false);
33460
33512
  const [isApproving, setIsApproving] = (0, import_react38.useState)(false);
33461
33513
  const [gasReservationWei, setGasReservationWei] = (0, import_react38.useState)(0n);
33514
+ const findUnmetApprovals = (0, import_react38.useCallback)(async () => {
33515
+ if (!walletAddress) return [];
33516
+ const met = await Promise.all(
33517
+ requiredApprovals.map(async (approval) => {
33518
+ try {
33519
+ const { allowance } = await getEVMAllowance({
33520
+ chainId: approval.chainId,
33521
+ tokenAddress: approval.token,
33522
+ ownerAddress: walletAddress,
33523
+ spenderAddress: approval.spender
33524
+ });
33525
+ return approvalSatisfied(
33526
+ BigInt(allowance || "0"),
33527
+ approval.amountWei
33528
+ );
33529
+ } catch {
33530
+ return false;
33531
+ }
33532
+ })
33533
+ );
33534
+ return requiredApprovals.filter((_, index) => !met[index]);
33535
+ }, [requiredApprovals, walletAddress]);
33462
33536
  const readAllowance = (0, import_react38.useCallback)(async () => {
33463
- if (!isEvm || isNativeSelected || !!routeResult?.sponsorship || // sponsored routes: SA approves bridge inside the UO batch
33464
- !backendChainId2 || !selectedTokenOnBackendChain || !walletAddress || !spender || !selectedToken?.address) {
33465
- setAllowanceWei(0n);
33537
+ if (requiredApprovals.length === 0) {
33538
+ setUnmetApprovals([]);
33466
33539
  return;
33467
33540
  }
33541
+ setIsReadingAllowance(true);
33468
33542
  try {
33469
- setIsReadingAllowance(true);
33470
- const result = await getEVMAllowance({
33471
- chainId: backendChainId2,
33472
- tokenAddress: selectedToken.address,
33473
- ownerAddress: walletAddress,
33474
- spenderAddress: spender
33475
- });
33476
- setAllowanceWei(BigInt(result.allowance || "0"));
33477
- } catch {
33478
- setAllowanceWei(0n);
33543
+ setUnmetApprovals(await findUnmetApprovals());
33479
33544
  } finally {
33480
33545
  setIsReadingAllowance(false);
33481
33546
  }
33482
- }, [
33483
- backendChainId2,
33484
- isEvm,
33485
- isNativeSelected,
33486
- routeResult?.sponsorship,
33487
- selectedTokenOnBackendChain,
33488
- selectedToken?.address,
33489
- spender,
33490
- walletAddress
33491
- ]);
33547
+ }, [findUnmetApprovals, requiredApprovals.length]);
33492
33548
  (0, import_react38.useEffect)(() => {
33493
33549
  void readAllowance();
33494
33550
  }, [readAllowance]);
33495
- const needsApproval = isEvm && !isNativeSelected && !routeResult?.sponsorship && // sponsored routes use Permit2 + SA internal batch — no EOA bridge approval
33496
- !!walletAddress && !!spender && amountWei > 0n && allowanceWei < amountWei;
33497
- const waitForApprovalConfirmation2 = (0, import_react38.useCallback)(
33498
- async (chainId, txHash) => {
33499
- const timeoutMs = 12e4;
33500
- const intervalMs = 2e3;
33501
- const started = Date.now();
33502
- while (Date.now() - started < timeoutMs) {
33503
- const status = await getEVMTxStatus({ chainId, txHash });
33504
- if (status.status === "success") {
33505
- return;
33506
- }
33507
- if (status.status === "reverted") {
33508
- throw new Error("Approval transaction reverted");
33509
- }
33510
- await sleep3(intervalMs);
33511
- }
33512
- throw new Error("Timed out waiting for approval confirmation");
33513
- },
33514
- []
33515
- );
33551
+ const needsApproval = !!walletAddress && amountWei > 0n && unmetApprovals.length > 0;
33516
33552
  const handleApproveExact = (0, import_react38.useCallback)(async () => {
33517
- if (isApproving || amountWei <= 0n || !walletAddress || !spender || !selectedToken?.address) {
33553
+ if (isApproving || amountWei <= 0n || !walletAddress || unmetApprovals.length === 0) {
33518
33554
  return;
33519
33555
  }
33520
33556
  const wallet = Trustware.getWallet();
@@ -33530,80 +33566,57 @@ function useTransactionActionModel({
33530
33566
  });
33531
33567
  setIsApproving(true);
33532
33568
  try {
33533
- const targetChain = Number(
33534
- routeResult?.txReq?.chainId ?? selectedChain?.chainId ?? selectedChain?.id
33535
- );
33536
- console.log("[approve] targetChain:", targetChain, {
33537
- txReqChainId: routeResult?.txReq?.chainId,
33538
- selectedChainId: selectedChain?.chainId,
33539
- selectedChainIdAlt: selectedChain?.id
33540
- });
33541
- if (Number.isFinite(targetChain)) {
33542
- const current = await wallet.getChainId();
33543
- if (current !== targetChain) {
33544
- try {
33545
- await wallet.switchChain(targetChain);
33546
- } catch {
33547
- }
33548
- }
33549
- }
33550
- const data = (0, import_viem42.encodeFunctionData)({
33551
- abi: import_viem42.erc20Abi,
33552
- functionName: "approve",
33553
- args: [spender, amountWei]
33554
- });
33555
- let hash;
33556
- if (wallet.type === "eip1193") {
33557
- const from = await wallet.getAddress();
33558
- const params = {
33559
- from,
33560
- to: selectedToken.address,
33561
- data,
33562
- value: "0x0"
33563
- };
33564
- if (Number.isFinite(targetChain)) {
33565
- params.chainId = `0x${targetChain.toString(16)}`;
33566
- }
33567
- hash = await wallet.request({
33568
- method: "eth_sendTransaction",
33569
- params: [params]
33569
+ for (const approval of unmetApprovals) {
33570
+ const approvalChain = Number(approval.chainId);
33571
+ await ensureWalletOnChain(wallet, approvalChain);
33572
+ const data = (0, import_viem42.encodeFunctionData)({
33573
+ abi: import_viem42.erc20Abi,
33574
+ functionName: "approve",
33575
+ args: [approval.spender, approval.amountWei]
33570
33576
  });
33571
- } else {
33572
- const response = await wallet.sendTransaction({
33573
- to: selectedToken.address,
33574
- data,
33575
- value: 0n,
33576
- chainId: Number.isFinite(targetChain) ? targetChain : void 0
33577
- });
33578
- hash = response.hash;
33579
- }
33580
- if (backendChainId2) {
33581
- await waitForApprovalConfirmation2(backendChainId2, hash);
33582
- await readAllowance();
33583
- } else {
33584
- setAllowanceWei(amountWei);
33577
+ let hash;
33578
+ if (wallet.type === "eip1193") {
33579
+ const from = await wallet.getAddress();
33580
+ hash = await wallet.request({
33581
+ method: "eth_sendTransaction",
33582
+ params: [
33583
+ {
33584
+ from,
33585
+ to: approval.token,
33586
+ data,
33587
+ value: "0x0",
33588
+ chainId: `0x${approvalChain.toString(16)}`
33589
+ }
33590
+ ]
33591
+ });
33592
+ } else {
33593
+ const response = await wallet.sendTransaction({
33594
+ to: approval.token,
33595
+ data,
33596
+ value: 0n,
33597
+ chainId: approvalChain
33598
+ });
33599
+ hash = response.hash;
33600
+ }
33601
+ await waitForApprovalConfirmation(approval.chainId, hash);
33585
33602
  }
33603
+ await readAllowance();
33586
33604
  } catch {
33587
33605
  } finally {
33588
33606
  setIsApproving(false);
33589
33607
  }
33590
33608
  }, [
33591
33609
  amountWei,
33592
- backendChainId2,
33593
33610
  destinationConfig?.routes.toChain,
33594
33611
  destinationConfig?.routes.toToken,
33595
33612
  isApproving,
33596
33613
  readAllowance,
33597
- routeResult?.txReq?.chainId,
33598
33614
  selectedChain?.axelarChainName,
33599
33615
  selectedChain?.chainId,
33600
- selectedChain?.id,
33601
33616
  selectedChain?.networkName,
33602
- selectedToken?.address,
33603
33617
  selectedToken?.symbol,
33604
- spender,
33605
33618
  trackEvent,
33606
- waitForApprovalConfirmation2,
33619
+ unmetApprovals,
33607
33620
  walletAddress
33608
33621
  ]);
33609
33622
  const getCachedFeeData = (0, import_react38.useCallback)(async () => {
@@ -33790,32 +33803,13 @@ function useTransactionActionModel({
33790
33803
  const handleSwipeConfirm = (0, import_react38.useCallback)(async () => {
33791
33804
  if (needsApproval) {
33792
33805
  await handleApproveExact();
33793
- if (!backendChainId2 || !selectedTokenOnBackendChain || !selectedToken?.address || !walletAddress || !spender) {
33794
- return;
33795
- }
33796
- const freshAllowance = await getEVMAllowance({
33797
- chainId: backendChainId2,
33798
- tokenAddress: selectedToken.address,
33799
- ownerAddress: walletAddress,
33800
- spenderAddress: spender
33801
- }).then((r) => BigInt(r.allowance || "0")).catch(() => 0n);
33802
- if (freshAllowance >= amountWei) {
33806
+ if ((await findUnmetApprovals()).length === 0) {
33803
33807
  await handleConfirm();
33804
33808
  }
33805
33809
  return;
33806
33810
  }
33807
33811
  await handleConfirm();
33808
- }, [
33809
- amountWei,
33810
- backendChainId2,
33811
- handleApproveExact,
33812
- handleConfirm,
33813
- needsApproval,
33814
- selectedTokenOnBackendChain,
33815
- selectedToken?.address,
33816
- spender,
33817
- walletAddress
33818
- ]);
33812
+ }, [findUnmetApprovals, handleApproveExact, handleConfirm, needsApproval]);
33819
33813
  const isWalletConnected = walletStatus === "connected";
33820
33814
  const canSwipe = parsedAmount > 0 && !!selectedToken && isWalletConnected && !isLoadingRoute && !isSubmitting && !!routeResult && !actionErrorMessage && !isApproving && !isReadingAllowance;
33821
33815
  const swipeResetKey = (0, import_react38.useMemo)(() => {
@@ -33862,9 +33856,11 @@ var init_useTransactionActionModel = __esm({
33862
33856
  import_viem42 = require("viem");
33863
33857
  init_core2();
33864
33858
  init_sdkRpc();
33859
+ init_tx();
33865
33860
  init_hooks2();
33866
33861
  init_chains();
33867
33862
  init_utils();
33863
+ init_plannedApprovals();
33868
33864
  init_hooks();
33869
33865
  init_routeChange();
33870
33866
  }
@@ -35305,33 +35301,15 @@ function useSwapExecution(fromChain) {
35305
35301
  const chainIdStr = String(txReq?.chainId ?? fromChain?.chainId ?? "");
35306
35302
  const amountWei = BigInt(routeResult.route?.estimate?.fromAmount ?? "0");
35307
35303
  const plannedApprovals = routeResult.route?.execution?.approvals ?? [];
35308
- const toCheck = plannedApprovals.length ? plannedApprovals.flatMap((a) => {
35309
- const token = a.tokenAddress ?? fromTokenAddress;
35310
- const required = requiredApprovalAmount(
35311
- BigInt(a.amount || "0"),
35312
- amountWei
35313
- );
35314
- if (!isEvmAddress2(token) || !isEvmAddress2(a.spender)) return [];
35315
- return [
35316
- {
35317
- chainId: a.chainId || chainIdStr,
35318
- token,
35319
- spender: a.spender,
35320
- required
35321
- }
35322
- ];
35323
- }) : (() => {
35324
- const spender = txReq?.to ?? txReq?.target;
35325
- if (!spender) return [];
35326
- return [
35327
- {
35328
- chainId: chainIdStr,
35329
- token: fromTokenAddress,
35330
- spender,
35331
- required: amountWei
35332
- }
35333
- ];
35334
- })();
35304
+ if (plannedApprovals.length === 0) {
35305
+ setState((p) => ({ ...p, allowanceStatus: "sufficient" }));
35306
+ return;
35307
+ }
35308
+ const toCheck = plannedErc20Approvals(plannedApprovals, {
35309
+ fromTokenAddress,
35310
+ chainId: chainIdStr,
35311
+ amountWei
35312
+ });
35335
35313
  if (toCheck.length === 0 || !chainIdStr || amountWei === 0n) {
35336
35314
  setState((p) => ({ ...p, allowanceStatus: "unknown" }));
35337
35315
  return;
@@ -35346,7 +35324,7 @@ function useSwapExecution(fromChain) {
35346
35324
  ownerAddress: walletAddress,
35347
35325
  spenderAddress: c.spender
35348
35326
  });
35349
- return approvalSatisfied(BigInt(allowance || "0"), c.required);
35327
+ return approvalSatisfied(BigInt(allowance || "0"), c.amountWei);
35350
35328
  })
35351
35329
  );
35352
35330
  setState((p) => ({
@@ -35401,7 +35379,6 @@ function useSwapExecution(fromChain) {
35401
35379
  const txReq = routeResult.txReq;
35402
35380
  const numericChainId = Number(txReq.chainId ?? fromChain?.chainId) || void 0;
35403
35381
  const chainIdStr = String(txReq.chainId ?? fromChain?.chainId ?? "");
35404
- const spender = txReq.to ?? txReq.target;
35405
35382
  const chainType = normalizeChainType(fromChain);
35406
35383
  const isNative = !fromTokenAddress || isNativeTokenAddress(fromTokenAddress, chainType);
35407
35384
  const fromAmountWei = BigInt(
@@ -35481,34 +35458,12 @@ function useSwapExecution(fromChain) {
35481
35458
  const acceptance = { tracking: false };
35482
35459
  try {
35483
35460
  const plannedApprovals = routeResult.route?.execution?.approvals ?? [];
35484
- const requiredApprovals = plannedApprovals.length ? plannedApprovals.flatMap((a, index) => {
35485
- const token = a.tokenAddress ?? fromTokenAddress;
35486
- const approvalSpender = a.spender;
35487
- if (!isEvmAddress2(token) || !isEvmAddress2(approvalSpender))
35488
- return [];
35489
- const amountWei = requiredApprovalAmount(
35490
- BigInt(a.amount || "0"),
35491
- fromAmountWei
35492
- );
35493
- return [
35494
- {
35495
- stepIndex: index,
35496
- token,
35497
- spender: approvalSpender,
35498
- amountWei,
35499
- chainId: a.chainId || chainIdStr
35500
- }
35501
- ];
35502
- }) : spender && fromTokenAddress ? [
35503
- {
35504
- stepIndex: null,
35505
- token: fromTokenAddress,
35506
- spender,
35507
- amountWei: fromAmountWei,
35508
- chainId: chainIdStr
35509
- }
35510
- ] : [];
35511
- const coversEveryPlannedApproval = plannedApprovals.length === 0 || requiredApprovals.length === plannedApprovals.length;
35461
+ const requiredApprovals = plannedErc20Approvals(plannedApprovals, {
35462
+ fromTokenAddress,
35463
+ chainId: chainIdStr,
35464
+ amountWei: fromAmountWei
35465
+ });
35466
+ const coversEveryPlannedApproval = requiredApprovals.length === plannedApprovals.length;
35512
35467
  const ownsApprovals = coversEveryPlannedApproval && needsErc20Approval(fromTokenAddress, chainType) && !!walletAddress && !!chainIdStr;
35513
35468
  const activeAddress = ownsApprovals ? await requireActiveAddress(wallet, walletAddress) : void 0;
35514
35469
  for (const required of requiredApprovals) {
@@ -35570,14 +35525,12 @@ function useSwapExecution(fromChain) {
35570
35525
  });
35571
35526
  approvalHash = response.hash;
35572
35527
  }
35573
- if (required.stepIndex !== null) {
35574
- void submitStepReceipt(
35575
- routeResult.intentId,
35576
- required.stepIndex,
35577
- approvalHash
35578
- ).catch(() => {
35579
- });
35580
- }
35528
+ void submitStepReceipt(
35529
+ routeResult.intentId,
35530
+ required.stepIndex,
35531
+ approvalHash
35532
+ ).catch(() => {
35533
+ });
35581
35534
  await waitForApprovalConfirmation(required.chainId, approvalHash);
35582
35535
  setState((p) => ({
35583
35536
  ...p,
@@ -35696,6 +35649,7 @@ var init_useSwapExecution = __esm({
35696
35649
  init_tx();
35697
35650
  init_sdkRpc();
35698
35651
  init_chainHelpers();
35652
+ init_plannedApprovals();
35699
35653
  init_chains();
35700
35654
  SA_COOLDOWN_MS = 3e4;
35701
35655
  }