@trustware/sdk-staging 1.1.9-staging.7 → 1.1.10-staging.0

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
@@ -495,7 +495,7 @@ var init_constants = __esm({
495
495
  "src/constants.ts"() {
496
496
  "use strict";
497
497
  SDK_NAME = "@trustware/sdk";
498
- SDK_VERSION = "1.1.9-staging.7";
498
+ SDK_VERSION = "1.1.10-staging.0";
499
499
  API_ROOT = "https://bv-staging-api.trustware.io";
500
500
  GTM_ID = "GTM-TZDGNCXB";
501
501
  API_PREFIX = "/api";
@@ -1136,12 +1136,33 @@ var init_deepLink = __esm({
1136
1136
  });
1137
1137
 
1138
1138
  // src/wallets/metadata.ts
1139
- var WALLETS;
1139
+ var POPULAR_ORDER, WALLETS;
1140
1140
  var init_metadata = __esm({
1141
1141
  "src/wallets/metadata.ts"() {
1142
1142
  "use strict";
1143
1143
  init_deepLink();
1144
1144
  init_constants();
1145
+ POPULAR_ORDER = [
1146
+ "rabby",
1147
+ "metamask",
1148
+ "walletconnect",
1149
+ "rainbow",
1150
+ "phantom-evm",
1151
+ "phantom-solana",
1152
+ "metamask-solana",
1153
+ "solflare",
1154
+ "okx",
1155
+ "brave",
1156
+ "coinbase",
1157
+ "zerion",
1158
+ "taho",
1159
+ "trust",
1160
+ "bitget",
1161
+ "backpack",
1162
+ "imtoken",
1163
+ "safe",
1164
+ "kucoin"
1165
+ ];
1145
1166
  WALLETS = [
1146
1167
  {
1147
1168
  id: "metamask",
@@ -4378,6 +4399,27 @@ var init_wallets = __esm({
4378
4399
  });
4379
4400
 
4380
4401
  // src/core/routes.ts
4402
+ function assertValidPostHook(hooks) {
4403
+ const postHook = hooks?.postHook;
4404
+ if (!postHook) return;
4405
+ if (!postHook.target?.trim()) {
4406
+ throw new Error("hooks.postHook.target is required.");
4407
+ }
4408
+ if (!postHook.callData?.trim()) {
4409
+ throw new Error("hooks.postHook.callData is required.");
4410
+ }
4411
+ if (postHook.fullAmount) {
4412
+ if (postHook.amountInputPos === void 0) {
4413
+ throw new Error(
4414
+ "hooks.postHook.amountInputPos is required when fullAmount is true."
4415
+ );
4416
+ }
4417
+ } else if (!postHook.fundAmount?.trim()) {
4418
+ throw new Error(
4419
+ "hooks.postHook.fundAmount is required unless fullAmount is set."
4420
+ );
4421
+ }
4422
+ }
4381
4423
  function isEvmTxRequest(txReq) {
4382
4424
  return Boolean(txReq?.data && (txReq.to || txReq.target));
4383
4425
  }
@@ -4396,6 +4438,7 @@ async function buildRoute(body, signal) {
4396
4438
  if (!addressValidation.isValid) {
4397
4439
  throw new Error(addressValidation.error || "Invalid route addresses.");
4398
4440
  }
4441
+ assertValidPostHook(body.hooks);
4399
4442
  const cfg = TrustwareConfigStore.get();
4400
4443
  const url = `${apiBase()}/v1/routes/route`;
4401
4444
  const payload = {
@@ -4434,6 +4477,7 @@ async function buildRoute(body, signal) {
4434
4477
  return { intentId, txReq, actions, finalExchangeRate, route, sponsorship };
4435
4478
  }
4436
4479
  async function buildDepositAddress(body, signal) {
4480
+ assertValidPostHook(body.hooks);
4437
4481
  const cfg = TrustwareConfigStore.get();
4438
4482
  const url = `${apiBase()}/v1/routes/deposit-address`;
4439
4483
  const payload = {
@@ -5186,6 +5230,87 @@ function backendChainId(chain, fallback) {
5186
5230
  const preferred = chain?.networkIdentifier ?? chain?.chainId ?? chain?.id;
5187
5231
  return String(preferred ?? fallback ?? "");
5188
5232
  }
5233
+ function sleep2(ms) {
5234
+ return new Promise((resolve) => setTimeout(resolve, ms));
5235
+ }
5236
+ async function sendEvmTx(w, tx) {
5237
+ if (w.type === "eip1193") {
5238
+ const from = await w.getAddress();
5239
+ const params = {
5240
+ from,
5241
+ to: tx.to,
5242
+ data: tx.data,
5243
+ value: tx.value ? `0x${tx.value.toString(16)}` : "0x0"
5244
+ };
5245
+ if (tx.chainId !== void 0) {
5246
+ params.chainId = `0x${tx.chainId.toString(16)}`;
5247
+ }
5248
+ const hash = await w.request({
5249
+ method: "eth_sendTransaction",
5250
+ params: [params]
5251
+ });
5252
+ return hash;
5253
+ }
5254
+ const response = await w.sendTransaction({
5255
+ to: tx.to,
5256
+ data: tx.data,
5257
+ value: tx.value,
5258
+ chainId: tx.chainId
5259
+ });
5260
+ return response.hash;
5261
+ }
5262
+ async function waitForTxConfirmation(chainId, txHash) {
5263
+ const timeoutMs = 12e4;
5264
+ const intervalMs = 2e3;
5265
+ const started = Date.now();
5266
+ while (Date.now() - started < timeoutMs) {
5267
+ const status = await getEVMTxStatus({ chainId, txHash });
5268
+ if (status.status === "success") return;
5269
+ if (status.status === "reverted") {
5270
+ throw new Error("Approval transaction reverted");
5271
+ }
5272
+ await sleep2(intervalMs);
5273
+ }
5274
+ throw new Error("Timed out waiting for approval confirmation");
5275
+ }
5276
+ async function ensureApprovals(w, approvals, fallbackChainId) {
5277
+ if (!approvals || approvals.length === 0) return;
5278
+ const owner = await w.getAddress();
5279
+ for (const approval of approvals) {
5280
+ const spender = approval.spender;
5281
+ const tokenAddress = approval.tokenAddress;
5282
+ const amount = approval.amount;
5283
+ if (!spender || !tokenAddress || !amount) continue;
5284
+ const chainId = approval.chainId || String(fallbackChainId ?? "");
5285
+ if (!chainId) continue;
5286
+ const amountWei = BigInt(amount);
5287
+ if (amountWei === 0n) continue;
5288
+ let currentAllowance = 0n;
5289
+ try {
5290
+ const { allowance } = await getEVMAllowance({
5291
+ chainId,
5292
+ tokenAddress,
5293
+ ownerAddress: owner,
5294
+ spenderAddress: spender
5295
+ });
5296
+ currentAllowance = BigInt(allowance || "0");
5297
+ } catch {
5298
+ }
5299
+ if (currentAllowance >= amountWei) continue;
5300
+ const data = (0, import_viem2.encodeFunctionData)({
5301
+ abi: erc20ApproveAbi,
5302
+ functionName: "approve",
5303
+ args: [spender, amountWei]
5304
+ });
5305
+ const hash = await sendEvmTx(w, {
5306
+ to: tokenAddress,
5307
+ data,
5308
+ value: 0n,
5309
+ chainId: Number.isFinite(Number(chainId)) ? Number(chainId) : void 0
5310
+ });
5311
+ await waitForTxConfirmation(chainId, hash);
5312
+ }
5313
+ }
5189
5314
  function isUserRejected(e2) {
5190
5315
  const code = e2?.code ?? e2?.data?.code;
5191
5316
  if (code === 4001) return true;
@@ -5219,6 +5344,13 @@ async function sendRouteTransaction(b, fallbackChainId) {
5219
5344
  }
5220
5345
  }
5221
5346
  }
5347
+ if (!validatedSponsorship) {
5348
+ await ensureApprovals(
5349
+ w,
5350
+ b.route?.execution?.approvals,
5351
+ Number.isFinite(target) ? target : void 0
5352
+ );
5353
+ }
5222
5354
  if (w.type === "eip1193") {
5223
5355
  const from = await w.getAddress();
5224
5356
  const hexValue = value ? `0x${value.toString(16)}` : "0x0";
@@ -5315,13 +5447,17 @@ async function runTopUp(params) {
5315
5447
  }
5316
5448
  }
5317
5449
  }
5318
- var import_viem2;
5450
+ var import_viem2, erc20ApproveAbi;
5319
5451
  var init_tx = __esm({
5320
5452
  "src/core/tx.ts"() {
5321
5453
  "use strict";
5322
5454
  init_wallets();
5323
5455
  init_routes();
5456
+ init_sdkRpc();
5324
5457
  import_viem2 = require("viem");
5458
+ erc20ApproveAbi = (0, import_viem2.parseAbi)([
5459
+ "function approve(address spender, uint256 amount) returns (bool)"
5460
+ ]);
5325
5461
  }
5326
5462
  });
5327
5463
 
@@ -31658,7 +31794,7 @@ function normalizeTokenAddressForCompare(chain, addr) {
31658
31794
  if (chainType === "solana") return value;
31659
31795
  return value.toLowerCase();
31660
31796
  }
31661
- function sleep2(ms) {
31797
+ function sleep3(ms) {
31662
31798
  return new Promise((resolve) => setTimeout(resolve, ms));
31663
31799
  }
31664
31800
  function useTransactionActionModel({
@@ -31776,7 +31912,7 @@ function useTransactionActionModel({
31776
31912
  if (status.status === "reverted") {
31777
31913
  throw new Error("Approval transaction reverted");
31778
31914
  }
31779
- await sleep2(intervalMs);
31915
+ await sleep3(intervalMs);
31780
31916
  }
31781
31917
  throw new Error("Timed out waiting for approval confirmation");
31782
31918
  },
@@ -33441,7 +33577,7 @@ function normalizeTx(raw) {
33441
33577
  toChainTxUrl: r.toChainTxUrl || r.to_chain_tx_url || void 0
33442
33578
  };
33443
33579
  }
33444
- function sleep3(ms) {
33580
+ function sleep4(ms) {
33445
33581
  return new Promise((resolve) => setTimeout(resolve, ms));
33446
33582
  }
33447
33583
  function isUserRejection(err) {
@@ -33459,7 +33595,7 @@ async function waitForApprovalConfirmation(chainId, txHash) {
33459
33595
  if (status.status === "success") return;
33460
33596
  if (status.status === "reverted")
33461
33597
  throw new Error("Approval transaction reverted");
33462
- await sleep3(2e3);
33598
+ await sleep4(2e3);
33463
33599
  }
33464
33600
  throw new Error("Timed out waiting for approval confirmation");
33465
33601
  }
@@ -39978,6 +40114,7 @@ __export(index_exports, {
39978
40114
  DEFAULT_SLIPPAGE: () => DEFAULT_SLIPPAGE2,
39979
40115
  GTM_ID: () => GTM_ID,
39980
40116
  IdentityStore: () => IdentityStore,
40117
+ POPULAR_ORDER: () => POPULAR_ORDER,
39981
40118
  RateLimitError: () => RateLimitError,
39982
40119
  SDK_NAME: () => SDK_NAME,
39983
40120
  SDK_VERSION: () => SDK_VERSION,
@@ -39986,6 +40123,7 @@ __export(index_exports, {
39986
40123
  TrustwareProvider: () => TrustwareProvider,
39987
40124
  TrustwareWidget: () => TrustwareWidgetV2,
39988
40125
  WALLETCONNECT_PROJECT_ID: () => WALLETCONNECT_PROJECT_ID,
40126
+ WALLETS: () => WALLETS,
39989
40127
  buildWalletIdentityAddress: () => buildWalletIdentityAddress,
39990
40128
  connectDetectedWallet: () => connectDetectedWallet,
39991
40129
  createWalletIdentity: () => createWalletIdentity,
@@ -40038,6 +40176,7 @@ init_index();
40038
40176
  DEFAULT_SLIPPAGE,
40039
40177
  GTM_ID,
40040
40178
  IdentityStore,
40179
+ POPULAR_ORDER,
40041
40180
  RateLimitError,
40042
40181
  SDK_NAME,
40043
40182
  SDK_VERSION,
@@ -40046,6 +40185,7 @@ init_index();
40046
40185
  TrustwareProvider,
40047
40186
  TrustwareWidget,
40048
40187
  WALLETCONNECT_PROJECT_ID,
40188
+ WALLETS,
40049
40189
  buildWalletIdentityAddress,
40050
40190
  connectDetectedWallet,
40051
40191
  createWalletIdentity,