@t2000/sdk 0.21.9 → 0.21.11

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.js CHANGED
@@ -2325,14 +2325,6 @@ var T2000 = class _T2000 extends EventEmitter {
2325
2325
  if (route.priceImpact > 0.05) {
2326
2326
  console.warn(`[swap] High price impact: ${(route.priceImpact * 100).toFixed(2)}%`);
2327
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
- }
2336
2328
  const gasResult = await executeWithGas(this.client, this._signer, async () => {
2337
2329
  const tx = new Transaction();
2338
2330
  tx.setSender(this._address);
@@ -2355,17 +2347,35 @@ var T2000 = class _T2000 extends EventEmitter {
2355
2347
  tx.transferObjects([outputCoin], this._address);
2356
2348
  return tx;
2357
2349
  });
2350
+ const toEntry = Object.entries(SUPPORTED_ASSETS).find(([, v]) => v.type === toType);
2351
+ const toDecimals = toEntry ? toEntry[1].decimals : toType === "0x2::sui::SUI" ? 9 : 6;
2358
2352
  const fromAmount = Number(route.amountIn) / 10 ** fromDecimals;
2359
2353
  let toAmount = Number(route.amountOut) / 10 ** toDecimals;
2360
- try {
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;
2354
+ const toTypeSuffix = toType.split("::").slice(1).join("::");
2355
+ for (let attempt = 0; attempt < 4; attempt++) {
2356
+ try {
2357
+ const txBlock = await this.client.getTransactionBlock({
2358
+ digest: gasResult.digest,
2359
+ options: { showBalanceChanges: true }
2360
+ });
2361
+ const changes = txBlock.balanceChanges ?? [];
2362
+ const received = changes.find((c) => {
2363
+ if (BigInt(c.amount) <= 0n) return false;
2364
+ const ownerAddr = c.owner?.AddressOwner;
2365
+ if (!ownerAddr || ownerAddr.toLowerCase() !== this._address.toLowerCase()) return false;
2366
+ if (c.coinType === toType) return true;
2367
+ return c.coinType.endsWith(toTypeSuffix);
2368
+ });
2369
+ if (received) {
2370
+ const actual = Number(BigInt(received.amount)) / 10 ** toDecimals;
2371
+ if (actual > 0) {
2372
+ toAmount = actual;
2373
+ break;
2374
+ }
2375
+ }
2376
+ } catch {
2366
2377
  }
2367
- } catch {
2368
- console.warn("[swap] Could not read post-swap balance, using route estimate");
2378
+ if (attempt < 3) await new Promise((r) => setTimeout(r, 600));
2369
2379
  }
2370
2380
  const fromName = fromEntry ? fromEntry[0] : this._resolveTokenName(fromType, params.from);
2371
2381
  const toName = toEntry ? toEntry[0] : this._resolveTokenName(toType, params.to);