@t2000/sdk 0.21.11 → 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 +42 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +42 -21
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2352,30 +2352,51 @@ var T2000 = class _T2000 extends EventEmitter {
|
|
|
2352
2352
|
const fromAmount = Number(route.amountIn) / 10 ** fromDecimals;
|
|
2353
2353
|
let toAmount = Number(route.amountOut) / 10 ** toDecimals;
|
|
2354
2354
|
const toTypeSuffix = toType.split("::").slice(1).join("::");
|
|
2355
|
-
|
|
2355
|
+
try {
|
|
2356
|
+
const fullTx = await this.client.waitForTransaction({
|
|
2357
|
+
digest: gasResult.digest,
|
|
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);
|
|
2373
|
+
});
|
|
2374
|
+
if (received) {
|
|
2375
|
+
const actual = Number(BigInt(received.amount)) / 10 ** toDecimals;
|
|
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`);
|
|
2356
2387
|
try {
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
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
|
-
}
|
|
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}`);
|
|
2375
2396
|
}
|
|
2376
|
-
} catch {
|
|
2397
|
+
} catch (err) {
|
|
2398
|
+
console.error(`[swap] Approach 2 failed:`, err);
|
|
2377
2399
|
}
|
|
2378
|
-
if (attempt < 3) await new Promise((r) => setTimeout(r, 600));
|
|
2379
2400
|
}
|
|
2380
2401
|
const fromName = fromEntry ? fromEntry[0] : this._resolveTokenName(fromType, params.from);
|
|
2381
2402
|
const toName = toEntry ? toEntry[0] : this._resolveTokenName(toType, params.to);
|