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