@t2000/sdk 5.4.0 → 5.5.1

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
@@ -1295,8 +1295,7 @@ function addSendToTx(tx, coin, recipient) {
1295
1295
  const validRecipient = validateAddress(recipient);
1296
1296
  tx.transferObjects([coin], validRecipient);
1297
1297
  }
1298
-
1299
- // src/wallet/balance.ts
1298
+ init_token_registry();
1300
1299
  var SUI_PRICE_FALLBACK = 1;
1301
1300
  var _cachedSuiPrice = 0;
1302
1301
  var _priceLastFetched = 0;
@@ -1329,30 +1328,67 @@ async function fetchSuiPrice(client) {
1329
1328
  }
1330
1329
  return _cachedSuiPrice || SUI_PRICE_FALLBACK;
1331
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
+ })();
1332
1344
  async function queryBalance(client, address) {
1333
- const stableBalancePromises = STABLE_ASSETS.map(
1334
- (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 }))
1335
- );
1336
- const [suiBalance, suiPriceUsd, ...stableResults] = await Promise.all([
1337
- client.core.getBalance({ owner: address, coinType: SUPPORTED_ASSETS.SUI.type }),
1338
- fetchSuiPrice(client),
1339
- ...stableBalancePromises
1340
- ]);
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);
1341
1356
  const stables = {};
1357
+ for (const asset of STABLE_ASSETS) stables[asset] = 0;
1342
1358
  let totalStables = 0;
1343
- for (const { asset, amount } of stableResults) {
1344
- stables[asset] = amount;
1345
- 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
+ }
1346
1373
  }
1347
- 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));
1348
1386
  const suiUsdValue = suiAmount * suiPriceUsd;
1349
1387
  return {
1350
1388
  stables,
1351
1389
  available: totalStables,
1352
- sui: {
1353
- amount: suiAmount,
1354
- usdValue: suiUsdValue
1355
- },
1390
+ sui: { amount: suiAmount, usdValue: suiUsdValue },
1391
+ tokens,
1356
1392
  totalUsd: totalStables + suiUsdValue
1357
1393
  };
1358
1394
  }
@@ -1522,7 +1558,7 @@ var TX_NODE_FRAGMENT = `
1522
1558
  }
1523
1559
  `;
1524
1560
  var HISTORY_QUERY = `query History($address: SuiAddress!, $last: Int!) {
1525
- transactions(last: $last, filter: { sentAddress: $address }) {
1561
+ transactions(last: $last, filter: { affectedAddress: $address }) {
1526
1562
  nodes {${TX_NODE_FRAGMENT}}
1527
1563
  }
1528
1564
  }`;
