@zubari/sdk 0.1.7 → 0.1.9

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.
@@ -1881,39 +1881,36 @@ var WalletManager = class _WalletManager {
1881
1881
  console.warn(`Failed to fetch ${chain} balance:`, error);
1882
1882
  }
1883
1883
  } else if (chain === "bitcoin") {
1884
- let apiUrl;
1885
- if (this.config.network === "mainnet" || address.startsWith("bc1") || address.startsWith("1") || address.startsWith("3")) {
1886
- apiUrl = "https://mempool.space/api";
1887
- } else if (address.startsWith("tb1") || address.startsWith("2") || address.startsWith("m") || address.startsWith("n")) {
1888
- apiUrl = "https://mempool.space/testnet4/api";
1889
- } else {
1890
- apiUrl = "https://mempool.space/testnet/api";
1891
- }
1892
- try {
1893
- let response = await fetch(`${apiUrl}/address/${address}`, {
1894
- headers: { "Accept": "application/json" }
1895
- });
1896
- if (!response.ok && apiUrl.includes("testnet4")) {
1897
- console.log("Trying testnet3 API...");
1898
- apiUrl = "https://mempool.space/testnet/api";
1899
- response = await fetch(`${apiUrl}/address/${address}`, {
1884
+ const isMainnet = this.config.network === "mainnet" || address.startsWith("bc1") || address.startsWith("1") || address.startsWith("3");
1885
+ const apisToTry = isMainnet ? ["https://mempool.space/api"] : [
1886
+ "https://mempool.space/testnet/api",
1887
+ // testnet3 first (more common)
1888
+ "https://mempool.space/testnet4/api"
1889
+ // then testnet4
1890
+ ];
1891
+ for (const apiUrl of apisToTry) {
1892
+ try {
1893
+ const response = await fetch(`${apiUrl}/address/${address}`, {
1900
1894
  headers: { "Accept": "application/json" }
1901
1895
  });
1896
+ if (response.ok) {
1897
+ const data = await response.json();
1898
+ const txCount = (data.chain_stats?.tx_count || 0) + (data.mempool_stats?.tx_count || 0);
1899
+ if (txCount > 0 || isMainnet) {
1900
+ const chainFunded = data.chain_stats?.funded_txo_sum || 0;
1901
+ const chainSpent = data.chain_stats?.spent_txo_sum || 0;
1902
+ const mempoolFunded = data.mempool_stats?.funded_txo_sum || 0;
1903
+ const mempoolSpent = data.mempool_stats?.spent_txo_sum || 0;
1904
+ const satoshis = chainFunded - chainSpent + (mempoolFunded - mempoolSpent);
1905
+ balance = (satoshis / 1e8).toFixed(8);
1906
+ console.log(`Bitcoin balance for ${address}: ${balance} BTC (${satoshis} sats) via ${apiUrl}`);
1907
+ break;
1908
+ }
1909
+ console.log(`No transactions found on ${apiUrl}, trying next...`);
1910
+ }
1911
+ } catch (error) {
1912
+ console.warn(`Failed to fetch from ${apiUrl}:`, error);
1902
1913
  }
1903
- if (response.ok) {
1904
- const data = await response.json();
1905
- const chainFunded = data.chain_stats?.funded_txo_sum || 0;
1906
- const chainSpent = data.chain_stats?.spent_txo_sum || 0;
1907
- const mempoolFunded = data.mempool_stats?.funded_txo_sum || 0;
1908
- const mempoolSpent = data.mempool_stats?.spent_txo_sum || 0;
1909
- const satoshis = chainFunded - chainSpent + (mempoolFunded - mempoolSpent);
1910
- balance = (satoshis / 1e8).toFixed(8);
1911
- console.log(`Bitcoin balance for ${address}: ${balance} BTC (${satoshis} sats) via ${apiUrl}`);
1912
- } else {
1913
- console.warn(`Bitcoin API returned status ${response.status} for ${address}`);
1914
- }
1915
- } catch (error) {
1916
- console.warn(`Failed to fetch ${chain} balance:`, error);
1917
1914
  }
1918
1915
  } else if (chain === "solana") {
1919
1916
  const rpcUrl = this.config.network === "mainnet" ? "https://api.mainnet-beta.solana.com" : "https://api.devnet.solana.com";