@trustware/sdk-staging 1.1.10-staging.0 → 1.1.11-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/constants.cjs +1 -1
- package/dist/constants.mjs +1 -1
- package/dist/{core-BPLiD1UG.d.ts → core-BgYLe9Q7.d.ts} +15 -2
- package/dist/{core-B7Ez-5-r.d.cts → core-fLSt4qD2.d.cts} +15 -2
- package/dist/core.cjs +281 -237
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.cts +2 -2
- package/dist/core.d.ts +2 -2
- package/dist/core.mjs +281 -237
- package/dist/core.mjs.map +1 -1
- package/dist/index.cjs +269 -116
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.mjs +269 -115
- package/dist/index.mjs.map +1 -1
- package/dist/{manager-B6NNV-E_.d.ts → manager-BY3NHAks.d.ts} +50 -2
- package/dist/{manager-BwjyFjjz.d.cts → manager-DT8enQbE.d.cts} +50 -2
- package/dist/{metadata-BiTMMvlY.d.ts → metadata-BPvFYMlc.d.ts} +1 -1
- package/dist/{metadata-HCflXcsF.d.cts → metadata-DyxUcKHk.d.cts} +1 -1
- package/dist/smart-account.cjs +1 -1
- package/dist/smart-account.cjs.map +1 -1
- package/dist/smart-account.d.cts +12 -12
- package/dist/smart-account.d.ts +12 -12
- package/dist/smart-account.mjs +1 -1
- package/dist/smart-account.mjs.map +1 -1
- package/dist/wallet.cjs +15 -1
- package/dist/wallet.cjs.map +1 -1
- package/dist/wallet.d.cts +3 -3
- package/dist/wallet.d.ts +3 -3
- package/dist/wallet.mjs +15 -1
- package/dist/wallet.mjs.map +1 -1
- package/dist/widget.cjs +269 -116
- package/dist/widget.cjs.map +1 -1
- package/dist/widget.mjs +269 -115
- package/dist/widget.mjs.map +1 -1
- package/package.json +1 -1
package/dist/core.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { T as Trustware, a as TrustwareCore } from './core-
|
|
2
|
-
import './manager-
|
|
1
|
+
export { T as Trustware, a as TrustwareCore } from './core-fLSt4qD2.cjs';
|
|
2
|
+
import './manager-DT8enQbE.cjs';
|
|
3
3
|
import './blockchain-BxAFzp0s.cjs';
|
|
4
4
|
import './types-DX3HtFcn.cjs';
|
package/dist/core.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { T as Trustware, a as TrustwareCore } from './core-
|
|
2
|
-
import './manager-
|
|
1
|
+
export { T as Trustware, a as TrustwareCore } from './core-BgYLe9Q7.js';
|
|
2
|
+
import './manager-BY3NHAks.js';
|
|
3
3
|
import './blockchain-BxAFzp0s.js';
|
|
4
4
|
import './types-BJY9r-mz.js';
|
package/dist/core.mjs
CHANGED
|
@@ -53,7 +53,7 @@ var init_constants = __esm({
|
|
|
53
53
|
"src/constants.ts"() {
|
|
54
54
|
"use strict";
|
|
55
55
|
SDK_NAME = "@trustware/sdk";
|
|
56
|
-
SDK_VERSION = "1.1.
|
|
56
|
+
SDK_VERSION = "1.1.11-staging.1";
|
|
57
57
|
API_ROOT = "https://bv-staging-api.trustware.io";
|
|
58
58
|
API_PREFIX = "/api";
|
|
59
59
|
WALLETCONNECT_PROJECT_ID = "72ea74c400f5111d43aea638d7d83a24";
|
|
@@ -1984,6 +1984,20 @@ var init_eipWallets = __esm({
|
|
|
1984
1984
|
"src/wallets/eipWallets.ts"() {
|
|
1985
1985
|
"use strict";
|
|
1986
1986
|
CHAIN_PARAMS = {
|
|
1987
|
+
1: {
|
|
1988
|
+
chainIdHex: "0x1",
|
|
1989
|
+
chainName: "Ethereum",
|
|
1990
|
+
rpcUrls: ["https://eth.llamarpc.com"],
|
|
1991
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
1992
|
+
blockExplorerUrls: ["https://etherscan.io"]
|
|
1993
|
+
},
|
|
1994
|
+
137: {
|
|
1995
|
+
chainIdHex: "0x89",
|
|
1996
|
+
chainName: "Polygon",
|
|
1997
|
+
rpcUrls: ["https://polygon-rpc.com"],
|
|
1998
|
+
nativeCurrency: { name: "POL", symbol: "POL", decimals: 18 },
|
|
1999
|
+
blockExplorerUrls: ["https://polygonscan.com"]
|
|
2000
|
+
},
|
|
1987
2001
|
8453: {
|
|
1988
2002
|
chainIdHex: "0x2105",
|
|
1989
2003
|
chainName: "Base",
|
|
@@ -4057,6 +4071,19 @@ async function submitReceipt(intentId, txHash, sponsorshipRequestId, eoaAddress)
|
|
|
4057
4071
|
const j = await r.json();
|
|
4058
4072
|
return j.data;
|
|
4059
4073
|
}
|
|
4074
|
+
async function submitStepReceipt(intentId, stepIndex, txHash) {
|
|
4075
|
+
const r = await rateLimitedFetch(
|
|
4076
|
+
`${apiBase()}/v1/route-intent/${intentId}/steps/${stepIndex}/receipt`,
|
|
4077
|
+
{
|
|
4078
|
+
method: "POST",
|
|
4079
|
+
headers: jsonHeaders({ "Idempotency-Key": txHash }),
|
|
4080
|
+
body: JSON.stringify({ txHash })
|
|
4081
|
+
}
|
|
4082
|
+
);
|
|
4083
|
+
await assertOK(r);
|
|
4084
|
+
const j = await r.json();
|
|
4085
|
+
return j.data;
|
|
4086
|
+
}
|
|
4060
4087
|
async function getStatus(intentId) {
|
|
4061
4088
|
const r = await rateLimitedFetch(
|
|
4062
4089
|
`${apiBase()}/v1/route-intent/${intentId}/status`,
|
|
@@ -6145,6 +6172,258 @@ var init_TrustwareError = __esm({
|
|
|
6145
6172
|
}
|
|
6146
6173
|
});
|
|
6147
6174
|
|
|
6175
|
+
// src/core/tx.ts
|
|
6176
|
+
import { keccak256, encodeFunctionData, parseAbi } from "viem";
|
|
6177
|
+
function backendChainId(chain, fallback) {
|
|
6178
|
+
const preferred = chain?.networkIdentifier ?? chain?.chainId ?? chain?.id;
|
|
6179
|
+
return String(preferred ?? fallback ?? "");
|
|
6180
|
+
}
|
|
6181
|
+
function sleep2(ms) {
|
|
6182
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
6183
|
+
}
|
|
6184
|
+
async function sendEvmTx(w, tx) {
|
|
6185
|
+
if (w.type === "eip1193") {
|
|
6186
|
+
const from = await w.getAddress();
|
|
6187
|
+
const params = {
|
|
6188
|
+
from,
|
|
6189
|
+
to: tx.to,
|
|
6190
|
+
data: tx.data,
|
|
6191
|
+
value: tx.value ? `0x${tx.value.toString(16)}` : "0x0"
|
|
6192
|
+
};
|
|
6193
|
+
if (tx.chainId !== void 0) {
|
|
6194
|
+
params.chainId = `0x${tx.chainId.toString(16)}`;
|
|
6195
|
+
}
|
|
6196
|
+
const hash = await w.request({
|
|
6197
|
+
method: "eth_sendTransaction",
|
|
6198
|
+
params: [params]
|
|
6199
|
+
});
|
|
6200
|
+
return hash;
|
|
6201
|
+
}
|
|
6202
|
+
const response = await w.sendTransaction({
|
|
6203
|
+
to: tx.to,
|
|
6204
|
+
data: tx.data,
|
|
6205
|
+
value: tx.value,
|
|
6206
|
+
chainId: tx.chainId
|
|
6207
|
+
});
|
|
6208
|
+
return response.hash;
|
|
6209
|
+
}
|
|
6210
|
+
async function waitForTxConfirmation(chainId, txHash) {
|
|
6211
|
+
const timeoutMs = 12e4;
|
|
6212
|
+
const intervalMs = 2e3;
|
|
6213
|
+
const started = Date.now();
|
|
6214
|
+
while (Date.now() - started < timeoutMs) {
|
|
6215
|
+
const status = await getEVMTxStatus({ chainId, txHash });
|
|
6216
|
+
if (status.status === "success") return;
|
|
6217
|
+
if (status.status === "reverted") {
|
|
6218
|
+
throw new Error("Approval transaction reverted");
|
|
6219
|
+
}
|
|
6220
|
+
await sleep2(intervalMs);
|
|
6221
|
+
}
|
|
6222
|
+
throw new Error("Timed out waiting for approval confirmation");
|
|
6223
|
+
}
|
|
6224
|
+
function requiredApprovalAmount(planAmountWei, routeAmountWei) {
|
|
6225
|
+
if (planAmountWei >= UNLIMITED_APPROVAL_FLOOR && routeAmountWei !== void 0 && routeAmountWei > 0n) {
|
|
6226
|
+
return routeAmountWei;
|
|
6227
|
+
}
|
|
6228
|
+
return planAmountWei;
|
|
6229
|
+
}
|
|
6230
|
+
function approvalSatisfied(currentAllowance, requiredWei) {
|
|
6231
|
+
if (requiredWei === 0n) return currentAllowance === 0n;
|
|
6232
|
+
return currentAllowance >= requiredWei;
|
|
6233
|
+
}
|
|
6234
|
+
async function ensureApprovals(w, approvals, fallbackChainId, intentId, routeAmountWei) {
|
|
6235
|
+
if (!approvals || approvals.length === 0) return;
|
|
6236
|
+
const owner = await w.getAddress();
|
|
6237
|
+
for (const [stepIndex, approval] of approvals.entries()) {
|
|
6238
|
+
const spender = approval.spender;
|
|
6239
|
+
const tokenAddress = approval.tokenAddress;
|
|
6240
|
+
const amount = approval.amount;
|
|
6241
|
+
if (!spender || !tokenAddress || !amount) continue;
|
|
6242
|
+
const chainId = approval.chainId || String(fallbackChainId ?? "");
|
|
6243
|
+
if (!chainId) continue;
|
|
6244
|
+
const amountWei = requiredApprovalAmount(BigInt(amount), routeAmountWei);
|
|
6245
|
+
let currentAllowance = 0n;
|
|
6246
|
+
try {
|
|
6247
|
+
const { allowance } = await getEVMAllowance({
|
|
6248
|
+
chainId,
|
|
6249
|
+
tokenAddress,
|
|
6250
|
+
ownerAddress: owner,
|
|
6251
|
+
spenderAddress: spender
|
|
6252
|
+
});
|
|
6253
|
+
currentAllowance = BigInt(allowance || "0");
|
|
6254
|
+
} catch {
|
|
6255
|
+
}
|
|
6256
|
+
if (approvalSatisfied(currentAllowance, amountWei)) continue;
|
|
6257
|
+
const data = encodeFunctionData({
|
|
6258
|
+
abi: erc20ApproveAbi,
|
|
6259
|
+
functionName: "approve",
|
|
6260
|
+
args: [spender, amountWei]
|
|
6261
|
+
});
|
|
6262
|
+
const hash = await sendEvmTx(w, {
|
|
6263
|
+
to: tokenAddress,
|
|
6264
|
+
data,
|
|
6265
|
+
value: 0n,
|
|
6266
|
+
chainId: Number.isFinite(Number(chainId)) ? Number(chainId) : void 0
|
|
6267
|
+
});
|
|
6268
|
+
if (intentId) {
|
|
6269
|
+
void submitStepReceipt(intentId, stepIndex, hash).catch(() => {
|
|
6270
|
+
});
|
|
6271
|
+
}
|
|
6272
|
+
await waitForTxConfirmation(chainId, hash);
|
|
6273
|
+
}
|
|
6274
|
+
}
|
|
6275
|
+
function isUserRejected(e2) {
|
|
6276
|
+
const code = e2?.code ?? e2?.data?.code;
|
|
6277
|
+
if (code === 4001) return true;
|
|
6278
|
+
const msg = String(e2?.message || e2)?.toLowerCase?.() || "";
|
|
6279
|
+
return msg.includes("user rejected") || msg.includes("user denied");
|
|
6280
|
+
}
|
|
6281
|
+
async function sendRouteTransaction(b, fallbackChainId, options) {
|
|
6282
|
+
const w = walletManager.wallet;
|
|
6283
|
+
if (!w) throw new Error("Trustware.wallet not configured");
|
|
6284
|
+
const txReq = b.txReq;
|
|
6285
|
+
if (isEvmTxRequest(txReq)) {
|
|
6286
|
+
if (w.ecosystem !== "evm") {
|
|
6287
|
+
throw new Error("An EVM wallet is required for this route");
|
|
6288
|
+
}
|
|
6289
|
+
const to = txReq.to ?? txReq.target;
|
|
6290
|
+
const data = txReq.data;
|
|
6291
|
+
const value = txReq.value ? BigInt(txReq.value) : 0n;
|
|
6292
|
+
const target = Number(txReq.chainId ?? fallbackChainId);
|
|
6293
|
+
let validatedSponsorship;
|
|
6294
|
+
if (b.sponsorship) {
|
|
6295
|
+
if (keccak256(data) === b.sponsorship.callDataHash) {
|
|
6296
|
+
validatedSponsorship = b.sponsorship;
|
|
6297
|
+
}
|
|
6298
|
+
}
|
|
6299
|
+
if (Number.isFinite(target)) {
|
|
6300
|
+
const current = await w.getChainId();
|
|
6301
|
+
if (current !== target) {
|
|
6302
|
+
try {
|
|
6303
|
+
await w.switchChain(target);
|
|
6304
|
+
} catch {
|
|
6305
|
+
}
|
|
6306
|
+
}
|
|
6307
|
+
}
|
|
6308
|
+
if (!validatedSponsorship && !options?.approvalsEnsured) {
|
|
6309
|
+
await ensureApprovals(
|
|
6310
|
+
w,
|
|
6311
|
+
b.route?.execution?.approvals,
|
|
6312
|
+
Number.isFinite(target) ? target : void 0,
|
|
6313
|
+
b.intentId,
|
|
6314
|
+
BigInt(b.route?.estimate?.fromAmount ?? "0")
|
|
6315
|
+
);
|
|
6316
|
+
}
|
|
6317
|
+
if (w.type === "eip1193") {
|
|
6318
|
+
const from = await w.getAddress();
|
|
6319
|
+
const hexValue = value ? `0x${value.toString(16)}` : "0x0";
|
|
6320
|
+
const params = {
|
|
6321
|
+
from,
|
|
6322
|
+
to,
|
|
6323
|
+
data,
|
|
6324
|
+
value: hexValue
|
|
6325
|
+
};
|
|
6326
|
+
if (Number.isFinite(target)) {
|
|
6327
|
+
params.chainId = `0x${target.toString(16)}`;
|
|
6328
|
+
}
|
|
6329
|
+
const hash = await w.request({
|
|
6330
|
+
method: "eth_sendTransaction",
|
|
6331
|
+
params: [params]
|
|
6332
|
+
});
|
|
6333
|
+
return hash;
|
|
6334
|
+
}
|
|
6335
|
+
const response = await w.sendTransaction({
|
|
6336
|
+
to,
|
|
6337
|
+
data,
|
|
6338
|
+
value,
|
|
6339
|
+
chainId: Number.isFinite(target) ? target : void 0,
|
|
6340
|
+
...validatedSponsorship ? {
|
|
6341
|
+
paymasterAndData: validatedSponsorship.paymasterAndData
|
|
6342
|
+
} : {}
|
|
6343
|
+
});
|
|
6344
|
+
return response.hash;
|
|
6345
|
+
}
|
|
6346
|
+
if (isSerializedSolanaTxRequest(txReq)) {
|
|
6347
|
+
if (w.ecosystem !== "solana") {
|
|
6348
|
+
throw new Error("A Solana wallet is required for this route");
|
|
6349
|
+
}
|
|
6350
|
+
const { getSharedRegistry: getSharedRegistry2 } = await Promise.resolve().then(() => (init_registryClient(), registryClient_exports));
|
|
6351
|
+
const registry = getSharedRegistry2();
|
|
6352
|
+
await registry.ensureChainsLoaded();
|
|
6353
|
+
const chain = registry.chain(
|
|
6354
|
+
String(fallbackChainId ?? txReq.chainId ?? "")
|
|
6355
|
+
);
|
|
6356
|
+
return w.sendSerializedTransaction(
|
|
6357
|
+
txReq.data,
|
|
6358
|
+
backendChainId(chain, fallbackChainId ?? txReq.chainId)
|
|
6359
|
+
);
|
|
6360
|
+
}
|
|
6361
|
+
throw new Error("Invalid route transaction payload");
|
|
6362
|
+
}
|
|
6363
|
+
async function runTopUp(params) {
|
|
6364
|
+
const w = walletManager.wallet;
|
|
6365
|
+
if (!w) throw new Error("Trustware.wallet not configured");
|
|
6366
|
+
const { getSharedRegistry: getSharedRegistry2 } = await Promise.resolve().then(() => (init_registryClient(), registryClient_exports));
|
|
6367
|
+
const reg = getSharedRegistry2();
|
|
6368
|
+
await reg.ensureLoaded();
|
|
6369
|
+
const fromAddress = await w.getAddress();
|
|
6370
|
+
const currentChainRef = w.ecosystem === "evm" ? String(await w.getChainId()) : await w.getChainKey?.() ?? "solana-mainnet-beta";
|
|
6371
|
+
const originalChain = w.ecosystem === "evm" ? await w.getChainId() : void 0;
|
|
6372
|
+
const fromChain = params.fromChain ?? currentChainRef;
|
|
6373
|
+
const { TrustwareConfigStore: TrustwareConfigStore2 } = await Promise.resolve().then(() => (init_store(), store_exports));
|
|
6374
|
+
const cfg = TrustwareConfigStore2.get();
|
|
6375
|
+
const toChain = params.toChain ?? String(cfg.routes.toChain);
|
|
6376
|
+
const fromToken = reg.resolveToken(
|
|
6377
|
+
fromChain,
|
|
6378
|
+
params.fromToken ?? cfg.routes.fromToken ?? void 0
|
|
6379
|
+
) ?? params.fromToken;
|
|
6380
|
+
const toToken = reg.resolveToken(
|
|
6381
|
+
toChain,
|
|
6382
|
+
params.toToken ?? cfg.routes.toToken ?? void 0
|
|
6383
|
+
) ?? params.toToken;
|
|
6384
|
+
if (!fromToken || !toToken) {
|
|
6385
|
+
throw new Error("Unable to resolve route tokens");
|
|
6386
|
+
}
|
|
6387
|
+
try {
|
|
6388
|
+
const build = await buildRoute({
|
|
6389
|
+
fromChain,
|
|
6390
|
+
toChain,
|
|
6391
|
+
fromToken,
|
|
6392
|
+
toToken,
|
|
6393
|
+
fromAmount: String(params.fromAmount),
|
|
6394
|
+
fromAddress,
|
|
6395
|
+
toAddress: params.toAddress ?? cfg.routes.toAddress ?? cfg.routes.fromAddress ?? fromAddress,
|
|
6396
|
+
slippage: cfg.routes.defaultSlippage
|
|
6397
|
+
});
|
|
6398
|
+
const hash = await sendRouteTransaction(build, fromChain);
|
|
6399
|
+
await submitReceipt(build.intentId, hash);
|
|
6400
|
+
return await pollStatus(build.intentId);
|
|
6401
|
+
} catch (e2) {
|
|
6402
|
+
if (isUserRejected(e2)) throw new Error("Transaction cancelled by user");
|
|
6403
|
+
throw e2;
|
|
6404
|
+
} finally {
|
|
6405
|
+
try {
|
|
6406
|
+
if (w.ecosystem === "evm" && originalChain && originalChain !== Number(fromChain)) {
|
|
6407
|
+
await w.switchChain(originalChain);
|
|
6408
|
+
}
|
|
6409
|
+
} catch {
|
|
6410
|
+
}
|
|
6411
|
+
}
|
|
6412
|
+
}
|
|
6413
|
+
var erc20ApproveAbi, UNLIMITED_APPROVAL_FLOOR;
|
|
6414
|
+
var init_tx = __esm({
|
|
6415
|
+
"src/core/tx.ts"() {
|
|
6416
|
+
"use strict";
|
|
6417
|
+
init_wallets();
|
|
6418
|
+
init_routes();
|
|
6419
|
+
init_sdkRpc();
|
|
6420
|
+
erc20ApproveAbi = parseAbi([
|
|
6421
|
+
"function approve(address spender, uint256 amount) returns (bool)"
|
|
6422
|
+
]);
|
|
6423
|
+
UNLIMITED_APPROVAL_FLOOR = 1n << 255n;
|
|
6424
|
+
}
|
|
6425
|
+
});
|
|
6426
|
+
|
|
6148
6427
|
// src/types/routes.ts
|
|
6149
6428
|
var init_routes2 = __esm({
|
|
6150
6429
|
"src/types/routes.ts"() {
|
|
@@ -6430,242 +6709,6 @@ var init_store = __esm({
|
|
|
6430
6709
|
}
|
|
6431
6710
|
});
|
|
6432
6711
|
|
|
6433
|
-
// src/core/tx.ts
|
|
6434
|
-
import { keccak256, encodeFunctionData, parseAbi } from "viem";
|
|
6435
|
-
function backendChainId(chain, fallback) {
|
|
6436
|
-
const preferred = chain?.networkIdentifier ?? chain?.chainId ?? chain?.id;
|
|
6437
|
-
return String(preferred ?? fallback ?? "");
|
|
6438
|
-
}
|
|
6439
|
-
function sleep2(ms) {
|
|
6440
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
6441
|
-
}
|
|
6442
|
-
async function sendEvmTx(w, tx) {
|
|
6443
|
-
if (w.type === "eip1193") {
|
|
6444
|
-
const from = await w.getAddress();
|
|
6445
|
-
const params = {
|
|
6446
|
-
from,
|
|
6447
|
-
to: tx.to,
|
|
6448
|
-
data: tx.data,
|
|
6449
|
-
value: tx.value ? `0x${tx.value.toString(16)}` : "0x0"
|
|
6450
|
-
};
|
|
6451
|
-
if (tx.chainId !== void 0) {
|
|
6452
|
-
params.chainId = `0x${tx.chainId.toString(16)}`;
|
|
6453
|
-
}
|
|
6454
|
-
const hash = await w.request({
|
|
6455
|
-
method: "eth_sendTransaction",
|
|
6456
|
-
params: [params]
|
|
6457
|
-
});
|
|
6458
|
-
return hash;
|
|
6459
|
-
}
|
|
6460
|
-
const response = await w.sendTransaction({
|
|
6461
|
-
to: tx.to,
|
|
6462
|
-
data: tx.data,
|
|
6463
|
-
value: tx.value,
|
|
6464
|
-
chainId: tx.chainId
|
|
6465
|
-
});
|
|
6466
|
-
return response.hash;
|
|
6467
|
-
}
|
|
6468
|
-
async function waitForTxConfirmation(chainId, txHash) {
|
|
6469
|
-
const timeoutMs = 12e4;
|
|
6470
|
-
const intervalMs = 2e3;
|
|
6471
|
-
const started = Date.now();
|
|
6472
|
-
while (Date.now() - started < timeoutMs) {
|
|
6473
|
-
const status = await getEVMTxStatus({ chainId, txHash });
|
|
6474
|
-
if (status.status === "success") return;
|
|
6475
|
-
if (status.status === "reverted") {
|
|
6476
|
-
throw new Error("Approval transaction reverted");
|
|
6477
|
-
}
|
|
6478
|
-
await sleep2(intervalMs);
|
|
6479
|
-
}
|
|
6480
|
-
throw new Error("Timed out waiting for approval confirmation");
|
|
6481
|
-
}
|
|
6482
|
-
async function ensureApprovals(w, approvals, fallbackChainId) {
|
|
6483
|
-
if (!approvals || approvals.length === 0) return;
|
|
6484
|
-
const owner = await w.getAddress();
|
|
6485
|
-
for (const approval of approvals) {
|
|
6486
|
-
const spender = approval.spender;
|
|
6487
|
-
const tokenAddress = approval.tokenAddress;
|
|
6488
|
-
const amount = approval.amount;
|
|
6489
|
-
if (!spender || !tokenAddress || !amount) continue;
|
|
6490
|
-
const chainId = approval.chainId || String(fallbackChainId ?? "");
|
|
6491
|
-
if (!chainId) continue;
|
|
6492
|
-
const amountWei = BigInt(amount);
|
|
6493
|
-
if (amountWei === 0n) continue;
|
|
6494
|
-
let currentAllowance = 0n;
|
|
6495
|
-
try {
|
|
6496
|
-
const { allowance } = await getEVMAllowance({
|
|
6497
|
-
chainId,
|
|
6498
|
-
tokenAddress,
|
|
6499
|
-
ownerAddress: owner,
|
|
6500
|
-
spenderAddress: spender
|
|
6501
|
-
});
|
|
6502
|
-
currentAllowance = BigInt(allowance || "0");
|
|
6503
|
-
} catch {
|
|
6504
|
-
}
|
|
6505
|
-
if (currentAllowance >= amountWei) continue;
|
|
6506
|
-
const data = encodeFunctionData({
|
|
6507
|
-
abi: erc20ApproveAbi,
|
|
6508
|
-
functionName: "approve",
|
|
6509
|
-
args: [spender, amountWei]
|
|
6510
|
-
});
|
|
6511
|
-
const hash = await sendEvmTx(w, {
|
|
6512
|
-
to: tokenAddress,
|
|
6513
|
-
data,
|
|
6514
|
-
value: 0n,
|
|
6515
|
-
chainId: Number.isFinite(Number(chainId)) ? Number(chainId) : void 0
|
|
6516
|
-
});
|
|
6517
|
-
await waitForTxConfirmation(chainId, hash);
|
|
6518
|
-
}
|
|
6519
|
-
}
|
|
6520
|
-
function isUserRejected(e2) {
|
|
6521
|
-
const code = e2?.code ?? e2?.data?.code;
|
|
6522
|
-
if (code === 4001) return true;
|
|
6523
|
-
const msg = String(e2?.message || e2)?.toLowerCase?.() || "";
|
|
6524
|
-
return msg.includes("user rejected") || msg.includes("user denied");
|
|
6525
|
-
}
|
|
6526
|
-
async function sendRouteTransaction(b, fallbackChainId) {
|
|
6527
|
-
const w = walletManager.wallet;
|
|
6528
|
-
if (!w) throw new Error("Trustware.wallet not configured");
|
|
6529
|
-
const txReq = b.txReq;
|
|
6530
|
-
if (isEvmTxRequest(txReq)) {
|
|
6531
|
-
if (w.ecosystem !== "evm") {
|
|
6532
|
-
throw new Error("An EVM wallet is required for this route");
|
|
6533
|
-
}
|
|
6534
|
-
const to = txReq.to ?? txReq.target;
|
|
6535
|
-
const data = txReq.data;
|
|
6536
|
-
const value = txReq.value ? BigInt(txReq.value) : 0n;
|
|
6537
|
-
const target = Number(txReq.chainId ?? fallbackChainId);
|
|
6538
|
-
let validatedSponsorship;
|
|
6539
|
-
if (b.sponsorship) {
|
|
6540
|
-
if (keccak256(data) === b.sponsorship.callDataHash) {
|
|
6541
|
-
validatedSponsorship = b.sponsorship;
|
|
6542
|
-
}
|
|
6543
|
-
}
|
|
6544
|
-
if (Number.isFinite(target)) {
|
|
6545
|
-
const current = await w.getChainId();
|
|
6546
|
-
if (current !== target) {
|
|
6547
|
-
try {
|
|
6548
|
-
await w.switchChain(target);
|
|
6549
|
-
} catch {
|
|
6550
|
-
}
|
|
6551
|
-
}
|
|
6552
|
-
}
|
|
6553
|
-
if (!validatedSponsorship) {
|
|
6554
|
-
await ensureApprovals(
|
|
6555
|
-
w,
|
|
6556
|
-
b.route?.execution?.approvals,
|
|
6557
|
-
Number.isFinite(target) ? target : void 0
|
|
6558
|
-
);
|
|
6559
|
-
}
|
|
6560
|
-
if (w.type === "eip1193") {
|
|
6561
|
-
const from = await w.getAddress();
|
|
6562
|
-
const hexValue = value ? `0x${value.toString(16)}` : "0x0";
|
|
6563
|
-
const params = {
|
|
6564
|
-
from,
|
|
6565
|
-
to,
|
|
6566
|
-
data,
|
|
6567
|
-
value: hexValue
|
|
6568
|
-
};
|
|
6569
|
-
if (Number.isFinite(target)) {
|
|
6570
|
-
params.chainId = `0x${target.toString(16)}`;
|
|
6571
|
-
}
|
|
6572
|
-
const hash = await w.request({
|
|
6573
|
-
method: "eth_sendTransaction",
|
|
6574
|
-
params: [params]
|
|
6575
|
-
});
|
|
6576
|
-
return hash;
|
|
6577
|
-
}
|
|
6578
|
-
const response = await w.sendTransaction({
|
|
6579
|
-
to,
|
|
6580
|
-
data,
|
|
6581
|
-
value,
|
|
6582
|
-
chainId: Number.isFinite(target) ? target : void 0,
|
|
6583
|
-
...validatedSponsorship ? {
|
|
6584
|
-
paymasterAndData: validatedSponsorship.paymasterAndData
|
|
6585
|
-
} : {}
|
|
6586
|
-
});
|
|
6587
|
-
return response.hash;
|
|
6588
|
-
}
|
|
6589
|
-
if (isSerializedSolanaTxRequest(txReq)) {
|
|
6590
|
-
if (w.ecosystem !== "solana") {
|
|
6591
|
-
throw new Error("A Solana wallet is required for this route");
|
|
6592
|
-
}
|
|
6593
|
-
const { getSharedRegistry: getSharedRegistry2 } = await Promise.resolve().then(() => (init_registryClient(), registryClient_exports));
|
|
6594
|
-
const registry = getSharedRegistry2();
|
|
6595
|
-
await registry.ensureChainsLoaded();
|
|
6596
|
-
const chain = registry.chain(
|
|
6597
|
-
String(fallbackChainId ?? txReq.chainId ?? "")
|
|
6598
|
-
);
|
|
6599
|
-
return w.sendSerializedTransaction(
|
|
6600
|
-
txReq.data,
|
|
6601
|
-
backendChainId(chain, fallbackChainId ?? txReq.chainId)
|
|
6602
|
-
);
|
|
6603
|
-
}
|
|
6604
|
-
throw new Error("Invalid route transaction payload");
|
|
6605
|
-
}
|
|
6606
|
-
async function runTopUp(params) {
|
|
6607
|
-
const w = walletManager.wallet;
|
|
6608
|
-
if (!w) throw new Error("Trustware.wallet not configured");
|
|
6609
|
-
const { getSharedRegistry: getSharedRegistry2 } = await Promise.resolve().then(() => (init_registryClient(), registryClient_exports));
|
|
6610
|
-
const reg = getSharedRegistry2();
|
|
6611
|
-
await reg.ensureLoaded();
|
|
6612
|
-
const fromAddress = await w.getAddress();
|
|
6613
|
-
const currentChainRef = w.ecosystem === "evm" ? String(await w.getChainId()) : await w.getChainKey?.() ?? "solana-mainnet-beta";
|
|
6614
|
-
const originalChain = w.ecosystem === "evm" ? await w.getChainId() : void 0;
|
|
6615
|
-
const fromChain = params.fromChain ?? currentChainRef;
|
|
6616
|
-
const { TrustwareConfigStore: TrustwareConfigStore2 } = await Promise.resolve().then(() => (init_store(), store_exports));
|
|
6617
|
-
const cfg = TrustwareConfigStore2.get();
|
|
6618
|
-
const toChain = params.toChain ?? String(cfg.routes.toChain);
|
|
6619
|
-
const fromToken = reg.resolveToken(
|
|
6620
|
-
fromChain,
|
|
6621
|
-
params.fromToken ?? cfg.routes.fromToken ?? void 0
|
|
6622
|
-
) ?? params.fromToken;
|
|
6623
|
-
const toToken = reg.resolveToken(
|
|
6624
|
-
toChain,
|
|
6625
|
-
params.toToken ?? cfg.routes.toToken ?? void 0
|
|
6626
|
-
) ?? params.toToken;
|
|
6627
|
-
if (!fromToken || !toToken) {
|
|
6628
|
-
throw new Error("Unable to resolve route tokens");
|
|
6629
|
-
}
|
|
6630
|
-
try {
|
|
6631
|
-
const build = await buildRoute({
|
|
6632
|
-
fromChain,
|
|
6633
|
-
toChain,
|
|
6634
|
-
fromToken,
|
|
6635
|
-
toToken,
|
|
6636
|
-
fromAmount: String(params.fromAmount),
|
|
6637
|
-
fromAddress,
|
|
6638
|
-
toAddress: params.toAddress ?? cfg.routes.toAddress ?? cfg.routes.fromAddress ?? fromAddress,
|
|
6639
|
-
slippage: cfg.routes.defaultSlippage
|
|
6640
|
-
});
|
|
6641
|
-
const hash = await sendRouteTransaction(build, fromChain);
|
|
6642
|
-
await submitReceipt(build.intentId, hash);
|
|
6643
|
-
return await pollStatus(build.intentId);
|
|
6644
|
-
} catch (e2) {
|
|
6645
|
-
if (isUserRejected(e2)) throw new Error("Transaction cancelled by user");
|
|
6646
|
-
throw e2;
|
|
6647
|
-
} finally {
|
|
6648
|
-
try {
|
|
6649
|
-
if (w.ecosystem === "evm" && originalChain && originalChain !== Number(fromChain)) {
|
|
6650
|
-
await w.switchChain(originalChain);
|
|
6651
|
-
}
|
|
6652
|
-
} catch {
|
|
6653
|
-
}
|
|
6654
|
-
}
|
|
6655
|
-
}
|
|
6656
|
-
var erc20ApproveAbi;
|
|
6657
|
-
var init_tx = __esm({
|
|
6658
|
-
"src/core/tx.ts"() {
|
|
6659
|
-
"use strict";
|
|
6660
|
-
init_wallets();
|
|
6661
|
-
init_routes();
|
|
6662
|
-
init_sdkRpc();
|
|
6663
|
-
erc20ApproveAbi = parseAbi([
|
|
6664
|
-
"function approve(address spender, uint256 amount) returns (bool)"
|
|
6665
|
-
]);
|
|
6666
|
-
}
|
|
6667
|
-
});
|
|
6668
|
-
|
|
6669
6712
|
// src/core/index.ts
|
|
6670
6713
|
var _lastValidatedKey, Trustware;
|
|
6671
6714
|
var init_core = __esm({
|
|
@@ -6808,6 +6851,7 @@ var init_core = __esm({
|
|
|
6808
6851
|
buildRoute,
|
|
6809
6852
|
buildDepositAddress,
|
|
6810
6853
|
submitReceipt,
|
|
6854
|
+
submitStepReceipt,
|
|
6811
6855
|
getStatus,
|
|
6812
6856
|
pollStatus,
|
|
6813
6857
|
getBalances,
|