@zubari/sdk 0.1.6 → 0.1.8

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.
@@ -1070,6 +1070,47 @@ function getZubariWdkService(config) {
1070
1070
  }
1071
1071
 
1072
1072
  // src/wallet/WalletManager.ts
1073
+ var COINGECKO_IDS = {
1074
+ ethereum: "ethereum",
1075
+ bitcoin: "bitcoin",
1076
+ ton: "the-open-network",
1077
+ tron: "tron",
1078
+ solana: "solana",
1079
+ spark: "bitcoin"
1080
+ // Spark uses BTC
1081
+ };
1082
+ var priceCache = null;
1083
+ var PRICE_CACHE_TTL = 6e4;
1084
+ async function fetchPrices() {
1085
+ if (priceCache && Date.now() - priceCache.timestamp < PRICE_CACHE_TTL) {
1086
+ return priceCache.prices;
1087
+ }
1088
+ const ids = Object.values(COINGECKO_IDS).filter((v, i, a) => a.indexOf(v) === i).join(",");
1089
+ try {
1090
+ const response = await fetch(
1091
+ `https://api.coingecko.com/api/v3/simple/price?ids=${ids}&vs_currencies=usd`,
1092
+ {
1093
+ headers: { "Accept": "application/json" }
1094
+ }
1095
+ );
1096
+ if (response.ok) {
1097
+ const data = await response.json();
1098
+ const prices = {};
1099
+ for (const [chain, geckoId] of Object.entries(COINGECKO_IDS)) {
1100
+ prices[chain] = data[geckoId]?.usd || 0;
1101
+ }
1102
+ priceCache = { prices, timestamp: Date.now() };
1103
+ return prices;
1104
+ }
1105
+ } catch (error) {
1106
+ console.warn("Failed to fetch prices from CoinGecko:", error);
1107
+ }
1108
+ return priceCache?.prices || {};
1109
+ }
1110
+ async function getPriceForChain(chain) {
1111
+ const prices = await fetchPrices();
1112
+ return prices[chain] || 0;
1113
+ }
1073
1114
  var STORAGE_KEYS = {
1074
1115
  ENCRYPTED_SEED: "encrypted_seed",
1075
1116
  ACTIVE_WALLET: "active_wallet"
@@ -1542,22 +1583,36 @@ var WalletManager = class _WalletManager {
1542
1583
  console.warn(`Failed to fetch ${chain} balance:`, error);
1543
1584
  }
1544
1585
  } else if (chain === "bitcoin") {
1545
- const baseUrl = this.config.network === "mainnet" ? "https://blockstream.info/api" : "https://blockstream.info/testnet/api";
1546
- try {
1547
- const response = await fetch(`${baseUrl}/address/${address}`, {
1548
- headers: { "Accept": "application/json" }
1549
- });
1550
- if (response.ok) {
1551
- const data = await response.json();
1552
- const chainFunded = data.chain_stats?.funded_txo_sum || 0;
1553
- const chainSpent = data.chain_stats?.spent_txo_sum || 0;
1554
- const mempoolFunded = data.mempool_stats?.funded_txo_sum || 0;
1555
- const mempoolSpent = data.mempool_stats?.spent_txo_sum || 0;
1556
- const satoshis = chainFunded - chainSpent + (mempoolFunded - mempoolSpent);
1557
- balance = (satoshis / 1e8).toFixed(8);
1586
+ const isMainnet = this.config.network === "mainnet" || address.startsWith("bc1") || address.startsWith("1") || address.startsWith("3");
1587
+ const apisToTry = isMainnet ? ["https://mempool.space/api"] : [
1588
+ "https://mempool.space/testnet/api",
1589
+ // testnet3 first (more common)
1590
+ "https://mempool.space/testnet4/api"
1591
+ // then testnet4
1592
+ ];
1593
+ for (const apiUrl of apisToTry) {
1594
+ try {
1595
+ const response = await fetch(`${apiUrl}/address/${address}`, {
1596
+ headers: { "Accept": "application/json" }
1597
+ });
1598
+ if (response.ok) {
1599
+ const data = await response.json();
1600
+ const txCount = (data.chain_stats?.tx_count || 0) + (data.mempool_stats?.tx_count || 0);
1601
+ if (txCount > 0 || isMainnet) {
1602
+ const chainFunded = data.chain_stats?.funded_txo_sum || 0;
1603
+ const chainSpent = data.chain_stats?.spent_txo_sum || 0;
1604
+ const mempoolFunded = data.mempool_stats?.funded_txo_sum || 0;
1605
+ const mempoolSpent = data.mempool_stats?.spent_txo_sum || 0;
1606
+ const satoshis = chainFunded - chainSpent + (mempoolFunded - mempoolSpent);
1607
+ balance = (satoshis / 1e8).toFixed(8);
1608
+ console.log(`Bitcoin balance for ${address}: ${balance} BTC (${satoshis} sats) via ${apiUrl}`);
1609
+ break;
1610
+ }
1611
+ console.log(`No transactions found on ${apiUrl}, trying next...`);
1612
+ }
1613
+ } catch (error) {
1614
+ console.warn(`Failed to fetch from ${apiUrl}:`, error);
1558
1615
  }
1559
- } catch (error) {
1560
- console.warn(`Failed to fetch ${chain} balance:`, error);
1561
1616
  }
1562
1617
  } else if (chain === "solana") {
1563
1618
  const rpcUrl = this.config.network === "mainnet" ? "https://api.mainnet-beta.solana.com" : "https://api.devnet.solana.com";
@@ -1596,13 +1651,53 @@ var WalletManager = class _WalletManager {
1596
1651
  } catch (error) {
1597
1652
  console.warn(`Failed to fetch ${chain} balance:`, error);
1598
1653
  }
1654
+ } else if (chain === "ton") {
1655
+ const baseUrl = this.config.network === "mainnet" ? "https://toncenter.com/api/v2" : "https://testnet.toncenter.com/api/v2";
1656
+ try {
1657
+ const response = await fetch(`${baseUrl}/getAddressBalance?address=${address}`, {
1658
+ headers: { "Accept": "application/json" }
1659
+ });
1660
+ if (response.ok) {
1661
+ const data = await response.json();
1662
+ if (data.ok && data.result !== void 0) {
1663
+ const nanotons = BigInt(data.result);
1664
+ balance = (Number(nanotons) / 1e9).toFixed(9);
1665
+ console.log(`TON balance for ${address}: ${balance} TON`);
1666
+ }
1667
+ }
1668
+ } catch (error) {
1669
+ console.warn(`Failed to fetch ${chain} balance:`, error);
1670
+ }
1671
+ } else if (chain === "spark") {
1672
+ try {
1673
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/balance`, {
1674
+ method: "POST",
1675
+ headers: { "Content-Type": "application/json" },
1676
+ body: JSON.stringify({
1677
+ chain: "spark",
1678
+ address,
1679
+ network: this.config.network
1680
+ })
1681
+ });
1682
+ if (response.ok) {
1683
+ const data = await response.json();
1684
+ if (data.success && data.balance !== void 0) {
1685
+ balance = (parseFloat(data.balance) / 1e8).toFixed(8);
1686
+ console.log(`Spark balance for ${address}: ${balance} BTC`);
1687
+ }
1688
+ }
1689
+ } catch (error) {
1690
+ console.warn(`Failed to fetch ${chain} balance:`, error);
1691
+ }
1599
1692
  }
1693
+ const priceUsd = await getPriceForChain(chain);
1694
+ const balanceNum = parseFloat(balance) || 0;
1695
+ const balanceUsd = balanceNum * priceUsd;
1600
1696
  return {
1601
1697
  chain,
1602
1698
  symbol: networkConfig.nativeCurrency.symbol,
1603
1699
  balance,
1604
- balanceUsd: 0,
1605
- // TODO: Implement price fetching
1700
+ balanceUsd,
1606
1701
  address,
1607
1702
  decimals: networkConfig.nativeCurrency.decimals
1608
1703
  };