@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.
@@ -1366,6 +1366,47 @@ function getZubariWdkService(config) {
1366
1366
  }
1367
1367
 
1368
1368
  // src/wallet/WalletManager.ts
1369
+ var COINGECKO_IDS = {
1370
+ ethereum: "ethereum",
1371
+ bitcoin: "bitcoin",
1372
+ ton: "the-open-network",
1373
+ tron: "tron",
1374
+ solana: "solana",
1375
+ spark: "bitcoin"
1376
+ // Spark uses BTC
1377
+ };
1378
+ var priceCache = null;
1379
+ var PRICE_CACHE_TTL = 6e4;
1380
+ async function fetchPrices() {
1381
+ if (priceCache && Date.now() - priceCache.timestamp < PRICE_CACHE_TTL) {
1382
+ return priceCache.prices;
1383
+ }
1384
+ const ids = Object.values(COINGECKO_IDS).filter((v, i, a) => a.indexOf(v) === i).join(",");
1385
+ try {
1386
+ const response = await fetch(
1387
+ `https://api.coingecko.com/api/v3/simple/price?ids=${ids}&vs_currencies=usd`,
1388
+ {
1389
+ headers: { "Accept": "application/json" }
1390
+ }
1391
+ );
1392
+ if (response.ok) {
1393
+ const data = await response.json();
1394
+ const prices = {};
1395
+ for (const [chain, geckoId] of Object.entries(COINGECKO_IDS)) {
1396
+ prices[chain] = data[geckoId]?.usd || 0;
1397
+ }
1398
+ priceCache = { prices, timestamp: Date.now() };
1399
+ return prices;
1400
+ }
1401
+ } catch (error) {
1402
+ console.warn("Failed to fetch prices from CoinGecko:", error);
1403
+ }
1404
+ return priceCache?.prices || {};
1405
+ }
1406
+ async function getPriceForChain(chain) {
1407
+ const prices = await fetchPrices();
1408
+ return prices[chain] || 0;
1409
+ }
1369
1410
  var STORAGE_KEYS = {
1370
1411
  ENCRYPTED_SEED: "encrypted_seed",
1371
1412
  ACTIVE_WALLET: "active_wallet"
@@ -1838,22 +1879,36 @@ var WalletManager = class _WalletManager {
1838
1879
  console.warn(`Failed to fetch ${chain} balance:`, error);
1839
1880
  }
1840
1881
  } else if (chain === "bitcoin") {
1841
- const baseUrl = this.config.network === "mainnet" ? "https://blockstream.info/api" : "https://blockstream.info/testnet/api";
1842
- try {
1843
- const response = await fetch(`${baseUrl}/address/${address}`, {
1844
- headers: { "Accept": "application/json" }
1845
- });
1846
- if (response.ok) {
1847
- const data = await response.json();
1848
- const chainFunded = data.chain_stats?.funded_txo_sum || 0;
1849
- const chainSpent = data.chain_stats?.spent_txo_sum || 0;
1850
- const mempoolFunded = data.mempool_stats?.funded_txo_sum || 0;
1851
- const mempoolSpent = data.mempool_stats?.spent_txo_sum || 0;
1852
- const satoshis = chainFunded - chainSpent + (mempoolFunded - mempoolSpent);
1853
- balance = (satoshis / 1e8).toFixed(8);
1882
+ const isMainnet = this.config.network === "mainnet" || address.startsWith("bc1") || address.startsWith("1") || address.startsWith("3");
1883
+ const apisToTry = isMainnet ? ["https://mempool.space/api"] : [
1884
+ "https://mempool.space/testnet/api",
1885
+ // testnet3 first (more common)
1886
+ "https://mempool.space/testnet4/api"
1887
+ // then testnet4
1888
+ ];
1889
+ for (const apiUrl of apisToTry) {
1890
+ try {
1891
+ const response = await fetch(`${apiUrl}/address/${address}`, {
1892
+ headers: { "Accept": "application/json" }
1893
+ });
1894
+ if (response.ok) {
1895
+ const data = await response.json();
1896
+ const txCount = (data.chain_stats?.tx_count || 0) + (data.mempool_stats?.tx_count || 0);
1897
+ if (txCount > 0 || isMainnet) {
1898
+ const chainFunded = data.chain_stats?.funded_txo_sum || 0;
1899
+ const chainSpent = data.chain_stats?.spent_txo_sum || 0;
1900
+ const mempoolFunded = data.mempool_stats?.funded_txo_sum || 0;
1901
+ const mempoolSpent = data.mempool_stats?.spent_txo_sum || 0;
1902
+ const satoshis = chainFunded - chainSpent + (mempoolFunded - mempoolSpent);
1903
+ balance = (satoshis / 1e8).toFixed(8);
1904
+ console.log(`Bitcoin balance for ${address}: ${balance} BTC (${satoshis} sats) via ${apiUrl}`);
1905
+ break;
1906
+ }
1907
+ console.log(`No transactions found on ${apiUrl}, trying next...`);
1908
+ }
1909
+ } catch (error) {
1910
+ console.warn(`Failed to fetch from ${apiUrl}:`, error);
1854
1911
  }
1855
- } catch (error) {
1856
- console.warn(`Failed to fetch ${chain} balance:`, error);
1857
1912
  }
1858
1913
  } else if (chain === "solana") {
1859
1914
  const rpcUrl = this.config.network === "mainnet" ? "https://api.mainnet-beta.solana.com" : "https://api.devnet.solana.com";
@@ -1892,13 +1947,53 @@ var WalletManager = class _WalletManager {
1892
1947
  } catch (error) {
1893
1948
  console.warn(`Failed to fetch ${chain} balance:`, error);
1894
1949
  }
1950
+ } else if (chain === "ton") {
1951
+ const baseUrl = this.config.network === "mainnet" ? "https://toncenter.com/api/v2" : "https://testnet.toncenter.com/api/v2";
1952
+ try {
1953
+ const response = await fetch(`${baseUrl}/getAddressBalance?address=${address}`, {
1954
+ headers: { "Accept": "application/json" }
1955
+ });
1956
+ if (response.ok) {
1957
+ const data = await response.json();
1958
+ if (data.ok && data.result !== void 0) {
1959
+ const nanotons = BigInt(data.result);
1960
+ balance = (Number(nanotons) / 1e9).toFixed(9);
1961
+ console.log(`TON balance for ${address}: ${balance} TON`);
1962
+ }
1963
+ }
1964
+ } catch (error) {
1965
+ console.warn(`Failed to fetch ${chain} balance:`, error);
1966
+ }
1967
+ } else if (chain === "spark") {
1968
+ try {
1969
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/balance`, {
1970
+ method: "POST",
1971
+ headers: { "Content-Type": "application/json" },
1972
+ body: JSON.stringify({
1973
+ chain: "spark",
1974
+ address,
1975
+ network: this.config.network
1976
+ })
1977
+ });
1978
+ if (response.ok) {
1979
+ const data = await response.json();
1980
+ if (data.success && data.balance !== void 0) {
1981
+ balance = (parseFloat(data.balance) / 1e8).toFixed(8);
1982
+ console.log(`Spark balance for ${address}: ${balance} BTC`);
1983
+ }
1984
+ }
1985
+ } catch (error) {
1986
+ console.warn(`Failed to fetch ${chain} balance:`, error);
1987
+ }
1895
1988
  }
1989
+ const priceUsd = await getPriceForChain(chain);
1990
+ const balanceNum = parseFloat(balance) || 0;
1991
+ const balanceUsd = balanceNum * priceUsd;
1896
1992
  return {
1897
1993
  chain,
1898
1994
  symbol: networkConfig.nativeCurrency.symbol,
1899
1995
  balance,
1900
- balanceUsd: 0,
1901
- // TODO: Implement price fetching
1996
+ balanceUsd,
1902
1997
  address,
1903
1998
  decimals: networkConfig.nativeCurrency.decimals
1904
1999
  };