@t2000/sdk 0.8.5 → 0.8.7

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.d.cts CHANGED
@@ -237,8 +237,8 @@ declare function getDecimals(asset: SupportedAsset): number;
237
237
  declare function formatUsd(amount: number): string;
238
238
  declare function formatSui(amount: number): string;
239
239
  /**
240
- * Case-insensitive lookup against SUPPORTED_ASSETS keys.
241
- * 'usde' → 'USDe', 'usdsui' → 'USDsui', 'usdc' → 'USDC'.
240
+ * Case-insensitive lookup against SUPPORTED_ASSETS keys AND display names.
241
+ * 'usde' → 'USDe', 'suiusde' → 'USDe', 'suiusdt' → 'USDT', 'usdsui' → 'USDsui'.
242
242
  * Returns the original input if not found so downstream validation can reject it.
243
243
  */
244
244
  declare function normalizeAsset(input: string): string;
@@ -282,7 +282,7 @@ declare function simulateTransaction(client: SuiJsonRpcClient, tx: Transaction,
282
282
  declare function throwIfSimulationFailed(sim: SimulationResult): void;
283
283
 
284
284
  declare function getPoolPrice(client: SuiJsonRpcClient): Promise<number>;
285
- declare function getSwapQuote(client: SuiJsonRpcClient, fromAsset: 'USDC' | 'SUI', toAsset: 'USDC' | 'SUI', amount: number): Promise<{
285
+ declare function getSwapQuote(client: SuiJsonRpcClient, fromAsset: string, toAsset: string, amount: number): Promise<{
286
286
  expectedOutput: number;
287
287
  priceImpact: number;
288
288
  poolPrice: number;
package/dist/index.d.ts CHANGED
@@ -237,8 +237,8 @@ declare function getDecimals(asset: SupportedAsset): number;
237
237
  declare function formatUsd(amount: number): string;
238
238
  declare function formatSui(amount: number): string;
239
239
  /**
240
- * Case-insensitive lookup against SUPPORTED_ASSETS keys.
241
- * 'usde' → 'USDe', 'usdsui' → 'USDsui', 'usdc' → 'USDC'.
240
+ * Case-insensitive lookup against SUPPORTED_ASSETS keys AND display names.
241
+ * 'usde' → 'USDe', 'suiusde' → 'USDe', 'suiusdt' → 'USDT', 'usdsui' → 'USDsui'.
242
242
  * Returns the original input if not found so downstream validation can reject it.
243
243
  */
244
244
  declare function normalizeAsset(input: string): string;
@@ -282,7 +282,7 @@ declare function simulateTransaction(client: SuiJsonRpcClient, tx: Transaction,
282
282
  declare function throwIfSimulationFailed(sim: SimulationResult): void;
283
283
 
284
284
  declare function getPoolPrice(client: SuiJsonRpcClient): Promise<number>;
285
- declare function getSwapQuote(client: SuiJsonRpcClient, fromAsset: 'USDC' | 'SUI', toAsset: 'USDC' | 'SUI', amount: number): Promise<{
285
+ declare function getSwapQuote(client: SuiJsonRpcClient, fromAsset: string, toAsset: string, amount: number): Promise<{
286
286
  expectedOutput: number;
287
287
  priceImpact: number;
288
288
  poolPrice: number;
package/dist/index.js CHANGED
@@ -122,10 +122,25 @@ function mapMoveAbortCode(code) {
122
122
  7: "Package version mismatch \u2014 upgrade required",
123
123
  8: "Timelock is active \u2014 wait for expiry",
124
124
  9: "No pending change to execute",
125
- 10: "Already at current version"
125
+ 10: "Already at current version",
126
+ // NAVI Protocol abort codes
127
+ 1502: "Oracle price is stale \u2014 try again in a moment",
128
+ 1600: "Health factor too low \u2014 withdrawal would risk liquidation",
129
+ 1605: "Asset borrowing is disabled or at capacity on this protocol"
126
130
  };
127
131
  return abortMessages[code] ?? `Move abort code: ${code}`;
128
132
  }
133
+ function isMoveAbort(msg) {
134
+ return msg.includes("MoveAbort") || msg.includes("MovePrimitiveRuntimeError");
135
+ }
136
+ function parseMoveAbortMessage(msg) {
137
+ const abortMatch = msg.match(/abort code:\s*(\d+)/i) ?? msg.match(/MoveAbort[^,]*,\s*(\d+)/);
138
+ if (abortMatch) {
139
+ const code = parseInt(abortMatch[1], 10);
140
+ return mapMoveAbortCode(code);
141
+ }
142
+ return msg;
143
+ }
129
144
 
130
145
  // src/utils/sui.ts
131
146
  var cachedClient = null;
@@ -276,9 +291,13 @@ function formatSui(amount) {
276
291
  if (amount < 1e-3) return `${amount.toFixed(6)} SUI`;
277
292
  return `${amount.toFixed(3)} SUI`;
278
293
  }
279
- var ASSET_LOOKUP = new Map(
280
- Object.keys(SUPPORTED_ASSETS).map((k) => [k.toUpperCase(), k])
281
- );
294
+ var ASSET_LOOKUP = /* @__PURE__ */ new Map();
295
+ for (const [key, info] of Object.entries(SUPPORTED_ASSETS)) {
296
+ ASSET_LOOKUP.set(key.toUpperCase(), key);
297
+ if (info.displayName && info.displayName.toUpperCase() !== key.toUpperCase()) {
298
+ ASSET_LOOKUP.set(info.displayName.toUpperCase(), key);
299
+ }
300
+ }
282
301
  function normalizeAsset(input) {
283
302
  return ASSET_LOOKUP.get(input.toUpperCase()) ?? input;
284
303
  }
@@ -1403,6 +1422,9 @@ async function buildSwapTx(params) {
1403
1422
  const { client, address, fromAsset, toAsset, amount, maxSlippageBps = DEFAULT_SLIPPAGE_BPS } = params;
1404
1423
  const fromInfo = SUPPORTED_ASSETS[fromAsset];
1405
1424
  const toInfo = SUPPORTED_ASSETS[toAsset];
1425
+ if (!fromInfo || !toInfo) {
1426
+ throw new T2000Error("ASSET_NOT_SUPPORTED", `Swap pair ${fromAsset}/${toAsset} is not supported`);
1427
+ }
1406
1428
  const rawAmount = BigInt(Math.floor(amount * 10 ** fromInfo.decimals));
1407
1429
  const aggClient = createAggregatorClient(client, address);
1408
1430
  const result = await aggClient.findRouters({
@@ -1455,6 +1477,9 @@ async function getPoolPrice(client) {
1455
1477
  async function getSwapQuote(client, fromAsset, toAsset, amount) {
1456
1478
  const fromInfo = SUPPORTED_ASSETS[fromAsset];
1457
1479
  const toInfo = SUPPORTED_ASSETS[toAsset];
1480
+ if (!fromInfo || !toInfo) {
1481
+ throw new T2000Error("ASSET_NOT_SUPPORTED", `Swap pair ${fromAsset}/${toAsset} is not supported`);
1482
+ }
1458
1483
  const rawAmount = BigInt(Math.floor(amount * 10 ** fromInfo.decimals));
1459
1484
  const poolPrice = await getPoolPrice(client);
1460
1485
  try {
@@ -1506,19 +1531,14 @@ var CetusAdapter = class {
1506
1531
  this.client = client;
1507
1532
  }
1508
1533
  async getQuote(from, to, amount) {
1509
- return getSwapQuote(
1510
- this.client,
1511
- from.toUpperCase(),
1512
- to.toUpperCase(),
1513
- amount
1514
- );
1534
+ return getSwapQuote(this.client, from, to, amount);
1515
1535
  }
1516
1536
  async buildSwapTx(address, from, to, amount, maxSlippageBps) {
1517
1537
  const result = await buildSwapTx({
1518
1538
  client: this.client,
1519
1539
  address,
1520
- fromAsset: from.toUpperCase(),
1521
- toAsset: to.toUpperCase(),
1540
+ fromAsset: from,
1541
+ toAsset: to,
1522
1542
  amount,
1523
1543
  maxSlippageBps
1524
1544
  });
@@ -2230,7 +2250,11 @@ async function executeWithGas(client, keypair, buildTx) {
2230
2250
  if (result) return result;
2231
2251
  errors.push("self-funded: SUI below threshold");
2232
2252
  } catch (err) {
2233
- errors.push(`self-funded: ${err instanceof Error ? err.message : String(err)}`);
2253
+ const msg = err instanceof Error ? err.message : String(err);
2254
+ if (isMoveAbort(msg)) {
2255
+ throw new T2000Error("TRANSACTION_FAILED", parseMoveAbortMessage(msg));
2256
+ }
2257
+ errors.push(`self-funded: ${msg}`);
2234
2258
  }
2235
2259
  try {
2236
2260
  const tx = await buildTx();