@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.
@@ -1583,39 +1583,36 @@ var WalletManager = class _WalletManager {
1583
1583
  console.warn(`Failed to fetch ${chain} balance:`, error);
1584
1584
  }
1585
1585
  } else if (chain === "bitcoin") {
1586
- let apiUrl;
1587
- if (this.config.network === "mainnet" || address.startsWith("bc1") || address.startsWith("1") || address.startsWith("3")) {
1588
- apiUrl = "https://mempool.space/api";
1589
- } else if (address.startsWith("tb1") || address.startsWith("2") || address.startsWith("m") || address.startsWith("n")) {
1590
- apiUrl = "https://mempool.space/testnet4/api";
1591
- } else {
1592
- apiUrl = "https://mempool.space/testnet/api";
1593
- }
1594
- try {
1595
- let response = await fetch(`${apiUrl}/address/${address}`, {
1596
- headers: { "Accept": "application/json" }
1597
- });
1598
- if (!response.ok && apiUrl.includes("testnet4")) {
1599
- console.log("Trying testnet3 API...");
1600
- apiUrl = "https://mempool.space/testnet/api";
1601
- response = await fetch(`${apiUrl}/address/${address}`, {
1586
+ const isMainnet = this.config.network === "mainnet" || address.startsWith("bc1") || address.startsWith("1") || address.startsWith("3");
1587
+ const apisToTry = isMainnet ? ["https://mempool.space/api"] : [
1588
+ "https://mempool.space/testnet/api",
1589
+ // testnet3 first (more common)
1590
+ "https://mempool.space/testnet4/api"
1591
+ // then testnet4
1592
+ ];
1593
+ for (const apiUrl of apisToTry) {
1594
+ try {
1595
+ const response = await fetch(`${apiUrl}/address/${address}`, {
1602
1596
  headers: { "Accept": "application/json" }
1603
1597
  });
1598
+ if (response.ok) {
1599
+ const data = await response.json();
1600
+ const txCount = (data.chain_stats?.tx_count || 0) + (data.mempool_stats?.tx_count || 0);
1601
+ if (txCount > 0 || isMainnet) {
1602
+ const chainFunded = data.chain_stats?.funded_txo_sum || 0;
1603
+ const chainSpent = data.chain_stats?.spent_txo_sum || 0;
1604
+ const mempoolFunded = data.mempool_stats?.funded_txo_sum || 0;
1605
+ const mempoolSpent = data.mempool_stats?.spent_txo_sum || 0;
1606
+ const satoshis = chainFunded - chainSpent + (mempoolFunded - mempoolSpent);
1607
+ balance = (satoshis / 1e8).toFixed(8);
1608
+ console.log(`Bitcoin balance for ${address}: ${balance} BTC (${satoshis} sats) via ${apiUrl}`);
1609
+ break;
1610
+ }
1611
+ console.log(`No transactions found on ${apiUrl}, trying next...`);
1612
+ }
1613
+ } catch (error) {
1614
+ console.warn(`Failed to fetch from ${apiUrl}:`, error);
1604
1615
  }
1605
- if (response.ok) {
1606
- const data = await response.json();
1607
- const chainFunded = data.chain_stats?.funded_txo_sum || 0;
1608
- const chainSpent = data.chain_stats?.spent_txo_sum || 0;
1609
- const mempoolFunded = data.mempool_stats?.funded_txo_sum || 0;
1610
- const mempoolSpent = data.mempool_stats?.spent_txo_sum || 0;
1611
- const satoshis = chainFunded - chainSpent + (mempoolFunded - mempoolSpent);
1612
- balance = (satoshis / 1e8).toFixed(8);
1613
- console.log(`Bitcoin balance for ${address}: ${balance} BTC (${satoshis} sats) via ${apiUrl}`);
1614
- } else {
1615
- console.warn(`Bitcoin API returned status ${response.status} for ${address}`);
1616
- }
1617
- } catch (error) {
1618
- console.warn(`Failed to fetch ${chain} balance:`, error);
1619
1616
  }
1620
1617
  } else if (chain === "solana") {
1621
1618
  const rpcUrl = this.config.network === "mainnet" ? "https://api.mainnet-beta.solana.com" : "https://api.devnet.solana.com";
@@ -1790,6 +1787,12 @@ function useWalletManager(options = {}) {
1790
1787
  manager.deriveAllAddresses();
1791
1788
  }
1792
1789
  updateState();
1790
+ try {
1791
+ const balances = await manager.fetchAllBalances();
1792
+ setChainBalances(balances);
1793
+ } catch (err) {
1794
+ console.warn("Failed to fetch balances after create:", err);
1795
+ }
1793
1796
  return result;
1794
1797
  } catch (err) {
1795
1798
  const message = err instanceof Error ? err.message : "Failed to create wallet";
@@ -1814,6 +1817,12 @@ function useWalletManager(options = {}) {
1814
1817
  manager.deriveAllAddresses();
1815
1818
  }
1816
1819
  updateState();
1820
+ try {
1821
+ const balances = await manager.fetchAllBalances();
1822
+ setChainBalances(balances);
1823
+ } catch (err) {
1824
+ console.warn("Failed to fetch balances after import:", err);
1825
+ }
1817
1826
  } catch (err) {
1818
1827
  const message = err instanceof Error ? err.message : "Failed to import wallet";
1819
1828
  setError(message);
@@ -1837,6 +1846,12 @@ function useWalletManager(options = {}) {
1837
1846
  manager.deriveAllAddresses();
1838
1847
  }
1839
1848
  updateState();
1849
+ try {
1850
+ const balances = await manager.fetchAllBalances();
1851
+ setChainBalances(balances);
1852
+ } catch (err) {
1853
+ console.warn("Failed to fetch balances after unlock:", err);
1854
+ }
1840
1855
  } catch (err) {
1841
1856
  const message = err instanceof Error ? err.message : "Invalid password";
1842
1857
  setError(message);