@trustware/sdk-staging 1.1.12-staging.6 → 1.1.12-staging.7

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.
@@ -30,7 +30,7 @@ __export(constants_exports, {
30
30
  });
31
31
  module.exports = __toCommonJS(constants_exports);
32
32
  var SDK_NAME = "@trustware/sdk";
33
- var SDK_VERSION = "1.1.12-staging.6";
33
+ var SDK_VERSION = "1.1.12-staging.7";
34
34
  var API_ROOT = "https://bv-staging-api.trustware.io";
35
35
  var GTM_ID = "GTM-TZDGNCXB";
36
36
  var API_PREFIX = "/api";
@@ -1,6 +1,6 @@
1
1
  // src/constants.ts
2
2
  var SDK_NAME = "@trustware/sdk";
3
- var SDK_VERSION = "1.1.12-staging.6";
3
+ var SDK_VERSION = "1.1.12-staging.7";
4
4
  var API_ROOT = "https://bv-staging-api.trustware.io";
5
5
  var GTM_ID = "GTM-TZDGNCXB";
6
6
  var API_PREFIX = "/api";
package/dist/core.cjs CHANGED
@@ -83,7 +83,7 @@ var init_constants = __esm({
83
83
  "src/constants.ts"() {
84
84
  "use strict";
85
85
  SDK_NAME = "@trustware/sdk";
86
- SDK_VERSION = "1.1.12-staging.6";
86
+ SDK_VERSION = "1.1.12-staging.7";
87
87
  API_ROOT = "https://bv-staging-api.trustware.io";
88
88
  API_PREFIX = "/api";
89
89
  WALLETCONNECT_PROJECT_ID = "72ea74c400f5111d43aea638d7d83a24";
@@ -307,6 +307,104 @@ var init_chains = __esm({
307
307
  }
308
308
  });
309
309
 
310
+ // src/wallets/chainParams.ts
311
+ function chainParamsFor(chainId) {
312
+ return registered.get(chainId) ?? BUILTIN_CHAIN_PARAMS[chainId];
313
+ }
314
+ function registerChainParams(chainId, params) {
315
+ registered.set(chainId, params);
316
+ }
317
+ function usableRpcUrls(chain) {
318
+ const candidates = [
319
+ ...Array.isArray(chain.rpcList) ? chain.rpcList : [],
320
+ ...chain.rpc ? [chain.rpc] : []
321
+ ];
322
+ const seen = /* @__PURE__ */ new Set();
323
+ const out = [];
324
+ for (const raw of candidates) {
325
+ const url = String(raw ?? "").trim();
326
+ if (!/^https?:\/\//i.test(url) || seen.has(url)) continue;
327
+ seen.add(url);
328
+ out.push(url);
329
+ }
330
+ return out;
331
+ }
332
+ function chainParamsFromChainDef(chain) {
333
+ const chainType = String(
334
+ chain.chainType ?? chain.type ?? "evm"
335
+ ).toLowerCase();
336
+ if (chainType !== "evm") return void 0;
337
+ const numericId = Number(chain.chainId ?? chain.id);
338
+ if (!Number.isSafeInteger(numericId) || numericId <= 0) return void 0;
339
+ const rpcUrls = usableRpcUrls(chain);
340
+ if (rpcUrls.length === 0) return void 0;
341
+ const native = chain.nativeCurrency;
342
+ const symbol = String(native?.symbol ?? "").trim();
343
+ if (!symbol) return void 0;
344
+ const decimals = Number(native?.decimals);
345
+ const chainName = String(
346
+ chain.networkName ?? chain.axelarChainName ?? chain.networkIdentifier ?? ""
347
+ ).trim();
348
+ const explorers = (chain.blockExplorerUrls ?? []).filter(
349
+ (u) => /^https?:\/\//i.test(String(u ?? ""))
350
+ );
351
+ return {
352
+ chainIdHex: `0x${numericId.toString(16)}`,
353
+ chainName: chainName || `Chain ${numericId}`,
354
+ rpcUrls,
355
+ nativeCurrency: {
356
+ name: String(native?.name ?? symbol),
357
+ symbol,
358
+ decimals: Number.isFinite(decimals) ? decimals : 18
359
+ },
360
+ ...explorers.length > 0 ? { blockExplorerUrls: explorers } : {}
361
+ };
362
+ }
363
+ var BUILTIN_CHAIN_PARAMS, registered;
364
+ var init_chainParams = __esm({
365
+ "src/wallets/chainParams.ts"() {
366
+ "use strict";
367
+ BUILTIN_CHAIN_PARAMS = {
368
+ 1: {
369
+ chainIdHex: "0x1",
370
+ chainName: "Ethereum",
371
+ rpcUrls: ["https://eth.llamarpc.com"],
372
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
373
+ blockExplorerUrls: ["https://etherscan.io"]
374
+ },
375
+ 137: {
376
+ chainIdHex: "0x89",
377
+ chainName: "Polygon",
378
+ rpcUrls: ["https://polygon-rpc.com"],
379
+ nativeCurrency: { name: "POL", symbol: "POL", decimals: 18 },
380
+ blockExplorerUrls: ["https://polygonscan.com"]
381
+ },
382
+ 8453: {
383
+ chainIdHex: "0x2105",
384
+ chainName: "Base",
385
+ rpcUrls: ["https://mainnet.base.org"],
386
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
387
+ blockExplorerUrls: ["https://basescan.org"]
388
+ },
389
+ 42161: {
390
+ chainIdHex: "0xa4b1",
391
+ chainName: "Arbitrum One",
392
+ rpcUrls: ["https://arb1.arbitrum.io/rpc"],
393
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
394
+ blockExplorerUrls: ["https://arbiscan.io"]
395
+ },
396
+ 43114: {
397
+ chainIdHex: "0xa86a",
398
+ chainName: "Avalanche C-Chain",
399
+ rpcUrls: ["https://api.avax.network/ext/bc/C/rpc"],
400
+ nativeCurrency: { name: "Avalanche", symbol: "AVAX", decimals: 18 },
401
+ blockExplorerUrls: ["https://snowtrace.io"]
402
+ }
403
+ };
404
+ registered = /* @__PURE__ */ new Map();
405
+ }
406
+ });
407
+
310
408
  // src/registry.ts
311
409
  function getChainAliases(chain) {
312
410
  const values = [
@@ -351,6 +449,7 @@ var init_registry = __esm({
351
449
  "use strict";
352
450
  init_store();
353
451
  init_chains();
452
+ init_chainParams();
354
453
  NATIVE = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
355
454
  PAGE_CACHE_TTL_MS = 5 * 60 * 1e3;
356
455
  PAGE_CACHE_LIMIT = 100;
@@ -429,6 +528,10 @@ var init_registry = __esm({
429
528
  for (const alias of getChainAliases(normalized)) {
430
529
  this._chainAliases.set(alias, canonicalKey);
431
530
  }
531
+ const addParams = chainParamsFromChainDef(normalized);
532
+ if (addParams) {
533
+ registerChainParams(Number(normalized.chainId ?? canonical), addParams);
534
+ }
432
535
  }
433
536
  storeTokenForAlias(chainKey, token) {
434
537
  const key = tokenKey(token);
@@ -2006,7 +2109,7 @@ var init_balances = __esm({
2006
2109
 
2007
2110
  // src/wallets/eipWallets.ts
2008
2111
  async function addThenSwitch(eth, chainId) {
2009
- const p = CHAIN_PARAMS[chainId];
2112
+ const p = chainParamsFor(chainId);
2010
2113
  if (!p) throw new Error(`Unknown chain ${chainId} (no params to add)`);
2011
2114
  await eth.request({ method: "wallet_addEthereumChain", params: [p] });
2012
2115
  await eth.request({
@@ -2034,7 +2137,7 @@ function useEIP1193(eth) {
2034
2137
  async switchChain(chainId) {
2035
2138
  if (switching) return;
2036
2139
  switching = true;
2037
- const hex = CHAIN_PARAMS[chainId]?.chainIdHex ?? `0x${chainId.toString(16)}`;
2140
+ const hex = chainParamsFor(chainId)?.chainIdHex ?? `0x${chainId.toString(16)}`;
2038
2141
  try {
2039
2142
  await eth.request({
2040
2143
  method: "wallet_switchEthereumChain",
@@ -2043,7 +2146,6 @@ function useEIP1193(eth) {
2043
2146
  } catch (e2) {
2044
2147
  if (e2?.code === 4902) {
2045
2148
  await addThenSwitch(eth, chainId);
2046
- } else if (e2?.code === 4001) {
2047
2149
  } else {
2048
2150
  throw e2;
2049
2151
  }
@@ -2054,47 +2156,10 @@ function useEIP1193(eth) {
2054
2156
  request: (args) => eth.request(args)
2055
2157
  };
2056
2158
  }
2057
- var CHAIN_PARAMS;
2058
2159
  var init_eipWallets = __esm({
2059
2160
  "src/wallets/eipWallets.ts"() {
2060
2161
  "use strict";
2061
- CHAIN_PARAMS = {
2062
- 1: {
2063
- chainIdHex: "0x1",
2064
- chainName: "Ethereum",
2065
- rpcUrls: ["https://eth.llamarpc.com"],
2066
- nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
2067
- blockExplorerUrls: ["https://etherscan.io"]
2068
- },
2069
- 137: {
2070
- chainIdHex: "0x89",
2071
- chainName: "Polygon",
2072
- rpcUrls: ["https://polygon-rpc.com"],
2073
- nativeCurrency: { name: "POL", symbol: "POL", decimals: 18 },
2074
- blockExplorerUrls: ["https://polygonscan.com"]
2075
- },
2076
- 8453: {
2077
- chainIdHex: "0x2105",
2078
- chainName: "Base",
2079
- rpcUrls: ["https://mainnet.base.org"],
2080
- nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
2081
- blockExplorerUrls: ["https://basescan.org"]
2082
- },
2083
- 42161: {
2084
- chainIdHex: "0xa4b1",
2085
- chainName: "Arbitrum One",
2086
- rpcUrls: ["https://arb1.arbitrum.io/rpc"],
2087
- nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
2088
- blockExplorerUrls: ["https://arbiscan.io"]
2089
- },
2090
- 43114: {
2091
- chainIdHex: "0xa86a",
2092
- chainName: "Avalanche C-Chain",
2093
- rpcUrls: ["https://api.avax.network/ext/bc/C/rpc"],
2094
- nativeCurrency: { name: "Avalanche", symbol: "AVAX", decimals: 18 },
2095
- blockExplorerUrls: ["https://snowtrace.io"]
2096
- }
2097
- };
2162
+ init_chainParams();
2098
2163
  }
2099
2164
  });
2100
2165
 
@@ -6319,6 +6384,25 @@ function backendChainId(chain, fallback) {
6319
6384
  function sleep2(ms) {
6320
6385
  return new Promise((resolve) => setTimeout(resolve, ms));
6321
6386
  }
6387
+ async function ensureWalletOnChain(wallet, target) {
6388
+ if (!Number.isSafeInteger(target) || target <= 0) {
6389
+ throw new Error(`Invalid chain id: ${target}`);
6390
+ }
6391
+ if (await wallet.getChainId() === target) return;
6392
+ let switchError;
6393
+ try {
6394
+ await wallet.switchChain(target);
6395
+ } catch (e2) {
6396
+ switchError = e2;
6397
+ }
6398
+ if (await wallet.getChainId() === target) return;
6399
+ if (switchError && isUserRejected(switchError)) {
6400
+ throw switchError;
6401
+ }
6402
+ throw new Error(
6403
+ `Wallet is on the wrong network. Switch to chain ${target} and try again.`
6404
+ );
6405
+ }
6322
6406
  async function sendEvmTx(w, tx) {
6323
6407
  if (w.type === "eip1193") {
6324
6408
  const from = await w.getAddress();
@@ -6345,19 +6429,37 @@ async function sendEvmTx(w, tx) {
6345
6429
  });
6346
6430
  return response.hash;
6347
6431
  }
6348
- async function waitForTxConfirmation(chainId, txHash) {
6349
- const timeoutMs = 12e4;
6350
- const intervalMs = 2e3;
6432
+ async function waitForEvmTxConfirmation(chainId, txHash, label, options = {}) {
6433
+ const {
6434
+ readStatus = getEVMTxStatus,
6435
+ timeoutMs = 12e4,
6436
+ intervalMs = 2e3,
6437
+ notFoundGraceMs = NOT_FOUND_GRACE_MS
6438
+ } = options;
6351
6439
  const started = Date.now();
6440
+ let notFoundSince = null;
6352
6441
  while (Date.now() - started < timeoutMs) {
6353
- const status = await getEVMTxStatus({ chainId, txHash });
6442
+ const status = await readStatus({ chainId, txHash });
6354
6443
  if (status.status === "success") return;
6355
6444
  if (status.status === "reverted") {
6356
- throw new Error("Approval transaction reverted");
6445
+ throw new Error(`${label} transaction reverted`);
6446
+ }
6447
+ if (status.status === "not_found") {
6448
+ notFoundSince ?? (notFoundSince = Date.now());
6449
+ if (Date.now() - notFoundSince >= notFoundGraceMs) {
6450
+ throw new Error(
6451
+ `${label} transaction ${txHash} was not found on chain ${chainId}. It may have been sent on a different network \u2014 check your wallet's selected network and try again.`
6452
+ );
6453
+ }
6454
+ } else {
6455
+ notFoundSince = null;
6357
6456
  }
6358
6457
  await sleep2(intervalMs);
6359
6458
  }
6360
- throw new Error("Timed out waiting for approval confirmation");
6459
+ throw new Error(`Timed out waiting for ${label.toLowerCase()} confirmation`);
6460
+ }
6461
+ function waitForApprovalConfirmation(chainId, txHash, options) {
6462
+ return waitForEvmTxConfirmation(chainId, txHash, "Approval", options);
6361
6463
  }
6362
6464
  function requiredApprovalAmount(planAmountWei, routeAmountWei) {
6363
6465
  if (planAmountWei >= UNLIMITED_APPROVAL_FLOOR && routeAmountWei !== void 0 && routeAmountWei > 0n) {
@@ -6397,17 +6499,22 @@ async function ensureApprovals(w, approvals, fallbackChainId, intentId, routeAmo
6397
6499
  functionName: "approve",
6398
6500
  args: [spender, amountWei]
6399
6501
  });
6502
+ const approvalChainId = Number(chainId);
6503
+ if (!Number.isSafeInteger(approvalChainId) || approvalChainId <= 0) {
6504
+ throw new Error(`Approval has an invalid chain id: ${chainId}`);
6505
+ }
6506
+ await ensureWalletOnChain(w, approvalChainId);
6400
6507
  const hash = await sendEvmTx(w, {
6401
6508
  to: tokenAddress,
6402
6509
  data,
6403
6510
  value: 0n,
6404
- chainId: Number.isFinite(Number(chainId)) ? Number(chainId) : void 0
6511
+ chainId: approvalChainId
6405
6512
  });
6406
6513
  if (intentId) {
6407
6514
  void submitStepReceipt(intentId, stepIndex, hash).catch(() => {
6408
6515
  });
6409
6516
  }
6410
- await waitForTxConfirmation(chainId, hash);
6517
+ await waitForApprovalConfirmation(chainId, hash);
6411
6518
  }
6412
6519
  }
6413
6520
  function isUserRejected(e2) {
@@ -6434,15 +6541,14 @@ async function sendRouteTransaction(b, fallbackChainId, options) {
6434
6541
  validatedSponsorship = b.sponsorship;
6435
6542
  }
6436
6543
  }
6437
- if (Number.isFinite(target)) {
6438
- const current = await w.getChainId();
6439
- if (current !== target) {
6440
- try {
6441
- await w.switchChain(target);
6442
- } catch {
6443
- }
6444
- }
6544
+ if (!Number.isSafeInteger(target) || target <= 0) {
6545
+ throw new Error(
6546
+ `Route is missing a usable chain id (got ${String(
6547
+ txReq.chainId ?? fallbackChainId
6548
+ )}).`
6549
+ );
6445
6550
  }
6551
+ await ensureWalletOnChain(w, target);
6446
6552
  if (!validatedSponsorship && !options?.approvalsEnsured) {
6447
6553
  await ensureApprovals(
6448
6554
  w,
@@ -6548,7 +6654,7 @@ async function runTopUp(params) {
6548
6654
  }
6549
6655
  }
6550
6656
  }
6551
- var import_viem2, erc20ApproveAbi, UNLIMITED_APPROVAL_FLOOR;
6657
+ var import_viem2, erc20ApproveAbi, NOT_FOUND_GRACE_MS, UNLIMITED_APPROVAL_FLOOR;
6552
6658
  var init_tx = __esm({
6553
6659
  "src/core/tx.ts"() {
6554
6660
  "use strict";
@@ -6559,6 +6665,7 @@ var init_tx = __esm({
6559
6665
  erc20ApproveAbi = (0, import_viem2.parseAbi)([
6560
6666
  "function approve(address spender, uint256 amount) returns (bool)"
6561
6667
  ]);
6668
+ NOT_FOUND_GRACE_MS = 2e4;
6562
6669
  UNLIMITED_APPROVAL_FLOOR = 1n << 255n;
6563
6670
  }
6564
6671
  });