@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.
package/dist/core.mjs CHANGED
@@ -61,7 +61,7 @@ var init_constants = __esm({
61
61
  "src/constants.ts"() {
62
62
  "use strict";
63
63
  SDK_NAME = "@trustware/sdk";
64
- SDK_VERSION = "1.1.12-staging.6";
64
+ SDK_VERSION = "1.1.12-staging.7";
65
65
  API_ROOT = "https://bv-staging-api.trustware.io";
66
66
  API_PREFIX = "/api";
67
67
  WALLETCONNECT_PROJECT_ID = "72ea74c400f5111d43aea638d7d83a24";
@@ -285,6 +285,104 @@ var init_chains = __esm({
285
285
  }
286
286
  });
287
287
 
288
+ // src/wallets/chainParams.ts
289
+ function chainParamsFor(chainId) {
290
+ return registered.get(chainId) ?? BUILTIN_CHAIN_PARAMS[chainId];
291
+ }
292
+ function registerChainParams(chainId, params) {
293
+ registered.set(chainId, params);
294
+ }
295
+ function usableRpcUrls(chain) {
296
+ const candidates = [
297
+ ...Array.isArray(chain.rpcList) ? chain.rpcList : [],
298
+ ...chain.rpc ? [chain.rpc] : []
299
+ ];
300
+ const seen = /* @__PURE__ */ new Set();
301
+ const out = [];
302
+ for (const raw of candidates) {
303
+ const url = String(raw ?? "").trim();
304
+ if (!/^https?:\/\//i.test(url) || seen.has(url)) continue;
305
+ seen.add(url);
306
+ out.push(url);
307
+ }
308
+ return out;
309
+ }
310
+ function chainParamsFromChainDef(chain) {
311
+ const chainType = String(
312
+ chain.chainType ?? chain.type ?? "evm"
313
+ ).toLowerCase();
314
+ if (chainType !== "evm") return void 0;
315
+ const numericId = Number(chain.chainId ?? chain.id);
316
+ if (!Number.isSafeInteger(numericId) || numericId <= 0) return void 0;
317
+ const rpcUrls = usableRpcUrls(chain);
318
+ if (rpcUrls.length === 0) return void 0;
319
+ const native = chain.nativeCurrency;
320
+ const symbol = String(native?.symbol ?? "").trim();
321
+ if (!symbol) return void 0;
322
+ const decimals = Number(native?.decimals);
323
+ const chainName = String(
324
+ chain.networkName ?? chain.axelarChainName ?? chain.networkIdentifier ?? ""
325
+ ).trim();
326
+ const explorers = (chain.blockExplorerUrls ?? []).filter(
327
+ (u) => /^https?:\/\//i.test(String(u ?? ""))
328
+ );
329
+ return {
330
+ chainIdHex: `0x${numericId.toString(16)}`,
331
+ chainName: chainName || `Chain ${numericId}`,
332
+ rpcUrls,
333
+ nativeCurrency: {
334
+ name: String(native?.name ?? symbol),
335
+ symbol,
336
+ decimals: Number.isFinite(decimals) ? decimals : 18
337
+ },
338
+ ...explorers.length > 0 ? { blockExplorerUrls: explorers } : {}
339
+ };
340
+ }
341
+ var BUILTIN_CHAIN_PARAMS, registered;
342
+ var init_chainParams = __esm({
343
+ "src/wallets/chainParams.ts"() {
344
+ "use strict";
345
+ BUILTIN_CHAIN_PARAMS = {
346
+ 1: {
347
+ chainIdHex: "0x1",
348
+ chainName: "Ethereum",
349
+ rpcUrls: ["https://eth.llamarpc.com"],
350
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
351
+ blockExplorerUrls: ["https://etherscan.io"]
352
+ },
353
+ 137: {
354
+ chainIdHex: "0x89",
355
+ chainName: "Polygon",
356
+ rpcUrls: ["https://polygon-rpc.com"],
357
+ nativeCurrency: { name: "POL", symbol: "POL", decimals: 18 },
358
+ blockExplorerUrls: ["https://polygonscan.com"]
359
+ },
360
+ 8453: {
361
+ chainIdHex: "0x2105",
362
+ chainName: "Base",
363
+ rpcUrls: ["https://mainnet.base.org"],
364
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
365
+ blockExplorerUrls: ["https://basescan.org"]
366
+ },
367
+ 42161: {
368
+ chainIdHex: "0xa4b1",
369
+ chainName: "Arbitrum One",
370
+ rpcUrls: ["https://arb1.arbitrum.io/rpc"],
371
+ nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
372
+ blockExplorerUrls: ["https://arbiscan.io"]
373
+ },
374
+ 43114: {
375
+ chainIdHex: "0xa86a",
376
+ chainName: "Avalanche C-Chain",
377
+ rpcUrls: ["https://api.avax.network/ext/bc/C/rpc"],
378
+ nativeCurrency: { name: "Avalanche", symbol: "AVAX", decimals: 18 },
379
+ blockExplorerUrls: ["https://snowtrace.io"]
380
+ }
381
+ };
382
+ registered = /* @__PURE__ */ new Map();
383
+ }
384
+ });
385
+
288
386
  // src/registry.ts
289
387
  function getChainAliases(chain) {
290
388
  const values = [
@@ -329,6 +427,7 @@ var init_registry = __esm({
329
427
  "use strict";
330
428
  init_store();
331
429
  init_chains();
430
+ init_chainParams();
332
431
  NATIVE = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
333
432
  PAGE_CACHE_TTL_MS = 5 * 60 * 1e3;
334
433
  PAGE_CACHE_LIMIT = 100;
@@ -407,6 +506,10 @@ var init_registry = __esm({
407
506
  for (const alias of getChainAliases(normalized)) {
408
507
  this._chainAliases.set(alias, canonicalKey);
409
508
  }
509
+ const addParams = chainParamsFromChainDef(normalized);
510
+ if (addParams) {
511
+ registerChainParams(Number(normalized.chainId ?? canonical), addParams);
512
+ }
410
513
  }
411
514
  storeTokenForAlias(chainKey, token) {
412
515
  const key = tokenKey(token);
@@ -1984,7 +2087,7 @@ var init_balances = __esm({
1984
2087
 
1985
2088
  // src/wallets/eipWallets.ts
1986
2089
  async function addThenSwitch(eth, chainId) {
1987
- const p = CHAIN_PARAMS[chainId];
2090
+ const p = chainParamsFor(chainId);
1988
2091
  if (!p) throw new Error(`Unknown chain ${chainId} (no params to add)`);
1989
2092
  await eth.request({ method: "wallet_addEthereumChain", params: [p] });
1990
2093
  await eth.request({
@@ -2012,7 +2115,7 @@ function useEIP1193(eth) {
2012
2115
  async switchChain(chainId) {
2013
2116
  if (switching) return;
2014
2117
  switching = true;
2015
- const hex = CHAIN_PARAMS[chainId]?.chainIdHex ?? `0x${chainId.toString(16)}`;
2118
+ const hex = chainParamsFor(chainId)?.chainIdHex ?? `0x${chainId.toString(16)}`;
2016
2119
  try {
2017
2120
  await eth.request({
2018
2121
  method: "wallet_switchEthereumChain",
@@ -2021,7 +2124,6 @@ function useEIP1193(eth) {
2021
2124
  } catch (e2) {
2022
2125
  if (e2?.code === 4902) {
2023
2126
  await addThenSwitch(eth, chainId);
2024
- } else if (e2?.code === 4001) {
2025
2127
  } else {
2026
2128
  throw e2;
2027
2129
  }
@@ -2032,47 +2134,10 @@ function useEIP1193(eth) {
2032
2134
  request: (args) => eth.request(args)
2033
2135
  };
2034
2136
  }
2035
- var CHAIN_PARAMS;
2036
2137
  var init_eipWallets = __esm({
2037
2138
  "src/wallets/eipWallets.ts"() {
2038
2139
  "use strict";
2039
- CHAIN_PARAMS = {
2040
- 1: {
2041
- chainIdHex: "0x1",
2042
- chainName: "Ethereum",
2043
- rpcUrls: ["https://eth.llamarpc.com"],
2044
- nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
2045
- blockExplorerUrls: ["https://etherscan.io"]
2046
- },
2047
- 137: {
2048
- chainIdHex: "0x89",
2049
- chainName: "Polygon",
2050
- rpcUrls: ["https://polygon-rpc.com"],
2051
- nativeCurrency: { name: "POL", symbol: "POL", decimals: 18 },
2052
- blockExplorerUrls: ["https://polygonscan.com"]
2053
- },
2054
- 8453: {
2055
- chainIdHex: "0x2105",
2056
- chainName: "Base",
2057
- rpcUrls: ["https://mainnet.base.org"],
2058
- nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
2059
- blockExplorerUrls: ["https://basescan.org"]
2060
- },
2061
- 42161: {
2062
- chainIdHex: "0xa4b1",
2063
- chainName: "Arbitrum One",
2064
- rpcUrls: ["https://arb1.arbitrum.io/rpc"],
2065
- nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
2066
- blockExplorerUrls: ["https://arbiscan.io"]
2067
- },
2068
- 43114: {
2069
- chainIdHex: "0xa86a",
2070
- chainName: "Avalanche C-Chain",
2071
- rpcUrls: ["https://api.avax.network/ext/bc/C/rpc"],
2072
- nativeCurrency: { name: "Avalanche", symbol: "AVAX", decimals: 18 },
2073
- blockExplorerUrls: ["https://snowtrace.io"]
2074
- }
2075
- };
2140
+ init_chainParams();
2076
2141
  }
2077
2142
  });
2078
2143
 
@@ -6298,6 +6363,25 @@ function backendChainId(chain, fallback) {
6298
6363
  function sleep2(ms) {
6299
6364
  return new Promise((resolve) => setTimeout(resolve, ms));
6300
6365
  }
6366
+ async function ensureWalletOnChain(wallet, target) {
6367
+ if (!Number.isSafeInteger(target) || target <= 0) {
6368
+ throw new Error(`Invalid chain id: ${target}`);
6369
+ }
6370
+ if (await wallet.getChainId() === target) return;
6371
+ let switchError;
6372
+ try {
6373
+ await wallet.switchChain(target);
6374
+ } catch (e2) {
6375
+ switchError = e2;
6376
+ }
6377
+ if (await wallet.getChainId() === target) return;
6378
+ if (switchError && isUserRejected(switchError)) {
6379
+ throw switchError;
6380
+ }
6381
+ throw new Error(
6382
+ `Wallet is on the wrong network. Switch to chain ${target} and try again.`
6383
+ );
6384
+ }
6301
6385
  async function sendEvmTx(w, tx) {
6302
6386
  if (w.type === "eip1193") {
6303
6387
  const from = await w.getAddress();
@@ -6324,19 +6408,37 @@ async function sendEvmTx(w, tx) {
6324
6408
  });
6325
6409
  return response.hash;
6326
6410
  }
6327
- async function waitForTxConfirmation(chainId, txHash) {
6328
- const timeoutMs = 12e4;
6329
- const intervalMs = 2e3;
6411
+ async function waitForEvmTxConfirmation(chainId, txHash, label, options = {}) {
6412
+ const {
6413
+ readStatus = getEVMTxStatus,
6414
+ timeoutMs = 12e4,
6415
+ intervalMs = 2e3,
6416
+ notFoundGraceMs = NOT_FOUND_GRACE_MS
6417
+ } = options;
6330
6418
  const started = Date.now();
6419
+ let notFoundSince = null;
6331
6420
  while (Date.now() - started < timeoutMs) {
6332
- const status = await getEVMTxStatus({ chainId, txHash });
6421
+ const status = await readStatus({ chainId, txHash });
6333
6422
  if (status.status === "success") return;
6334
6423
  if (status.status === "reverted") {
6335
- throw new Error("Approval transaction reverted");
6424
+ throw new Error(`${label} transaction reverted`);
6425
+ }
6426
+ if (status.status === "not_found") {
6427
+ notFoundSince ?? (notFoundSince = Date.now());
6428
+ if (Date.now() - notFoundSince >= notFoundGraceMs) {
6429
+ throw new Error(
6430
+ `${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.`
6431
+ );
6432
+ }
6433
+ } else {
6434
+ notFoundSince = null;
6336
6435
  }
6337
6436
  await sleep2(intervalMs);
6338
6437
  }
6339
- throw new Error("Timed out waiting for approval confirmation");
6438
+ throw new Error(`Timed out waiting for ${label.toLowerCase()} confirmation`);
6439
+ }
6440
+ function waitForApprovalConfirmation(chainId, txHash, options) {
6441
+ return waitForEvmTxConfirmation(chainId, txHash, "Approval", options);
6340
6442
  }
6341
6443
  function requiredApprovalAmount(planAmountWei, routeAmountWei) {
6342
6444
  if (planAmountWei >= UNLIMITED_APPROVAL_FLOOR && routeAmountWei !== void 0 && routeAmountWei > 0n) {
@@ -6376,17 +6478,22 @@ async function ensureApprovals(w, approvals, fallbackChainId, intentId, routeAmo
6376
6478
  functionName: "approve",
6377
6479
  args: [spender, amountWei]
6378
6480
  });
6481
+ const approvalChainId = Number(chainId);
6482
+ if (!Number.isSafeInteger(approvalChainId) || approvalChainId <= 0) {
6483
+ throw new Error(`Approval has an invalid chain id: ${chainId}`);
6484
+ }
6485
+ await ensureWalletOnChain(w, approvalChainId);
6379
6486
  const hash = await sendEvmTx(w, {
6380
6487
  to: tokenAddress,
6381
6488
  data,
6382
6489
  value: 0n,
6383
- chainId: Number.isFinite(Number(chainId)) ? Number(chainId) : void 0
6490
+ chainId: approvalChainId
6384
6491
  });
6385
6492
  if (intentId) {
6386
6493
  void submitStepReceipt(intentId, stepIndex, hash).catch(() => {
6387
6494
  });
6388
6495
  }
6389
- await waitForTxConfirmation(chainId, hash);
6496
+ await waitForApprovalConfirmation(chainId, hash);
6390
6497
  }
6391
6498
  }
6392
6499
  function isUserRejected(e2) {
@@ -6413,15 +6520,14 @@ async function sendRouteTransaction(b, fallbackChainId, options) {
6413
6520
  validatedSponsorship = b.sponsorship;
6414
6521
  }
6415
6522
  }
6416
- if (Number.isFinite(target)) {
6417
- const current = await w.getChainId();
6418
- if (current !== target) {
6419
- try {
6420
- await w.switchChain(target);
6421
- } catch {
6422
- }
6423
- }
6523
+ if (!Number.isSafeInteger(target) || target <= 0) {
6524
+ throw new Error(
6525
+ `Route is missing a usable chain id (got ${String(
6526
+ txReq.chainId ?? fallbackChainId
6527
+ )}).`
6528
+ );
6424
6529
  }
6530
+ await ensureWalletOnChain(w, target);
6425
6531
  if (!validatedSponsorship && !options?.approvalsEnsured) {
6426
6532
  await ensureApprovals(
6427
6533
  w,
@@ -6527,7 +6633,7 @@ async function runTopUp(params) {
6527
6633
  }
6528
6634
  }
6529
6635
  }
6530
- var erc20ApproveAbi, UNLIMITED_APPROVAL_FLOOR;
6636
+ var erc20ApproveAbi, NOT_FOUND_GRACE_MS, UNLIMITED_APPROVAL_FLOOR;
6531
6637
  var init_tx = __esm({
6532
6638
  "src/core/tx.ts"() {
6533
6639
  "use strict";
@@ -6537,6 +6643,7 @@ var init_tx = __esm({
6537
6643
  erc20ApproveAbi = parseAbi([
6538
6644
  "function approve(address spender, uint256 amount) returns (bool)"
6539
6645
  ]);
6646
+ NOT_FOUND_GRACE_MS = 2e4;
6540
6647
  UNLIMITED_APPROVAL_FLOOR = 1n << 255n;
6541
6648
  }
6542
6649
  });