@t2000/sdk 0.21.11 → 0.21.13

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
@@ -2327,6 +2327,14 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
2327
2327
  if (route.priceImpact > 0.05) {
2328
2328
  console.warn(`[swap] High price impact: ${(route.priceImpact * 100).toFixed(2)}%`);
2329
2329
  }
2330
+ const toEntry = Object.entries(SUPPORTED_ASSETS).find(([, v]) => v.type === toType);
2331
+ const toDecimals = toEntry ? toEntry[1].decimals : toType === "0x2::sui::SUI" ? 9 : 6;
2332
+ let preBalRaw = 0n;
2333
+ try {
2334
+ const preBal = await this.client.getBalance({ owner: this._address, coinType: toType });
2335
+ preBalRaw = BigInt(preBal.totalBalance);
2336
+ } catch {
2337
+ }
2330
2338
  const gasResult = await executeWithGas(this.client, this._signer, async () => {
2331
2339
  const tx = new transactions.Transaction();
2332
2340
  tx.setSender(this._address);
@@ -2349,35 +2357,54 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
2349
2357
  tx.transferObjects([outputCoin], this._address);
2350
2358
  return tx;
2351
2359
  });
2352
- const toEntry = Object.entries(SUPPORTED_ASSETS).find(([, v]) => v.type === toType);
2353
- const toDecimals = toEntry ? toEntry[1].decimals : toType === "0x2::sui::SUI" ? 9 : 6;
2354
2360
  const fromAmount = Number(route.amountIn) / 10 ** fromDecimals;
2355
2361
  let toAmount = Number(route.amountOut) / 10 ** toDecimals;
2356
2362
  const toTypeSuffix = toType.split("::").slice(1).join("::");
2357
- for (let attempt = 0; attempt < 4; attempt++) {
2363
+ try {
2364
+ const fullTx = await this.client.waitForTransaction({
2365
+ digest: gasResult.digest,
2366
+ options: { showBalanceChanges: true },
2367
+ timeout: 8e3,
2368
+ pollInterval: 400
2369
+ });
2370
+ const changes = fullTx.balanceChanges ?? [];
2371
+ console.error(`[swap] balanceChanges count=${changes.length}, toType=${toType}, suffix=${toTypeSuffix}`);
2372
+ for (const c of changes) {
2373
+ console.error(`[swap] coinType=${c.coinType} amount=${c.amount} owner=${JSON.stringify(c.owner)}`);
2374
+ }
2375
+ const received = changes.find((c) => {
2376
+ if (BigInt(c.amount) <= 0n) return false;
2377
+ const ownerAddr = c.owner?.AddressOwner;
2378
+ if (!ownerAddr || ownerAddr.toLowerCase() !== this._address.toLowerCase()) return false;
2379
+ if (c.coinType === toType) return true;
2380
+ return c.coinType.endsWith(toTypeSuffix);
2381
+ });
2382
+ if (received) {
2383
+ const actual = Number(BigInt(received.amount)) / 10 ** toDecimals;
2384
+ if (actual > 0) toAmount = actual;
2385
+ console.error(`[swap] Primary: toAmount=${toAmount}`);
2386
+ } else {
2387
+ console.error(`[swap] Primary: no matching balance change found`);
2388
+ }
2389
+ } catch (err) {
2390
+ console.error(`[swap] Primary failed:`, err);
2391
+ }
2392
+ const cetusEstimate = Number(route.amountOut) / 10 ** toDecimals;
2393
+ if (Math.abs(toAmount - cetusEstimate) < 1e-3) {
2394
+ console.error(`[swap] toAmount still equals Cetus estimate (${cetusEstimate}), trying balance diff`);
2358
2395
  try {
2359
- const txBlock = await this.client.getTransactionBlock({
2360
- digest: gasResult.digest,
2361
- options: { showBalanceChanges: true }
2362
- });
2363
- const changes = txBlock.balanceChanges ?? [];
2364
- const received = changes.find((c) => {
2365
- if (BigInt(c.amount) <= 0n) return false;
2366
- const ownerAddr = c.owner?.AddressOwner;
2367
- if (!ownerAddr || ownerAddr.toLowerCase() !== this._address.toLowerCase()) return false;
2368
- if (c.coinType === toType) return true;
2369
- return c.coinType.endsWith(toTypeSuffix);
2370
- });
2371
- if (received) {
2372
- const actual = Number(BigInt(received.amount)) / 10 ** toDecimals;
2373
- if (actual > 0) {
2374
- toAmount = actual;
2375
- break;
2376
- }
2396
+ await new Promise((r) => setTimeout(r, 2e3));
2397
+ const postBal = await this.client.getBalance({ owner: this._address, coinType: toType });
2398
+ const postRaw = BigInt(postBal.totalBalance);
2399
+ const delta = Number(postRaw - preBalRaw) / 10 ** toDecimals;
2400
+ console.error(`[swap] Fallback: pre=${preBalRaw} post=${postRaw} delta=${delta}`);
2401
+ if (delta > 0) {
2402
+ toAmount = delta;
2403
+ console.error(`[swap] Fallback: using balance diff: ${toAmount}`);
2377
2404
  }
2378
- } catch {
2405
+ } catch (err) {
2406
+ console.error(`[swap] Fallback failed:`, err);
2379
2407
  }
2380
- if (attempt < 3) await new Promise((r) => setTimeout(r, 600));
2381
2408
  }
2382
2409
  const fromName = fromEntry ? fromEntry[0] : this._resolveTokenName(fromType, params.from);
2383
2410
  const toName = toEntry ? toEntry[0] : this._resolveTokenName(toType, params.to);