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