@t2000/sdk 0.21.11 → 0.21.13
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 +50 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +50 -23
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -147,7 +147,7 @@ interface TxMetadata {
|
|
|
147
147
|
operation: 'send' | 'save' | 'withdraw' | 'borrow' | 'repay' | 'pay';
|
|
148
148
|
amount?: number;
|
|
149
149
|
}
|
|
150
|
-
declare const OUTBOUND_OPS: Set<"save" | "
|
|
150
|
+
declare const OUTBOUND_OPS: Set<"save" | "withdraw" | "borrow" | "repay" | "send" | "pay">;
|
|
151
151
|
declare const DEFAULT_SAFEGUARD_CONFIG: SafeguardConfig;
|
|
152
152
|
|
|
153
153
|
declare class SafeguardEnforcer {
|
package/dist/index.d.ts
CHANGED
|
@@ -147,7 +147,7 @@ interface TxMetadata {
|
|
|
147
147
|
operation: 'send' | 'save' | 'withdraw' | 'borrow' | 'repay' | 'pay';
|
|
148
148
|
amount?: number;
|
|
149
149
|
}
|
|
150
|
-
declare const OUTBOUND_OPS: Set<"save" | "
|
|
150
|
+
declare const OUTBOUND_OPS: Set<"save" | "withdraw" | "borrow" | "repay" | "send" | "pay">;
|
|
151
151
|
declare const DEFAULT_SAFEGUARD_CONFIG: SafeguardConfig;
|
|
152
152
|
|
|
153
153
|
declare class SafeguardEnforcer {
|
package/dist/index.js
CHANGED
|
@@ -2325,6 +2325,14 @@ 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 preBalRaw = 0n;
|
|
2331
|
+
try {
|
|
2332
|
+
const preBal = await this.client.getBalance({ owner: this._address, coinType: toType });
|
|
2333
|
+
preBalRaw = BigInt(preBal.totalBalance);
|
|
2334
|
+
} catch {
|
|
2335
|
+
}
|
|
2328
2336
|
const gasResult = await executeWithGas(this.client, this._signer, async () => {
|
|
2329
2337
|
const tx = new Transaction();
|
|
2330
2338
|
tx.setSender(this._address);
|
|
@@ -2347,35 +2355,54 @@ var T2000 = class _T2000 extends EventEmitter {
|
|
|
2347
2355
|
tx.transferObjects([outputCoin], this._address);
|
|
2348
2356
|
return tx;
|
|
2349
2357
|
});
|
|
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;
|
|
2352
2358
|
const fromAmount = Number(route.amountIn) / 10 ** fromDecimals;
|
|
2353
2359
|
let toAmount = Number(route.amountOut) / 10 ** toDecimals;
|
|
2354
2360
|
const toTypeSuffix = toType.split("::").slice(1).join("::");
|
|
2355
|
-
|
|
2361
|
+
try {
|
|
2362
|
+
const fullTx = await this.client.waitForTransaction({
|
|
2363
|
+
digest: gasResult.digest,
|
|
2364
|
+
options: { showBalanceChanges: true },
|
|
2365
|
+
timeout: 8e3,
|
|
2366
|
+
pollInterval: 400
|
|
2367
|
+
});
|
|
2368
|
+
const changes = fullTx.balanceChanges ?? [];
|
|
2369
|
+
console.error(`[swap] balanceChanges count=${changes.length}, toType=${toType}, suffix=${toTypeSuffix}`);
|
|
2370
|
+
for (const c of changes) {
|
|
2371
|
+
console.error(`[swap] coinType=${c.coinType} amount=${c.amount} owner=${JSON.stringify(c.owner)}`);
|
|
2372
|
+
}
|
|
2373
|
+
const received = changes.find((c) => {
|
|
2374
|
+
if (BigInt(c.amount) <= 0n) return false;
|
|
2375
|
+
const ownerAddr = c.owner?.AddressOwner;
|
|
2376
|
+
if (!ownerAddr || ownerAddr.toLowerCase() !== this._address.toLowerCase()) return false;
|
|
2377
|
+
if (c.coinType === toType) return true;
|
|
2378
|
+
return c.coinType.endsWith(toTypeSuffix);
|
|
2379
|
+
});
|
|
2380
|
+
if (received) {
|
|
2381
|
+
const actual = Number(BigInt(received.amount)) / 10 ** toDecimals;
|
|
2382
|
+
if (actual > 0) toAmount = actual;
|
|
2383
|
+
console.error(`[swap] Primary: toAmount=${toAmount}`);
|
|
2384
|
+
} else {
|
|
2385
|
+
console.error(`[swap] Primary: no matching balance change found`);
|
|
2386
|
+
}
|
|
2387
|
+
} catch (err) {
|
|
2388
|
+
console.error(`[swap] Primary failed:`, err);
|
|
2389
|
+
}
|
|
2390
|
+
const cetusEstimate = Number(route.amountOut) / 10 ** toDecimals;
|
|
2391
|
+
if (Math.abs(toAmount - cetusEstimate) < 1e-3) {
|
|
2392
|
+
console.error(`[swap] toAmount still equals Cetus estimate (${cetusEstimate}), trying balance diff`);
|
|
2356
2393
|
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
|
-
}
|
|
2394
|
+
await new Promise((r) => setTimeout(r, 2e3));
|
|
2395
|
+
const postBal = await this.client.getBalance({ owner: this._address, coinType: toType });
|
|
2396
|
+
const postRaw = BigInt(postBal.totalBalance);
|
|
2397
|
+
const delta = Number(postRaw - preBalRaw) / 10 ** toDecimals;
|
|
2398
|
+
console.error(`[swap] Fallback: pre=${preBalRaw} post=${postRaw} delta=${delta}`);
|
|
2399
|
+
if (delta > 0) {
|
|
2400
|
+
toAmount = delta;
|
|
2401
|
+
console.error(`[swap] Fallback: using balance diff: ${toAmount}`);
|
|
2375
2402
|
}
|
|
2376
|
-
} catch {
|
|
2403
|
+
} catch (err) {
|
|
2404
|
+
console.error(`[swap] Fallback failed:`, err);
|
|
2377
2405
|
}
|
|
2378
|
-
if (attempt < 3) await new Promise((r) => setTimeout(r, 600));
|
|
2379
2406
|
}
|
|
2380
2407
|
const fromName = fromEntry ? fromEntry[0] : this._resolveTokenName(fromType, params.from);
|
|
2381
2408
|
const toName = toEntry ? toEntry[0] : this._resolveTokenName(toType, params.to);
|