@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.
package/dist/index.js CHANGED
@@ -1417,6 +1417,47 @@ function createZubariWdkService(config) {
1417
1417
  }
1418
1418
 
1419
1419
  // src/wallet/WalletManager.ts
1420
+ var COINGECKO_IDS = {
1421
+ ethereum: "ethereum",
1422
+ bitcoin: "bitcoin",
1423
+ ton: "the-open-network",
1424
+ tron: "tron",
1425
+ solana: "solana",
1426
+ spark: "bitcoin"
1427
+ // Spark uses BTC
1428
+ };
1429
+ var priceCache = null;
1430
+ var PRICE_CACHE_TTL = 6e4;
1431
+ async function fetchPrices() {
1432
+ if (priceCache && Date.now() - priceCache.timestamp < PRICE_CACHE_TTL) {
1433
+ return priceCache.prices;
1434
+ }
1435
+ const ids = Object.values(COINGECKO_IDS).filter((v, i, a) => a.indexOf(v) === i).join(",");
1436
+ try {
1437
+ const response = await fetch(
1438
+ `https://api.coingecko.com/api/v3/simple/price?ids=${ids}&vs_currencies=usd`,
1439
+ {
1440
+ headers: { "Accept": "application/json" }
1441
+ }
1442
+ );
1443
+ if (response.ok) {
1444
+ const data = await response.json();
1445
+ const prices = {};
1446
+ for (const [chain, geckoId] of Object.entries(COINGECKO_IDS)) {
1447
+ prices[chain] = data[geckoId]?.usd || 0;
1448
+ }
1449
+ priceCache = { prices, timestamp: Date.now() };
1450
+ return prices;
1451
+ }
1452
+ } catch (error) {
1453
+ console.warn("Failed to fetch prices from CoinGecko:", error);
1454
+ }
1455
+ return priceCache?.prices || {};
1456
+ }
1457
+ async function getPriceForChain(chain) {
1458
+ const prices = await fetchPrices();
1459
+ return prices[chain] || 0;
1460
+ }
1420
1461
  var STORAGE_KEYS = {
1421
1462
  ENCRYPTED_SEED: "encrypted_seed",
1422
1463
  ACTIVE_WALLET: "active_wallet"
@@ -1889,11 +1930,25 @@ var WalletManager = class _WalletManager {
1889
1930
  console.warn(`Failed to fetch ${chain} balance:`, error);
1890
1931
  }
1891
1932
  } else if (chain === "bitcoin") {
1892
- const baseUrl = this.config.network === "mainnet" ? "https://blockstream.info/api" : "https://blockstream.info/testnet/api";
1933
+ let apiUrl;
1934
+ if (this.config.network === "mainnet" || address.startsWith("bc1") || address.startsWith("1") || address.startsWith("3")) {
1935
+ apiUrl = "https://mempool.space/api";
1936
+ } else if (address.startsWith("tb1") || address.startsWith("2") || address.startsWith("m") || address.startsWith("n")) {
1937
+ apiUrl = "https://mempool.space/testnet4/api";
1938
+ } else {
1939
+ apiUrl = "https://mempool.space/testnet/api";
1940
+ }
1893
1941
  try {
1894
- const response = await fetch(`${baseUrl}/address/${address}`, {
1942
+ let response = await fetch(`${apiUrl}/address/${address}`, {
1895
1943
  headers: { "Accept": "application/json" }
1896
1944
  });
1945
+ if (!response.ok && apiUrl.includes("testnet4")) {
1946
+ console.log("Trying testnet3 API...");
1947
+ apiUrl = "https://mempool.space/testnet/api";
1948
+ response = await fetch(`${apiUrl}/address/${address}`, {
1949
+ headers: { "Accept": "application/json" }
1950
+ });
1951
+ }
1897
1952
  if (response.ok) {
1898
1953
  const data = await response.json();
1899
1954
  const chainFunded = data.chain_stats?.funded_txo_sum || 0;
@@ -1902,6 +1957,9 @@ var WalletManager = class _WalletManager {
1902
1957
  const mempoolSpent = data.mempool_stats?.spent_txo_sum || 0;
1903
1958
  const satoshis = chainFunded - chainSpent + (mempoolFunded - mempoolSpent);
1904
1959
  balance = (satoshis / 1e8).toFixed(8);
1960
+ console.log(`Bitcoin balance for ${address}: ${balance} BTC (${satoshis} sats) via ${apiUrl}`);
1961
+ } else {
1962
+ console.warn(`Bitcoin API returned status ${response.status} for ${address}`);
1905
1963
  }
1906
1964
  } catch (error) {
1907
1965
  console.warn(`Failed to fetch ${chain} balance:`, error);
@@ -1943,13 +2001,53 @@ var WalletManager = class _WalletManager {
1943
2001
  } catch (error) {
1944
2002
  console.warn(`Failed to fetch ${chain} balance:`, error);
1945
2003
  }
2004
+ } else if (chain === "ton") {
2005
+ const baseUrl = this.config.network === "mainnet" ? "https://toncenter.com/api/v2" : "https://testnet.toncenter.com/api/v2";
2006
+ try {
2007
+ const response = await fetch(`${baseUrl}/getAddressBalance?address=${address}`, {
2008
+ headers: { "Accept": "application/json" }
2009
+ });
2010
+ if (response.ok) {
2011
+ const data = await response.json();
2012
+ if (data.ok && data.result !== void 0) {
2013
+ const nanotons = BigInt(data.result);
2014
+ balance = (Number(nanotons) / 1e9).toFixed(9);
2015
+ console.log(`TON balance for ${address}: ${balance} TON`);
2016
+ }
2017
+ }
2018
+ } catch (error) {
2019
+ console.warn(`Failed to fetch ${chain} balance:`, error);
2020
+ }
2021
+ } else if (chain === "spark") {
2022
+ try {
2023
+ const response = await fetch(`${this.config.apiUrl}/api/wallets/wdk/balance`, {
2024
+ method: "POST",
2025
+ headers: { "Content-Type": "application/json" },
2026
+ body: JSON.stringify({
2027
+ chain: "spark",
2028
+ address,
2029
+ network: this.config.network
2030
+ })
2031
+ });
2032
+ if (response.ok) {
2033
+ const data = await response.json();
2034
+ if (data.success && data.balance !== void 0) {
2035
+ balance = (parseFloat(data.balance) / 1e8).toFixed(8);
2036
+ console.log(`Spark balance for ${address}: ${balance} BTC`);
2037
+ }
2038
+ }
2039
+ } catch (error) {
2040
+ console.warn(`Failed to fetch ${chain} balance:`, error);
2041
+ }
1946
2042
  }
2043
+ const priceUsd = await getPriceForChain(chain);
2044
+ const balanceNum = parseFloat(balance) || 0;
2045
+ const balanceUsd = balanceNum * priceUsd;
1947
2046
  return {
1948
2047
  chain,
1949
2048
  symbol: networkConfig.nativeCurrency.symbol,
1950
2049
  balance,
1951
- balanceUsd: 0,
1952
- // TODO: Implement price fetching
2050
+ balanceUsd,
1953
2051
  address,
1954
2052
  decimals: networkConfig.nativeCurrency.decimals
1955
2053
  };
@@ -2465,6 +2563,47 @@ var WalletManagerTron;
2465
2563
  var WalletManagerSpark;
2466
2564
  var wdkLoaded = false;
2467
2565
  var wdkLoadError = null;
2566
+ var COINGECKO_IDS2 = {
2567
+ ethereum: "ethereum",
2568
+ bitcoin: "bitcoin",
2569
+ ton: "the-open-network",
2570
+ tron: "tron",
2571
+ solana: "solana",
2572
+ spark: "bitcoin"
2573
+ // Spark uses BTC
2574
+ };
2575
+ var priceCache2 = null;
2576
+ var PRICE_CACHE_TTL2 = 6e4;
2577
+ async function fetchPrices2() {
2578
+ if (priceCache2 && Date.now() - priceCache2.timestamp < PRICE_CACHE_TTL2) {
2579
+ return priceCache2.prices;
2580
+ }
2581
+ const ids = Object.values(COINGECKO_IDS2).filter((v, i, a) => a.indexOf(v) === i).join(",");
2582
+ try {
2583
+ const response = await fetch(
2584
+ `https://api.coingecko.com/api/v3/simple/price?ids=${ids}&vs_currencies=usd`,
2585
+ {
2586
+ headers: { "Accept": "application/json" }
2587
+ }
2588
+ );
2589
+ if (response.ok) {
2590
+ const data = await response.json();
2591
+ const prices = {};
2592
+ for (const [chain, geckoId] of Object.entries(COINGECKO_IDS2)) {
2593
+ prices[chain] = data[geckoId]?.usd || 0;
2594
+ }
2595
+ priceCache2 = { prices, timestamp: Date.now() };
2596
+ return prices;
2597
+ }
2598
+ } catch (error) {
2599
+ console.warn("Failed to fetch prices from CoinGecko:", error);
2600
+ }
2601
+ return priceCache2?.prices || {};
2602
+ }
2603
+ async function getPriceForChain2(chain) {
2604
+ const prices = await fetchPrices2();
2605
+ return prices[chain] || 0;
2606
+ }
2468
2607
  var dynamicImport2 = new Function("specifier", "return import(specifier)");
2469
2608
  async function loadWdkModules() {
2470
2609
  if (wdkLoaded) return;
@@ -2816,10 +2955,13 @@ var TransactionService = class {
2816
2955
  const account = await wallet.getAccount(0);
2817
2956
  try {
2818
2957
  const balance = await account.getBalance();
2958
+ const balanceStr = balance.toString();
2959
+ const priceUsd = await getPriceForChain2(chain);
2960
+ const balanceNum = parseFloat(balanceStr) || 0;
2961
+ const balanceUsd = balanceNum * priceUsd;
2819
2962
  return {
2820
- balance: balance.toString(),
2821
- balanceUsd: 0
2822
- // TODO: Implement price fetching
2963
+ balance: balanceStr,
2964
+ balanceUsd
2823
2965
  };
2824
2966
  } catch (error) {
2825
2967
  console.error(`Error getting balance for ${chain}:`, error);