@piprail/sdk 2.16.2 → 3.0.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.
Files changed (33) hide show
  1. package/CHANGELOG.md +343 -0
  2. package/README.md +1 -0
  3. package/dist/{algorand-25FMBCT3.js → algorand-HL57PQHE.js} +157 -2
  4. package/dist/{algorand-Q3TUQLPK.cjs → algorand-W776EEM2.cjs} +205 -50
  5. package/dist/{aptos-VBJONBFY.cjs → aptos-5QBXJ6NV.cjs} +255 -39
  6. package/dist/{aptos-SBV6SGDP.js → aptos-H5VLH2QL.js} +217 -1
  7. package/dist/{chunk-QONQSZHJ.cjs → chunk-6XTNI2OQ.cjs} +44 -44
  8. package/dist/{chunk-OXEFPLZA.cjs → chunk-6ZRAIQXF.cjs} +4 -4
  9. package/dist/chunk-GVCGUSTE.js +30 -0
  10. package/dist/{chunk-V2IJ5HUW.cjs → chunk-MZAJQYM3.cjs} +1 -0
  11. package/dist/chunk-MZXVXC3C.cjs +30 -0
  12. package/dist/{chunk-2CX7XRZK.js → chunk-TZDVZCTC.js} +1 -0
  13. package/dist/{chunk-C52H5TYB.js → chunk-YXBQKBDH.js} +2 -2
  14. package/dist/index.cjs +2929 -1444
  15. package/dist/index.d.cts +625 -13
  16. package/dist/index.d.ts +625 -13
  17. package/dist/index.js +2506 -1021
  18. package/dist/{near-OLKCMTBI.js → near-5X2CQGML.js} +197 -1
  19. package/dist/{near-5LTTDU6G.cjs → near-XSQXEIHT.cjs} +242 -46
  20. package/dist/{solana-AI2G7V33.cjs → solana-EBV6PUCU.cjs} +211 -52
  21. package/dist/{solana-HTKDRTD3.js → solana-O6Q6QILH.js} +167 -8
  22. package/dist/{stellar-E2KWEV2E.cjs → stellar-5C7FQLPS.cjs} +191 -29
  23. package/dist/{stellar-EUFZLX6J.js → stellar-YF5LOJEM.js} +163 -1
  24. package/dist/{sui-KCIITCYH.js → sui-JFQSDNSZ.js} +165 -3
  25. package/dist/{sui-Q3NJOJZS.cjs → sui-WIZEAO7E.cjs} +185 -23
  26. package/dist/{ton-J7TQWRN4.cjs → ton-AGTKLQA5.cjs} +264 -22
  27. package/dist/{ton-WH2JVQOO.js → ton-OZKFNRLT.js} +244 -2
  28. package/dist/{tron-FXBXDNEY.js → tron-HG3IOOCG.js} +283 -1
  29. package/dist/{tron-HIPMOX7S.cjs → tron-S4WX7OHY.cjs} +323 -41
  30. package/dist/{xrpl-RUOB37QH.js → xrpl-GOVHMYYK.js} +222 -5
  31. package/dist/{xrpl-SVVS445B.cjs → xrpl-YS3IXPLV.cjs} +273 -56
  32. package/package.json +12 -2
  33. package/dist/{chunk-QU25LSVS.js → chunk-5GRBEMCA.js} +44 -44
@@ -1,5 +1,8 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
2
 
3
+ var _chunkMZXVXC3Ccjs = require('./chunk-MZXVXC3C.cjs');
4
+
5
+
3
6
  var _chunkCQREG5LEcjs = require('./chunk-CQREG5LE.cjs');
4
7
 
5
8
 
@@ -10,7 +13,8 @@ var _chunkCQREG5LEcjs = require('./chunk-CQREG5LE.cjs');
10
13
 
11
14
 
12
15
 
13
- var _chunkQONQSZHJcjs = require('./chunk-QONQSZHJ.cjs');
16
+
17
+ var _chunk6XTNI2OQcjs = require('./chunk-6XTNI2OQ.cjs');
14
18
 
15
19
  // src/drivers/tron/index.ts
16
20
  var _tronweb = require('tronweb');
@@ -62,7 +66,7 @@ async function payTron(params) {
62
66
  return _nullishCoalesce(_nullishCoalesce(broadcast.txid, () => ( _optionalChain([broadcast, 'access', _7 => _7.transaction, 'optionalAccess', _8 => _8.txID]))), () => ( signed.txID));
63
67
  }
