@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.
@@ -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.9-staging.7";
33
+ var SDK_VERSION = "1.1.10-staging.0";
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.9-staging.7";
3
+ var SDK_VERSION = "1.1.10-staging.0";
4
4
  var API_ROOT = "https://bv-staging-api.trustware.io";
5
5
  var GTM_ID = "GTM-TZDGNCXB";
6
6
  var API_PREFIX = "/api";
@@ -1,4 +1,4 @@
1
- import { B as BuildRouteResult, b as Transaction, T as TrustwareConfigOptions, W as WalletInterFaceAPI, m as ResolvedTrustwareConfig, z as TrustwareTheme, O as WalletIdentity, a1 as walletManager, H as WalletAddressResolution, a4 as buildRoute, a5 as buildDepositAddress, a6 as submitReceipt, a7 as getStatus, a8 as pollStatus } from './manager-DWFNyouP.cjs';
1
+ import { n as BuildRouteResult, k as Transaction, T as TrustwareConfigOptions, i as WalletInterFaceAPI, z as ResolvedTrustwareConfig, V as TrustwareTheme, a1 as WalletIdentity, w as walletManager, Y as WalletAddressResolution, a8 as buildRoute, a9 as buildDepositAddress, aa as submitReceipt, ab as getStatus, ac as pollStatus } from './manager-BwjyFjjz.cjs';
2
2
  import { C as ChainDef, c as ChainType, G as GetBalancesOptions, B as BalanceRow, a as BalanceStreamOptions, W as WalletAddressBalanceWrapper } from './blockchain-BxAFzp0s.cjs';
3
3
  import { T as Token } from './types-DX3HtFcn.cjs';
4
4
 
@@ -1,4 +1,4 @@
1
- import { B as BuildRouteResult, b as Transaction, T as TrustwareConfigOptions, W as WalletInterFaceAPI, m as ResolvedTrustwareConfig, z as TrustwareTheme, O as WalletIdentity, a1 as walletManager, H as WalletAddressResolution, a4 as buildRoute, a5 as buildDepositAddress, a6 as submitReceipt, a7 as getStatus, a8 as pollStatus } from './manager-Dn4muk74.js';
1
+ import { n as BuildRouteResult, k as Transaction, T as TrustwareConfigOptions, i as WalletInterFaceAPI, z as ResolvedTrustwareConfig, V as TrustwareTheme, a1 as WalletIdentity, w as walletManager, Y as WalletAddressResolution, a8 as buildRoute, a9 as buildDepositAddress, aa as submitReceipt, ab as getStatus, ac as pollStatus } from './manager-B6NNV-E_.js';
2
2
  import { C as ChainDef, c as ChainType, G as GetBalancesOptions, B as BalanceRow, a as BalanceStreamOptions, W as WalletAddressBalanceWrapper } from './blockchain-BxAFzp0s.js';
3
3
  import { T as Token } from './types-BJY9r-mz.js';
4
4
 
