@t2000/sdk 0.8.4 → 0.8.6

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 CHANGED
@@ -278,9 +278,13 @@ function formatSui(amount) {
278
278
  if (amount < 1e-3) return `${amount.toFixed(6)} SUI`;
279
279
  return `${amount.toFixed(3)} SUI`;
280
280
  }
281
- var ASSET_LOOKUP = new Map(
282
- Object.keys(SUPPORTED_ASSETS).map((k) => [k.toUpperCase(), k])
283
- );
281
+ var ASSET_LOOKUP = /* @__PURE__ */ new Map();
282
+ for (const [key, info] of Object.entries(SUPPORTED_ASSETS)) {
283
+ ASSET_LOOKUP.set(key.toUpperCase(), key);
284
+ if (info.displayName && info.displayName.toUpperCase() !== key.toUpperCase()) {
285
+ ASSET_LOOKUP.set(info.displayName.toUpperCase(), key);
286
+ }
287
+ }
284
288
  function normalizeAsset(input) {
285
289
  return ASSET_LOOKUP.get(input.toUpperCase()) ?? input;
286
290
  }
@@ -1405,6 +1409,9 @@ async function buildSwapTx(params) {
1405
1409
  const { client, address, fromAsset, toAsset, amount, maxSlippageBps = DEFAULT_SLIPPAGE_BPS } = params;
1406
1410
  const fromInfo = SUPPORTED_ASSETS[fromAsset];
1407
1411
  const toInfo = SUPPORTED_ASSETS[toAsset];
1412
+ if (!fromInfo || !toInfo) {
1413
+ throw new T2000Error("ASSET_NOT_SUPPORTED", `Swap pair ${fromAsset}/${toAsset} is not supported`);
1414
+ }
1408
1415
  const rawAmount = BigInt(Math.floor(amount * 10 ** fromInfo.decimals));
1409
1416
  const aggClient = createAggregatorClient(client, address);
1410
1417
  const result = await aggClient.findRouters({
@@ -1457,6 +1464,9 @@ async function getPoolPrice(client) {
1457
1464
  async function getSwapQuote(client, fromAsset, toAsset, amount) {
1458
1465
  const fromInfo = SUPPORTED_ASSETS[fromAsset];
1459
1466
  const toInfo = SUPPORTED_ASSETS[toAsset];
1467
+ if (!fromInfo || !toInfo) {
1468
+ throw new T2000Error("ASSET_NOT_SUPPORTED", `Swap pair ${fromAsset}/${toAsset} is not supported`);
1469
+ }
1460
1470
  const rawAmount = BigInt(Math.floor(amount * 10 ** fromInfo.decimals));
1461
1471
  const poolPrice = await getPoolPrice(client);
1462
1472
  try {
@@ -1508,19 +1518,14 @@ var CetusAdapter = class {
1508
1518
  this.client = client;
1509
1519
  }
1510
1520
  async getQuote(from, to, amount) {
1511
- return getSwapQuote(
1512
- this.client,
1513
- from.toUpperCase(),
1514
- to.toUpperCase(),
1515
- amount
1516
- );
1521
+ return getSwapQuote(this.client, from, to, amount);
1517
1522
  }
1518
1523
  async buildSwapTx(address, from, to, amount, maxSlippageBps) {
1519
1524
  const result = await buildSwapTx({
1520
1525
  client: this.client,
1521
1526
  address,
1522
- fromAsset: from.toUpperCase(),
1523
- toAsset: to.toUpperCase(),
1527
+ fromAsset: from,
1528
+ toAsset: to,
1524
1529
  amount,
1525
1530
  maxSlippageBps
1526
1531
  });
@@ -2795,6 +2800,22 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
2795
2800
  const current = savePositions.reduce(
2796
2801
  (worst, p) => p.apy < worst.apy ? p : worst
2797
2802
  );
2803
+ const withdrawAdapter = this.registry.getLending(current.protocolId);
2804
+ if (withdrawAdapter) {
2805
+ try {
2806
+ const maxResult = await withdrawAdapter.maxWithdraw(this._address, current.asset);
2807
+ if (maxResult.maxAmount < current.amount) {
2808
+ current.amount = Math.max(0, maxResult.maxAmount - 0.01);
2809
+ }
2810
+ } catch {
2811
+ }
2812
+ }
2813
+ if (current.amount <= 0.01) {
2814
+ throw new T2000Error(
2815
+ "HEALTH_FACTOR_TOO_LOW",
2816
+ "Cannot rebalance \u2014 active borrows prevent safe withdrawal. Repay some debt first."
2817
+ );
2818
+ }
2798
2819
  const apyDiff = bestRate.rates.saveApy - current.apy;
2799
2820
  const isSameProtocol = current.protocolId === bestRate.protocolId;
2800
2821
  const isSameAsset = current.asset === bestRate.asset;
@@ -2906,7 +2927,6 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
2906
2927
  }
2907
2928
  const txDigests = [];
2908
2929
  let totalGasCost = 0;
2909
- const withdrawAdapter = this.registry.getLending(current.protocolId);
2910
2930
  if (!withdrawAdapter) throw new T2000Error("PROTOCOL_UNAVAILABLE", `Protocol ${current.protocolId} not found`);
2911
2931
  const withdrawResult = await executeWithGas(this.client, this.keypair, async () => {
2912
2932
  const built = await withdrawAdapter.buildWithdrawTx(this._address, current.amount, current.asset);