@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.
@@ -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,11 +1583,25 @@ 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";
1586
+ let apiUrl;
1587
+ if (this.config.network === "mainnet" || address.startsWith("bc1") || address.startsWith("1") || address.startsWith("3")) {
1588
+ apiUrl = "https://mempool.space/api";
1589
+ } else if (address.startsWith("tb1") || address.startsWith("2") || address.startsWith("m") || address.startsWith("n")) {
1590
+ apiUrl = "https://mempool.space/testnet4/api";
1591
+ } else {
1592
+ apiUrl = "https://mempool.space/testnet/api";
1593
+ }
1546
1594
  try {
1547
- const response = await fetch(`${baseUrl}/address/${address}`, {
1595
+ let response = await fetch(`${apiUrl}/address/${address}`, {
1548
1596
  headers: { "Accept": "application/json" }
1549
1597
  });
1598
+ if (!response.ok && apiUrl.includes("testnet4")) {
1599
+ console.log("Trying testnet3 API...");
1600
+ apiUrl = "https://mempool.space/testnet/api";
1601
+ response = await fetch(`${apiUrl}/address/${address}`, {
1602
+ headers: { "Accept": "application/json" }
1603
+ });
1604
+ }
1550
1605
  if (response.ok) {
1551
1606
  const data = await response.json();
1552
1607
  const chainFunded = data.chain_stats?.funded_txo_sum || 0;
@@ -1555,6 +1610,9 @@ var WalletManager = class _WalletManager {
1555
1610
  const mempoolSpent = data.mempool_stats?.spent_txo_sum || 0;
1556
1611
  const satoshis = chainFunded - chainSpent + (mempoolFunded - mempoolSpent);
1557
1612
  balance = (satoshis / 1e8).toFixed(8);
1613
+ console.log(`Bitcoin balance for ${address}: ${balance} BTC (${satoshis} sats) via ${apiUrl}`);
1614
+ } else {
1615
+ console.warn(`Bitcoin API returned status ${response.status} for ${address}`);
1558
1616
  }
1559
1617
  } catch (error) {
1560
1618
  console.warn(`Failed to fetch ${chain} balance:`, error);
@@ -1596,13 +1654,53 @@ var WalletManager = class _WalletManager {
1596
1654
  } catch (error) {
1597
1655
  console.warn(`Failed to fetch ${chain} balance:`, error);
1598
1656
  }
1657
+ } else if (chain === "ton") {
1658
+ const baseUrl = this.config.network === "mainnet" ? "https://toncenter.com/api/v2" : "https://testnet.toncenter.com/api/v2";
1659
+ try {
1660
+ const response = await fetch(`${baseUrl}/getAddressBalance?address=${address}`, {
1661
+ headers: { "Accept": "application/json" }
1662
+ });
1663
+ if (response.ok) {
1664
+ const data = await response.json();
1665
+ if (data.ok && data.result !== void 0) {
1666
+ const nanotons = BigInt(data.result);
1667
+ balance = (Number(nanotons) / 1e9).toFixed(9);
1668
+ console.log(`TON balance for ${address}: ${balance} TON`);
1669
+ }
1670
+ }
1671
+ } catch (error) {
1672
+ console.warn(`Failed to fetch ${chain} balance:`, error);
1673
+ }
1674
+ } else if (chain === "spark") {
1675
+ try {
1676
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/balance`, {
1677
+ method: "POST",
1678
+ headers: { "Content-Type": "application/json" },
1679
+ body: JSON.stringify({
1680
+ chain: "spark",
1681
+ address,
1682
+ network: this.config.network
1683
+ })
1684
+ });
1685
+ if (response.ok) {
1686
+ const data = await response.json();
1687
+ if (data.success && data.balance !== void 0) {
1688
+ balance = (parseFloat(data.balance) / 1e8).toFixed(8);
1689
+ console.log(`Spark balance for ${address}: ${balance} BTC`);
1690
+ }
1691
+ }
1692
+ } catch (error) {
1693
+ console.warn(`Failed to fetch ${chain} balance:`, error);
1694
+ }
1599
1695
  }
1696
+ const priceUsd = await getPriceForChain(chain);
1697
+ const balanceNum = parseFloat(balance) || 0;
1698
+ const balanceUsd = balanceNum * priceUsd;
1600
1699
  return {
1601
1700
  chain,
1602
1701
  symbol: networkConfig.nativeCurrency.symbol,
1603
1702
  balance,
1604
- balanceUsd: 0,
1605
- // TODO: Implement price fetching
1703
+ balanceUsd,
1606
1704
  address,
1607
1705
  decimals: networkConfig.nativeCurrency.decimals
1608
1706
  };