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