@@ -1649,7 +1685,12 @@ function extractCommands(txBlock) {
1649
1685
  init_token_registry();
1650
1686
 
1651
1687
  // src/utils/suins.ts
1652
- var SUI_MAINNET_URL = "https://fullnode.mainnet.sui.io:443";
1688
+ var RESOLVE_NAME_QUERY = `query ResolveSuins($name: String!) {
1689
+ address(name: $name) { address }
1690
+ }`;
1691
+ var REVERSE_NAME_QUERY = `query ReverseSuins($address: SuiAddress!) {
1692
+ address(address: $address) { defaultNameRecord { domain } }
1693
+ }`;
1653
1694
  var SUI_ADDRESS_REGEX = /^0x[a-fA-F0-9]{1,64}$/i;
1654
1695
  var SUI_ADDRESS_STRICT_REGEX = /^0x[a-fA-F0-9]{64}$/i;
1655
1696
  var SUINS_NAME_REGEX = /^[a-z0-9-]+(\.[a-z0-9-]+)*\.sui$/;
@@ -1687,76 +1728,45 @@ async function resolveSuinsViaRpc(rawName, ctx = {}) {
1687
1728
  if (!SUINS_NAME_REGEX.test(name)) {
1688
1729
  throw new InvalidAddressError(rawName);
1689
1730
  }
1690
- const url = ctx.suiRpcUrl || SUI_MAINNET_URL;
1731
+ const gql = getSuiGraphQLClient();
1691
1732
  let res;
1692
1733
  try {
1693
- res = await fetch(url, {
1694
- method: "POST",
1695
- headers: { "Content-Type": "application/json" },
1696
- body: JSON.stringify({
1697
- jsonrpc: "2.0",
1698
- id: 1,
1699
- method: "suix_resolveNameServiceAddress",
1700
- params: [name]
1701
- }),
1702
- signal: ctx.signal ?? AbortSignal.timeout(8e3)
1734
+ res = await gql.query({
1735
+ query: RESOLVE_NAME_QUERY,
1736
+ variables: { name },
1737
+ signal: ctx.signal
1703
1738
  });
1704
1739
  } catch (err) {
1705
1740
  const msg = err instanceof Error ? err.message : String(err);
1706
1741
  throw new SuinsRpcError(name, msg);
1707
1742
  }
1708
- if (!res.ok) {
1709
- throw new SuinsRpcError(name, `HTTP ${res.status}`);
1710
- }
1711
- let body;
1712
- try {
1713
- body = await res.json();
1714
- } catch (err) {
1715
- const msg = err instanceof Error ? err.message : String(err);
1716
- throw new SuinsRpcError(name, `JSON parse failed: ${msg}`);
1717
- }
1718
- if (body.error) {
1719
- throw new SuinsRpcError(name, body.error.message);
1743
+ if (res.errors?.length) {
1744
+ throw new SuinsRpcError(name, res.errors.map((e) => e.message ?? "unknown error").join("; "));
1720
1745
  }
1721
- return body.result ?? null;
1746
+ return res.data?.address?.address ?? null;
1722
1747
  }
1723
1748
  async function resolveAddressToSuinsViaRpc(rawAddress, ctx = {}) {
1724
1749
  const address = rawAddress.trim().toLowerCase();
1725
1750
  if (!SUI_ADDRESS_REGEX.test(address)) {
1726
1751
  throw new InvalidAddressError(rawAddress);
1727
1752
  }
1728
- const url = ctx.suiRpcUrl || SUI_MAINNET_URL;
1753
+ const gql = getSuiGraphQLClient();
1729
1754
  let res;
1730
1755
  try {
1731
- res = await fetch(url, {
1732
- method: "POST",
1733
- headers: { "Content-Type": "application/json" },
1734
- body: JSON.stringify({
1735
- jsonrpc: "2.0",
1736
- id: 1,
1737
- method: "suix_resolveNameServiceNames",
1738
- params: [address]
1739
- }),
1740
- signal: ctx.signal ?? AbortSignal.timeout(8e3)
1756
+ res = await gql.query({
1757
+ query: REVERSE_NAME_QUERY,
1758
+ variables: { address },
1759
+ signal: ctx.signal
1741
1760
  });
1742
1761
  } catch (err) {
1743
1762
  const msg = err instanceof Error ? err.message : String(err);
1744
1763
  throw new SuinsRpcError(address, msg);
1745
1764
  }
1746
- if (!res.ok) {
1747
- throw new SuinsRpcError(address, `HTTP ${res.status}`);
1748
- }
1749
- let body;
1750
- try {
1751
- body = await res.json();
1752
- } catch (err) {
1753
- const msg = err instanceof Error ? err.message : String(err);
1754
- throw new SuinsRpcError(address, `JSON parse failed: ${msg}`);
1755
- }
1756
- if (body.error) {
1757
- throw new SuinsRpcError(address, body.error.message);
1765
+ if (res.errors?.length) {
1766
+ throw new SuinsRpcError(address, res.errors.map((e) => e.message ?? "unknown error").join("; "));
1758
1767
  }
1759
- return body.result?.data ?? [];
1768
+ const domain = res.data?.address?.defaultNameRecord?.domain;
1769
+ return domain ? [domain] : [];
1760
1770
  }
1761
1771
  async function normalizeAddressInput(value, ctx = {}) {
1762
1772
  const trimmed = value.trim();