64
68
  if (isTronAffordability(broadcast.code, broadcast.message)) {
65
- throw new (0, _chunkQONQSZHJcjs.InsufficientFundsError)(
69
+ throw new (0, _chunk6XTNI2OQcjs.InsufficientFundsError)(
66
70
  `Tron payment rejected (${_nullishCoalesce(broadcast.code, () => ( "broadcast"))}): not enough TRX for energy/bandwidth, or insufficient token balance.`
67
71
  );
68
72
  }
@@ -70,15 +74,15 @@ async function payTron(params) {
70
74
  `Tron broadcast failed: ${_nullishCoalesce(broadcast.code, () => ( ""))} ${decodeMaybeHex(broadcast.message)}`.trim()
71
75
  );
72
76
  } catch (err) {
73
- if (err instanceof _chunkQONQSZHJcjs.InsufficientFundsError) throw err;
74
- throw _nullishCoalesce(_chunkQONQSZHJcjs.toInsufficientFundsError.call(void 0, err), () => ( err));
77
+ if (err instanceof _chunk6XTNI2OQcjs.InsufficientFundsError) throw err;
78
+ throw _nullishCoalesce(_chunk6XTNI2OQcjs.toInsufficientFundsError.call(void 0, err), () => ( err));
75
79
  }
76
80
  }
