@t2000/sdk 0.21.7 → 0.21.9

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.d.cts CHANGED
@@ -147,7 +147,7 @@ interface TxMetadata {
147
147
  operation: 'send' | 'save' | 'withdraw' | 'borrow' | 'repay' | 'pay';
148
148
  amount?: number;
149
149
  }
150
- declare const OUTBOUND_OPS: Set<"save" | "withdraw" | "borrow" | "repay" | "send" | "pay">;
150
+ declare const OUTBOUND_OPS: Set<"save" | "borrow" | "send" | "withdraw" | "repay" | "pay">;
151
151
  declare const DEFAULT_SAFEGUARD_CONFIG: SafeguardConfig;
152
152
 
153
153
  declare class SafeguardEnforcer {
@@ -313,6 +313,7 @@ declare class T2000 extends EventEmitter<T2000Events> {
313
313
  }): Promise<WithdrawResult>;
314
314
  private withdrawAllProtocols;
315
315
  private _fetchCoins;
316
+ private _resolveTokenName;
316
317
  private _mergeCoinsInTx;
317
318
  private _lastFundDigest;
318
319
  private _autoFundFromSavings;
@@ -516,6 +517,7 @@ declare class SafeguardError extends T2000Error {
516
517
  interface GasExecutionResult {
517
518
  digest: string;
518
519
  effects: unknown;
520
+ balanceChanges?: unknown[];
519
521
  gasMethod: GasMethod;
520
522
  gasCostSui: number;
521
523
  /** Pre-TX SUI balance in MIST — used internally for proactive gas maintenance. */
package/dist/index.d.ts CHANGED
@@ -147,7 +147,7 @@ interface TxMetadata {
147
147
  operation: 'send' | 'save' | 'withdraw' | 'borrow' | 'repay' | 'pay';
148
148
  amount?: number;
149
149
  }
150
- declare const OUTBOUND_OPS: Set<"save" | "withdraw" | "borrow" | "repay" | "send" | "pay">;
150
+ declare const OUTBOUND_OPS: Set<"save" | "borrow" | "send" | "withdraw" | "repay" | "pay">;
151
151
  declare const DEFAULT_SAFEGUARD_CONFIG: SafeguardConfig;
152
152
 
153
153
  declare class SafeguardEnforcer {
@@ -313,6 +313,7 @@ declare class T2000 extends EventEmitter<T2000Events> {
313
313
  }): Promise<WithdrawResult>;
314
314
  private withdrawAllProtocols;
315
315
  private _fetchCoins;
316
+ private _resolveTokenName;
316
317
  private _mergeCoinsInTx;
317
318
  private _lastFundDigest;
318
319
  private _autoFundFromSavings;
@@ -516,6 +517,7 @@ declare class SafeguardError extends T2000Error {
516
517
  interface GasExecutionResult {
517
518
  digest: string;
518
519
  effects: unknown;
520
+ balanceChanges?: unknown[];
519
521
  gasMethod: GasMethod;
520
522
  gasCostSui: number;
521
523
  /** Pre-TX SUI balance in MIST — used internally for proactive gas maintenance. */
package/dist/index.js CHANGED
@@ -1678,13 +1678,14 @@ async function trySelfFunded(client, signer, tx) {
1678
1678
  const result = await client.executeTransactionBlock({
1679
1679
  transactionBlock: toBase64(builtBytes),
1680
1680
  signature: [signature],
1681
- options: { showEffects: true }
1681
+ options: { showEffects: true, showBalanceChanges: true }
1682
1682
  });
1683
1683
  await client.waitForTransaction({ digest: result.digest });
1684
1684
  await assertTxSuccess(result.effects, result.digest);
1685
1685
  return {
1686
1686
  digest: result.digest,
1687
1687
  effects: result.effects,
1688
+ balanceChanges: result.balanceChanges,
1688
1689
  gasMethod: "self-funded",
1689
1690
  gasCostSui: extractGasCost(result.effects),
1690
1691
  preTxSuiMist: suiBalance
@@ -1703,13 +1704,14 @@ async function tryAutoTopUpThenSelfFund(client, signer, buildTx) {
1703
1704
  const result = await client.executeTransactionBlock({
1704
1705
  transactionBlock: toBase64(builtBytes),
1705
1706
  signature: [signature],
1706
- options: { showEffects: true }
1707
+ options: { showEffects: true, showBalanceChanges: true }
1707
1708
  });
1708
1709
  await client.waitForTransaction({ digest: result.digest });
1709
1710
  await assertTxSuccess(result.effects, result.digest);
1710
1711
  return {
1711
1712
  digest: result.digest,
1712
1713
  effects: result.effects,
1714
+ balanceChanges: result.balanceChanges,
1713
1715
  gasMethod: "auto-topup",
1714
1716
  gasCostSui: extractGasCost(result.effects),
1715
1717
  preTxSuiMist: suiAfterTopUp
@@ -1733,7 +1735,7 @@ async function trySponsored(client, signer, tx) {
1733
1735
  const result = await client.executeTransactionBlock({
1734
1736
  transactionBlock: sponsoredResult.txBytes,
1735
1737
  signature: [agentSig, sponsoredResult.sponsorSignature],
1736
- options: { showEffects: true }
1738
+ options: { showEffects: true, showBalanceChanges: true }
1737
1739
  });
1738
1740
  await client.waitForTransaction({ digest: result.digest });
1739
1741
  await assertTxSuccess(result.effects, result.digest);
@@ -1742,6 +1744,7 @@ async function trySponsored(client, signer, tx) {
1742
1744
  return {
1743
1745
  digest: result.digest,
1744
1746
  effects: result.effects,
1747
+ balanceChanges: result.balanceChanges,
1745
1748
  gasMethod: "sponsored",
1746
1749
  gasCostSui: gasCost,
1747
1750
  preTxSuiMist: suiBalance
@@ -2322,6 +2325,14 @@ var T2000 = class _T2000 extends EventEmitter {
2322
2325
  if (route.priceImpact > 0.05) {
2323
2326
  console.warn(`[swap] High price impact: ${(route.priceImpact * 100).toFixed(2)}%`);
2324
2327
  }
2328
+ const toEntry = Object.entries(SUPPORTED_ASSETS).find(([, v]) => v.type === toType);
2329
+ const toDecimals = toEntry ? toEntry[1].decimals : toType === "0x2::sui::SUI" ? 9 : 6;
2330
+ let preBalance = 0n;
2331
+ try {
2332
+ const bal = await this.client.getBalance({ owner: this._address, coinType: toType });
2333
+ preBalance = BigInt(bal.totalBalance);
2334
+ } catch {
2335
+ }
2325
2336
  const gasResult = await executeWithGas(this.client, this._signer, async () => {
2326
2337
  const tx = new Transaction();
2327
2338
  tx.setSender(this._address);
@@ -2344,32 +2355,26 @@ var T2000 = class _T2000 extends EventEmitter {
2344
2355
  tx.transferObjects([outputCoin], this._address);
2345
2356
  return tx;
2346
2357
  });
2347
- const toEntry = Object.entries(SUPPORTED_ASSETS).find(([, v]) => v.type === toType);
2348
- const toDecimals = toEntry ? toEntry[1].decimals : toType === "0x2::sui::SUI" ? 9 : 6;
2349
2358
  const fromAmount = Number(route.amountIn) / 10 ** fromDecimals;
2350
2359
  let toAmount = Number(route.amountOut) / 10 ** toDecimals;
2351
2360
  try {
2352
- const txBlock = await this.client.getTransactionBlock({
2353
- digest: gasResult.digest,
2354
- options: { showBalanceChanges: true }
2355
- });
2356
- const changes = txBlock.balanceChanges ?? [];
2357
- const received = changes.find(
2358
- (c) => c.coinType === toType && BigInt(c.amount) > 0n && c.owner.AddressOwner === this._address
2359
- );
2360
- if (received) {
2361
- const actual = Number(BigInt(received.amount)) / 10 ** toDecimals;
2362
- if (actual > 0) toAmount = actual;
2361
+ const postBal = await this.client.getBalance({ owner: this._address, coinType: toType });
2362
+ const postBalance = BigInt(postBal.totalBalance);
2363
+ const diff = postBalance - preBalance;
2364
+ if (diff > 0n) {
2365
+ toAmount = Number(diff) / 10 ** toDecimals;
2363
2366
  }
2364
- } catch (e) {
2365
- console.warn("[swap] Could not parse on-chain balance changes, using route estimate:", e);
2367
+ } catch {
2368
+ console.warn("[swap] Could not read post-swap balance, using route estimate");
2366
2369
  }
2370
+ const fromName = fromEntry ? fromEntry[0] : this._resolveTokenName(fromType, params.from);
2371
+ const toName = toEntry ? toEntry[0] : this._resolveTokenName(toType, params.to);
2367
2372
  const routeDesc = route.routerData.paths?.map((p) => p.provider).filter(Boolean).slice(0, 3).join(" + ") ?? "Cetus Aggregator";
2368
2373
  return {
2369
2374
  success: true,
2370
2375
  tx: gasResult.digest,
2371
- fromToken: params.from,
2372
- toToken: params.to,
2376
+ fromToken: fromName,
2377
+ toToken: toName,
2373
2378
  fromAmount,
2374
2379
  toAmount,
2375
2380
  priceImpact: route.priceImpact,
@@ -2774,6 +2779,12 @@ var T2000 = class _T2000 extends EventEmitter {
2774
2779
  }
2775
2780
  return all;
2776
2781
  }
2782
+ _resolveTokenName(coinType, fallback) {
2783
+ const entry = Object.entries(SUPPORTED_ASSETS).find(([, v]) => v.type === coinType);
2784
+ if (entry) return entry[0];
2785
+ const suffix = coinType.split("::").pop();
2786
+ return suffix && suffix !== coinType ? suffix : fallback;
2787
+ }
2777
2788
  _mergeCoinsInTx(tx, coins) {
2778
2789
  if (coins.length === 0) throw new T2000Error("INSUFFICIENT_BALANCE", "No coins to merge");
2779
2790
  const primary = tx.object(coins[0].coinObjectId);