@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.
@@ -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,11 +1879,25 @@ 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";
1882
+ let apiUrl;
1883
+ if (this.config.network === "mainnet" || address.startsWith("bc1") || address.startsWith("1") || address.startsWith("3")) {
1884
+ apiUrl = "https://mempool.space/api";
1885
+ } else if (address.startsWith("tb1") || address.startsWith("2") || address.startsWith("m") || address.startsWith("n")) {
1886
+ apiUrl = "https://mempool.space/testnet4/api";
1887
+ } else {
1888
+ apiUrl = "https://mempool.space/testnet/api";
1889
+ }
1842
1890
  try {
1843
- const response = await fetch(`${baseUrl}/address/${address}`, {
1891
+ let response = await fetch(`${apiUrl}/address/${address}`, {
1844
1892
  headers: { "Accept": "application/json" }
1845
1893
  });
1894
+ if (!response.ok && apiUrl.includes("testnet4")) {
1895
+ console.log("Trying testnet3 API...");
1896
+ apiUrl = "https://mempool.space/testnet/api";
1897
+ response = await fetch(`${apiUrl}/address/${address}`, {
1898
+ headers: { "Accept": "application/json" }
1899
+ });
1900
+ }
1846
1901
  if (response.ok) {
1847
1902
  const data = await response.json();
1848
1903
  const chainFunded = data.chain_stats?.funded_txo_sum || 0;
@@ -1851,6 +1906,9 @@ var WalletManager = class _WalletManager {
1851
1906
  const mempoolSpent = data.mempool_stats?.spent_txo_sum || 0;
1852
1907
  const satoshis = chainFunded - chainSpent + (mempoolFunded - mempoolSpent);
1853
1908
  balance = (satoshis / 1e8).toFixed(8);
1909
+ console.log(`Bitcoin balance for ${address}: ${balance} BTC (${satoshis} sats) via ${apiUrl}`);
1910
+ } else {
1911
+ console.warn(`Bitcoin API returned status ${response.status} for ${address}`);
1854
1912
  }
1855
1913
  } catch (error) {
1856
1914
  console.warn(`Failed to fetch ${chain} balance:`, error);
@@ -1892,13 +1950,53 @@ var WalletManager = class _WalletManager {
1892
1950
  } catch (error) {
1893
1951
  console.warn(`Failed to fetch ${chain} balance:`, error);
1894
1952
  }
1953
+ } else if (chain === "ton") {
1954
+ const baseUrl = this.config.network === "mainnet" ? "https://toncenter.com/api/v2" : "https://testnet.toncenter.com/api/v2";
1955
+ try {
1956
+ const response = await fetch(`${baseUrl}/getAddressBalance?address=${address}`, {
1957
+ headers: { "Accept": "application/json" }
1958
+ });
1959
+ if (response.ok) {
1960
+ const data = await response.json();
1961
+ if (data.ok && data.result !== void 0) {
1962
+ const nanotons = BigInt(data.result);
1963
+ balance = (Number(nanotons) / 1e9).toFixed(9);
1964
+ console.log(`TON balance for ${address}: ${balance} TON`);
1965
+ }
1966
+ }
1967
+ } catch (error) {
1968
+ console.warn(`Failed to fetch ${chain} balance:`, error);
1969
+ }
1970
+ } else if (chain === "spark") {
1971
+ try {
1972
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/balance`, {
1973
+ method: "POST",
1974
+ headers: { "Content-Type": "application/json" },
1975
+ body: JSON.stringify({
1976
+ chain: "spark",
1977
+ address,
1978
+ network: this.config.network
1979
+ })
1980
+ });
1981
+ if (response.ok) {
1982
+ const data = await response.json();
1983
+ if (data.success && data.balance !== void 0) {
1984
+ balance = (parseFloat(data.balance) / 1e8).toFixed(8);
1985
+ console.log(`Spark balance for ${address}: ${balance} BTC`);
1986
+ }
1987
+ }
1988
+ } catch (error) {
1989
+ console.warn(`Failed to fetch ${chain} balance:`, error);
1990
+ }
1895
1991
  }
1992
+ const priceUsd = await getPriceForChain(chain);
1993
+ const balanceNum = parseFloat(balance) || 0;
1994
+ const balanceUsd = balanceNum * priceUsd;
1896
1995
  return {
1897
1996
  chain,
1898
1997
  symbol: networkConfig.nativeCurrency.symbol,
1899
1998
  balance,
1900
- balanceUsd: 0,
1901
- // TODO: Implement price fetching
1999
+ balanceUsd,
1902
2000
  address,
1903
2001
  decimals: networkConfig.nativeCurrency.decimals
1904
2002
  };