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