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