@t2000/sdk 5.3.0 → 5.5.0

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
@@ -139,6 +139,7 @@ __export(token_registry_exports, {
139
139
  getCoinMeta: () => getCoinMeta,
140
140
  getDecimalsForCoinType: () => getDecimalsForCoinType,
141
141
  isInRegistry: () => isInRegistry,
142
+ resolveCoinDecimals: () => resolveCoinDecimals,
142
143
  resolveSymbol: () => resolveSymbol,
143
144
  resolveTokenType: () => resolveTokenType
144
145
  });
@@ -160,6 +161,16 @@ function getDecimalsForCoinType(coinType) {
160
161
  }
161
162
  return 9;
162
163
  }
164
+ async function resolveCoinDecimals(client, coinType) {
165
+ if (isInRegistry(coinType)) return getDecimalsForCoinType(coinType);
166
+ try {
167
+ const res = await client.core.getCoinMetadata({ coinType });
168
+ const d = res?.metadata?.decimals ?? res?.decimals;
169
+ if (typeof d === "number" && Number.isFinite(d)) return d;
170
+ } catch {
171
+ }
172
+ return getDecimalsForCoinType(coinType);
173
+ }
163
174
  function resolveSymbol(coinType) {
164
175
  const direct = BY_TYPE.get(coinType);
165
176
  if (direct) return direct.symbol;
@@ -654,7 +665,7 @@ async function getSwapQuote(params) {
654
665
  );
655
666
  }
656
667
  const byAmountIn = params.byAmountIn ?? true;
657
- const fromDecimals = getDecimalsForCoinType(fromType);
668
+ const fromDecimals = params.fromDecimals ?? getDecimalsForCoinType(fromType);
658
669
  const rawAmount = BigInt(Math.floor(params.amount * 10 ** fromDecimals));
