@t2000/sdk 0.21.10 → 0.21.12
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 +39 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +39 -8
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2351,21 +2351,52 @@ var T2000 = class _T2000 extends EventEmitter {
|
|
|
2351
2351
|
const toDecimals = toEntry ? toEntry[1].decimals : toType === "0x2::sui::SUI" ? 9 : 6;
|
|
2352
2352
|
const fromAmount = Number(route.amountIn) / 10 ** fromDecimals;
|
|
2353
2353
|
let toAmount = Number(route.amountOut) / 10 ** toDecimals;
|
|
2354
|
+
const toTypeSuffix = toType.split("::").slice(1).join("::");
|
|
2354
2355
|
try {
|
|
2355
|
-
const
|
|
2356
|
+
const fullTx = await this.client.waitForTransaction({
|
|
2356
2357
|
digest: gasResult.digest,
|
|
2357
|
-
options: { showBalanceChanges: true }
|
|
2358
|
+
options: { showBalanceChanges: true },
|
|
2359
|
+
timeout: 8e3,
|
|
2360
|
+
pollInterval: 400
|
|
2361
|
+
});
|
|
2362
|
+
const changes = fullTx.balanceChanges ?? [];
|
|
2363
|
+
console.error(`[swap] balanceChanges count=${changes.length}, toType=${toType}`);
|
|
2364
|
+
for (const c of changes) {
|
|
2365
|
+
console.error(`[swap] coinType=${c.coinType} amount=${c.amount} owner=${JSON.stringify(c.owner)}`);
|
|
2366
|
+
}
|
|
2367
|
+
const received = changes.find((c) => {
|
|
2368
|
+
if (BigInt(c.amount) <= 0n) return false;
|
|
2369
|
+
const ownerAddr = c.owner?.AddressOwner;
|
|
2370
|
+
if (!ownerAddr || ownerAddr.toLowerCase() !== this._address.toLowerCase()) return false;
|
|
2371
|
+
if (c.coinType === toType) return true;
|
|
2372
|
+
return c.coinType.endsWith(toTypeSuffix);
|
|
2358
2373
|
});
|
|
2359
|
-
const changes = txBlock.balanceChanges ?? [];
|
|
2360
|
-
const received = changes.find(
|
|
2361
|
-
(c) => c.coinType === toType && BigInt(c.amount) > 0n && c.owner.AddressOwner === this._address
|
|
2362
|
-
);
|
|
2363
2374
|
if (received) {
|
|
2364
2375
|
const actual = Number(BigInt(received.amount)) / 10 ** toDecimals;
|
|
2365
2376
|
if (actual > 0) toAmount = actual;
|
|
2377
|
+
console.error(`[swap] Approach 1 success: toAmount=${toAmount}`);
|
|
2378
|
+
} else {
|
|
2379
|
+
console.error(`[swap] Approach 1: no matching balance change found`);
|
|
2380
|
+
}
|
|
2381
|
+
} catch (err) {
|
|
2382
|
+
console.error(`[swap] Approach 1 failed:`, err);
|
|
2383
|
+
}
|
|
2384
|
+
const cetusEstimate = Number(route.amountOut) / 10 ** toDecimals;
|
|
2385
|
+
if (Math.abs(toAmount - cetusEstimate) < 1e-3) {
|
|
2386
|
+
console.error(`[swap] toAmount still equals Cetus estimate (${cetusEstimate}), trying balance diff`);
|
|
2387
|
+
try {
|
|
2388
|
+
await new Promise((r) => setTimeout(r, 2e3));
|
|
2389
|
+
const postBal = await this.client.getBalance({ owner: this._address, coinType: toType });
|
|
2390
|
+
const postRaw = BigInt(postBal.totalBalance);
|
|
2391
|
+
const human = Number(postRaw) / 10 ** toDecimals;
|
|
2392
|
+
console.error(`[swap] Approach 2: postBalance raw=${postRaw} human=${human}`);
|
|
2393
|
+
if (human > toAmount * 10) {
|
|
2394
|
+
toAmount = human;
|
|
2395
|
+
console.error(`[swap] Approach 2: using total balance as estimate: ${toAmount}`);
|
|
2396
|
+
}
|
|
2397
|
+
} catch (err) {
|
|
2398
|
+
console.error(`[swap] Approach 2 failed:`, err);
|
|
2366
2399
|
}
|
|
2367
|
-
} catch {
|
|
2368
|
-
console.warn("[swap] Could not read balance changes, using route estimate");
|
|
2369
2400
|
}
|
|
2370
2401
|
const fromName = fromEntry ? fromEntry[0] : this._resolveTokenName(fromType, params.from);
|
|
2371
2402
|
const toName = toEntry ? toEntry[0] : this._resolveTokenName(toType, params.to);
|