77
81
  async function payTronNative(params) {
78
82
  const { client, from, privateKey, accept } = params;
79
83
  const sun = BigInt(accept.amount);
80
84
  if (sun > BigInt(Number.MAX_SAFE_INTEGER)) {
81
- throw new (0, _chunkQONQSZHJcjs.InsufficientFundsError)(
85
+ throw new (0, _chunk6XTNI2OQcjs.InsufficientFundsError)(
82
86
  `Tron native amount ${accept.amount} sun exceeds the safe-integer range tronweb.sendTrx accepts \u2014 price this resource in a smaller native amount or a TRC-20 token.`
83
87
  );
84
88
  }
@@ -90,7 +94,7 @@ async function payTronNative(params) {
90
94
  return _nullishCoalesce(_nullishCoalesce(broadcast.txid, () => ( _optionalChain([broadcast, 'access', _11 => _11.transaction, 'optionalAccess', _12 => _12.txID]))), () => ( signed.txID));
91
95
  }
92
96
  if (isTronAffordability(broadcast.code, broadcast.message)) {
93
- throw new (0, _chunkQONQSZHJcjs.InsufficientFundsError)(
97
+ throw new (0, _chunk6XTNI2OQcjs.InsufficientFundsError)(
94
98
  `Tron TRX payment rejected (${_nullishCoalesce(broadcast.code, () => ( "broadcast"))}): not enough TRX for the amount plus bandwidth (and the ~1 TRX account-creation fee if the recipient is new).`
95
99
  );
96
100
  }
@@ -98,8 +102,8 @@ async function payTronNative(params) {
98
102
  `Tron TRX broadcast failed: ${_nullishCoalesce(broadcast.code, () => ( ""))} ${decodeMaybeHex(broadcast.message)}`.trim()
99
103
  );
100
104
  } catch (err) {
101
- if (err instanceof _chunkQONQSZHJcjs.InsufficientFundsError) throw err;
102
- throw _nullishCoalesce(_chunkQONQSZHJcjs.toInsufficientFundsError.call(void 0, err), () => ( err));
105
+ if (err instanceof _chunk6XTNI2OQcjs.InsufficientFundsError) throw err;
106
+ throw _nullishCoalesce(_chunk6XTNI2OQcjs.toInsufficientFundsError.call(void 0, err), () => ( err));
103
107
  }
104
108
  }
105
109
  function decodeMaybeHex(message) {
@@ -121,6 +125,257 @@ function isTronAffordability(code, message) {
121
125
  return /balance is not sufficient|insufficient (funds|balance|energy)|not enough/i.test(decoded);
122
126
  }
123
127
 
128
+ // src/drivers/tron/swap.ts
129
+ var SUNSWAP_V2_ROUTER = "TKzxdSv2FZKQrEqkKVgp5DcwEXBEKMg2Ax";
130
+ var WTRX = "TNUC9Qb1rRpS5CbWLmNMxXBjyFoydXjWFR";
131
+ var TRX_SYMBOL = "TRX";
132
+ var TRON_SWAP_ENERGY = 230629;
133
+ var FEE_LIMIT_SUN = 4e7;
134
+ var APPROVE_FEE_LIMIT_SUN = 2e7;
135
+ var APPROVE_TIMEOUT_MS = 6e4;
136
+ var APPROVE_POLL_MS = 3e3;
137
+ var DEADLINE_SECONDS = 300;
138
+ function sourceFor(needsApproval) {
139
+ return {
140
+ kind: "provider",
141
+ name: "SunSwap V2",
142
+ note: "Third-party Tron DEX (SunSwap V2), called contract-to-contract with no API and no API key: the quote is a constant contract call and the swap is a router call your own key signs. Cost on this route: the pool's own 0.30% constant-product fee. PipRail adds nothing, and the router has no fee parameter to add. Note Tron charges roughly 23 TRX of ENERGY per swap regardless of size, which is usually the dominant cost" + (needsApproval ? ", and selling a TRC-20 costs a second transaction on top: an approve to the router, about another 10 TRX the first time, so budget roughly 33 TRX for this direction." : "; selling native TRX needs no approval, so this is a single transaction.")
143
+ };
144
+ }
145
+ function pathFor(from, to) {
146
+ const a = from.asset === "native" ? WTRX : from.asset;
147
+ const b = to.asset === "native" ? WTRX : to.asset;
148
+ return [a, b];
149
+ }
150
+ function side(t, amount) {
151
+ const decimals = t.asset === "native" ? TRX_DECIMALS : t.decimals;
152
+ return {
153
+ asset: t.asset,
154
+ symbol: _nullishCoalesce(t.symbol, () => ( (t.asset === "native" ? TRX_SYMBOL : t.asset))),
155
+ decimals,
156
+ amount: amount.toString(),
157
+ amountFormatted: _chunk6XTNI2OQcjs.formatUnits.call(void 0, amount, decimals)
158
+ };
159
+ }
160
+ function decodeUintArray(hex) {
161
+ try {
162
+ if (!hex) return null;
163
+ const words = hex.match(/.{64}/g);
164
+ if (!words || words.length < 3) return null;
165
+ const len = Number(BigInt("0x" + words[1]));
166
+ if (!Number.isSafeInteger(len) || len < 2) return null;
167
+ const vals = words.slice(2, 2 + len).map((w) => BigInt("0x" + w));
168
+ return vals.length === len ? vals : null;
169
+ } catch (e2) {
170
+ return null;
171
+ }
172
+ }
173
+ async function quoteTronSwap(p) {
174
+ if (p.wantAmount <= 0n) return null;
175
+ const path = pathFor(p.from, p.to);
176
+ if (path[0] === path[1]) return null;
177
+ let amounts = null;
178
+ try {
179
+ const res = await p.client.transactionBuilder.triggerConstantContract(
180
+ SUNSWAP_V2_ROUTER,
181
+ "getAmountsIn(uint256,address[])",
182
+ {},
183
+ [
184
+ { type: "uint256", value: p.wantAmount.toString() },
185
+ { type: "address[]", value: path }
186
+ ],
187
+ p.owner
188
+ );
189
+ if (_optionalChain([res, 'optionalAccess', _13 => _13.result, 'optionalAccess', _14 => _14.result]) !== true) return null;
190
+ amounts = decodeUintArray(_optionalChain([res, 'access', _15 => _15.constant_result, 'optionalAccess', _16 => _16[0]]));
191
+ } catch (e3) {
192
+ return null;
193
+ }
194
+ if (!_optionalChain([amounts, 'optionalAccess', _17 => _17.length])) return null;
195
+ const amountIn = amounts[0];
196
+ if (amountIn === void 0 || amountIn <= 0n) return null;
197
+ const amountInMax = _chunkMZXVXC3Ccjs.applySlippage.call(void 0, amountIn, p.slippageBps);
198
+ return {
199
+ source: sourceFor(p.from.asset !== "native"),
200
+ network: p.network,
201
+ from: side(p.from, amountIn),
202
+ to: side(p.to, p.wantAmount),
203
+ maxSpend: amountInMax.toString(),
204
+ maxSpendFormatted: _chunk6XTNI2OQcjs.formatUnits.call(void 0, amountInMax, p.from.asset === "native" ? TRX_DECIMALS : p.from.decimals),
205
+ slippageBps: p.slippageBps,
206
+ route: {
207
+ router: SUNSWAP_V2_ROUTER,
208
+ path,
209
+ amountInMax: amountInMax.toString(),
210
+ fromIsNative: p.from.asset === "native",
211
+ toIsNative: p.to.asset === "native",
212
+ needsApproval: p.from.asset !== "native",
213
+ tokenIn: p.from.asset === "native" ? "" : p.from.asset
214
+ }
215
+ };
216
+ }
217
+ async function readAllowance(client, token, owner, spender) {
218
+ try {
219
+ const res = await client.transactionBuilder.triggerConstantContract(
220
+ token,
221
+ "allowance(address,address)",
222
+ {},
223
+ [
224
+ { type: "address", value: owner },
225
+ { type: "address", value: spender }
226
+ ],
227
+ owner
228
+ );
229
+ if (_optionalChain([res, 'optionalAccess', _18 => _18.result, 'optionalAccess', _19 => _19.result]) !== true) return 0n;
230
+ const raw = _optionalChain([res, 'access', _20 => _20.constant_result, 'optionalAccess', _21 => _21[0]]);
231
+ return raw ? BigInt("0x" + raw.slice(0, 64)) : 0n;
232
+ } catch (e4) {
233
+ return 0n;
234
+ }
235
+ }
236
+ async function sendAndConfirm(client, contract, selector, params, owner, privateKey, feeLimit) {
237
+ const built = await client.transactionBuilder.triggerSmartContract(
238
+ contract,
239
+ selector,
240
+ { feeLimit },
241
+ params,
242
+ owner
243
+ );
244
+ if (_optionalChain([built, 'optionalAccess', _22 => _22.result, 'optionalAccess', _23 => _23.result]) !== true) {
245
+ throw new Error(_nullishCoalesce(_optionalChain([built, 'optionalAccess', _24 => _24.result, 'optionalAccess', _25 => _25.message]), () => ( `Tron: ${selector} could not be built`)));
246
+ }
247
+ const signed = await client.trx.sign(built.transaction, privateKey);
248
+ const broadcast = await client.trx.sendRawTransaction(signed);
249
+ if (_optionalChain([broadcast, 'optionalAccess', _26 => _26.result]) === false || _optionalChain([broadcast, 'optionalAccess', _27 => _27.code])) {
250
+ throw new Error(`${_nullishCoalesce(broadcast.code, () => ( "broadcast failed"))}: ${_nullishCoalesce(broadcast.message, () => ( ""))}`);
251
+ }
252
+ const txid = _nullishCoalesce(_nullishCoalesce(_optionalChain([broadcast, 'optionalAccess', _28 => _28.txid]), () => ( _optionalChain([broadcast, 'optionalAccess', _29 => _29.transaction, 'optionalAccess', _30 => _30.txID]))), () => ( signed.txID));
253
+ const deadline = Date.now() + APPROVE_TIMEOUT_MS;
254
+ while (Date.now() < deadline) {
255
+ await new Promise((r) => setTimeout(r, APPROVE_POLL_MS));
256
+ let info = null;
257
+ try {
258
+ info = await client.trx.getTransactionInfo(txid);
259
+ } catch (e5) {
260
+ continue;
261
+ }
262
+ const result = _optionalChain([info, 'optionalAccess', _31 => _31.receipt, 'optionalAccess', _32 => _32.result]);
263
+ if (!result) continue;
264
+ if (result !== "SUCCESS") {
265
+ throw new Error(`Tron: ${selector} failed on-chain (${result}, tx ${txid})`);
266
+ }
267
+ return txid;
268
+ }
269
+ throw new Error(`Tron: ${selector} was not confirmed within ${APPROVE_TIMEOUT_MS / 1e3}s (tx ${txid})`);
270
+ }
271
+ function safeCallValue(sun) {
272
+ const n = Number(sun);
273
+ if (!Number.isSafeInteger(n)) {
274
+ throw new (0, _chunk6XTNI2OQcjs.InsufficientFundsError)(
275
+ `Tron: a native input of ${sun} sun exceeds the largest amount that can be sent without rounding. Split the swap into smaller amounts.`
276
+ );
277
+ }
278
+ return n;
279
+ }
280
+ async function swapTron(p) {
281
+ const route = p.quote.route;
282
+ if (!_optionalChain([route, 'optionalAccess', _33 => _33.router]) || !_optionalChain([route, 'access', _34 => _34.path, 'optionalAccess', _35 => _35.length])) {
283
+ throw new Error("Tron: swap quote is missing its SunSwap routing data \u2014 re-quote before swapping.");
284
+ }
285
+ const amountOut = BigInt(p.quote.to.amount);
286
+ const amountInMax = BigInt(route.amountInMax);
287
+ const deadline = Math.floor(Date.now() / 1e3) + DEADLINE_SECONDS;
288
+ const selector = route.fromIsNative ? "swapETHForExactTokens(uint256,address[],address,uint256)" : route.toIsNative ? "swapTokensForExactETH(uint256,uint256,address[],address,uint256)" : "swapTokensForExactTokens(uint256,uint256,address[],address,uint256)";
289
+ const params = route.fromIsNative ? [
290
+ { type: "uint256", value: amountOut.toString() },
291
+ { type: "address[]", value: route.path },
292
+ { type: "address", value: p.owner },
293
+ { type: "uint256", value: deadline }
294
+ ] : [
295
+ { type: "uint256", value: amountOut.toString() },
296
+ { type: "uint256", value: amountInMax.toString() },
297
+ { type: "address[]", value: route.path },
298
+ { type: "address", value: p.owner },
299
+ { type: "uint256", value: deadline }
300
+ ];
301
+ try {
302
+ if (route.needsApproval && route.tokenIn) {
303
+ const allowance = await readAllowance(p.client, route.tokenIn, p.owner, route.router);
304
+ if (allowance < amountInMax) {
305
+ if (allowance > 0n) {
306
+ await sendAndConfirm(
307
+ p.client,
308
+ route.tokenIn,
309
+ "approve(address,uint256)",
310
+ [
311
+ { type: "address", value: route.router },
312
+ { type: "uint256", value: "0" }
313
+ ],
314
+ p.owner,
315
+ p.privateKey,
316
+ APPROVE_FEE_LIMIT_SUN
317
+ );
318
+ }
319
+ await sendAndConfirm(
320
+ p.client,
321
+ route.tokenIn,
322
+ "approve(address,uint256)",
323
+ [
324
+ { type: "address", value: route.router },
325
+ { type: "uint256", value: amountInMax.toString() }
326
+ ],
327
+ p.owner,
328
+ p.privateKey,
329
+ APPROVE_FEE_LIMIT_SUN
330
+ );
331
+ }
332
+ }
333
+ const built = await p.client.transactionBuilder.triggerSmartContract(
334
+ route.router,
335
+ selector,
336
+ {
337
+ feeLimit: FEE_LIMIT_SUN,
338
+ /*
339
+ * A native input is sent as callValue, capped at the ceiling: the router refunds
340
+ * whatever the exact-output trade did not need.
341
+ *
342
+ * TronWeb's callValue is a JS number, so a value past 2^53 would be silently rounded
343
+ * and the swap would spend an amount nobody chose. That is ~9 billion TRX, far more
344
+ * than anyone will ever swap, but a payment library must never round money quietly.
345
+ */
346
+ ...route.fromIsNative ? { callValue: safeCallValue(amountInMax) } : {}
347
+ },
348
+ params,
349
+ p.owner
350
+ );
351
+ if (_optionalChain([built, 'optionalAccess', _36 => _36.result, 'optionalAccess', _37 => _37.result]) !== true) {
352
+ throw new Error(_nullishCoalesce(_optionalChain([built, 'optionalAccess', _38 => _38.result, 'optionalAccess', _39 => _39.message]), () => ( "SunSwap router refused to build the swap")));
353
+ }
354
+ const signed = await p.client.trx.sign(built.transaction, p.privateKey);
355
+ const broadcast = await p.client.trx.sendRawTransaction(signed);
356
+ if (_optionalChain([broadcast, 'optionalAccess', _40 => _40.result]) === false || _optionalChain([broadcast, 'optionalAccess', _41 => _41.code])) {
357
+ throw new Error(`${_nullishCoalesce(broadcast.code, () => ( "broadcast failed"))}: ${_nullishCoalesce(broadcast.message, () => ( ""))}`);
358
+ }
359
+ const txid = _nullishCoalesce(_nullishCoalesce(_optionalChain([broadcast, 'optionalAccess', _42 => _42.txid]), () => ( _optionalChain([broadcast, 'optionalAccess', _43 => _43.transaction, 'optionalAccess', _44 => _44.txID]))), () => ( signed.txID));
360
+ return {
361
+ transaction: txid,
362
+ network: p.quote.network,
363
+ source: p.quote.source,
364
+ from: p.quote.from,
365
+ to: p.quote.to
366
+ };
367
+ } catch (err) {
368
+ const msg = String(_nullishCoalesce(_optionalChain([err, 'optionalAccess', _45 => _45.message]), () => ( err)));
369
+ if (/energy|bandwidth|balance|insufficient|OUT_OF_ENERGY/i.test(msg)) {
370
+ throw new (0, _chunk6XTNI2OQcjs.InsufficientFundsError)(
371
+ `Tron swap failed: the account cannot cover it. Note a SunSwap swap burns about ${TRON_SWAP_ENERGY.toLocaleString("en-US")} energy, which without staked energy is charged as roughly 23 TRX regardless of trade size. (${msg.slice(0, 140)})`,
372
+ { cause: err }
373
+ );
374
+ }
375
+ throw err;
376
+ }
377
+ }
378
+
124
379
  // src/drivers/tron/verify.ts
125
380
  var TRANSFER_TOPIC = "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
126
381
  async function verifyTron(params) {
@@ -129,15 +384,15 @@ async function verifyTron(params) {
129
384
  let info;
130
385
  try {
131
386
  info = await reader.getTransactionInfo(txid);
132
- } catch (e2) {
387
+ } catch (e6) {
133
388
  return txNotFound(txid);
134
389
  }
135
390
  if (!info || !info.id) return txNotFound(txid);
136
- if (_optionalChain([info, 'access', _13 => _13.receipt, 'optionalAccess', _14 => _14.result]) !== "SUCCESS") {
391
+ if (_optionalChain([info, 'access', _46 => _46.receipt, 'optionalAccess', _47 => _47.result]) !== "SUCCESS") {
137
392
  return {
138
393
  ok: false,
139
394
  error: "tx_reverted",
140
- detail: `Tron tx ${txid} did not succeed (receipt.result=${_nullishCoalesce(_optionalChain([info, 'access', _15 => _15.receipt, 'optionalAccess', _16 => _16.result]), () => ( "none"))}).`
395
+ detail: `Tron tx ${txid} did not succeed (receipt.result=${_nullishCoalesce(_optionalChain([info, 'access', _48 => _48.receipt, 'optionalAccess', _49 => _49.result]), () => ( "none"))}).`
141
396
  };
142
397
  }
143
398
  const ageSeconds = Math.floor(Date.now() / 1e3) - Math.floor(info.blockTimeStamp / 1e3);
@@ -177,11 +432,11 @@ async function verifyTronNative(params) {
177
432
  let info;
178
433
  try {
179
434
  info = await reader.getTransactionInfo(txid);
180
- } catch (e3) {
435
+ } catch (e7) {
181
436
  return txNotFound(txid);
182
437
  }
183
438
  if (!info || !info.id) return txNotFound(txid);
184
- if (_optionalChain([info, 'access', _17 => _17.receipt, 'optionalAccess', _18 => _18.result]) && info.receipt.result !== "SUCCESS") {
439
+ if (_optionalChain([info, 'access', _50 => _50.receipt, 'optionalAccess', _51 => _51.result]) && info.receipt.result !== "SUCCESS") {
185
440
  return { ok: false, error: "tx_reverted", detail: `Tron tx ${txid} did not succeed (receipt.result=${info.receipt.result}).` };
186
441
  }
187
442
  const ageSeconds = Math.floor(Date.now() / 1e3) - Math.floor(info.blockTimeStamp / 1e3);
@@ -195,15 +450,15 @@ async function verifyTronNative(params) {
195
450
  let tx;
196
451
  try {
197
452
  tx = await reader.getTransaction(txid);
198
- } catch (e4) {
453
+ } catch (e8) {
199
454
  return txNotFound(txid);
200
455
  }
201
456
  if (!tx) return txNotFound(txid);
202
- const ret = _optionalChain([tx, 'access', _19 => _19.ret, 'optionalAccess', _20 => _20[0], 'optionalAccess', _21 => _21.contractRet]);
457
+ const ret = _optionalChain([tx, 'access', _52 => _52.ret, 'optionalAccess', _53 => _53[0], 'optionalAccess', _54 => _54.contractRet]);
203
458
  if (ret && ret !== "SUCCESS") {
204
459
  return { ok: false, error: "tx_reverted", detail: `Tron tx ${txid} did not succeed (contractRet=${ret}).` };
205
460
  }
206
- const contract = (_nullishCoalesce(_optionalChain([tx, 'access', _22 => _22.raw_data, 'optionalAccess', _23 => _23.contract]), () => ( [])))[0];
461
+ const contract = (_nullishCoalesce(_optionalChain([tx, 'access', _55 => _55.raw_data, 'optionalAccess', _56 => _56.contract]), () => ( [])))[0];
207
462
  if (!contract || contract.type !== "TransferContract") {
208
463
  return {
209
464
  ok: false,
@@ -211,7 +466,7 @@ async function verifyTronNative(params) {
211
466
  detail: `Tron tx ${txid} is not a native TRX transfer (TransferContract).`
212
467
  };
213
468
  }
214
- const value = _nullishCoalesce(_optionalChain([contract, 'access', _24 => _24.parameter, 'optionalAccess', _25 => _25.value]), () => ( {}));
469
+ const value = _nullishCoalesce(_optionalChain([contract, 'access', _57 => _57.parameter, 'optionalAccess', _58 => _58.value]), () => ( {}));
215
470
  const to = String(_nullishCoalesce(value.to_address, () => ( ""))).toLowerCase().replace(/^0x/, "");
216
471
  if (to !== payToHex41) {
217
472
  return { ok: false, error: "wrong_recipient", detail: `Native TRX in ${txid} was not sent to ${accept.payTo}.` };
@@ -219,7 +474,7 @@ async function verifyTronNative(params) {
219
474
  const amount = (() => {
220
475
  try {
221
476
  return BigInt(_nullishCoalesce(value.amount, () => ( 0)));
222
- } catch (e5) {
477
+ } catch (e9) {
223
478
  return 0n;
224
479
  }
225
480
  })();
@@ -254,7 +509,7 @@ function sumTransfersTo(logs, tokenHex20, payToHex20) {
254
509
  try {
255
510
  total += BigInt(`0x${log.data}`);
256
511
  from = last20(topics[1]);
257
- } catch (e6) {
512
+ } catch (e10) {
258
513
  }
259
514
  }
260
515
  return { total, from };
@@ -273,13 +528,13 @@ function txNotFound(txid) {
273
528
  // src/drivers/tron/wallet.ts
274
529
  function assertTronWallet(wallet, network) {
275
530
  if (typeof wallet !== "object" || wallet === null) {
276
- throw new (0, _chunkQONQSZHJcjs.WrongFamilyError)(
531
+ throw new (0, _chunk6XTNI2OQcjs.WrongFamilyError)(
277
532
  `chain ${network} is Tron; wallet must be { key } (32-byte hex).`
278
533
  );
279
534
  }
280
- _chunkQONQSZHJcjs.assertNoLegacyWalletKey.call(void 0, wallet, "Tron");
535
+ _chunk6XTNI2OQcjs.assertNoLegacyWalletKey.call(void 0, wallet, "Tron");
281
536
  if (!("key" in wallet)) {
282
- throw new (0, _chunkQONQSZHJcjs.WrongFamilyError)(
537
+ throw new (0, _chunk6XTNI2OQcjs.WrongFamilyError)(
283
538
  `chain ${network} is Tron; wallet must be { key } (32-byte hex).`
284
539
  );
285
540
  }
@@ -287,14 +542,14 @@ function assertTronWallet(wallet, network) {
287
542
  }
288
543
  function resolveTronPrivateKey(config) {
289
544
  if (!config.key) {
290
- throw new (0, _chunkQONQSZHJcjs.WrongFamilyError)("Tron wallet needs { key } (32-byte hex).");
545
+ throw new (0, _chunk6XTNI2OQcjs.WrongFamilyError)("Tron wallet needs { key } (32-byte hex).");
291
546
  }
292
547
  if (typeof config.key !== "string") {
293
- throw new (0, _chunkQONQSZHJcjs.WrongFamilyError)("Tron wallet { key } must be a 32-byte hex string.");
548
+ throw new (0, _chunk6XTNI2OQcjs.WrongFamilyError)("Tron wallet { key } must be a 32-byte hex string.");
294
549
  }
295
550
  const hex = config.key.replace(/^0x/i, "").toLowerCase();
296
551
  if (!/^[0-9a-f]{64}$/.test(hex)) {
297
- throw new (0, _chunkQONQSZHJcjs.WrongFamilyError)(
552
+ throw new (0, _chunk6XTNI2OQcjs.WrongFamilyError)(
298
553
  "Tron wallet { key } must be a 32-byte hex string (64 hex chars)."
299
554
  );
300
555
  }
@@ -344,21 +599,21 @@ function makeTronNetwork(preset, rpcUrl) {
344
599
  const info = preset.tokens[token.toUpperCase()];
345
600
  if (!info) {
346
601
  const known = Object.keys(preset.tokens).join(", ") || "(none built in)";
347
- throw new (0, _chunkQONQSZHJcjs.UnknownTokenError)(
602
+ throw new (0, _chunk6XTNI2OQcjs.UnknownTokenError)(
348
603
  `token "${token}" isn't built in for Tron (known: ${known}). Note: native USDC doesn't exist on Tron. Pass { address, decimals } for a custom TRC-20.`
349
604
  );
350
605
  }
351
606
  return { asset: info.address, decimals: info.decimals, symbol: info.symbol };
352
607
  }
353
- _chunkQONQSZHJcjs.rejectForeignToken.call(void 0, token, "tron", network);
608
+ _chunk6XTNI2OQcjs.rejectForeignToken.call(void 0, token, "tron", network);
354
609
  if (!("address" in token)) {
355
- throw new (0, _chunkQONQSZHJcjs.WrongFamilyError)(
610
+ throw new (0, _chunk6XTNI2OQcjs.WrongFamilyError)(
356
611
  `chain ${network} is Tron; a custom token must be { address, decimals } (Base58 T\u2026 contract).`
357
612
  );
358
613
  }
359
614
  const t = token;
360
615
  if (t.address.startsWith("0x") || !tronWeb.isAddress(t.address)) {
361
- throw new (0, _chunkQONQSZHJcjs.WrongFamilyError)(
616
+ throw new (0, _chunk6XTNI2OQcjs.WrongFamilyError)(
362
617
  `chain ${network} is Tron, but token address "${t.address}" is not a valid Tron contract (T\u2026).`
363
618
  );
364
619
  }
@@ -377,12 +632,12 @@ function makeTronNetwork(preset, rpcUrl) {
377
632
  },
378
633
  assertValidPayTo(payTo) {
379
634
  if (payTo.startsWith("0x")) {
380
- throw new (0, _chunkQONQSZHJcjs.WrongFamilyError)(
635
+ throw new (0, _chunk6XTNI2OQcjs.WrongFamilyError)(
381
636
  `chain ${network} is Tron, but payTo "${payTo}" looks like an EVM address.`
382
637
  );
383
638
  }
384
639
  if (!tronWeb.isAddress(payTo)) {
385
- throw new (0, _chunkQONQSZHJcjs.WrongFamilyError)(
640
+ throw new (0, _chunk6XTNI2OQcjs.WrongFamilyError)(
386
641
  `chain ${network} is Tron, but payTo "${payTo}" is not a valid Tron address (T\u2026).`
387
642
  );
388
643
  }
@@ -394,7 +649,7 @@ function makeTronNetwork(preset, rpcUrl) {
394
649
  const privateKey = resolveTronPrivateKey(wallet._native);
395
650
  const from = tronWeb.address.fromPrivateKey(privateKey);
396
651
  if (!from) {
397
- throw new (0, _chunkQONQSZHJcjs.WrongFamilyError)("Tron wallet { key } could not derive an address.");
652
+ throw new (0, _chunk6XTNI2OQcjs.WrongFamilyError)("Tron wallet { key } could not derive an address.");
398
653
  }
399
654
  if (accept.asset === "native") {
400
655
  return payTronNative({ client: tronWeb, from, privateKey, accept });
@@ -413,15 +668,15 @@ function makeTronNetwork(preset, rpcUrl) {
413
668
  try {
414
669
  const info = await reader.getTransactionInfo(txid);
415
670
  if (info) return { height: String(_nullishCoalesce(info.blockNumber, () => ( 0))) };
416
- } catch (e7) {
671
+ } catch (e11) {
417
672
  }
418
673
  await _chunkCQREG5LEcjs.delay.call(void 0, 2500);
419
674
  }
420
- throw new (0, _chunkQONQSZHJcjs.ConfirmationTimeoutError)(`Tron tx ${txid} did not solidify in time.`);
675
+ throw new (0, _chunk6XTNI2OQcjs.ConfirmationTimeoutError)(`Tron tx ${txid} did not solidify in time.`);
421
676
  },
422
677
  async estimateCost(accept, opts) {
423
678
  if (accept.asset === "native") {
424
- return _chunkQONQSZHJcjs.nativeCost.call(void 0, {
679
+ return _chunk6XTNI2OQcjs.nativeCost.call(void 0, {
425
680
  symbol: "TRX",
426
681
  decimals: TRX_DECIMALS,
427
682
  fee: 1300000n,
@@ -431,7 +686,7 @@ function makeTronNetwork(preset, rpcUrl) {
431
686
  }
432
687
  const ENERGY_PRICE = 420n;
433
688
  const BANDWIDTH_SUN = 345000n;
434
- if (_optionalChain([opts, 'optionalAccess', _26 => _26.from])) {
689
+ if (_optionalChain([opts, 'optionalAccess', _59 => _59.from])) {
435
690
  try {
436
691
  const r = await tronWeb.transactionBuilder.triggerConstantContract(
437
692
  accept.asset,
@@ -445,7 +700,7 @@ function makeTronNetwork(preset, rpcUrl) {
445
700
  );
446
701
  const energy = BigInt(_nullishCoalesce(r.energy_used, () => ( 0)));
447
702
  if (energy > 0n) {
448
- return _chunkQONQSZHJcjs.nativeCost.call(void 0, {
703
+ return _chunk6XTNI2OQcjs.nativeCost.call(void 0, {
449
704
  symbol: "TRX",
450
705
  decimals: TRX_DECIMALS,
451
706
  fee: energy * ENERGY_PRICE + BANDWIDTH_SUN,
@@ -453,10 +708,10 @@ function makeTronNetwork(preset, rpcUrl) {
453
708
  detail: `energy ${energy} @ ${ENERGY_PRICE} sun + bandwidth`
454
709
  });
455
710
  }
456
- } catch (e8) {
711
+ } catch (e12) {
457
712
  }
458
713
  }
459
- return _chunkQONQSZHJcjs.nativeCost.call(void 0, {
714
+ return _chunk6XTNI2OQcjs.nativeCost.call(void 0, {
460
715
  symbol: "TRX",
461
716
  decimals: TRX_DECIMALS,
462
717
  fee: 30000n * ENERGY_PRICE + BANDWIDTH_SUN,
@@ -464,6 +719,13 @@ function makeTronNetwork(preset, rpcUrl) {
464
719
  detail: "~30k energy + bandwidth (sender not staked; pass { from } for a precise estimate)"
465
720
  });
466
721
  },
722
+ /** The bound wallet's own address — where THIS wallet gets paid. Derived from the key
723
+ * material only: no RPC, nothing moved. See {@link ResolvedNetwork.addressOf}. */
724
+ async addressOf(wallet) {
725
+ const addr = tronWeb.address.fromPrivateKey(resolveTronPrivateKey(wallet._native));
726
+ if (!addr) throw new (0, _chunk6XTNI2OQcjs.WrongFamilyError)(`chain ${network}: the Tron wallet key is not a valid private key.`);
727
+ return addr;
728
+ },
467
729
  async balanceOf(wallet, asset) {
468
730
  const owner = tronWeb.address.fromPrivateKey(
469
731
  resolveTronPrivateKey(wallet._native)
@@ -481,14 +743,34 @@ function makeTronNetwork(preset, rpcUrl) {
481
743
  [{ type: "address", value: owner }],
482
744
  owner
483
745
  );
484
- const hex = _optionalChain([res, 'optionalAccess', _27 => _27.constant_result, 'optionalAccess', _28 => _28[0]]);
746
+ const hex = _optionalChain([res, 'optionalAccess', _60 => _60.constant_result, 'optionalAccess', _61 => _61[0]]);
485
747
  token = hex == null ? null : BigInt("0x" + hex);
486
- } catch (e9) {
748
+ } catch (e13) {
487
749
  token = null;
488
750
  }
489
751
  return { token, native };
490
752
  },
491
753
  // No receive prerequisite — any Tron account receives TRX/TRC-20 immediately.
754
+ /* ---- swap (OPTIONAL, opt-in): via SunSwap V2, no API, no key, no added fee. See ./swap.ts ---- */
755
+ async quoteSwap({ from, to, wantAmount, slippageBps, wallet }) {
756
+ const owner = tronWeb.address.fromPrivateKey(resolveTronPrivateKey(wallet._native));
757
+ if (!owner) return null;
758
+ return quoteTronSwap({
759
+ client: tronWeb,
760
+ network,
761
+ from,
762
+ to,
763
+ wantAmount,
764
+ slippageBps,
765
+ owner
766
+ });
767
+ },
768
+ async swap(wallet, quote) {
769
+ const privateKey = resolveTronPrivateKey(wallet._native);
770
+ const owner = tronWeb.address.fromPrivateKey(privateKey);
771
+ if (!owner) throw new Error("Tron: could not derive the payer address from the wallet key.");
772
+ return swapTron({ client: tronWeb, owner, privateKey, quote });
773
+ },
492
774
  async recipientReady() {
493
775
  return { ready: "n/a" };
494
776
  },