659
670
  const route = await findSwapRoute2({
660
671
  walletAddress: params.walletAddress,
@@ -668,7 +679,7 @@ async function getSwapQuote(params) {
668
679
  if (route.insufficientLiquidity) {
669
680
  throw new exports.T2000Error("SWAP_FAILED", `Insufficient liquidity for ${params.from} -> ${params.to}.`);
670
681
  }
671
- const toDecimals = getDecimalsForCoinType(toType);
682
+ const toDecimals = params.toDecimals ?? getDecimalsForCoinType(toType);
672
683
  const fromAmount = Number(route.amountIn) / 10 ** fromDecimals;
673
684
  const toAmount = Number(route.amountOut) / 10 ** toDecimals;
674
685
  const routeDesc = route.routerData.paths?.map((p) => p.provider).filter(Boolean).slice(0, 3).join(" + ") ?? "Cetus Aggregator";
@@ -1284,8 +1295,7 @@ function addSendToTx(tx, coin, recipient) {
1284
1295
  const validRecipient = validateAddress(recipient);
1285
1296
  tx.transferObjects([coin], validRecipient);
1286
1297
  }
1287
-
1288
- // src/wallet/balance.ts
1298
+ init_token_registry();
1289
1299
  var SUI_PRICE_FALLBACK = 1;
1290
1300
  var _cachedSuiPrice = 0;
1291
1301
  var _priceLastFetched = 0;
@@ -1318,30 +1328,67 @@ async function fetchSuiPrice(client) {
1318
1328
  }
1319
1329
  return _cachedSuiPrice || SUI_PRICE_FALLBACK;
1320
1330
  }
1331
+ function safeNorm(coinType) {
1332
+ try {
1333
+ return utils.normalizeStructTag(coinType);
1334
+ } catch {
1335
+ return coinType;
1336
+ }
1337
+ }
1338
+ var SUI_TYPE_NORM = safeNorm(SUPPORTED_ASSETS.SUI.type);
1339
+ var STABLE_BY_NORM = (() => {
1340
+ const m = {};
1341
+ for (const asset of STABLE_ASSETS) m[safeNorm(SUPPORTED_ASSETS[asset].type)] = asset;
1342
+ return m;
1343
+ })();
1321
1344
  async function queryBalance(client, address) {
1322
- const stableBalancePromises = STABLE_ASSETS.map(
1323
- (asset) => client.core.getBalance({ owner: address, coinType: SUPPORTED_ASSETS[asset].type }).then((b) => ({ asset, amount: Number(b.balance.balance) / 10 ** SUPPORTED_ASSETS[asset].decimals })).catch(() => ({ asset, amount: 0 }))
1324
- );
1325
- const [suiBalance, suiPriceUsd, ...stableResults] = await Promise.all([
1326
- client.core.getBalance({ owner: address, coinType: SUPPORTED_ASSETS.SUI.type }),
1327
- fetchSuiPrice(client),
1328
- ...stableBalancePromises
1329
- ]);
1345
+ const held = [];
1346
+ let cursor;
1347
+ do {
1348
+ const page = await client.core.listBalances({ owner: address, cursor: cursor ?? void 0 });
1349
+ for (const b of page.balances) {
1350
+ const raw = BigInt(b.balance);
1351
+ if (raw > 0n) held.push({ coinType: b.coinType, raw });
1352
+ }
1353
+ cursor = page.hasNextPage ? page.cursor : null;
1354
+ } while (cursor);
1355
+ const suiPriceUsd = await fetchSuiPrice(client);
1330
1356
  const stables = {};
1357
+ for (const asset of STABLE_ASSETS) stables[asset] = 0;
1331
1358
  let totalStables = 0;
1332
- for (const { asset, amount } of stableResults) {
1333
- stables[asset] = amount;
1334
- totalStables += amount;
1359
+ let suiAmount = 0;
1360
+ const otherCoins = [];
1361
+ for (const { coinType, raw } of held) {
1362
+ const norm = safeNorm(coinType);
1363
+ if (norm === SUI_TYPE_NORM) {
1364
+ suiAmount = Number(raw) / Number(MIST_PER_SUI);
1365
+ } else if (STABLE_BY_NORM[norm]) {
1366
+ const asset = STABLE_BY_NORM[norm];
1367
+ const amount = Number(raw) / 10 ** SUPPORTED_ASSETS[asset].decimals;
1368
+ stables[asset] = amount;
1369
+ totalStables += amount;
1370
+ } else {
1371
+ otherCoins.push({ coinType, raw });
1372
+ }
1335
1373
  }
1336
- const suiAmount = Number(suiBalance.balance.balance) / Number(MIST_PER_SUI);
1374
+ const tokens = await Promise.all(
1375
+ otherCoins.map(async ({ coinType, raw }) => {
1376
+ const decimals = await resolveCoinDecimals(client, coinType);
1377
+ return {
1378
+ coinType,
1379
+ symbol: resolveSymbol(coinType),
1380
+ amount: Number(raw) / 10 ** decimals,
1381
+ usdValue: null
1382
+ };
1383
+ })
1384
+ );
1385
+ tokens.sort((a, b) => a.symbol.localeCompare(b.symbol));
1337
1386
  const suiUsdValue = suiAmount * suiPriceUsd;
1338
1387
  return {
1339
1388
  stables,
1340
1389
  available: totalStables,
1341
- sui: {
1342
- amount: suiAmount,
1343
- usdValue: suiUsdValue
1344
- },
1390
+ sui: { amount: suiAmount, usdValue: suiUsdValue },
1391
+ tokens,
1345
1392
  totalUsd: totalStables + suiUsdValue
1346
1393
  };
1347
1394
  }
@@ -2045,7 +2092,7 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
2045
2092
  if (!toType) throw new exports.T2000Error("ASSET_NOT_SUPPORTED", `Unknown token: ${params.to}. Provide the full coin type.`);
2046
2093
  const byAmountIn = params.byAmountIn ?? true;
2047
2094
  const slippage = Math.min(params.slippage ?? 0.01, 0.05);
2048
- const fromDecimals = getDecimalsForCoinType(fromType);
2095
+ const fromDecimals = await resolveCoinDecimals(this.client, fromType);
2049
2096
  const rawAmount = BigInt(Math.floor(params.amount * 10 ** fromDecimals));
2050
2097
  const route = await findSwapRoute2({
2051
2098
  walletAddress: this._address,
@@ -2059,7 +2106,7 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
2059
2106
  if (route.priceImpact > 0.05) {
2060
2107
  console.warn(`[swap] High price impact: ${(route.priceImpact * 100).toFixed(2)}%`);
2061
2108
  }
2062
- const toDecimals = getDecimalsForCoinType(toType);
2109
+ const toDecimals = await resolveCoinDecimals(this.client, toType);
2063
2110
  let preBalRaw = 0n;
2064
2111
  try {
2065
2112
  const preBal = await this.client.core.getBalance({ owner: this._address, coinType: toType });
@@ -2152,13 +2199,18 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
2152
2199
  */
2153
2200
  async swapQuote(params) {
2154
2201
  const { getSwapQuote: getSwapQuote2 } = await Promise.resolve().then(() => (init_swap_quote(), swap_quote_exports));
2202
+ const { resolveTokenType: resolveTokenType2 } = await Promise.resolve().then(() => (init_cetus_swap(), cetus_swap_exports));
2203
+ const fromType = resolveTokenType2(params.from);
2204
+ const toType = resolveTokenType2(params.to);
2155
2205
  return getSwapQuote2({
2156
2206
  walletAddress: this._address,
2157
2207
  from: params.from,
2158
2208
  to: params.to,
2159
2209
  amount: params.amount,
2160
2210
  byAmountIn: params.byAmountIn,
2161
- providers: params.providers
2211
+ providers: params.providers,
2212
+ fromDecimals: fromType ? await resolveCoinDecimals(this.client, fromType) : void 0,
2213
+ toDecimals: toType ? await resolveCoinDecimals(this.client, toType) : void 0
2162
2214
  });
2163
2215
  }
2164
2216
  // -- Wallet --
@@ -2294,8 +2346,8 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
2294
2346
  /**
2295
2347
  * [SPEC_AGENTIC_STACK P1 / SDK F2 — 2026-05-25; refreshed S.342 / 2026-05-26]
2296
2348
  * Preferred alias of `deposit()`. Was introduced to mirror the v3 `t2000 fund`
2297
- * CLI command; the v4 CLI surface is `t2 receive` (deleted `fund` in the
2298
- * S.332 bulk cut). `deposit()` stays as the canonical method name for
2349
+ * CLI command; the v4 CLI surface is `t2 fund` (renamed back from the
2350
+ * interim `t2 receive` in S.464). `deposit()` stays as the canonical method name for
2299
2351
  * back-compat; `fund()` stays as a programmatic alias for audric + other
2300
2352
  * SDK consumers that prefer the verb.
2301
2353
  */