package/dist/core.cjs CHANGED
@@ -75,7 +75,7 @@ var init_constants = __esm({
75
75
  "src/constants.ts"() {
76
76
  "use strict";
77
77
  SDK_NAME = "@trustware/sdk";
78
- SDK_VERSION = "1.1.9-staging.7";
78
+ SDK_VERSION = "1.1.10-staging.0";
79
79
  API_ROOT = "https://bv-staging-api.trustware.io";
80
80
  API_PREFIX = "/api";
81
81
  WALLETCONNECT_PROJECT_ID = "72ea74c400f5111d43aea638d7d83a24";
@@ -2055,6 +2055,20 @@ async function requestSDKRPC(path, init = {}) {
2055
2055
  }
2056
2056
  );
2057
2057
  }
2058
+ async function getEVMAllowance(params) {
2059
+ const search = new URLSearchParams(params);
2060
+ return requestSDKRPC(
2061
+ `/evm/allowance?${search.toString()}`,
2062
+ { method: "GET" }
2063
+ );
2064
+ }
2065
+ async function getEVMTxStatus(params) {
2066
+ const search = new URLSearchParams(params);
2067
+ return requestSDKRPC(
2068
+ `/evm/tx-status?${search.toString()}`,
2069
+ { method: "GET" }
2070
+ );
2071
+ }
2058
2072
  async function sendSolanaSerialized(body) {
2059
2073
  return requestSDKRPC(
2060
2074
  "/solana/send-serialized",
@@ -3928,6 +3942,27 @@ var init_wallets = __esm({
3928
3942
  });
3929
3943
 
3930
3944
  // src/core/routes.ts
3945
+ function assertValidPostHook(hooks) {
3946
+ const postHook = hooks?.postHook;
3947
+ if (!postHook) return;
3948
+ if (!postHook.target?.trim()) {
3949
+ throw new Error("hooks.postHook.target is required.");
3950
+ }
3951
+ if (!postHook.callData?.trim()) {
3952
+ throw new Error("hooks.postHook.callData is required.");
3953
+ }
3954
+ if (postHook.fullAmount) {
3955
+ if (postHook.amountInputPos === void 0) {
3956
+ throw new Error(
3957
+ "hooks.postHook.amountInputPos is required when fullAmount is true."
3958
+ );
3959
+ }
3960
+ } else if (!postHook.fundAmount?.trim()) {
3961
+ throw new Error(
3962
+ "hooks.postHook.fundAmount is required unless fullAmount is set."
3963
+ );
3964
+ }
3965
+ }
3931
3966
  function isEvmTxRequest(txReq) {
3932
3967
  return Boolean(txReq?.data && (txReq.to || txReq.target));
3933
3968
  }
@@ -3946,6 +3981,7 @@ async function buildRoute(body, signal) {
3946
3981
  if (!addressValidation.isValid) {
3947
3982
  throw new Error(addressValidation.error || "Invalid route addresses.");
3948
3983
  }
3984
+ assertValidPostHook(body.hooks);
3949
3985
  const cfg = TrustwareConfigStore.get();
3950
3986
  const url = `${apiBase()}/v1/routes/route`;
3951
3987
  const payload = {
@@ -3984,6 +4020,7 @@ async function buildRoute(body, signal) {
3984
4020
  return { intentId, txReq, actions, finalExchangeRate, route, sponsorship };
3985
4021
  }
3986
4022
  async function buildDepositAddress(body, signal) {
4023
+ assertValidPostHook(body.hooks);
3987
4024
  const cfg = TrustwareConfigStore.get();
3988
4025
  const url = `${apiBase()}/v1/routes/deposit-address`;
3989
4026
  const payload = {
@@ -6420,6 +6457,87 @@ function backendChainId(chain, fallback) {
6420
6457
  const preferred = chain?.networkIdentifier ?? chain?.chainId ?? chain?.id;
6421
6458
  return String(preferred ?? fallback ?? "");
6422
6459
  }
6460
+ function sleep2(ms) {
6461
+ return new Promise((resolve) => setTimeout(resolve, ms));
6462
+ }
6463
+ async function sendEvmTx(w, tx) {
6464
+ if (w.type === "eip1193") {
6465
+ const from = await w.getAddress();
6466
+ const params = {
6467
+ from,
6468
+ to: tx.to,
6469
+ data: tx.data,
6470
+ value: tx.value ? `0x${tx.value.toString(16)}` : "0x0"
6471
+ };
6472
+ if (tx.chainId !== void 0) {
6473
+ params.chainId = `0x${tx.chainId.toString(16)}`;
6474
+ }
6475
+ const hash = await w.request({
6476
+ method: "eth_sendTransaction",
6477
+ params: [params]
6478
+ });
6479
+ return hash;
6480
+ }
6481
+ const response = await w.sendTransaction({
6482
+ to: tx.to,
6483
+ data: tx.data,
6484
+ value: tx.value,
6485
+ chainId: tx.chainId
6486
+ });
6487
+ return response.hash;
6488
+ }
6489
+ async function waitForTxConfirmation(chainId, txHash) {
6490
+ const timeoutMs = 12e4;
6491
+ const intervalMs = 2e3;
6492
+ const started = Date.now();
6493
+ while (Date.now() - started < timeoutMs) {
6494
+ const status = await getEVMTxStatus({ chainId, txHash });
6495
+ if (status.status === "success") return;
6496
+ if (status.status === "reverted") {
6497
+ throw new Error("Approval transaction reverted");
6498
+ }
6499
+ await sleep2(intervalMs);
6500
+ }
6501
+ throw new Error("Timed out waiting for approval confirmation");
6502
+ }
6503
+ async function ensureApprovals(w, approvals, fallbackChainId) {
6504
+ if (!approvals || approvals.length === 0) return;
6505
+ const owner = await w.getAddress();
6506
+ for (const approval of approvals) {
6507
+ const spender = approval.spender;
6508
+ const tokenAddress = approval.tokenAddress;
6509
+ const amount = approval.amount;
6510
+ if (!spender || !tokenAddress || !amount) continue;
6511
+ const chainId = approval.chainId || String(fallbackChainId ?? "");
6512
+ if (!chainId) continue;
6513
+ const amountWei = BigInt(amount);
6514
+ if (amountWei === 0n) continue;
6515
+ let currentAllowance = 0n;
6516
+ try {
6517
+ const { allowance } = await getEVMAllowance({
6518
+ chainId,
6519
+ tokenAddress,
6520
+ ownerAddress: owner,
6521
+ spenderAddress: spender
6522
+ });
6523
+ currentAllowance = BigInt(allowance || "0");
6524
+ } catch {
6525
+ }
6526
+ if (currentAllowance >= amountWei) continue;
6527
+ const data = (0, import_viem2.encodeFunctionData)({
6528
+ abi: erc20ApproveAbi,
6529
+ functionName: "approve",
6530
+ args: [spender, amountWei]
6531
+ });
6532
+ const hash = await sendEvmTx(w, {
6533
+ to: tokenAddress,
6534
+ data,
6535
+ value: 0n,
6536
+ chainId: Number.isFinite(Number(chainId)) ? Number(chainId) : void 0
6537
+ });
6538
+ await waitForTxConfirmation(chainId, hash);
6539
+ }
6540
+ }
6423
6541
  function isUserRejected(e2) {
6424
6542
  const code = e2?.code ?? e2?.data?.code;
6425
6543
  if (code === 4001) return true;
@@ -6453,6 +6571,13 @@ async function sendRouteTransaction(b, fallbackChainId) {
6453
6571
  }
6454
6572
  }
6455
6573
  }
6574
+ if (!validatedSponsorship) {
6575
+ await ensureApprovals(
6576
+ w,
6577
+ b.route?.execution?.approvals,
6578
+ Number.isFinite(target) ? target : void 0
6579
+ );
6580
+ }
6456
6581
  if (w.type === "eip1193") {
6457
6582
  const from = await w.getAddress();
6458
6583
  const hexValue = value ? `0x${value.toString(16)}` : "0x0";
@@ -6549,13 +6674,17 @@ async function runTopUp(params) {
6549
6674
  }
6550
6675
  }
6551
6676
  }
6552
- var import_viem2;
6677
+ var import_viem2, erc20ApproveAbi;
6553
6678
  var init_tx = __esm({
6554
6679
  "src/core/tx.ts"() {
6555
6680
  "use strict";
6556
6681
  init_wallets();
6557
6682
  init_routes();
6683
+ init_sdkRpc();
6558
6684
  import_viem2 = require("viem");
6685
+ erc20ApproveAbi = (0, import_viem2.parseAbi)([
6686
+ "function approve(address spender, uint256 amount) returns (bool)"
6687
+ ]);
6559
6688
  }
6560
6689
  });
6561
6690