@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.
package/dist/index.mjs CHANGED
@@ -1928,39 +1928,36 @@ var WalletManager = class _WalletManager {
1928
1928
  console.warn(`Failed to fetch ${chain} balance:`, error);
1929
1929
  }
1930
1930
  } else if (chain === "bitcoin") {
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
- }
1939
- try {
1940
- let response = await fetch(`${apiUrl}/address/${address}`, {
1941
- headers: { "Accept": "application/json" }
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}`, {
1931
+ const isMainnet = this.config.network === "mainnet" || address.startsWith("bc1") || address.startsWith("1") || address.startsWith("3");
1932
+ const apisToTry = isMainnet ? ["https://mempool.space/api"] : [
1933
+ "https://mempool.space/testnet/api",
1934
+ // testnet3 first (more common)
1935
+ "https://mempool.space/testnet4/api"
1936
+ // then testnet4
1937
+ ];
1938
+ for (const apiUrl of apisToTry) {
1939
+ try {
1940
+ const response = await fetch(`${apiUrl}/address/${address}`, {
1947
1941
  headers: { "Accept": "application/json" }
1948
1942
  });
1943
+ if (response.ok) {
1944
+ const data = await response.json();
1945
+ const txCount = (data.chain_stats?.tx_count || 0) + (data.mempool_stats?.tx_count || 0);
1946
+ if (txCount > 0 || isMainnet) {
1947
+ const chainFunded = data.chain_stats?.funded_txo_sum || 0;
1948
+ const chainSpent = data.chain_stats?.spent_txo_sum || 0;
1949
+ const mempoolFunded = data.mempool_stats?.funded_txo_sum || 0;
1950
+ const mempoolSpent = data.mempool_stats?.spent_txo_sum || 0;
1951
+ const satoshis = chainFunded - chainSpent + (mempoolFunded - mempoolSpent);
1952
+ balance = (satoshis / 1e8).toFixed(8);
1953
+ console.log(`Bitcoin balance for ${address}: ${balance} BTC (${satoshis} sats) via ${apiUrl}`);
1954
+ break;
1955
+ }
1956
+ console.log(`No transactions found on ${apiUrl}, trying next...`);
1957
+ }
1958
+ } catch (error) {
1959
+ console.warn(`Failed to fetch from ${apiUrl}:`, error);
1949
1960
  }
1950
- if (response.ok) {
1951
- const data = await response.json();
1952
- const chainFunded = data.chain_stats?.funded_txo_sum || 0;
1953
- const chainSpent = data.chain_stats?.spent_txo_sum || 0;
1954
- const mempoolFunded = data.mempool_stats?.funded_txo_sum || 0;
1955
- const mempoolSpent = data.mempool_stats?.spent_txo_sum || 0;
1956
- const satoshis = chainFunded - chainSpent + (mempoolFunded - mempoolSpent);
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}`);
1961
- }
1962
- } catch (error) {
1963
- console.warn(`Failed to fetch ${chain} balance:`, error);
1964
1961
  }
1965
1962
  } else if (chain === "solana") {
1966
1963
  const rpcUrl = this.config.network === "mainnet" ? "https://api.mainnet-beta.solana.com" : "https://api.devnet.solana.com";
@@ -3053,6 +3050,12 @@ function useWalletManager(options = {}) {
3053
3050
  manager.deriveAllAddresses();
3054
3051
  }
3055
3052
  updateState();
3053
+ try {
3054
+ const balances = await manager.fetchAllBalances();
3055
+ setChainBalances(balances);
3056
+ } catch (err) {
3057
+ console.warn("Failed to fetch balances after create:", err);
3058
+ }
3056
3059
  return result;
3057
3060
  } catch (err) {
3058
3061
  const message = err instanceof Error ? err.message : "Failed to create wallet";
@@ -3077,6 +3080,12 @@ function useWalletManager(options = {}) {
3077
3080
  manager.deriveAllAddresses();
3078
3081
  }
3079
3082
  updateState();
3083
+ try {
3084
+ const balances = await manager.fetchAllBalances();
3085
+ setChainBalances(balances);
3086
+ } catch (err) {
3087
+ console.warn("Failed to fetch balances after import:", err);
3088
+ }
3080
3089
  } catch (err) {
3081
3090
  const message = err instanceof Error ? err.message : "Failed to import wallet";
3082
3091
  setError(message);
@@ -3100,6 +3109,12 @@ function useWalletManager(options = {}) {
3100
3109
  manager.deriveAllAddresses();
3101
3110
  }
3102
3111
  updateState();
3112
+ try {
3113
+ const balances = await manager.fetchAllBalances();
3114
+ setChainBalances(balances);
3115
+ } catch (err) {
3116
+ console.warn("Failed to fetch balances after unlock:", err);
3117
+ }
3103
3118
  } catch (err) {
3104
3119
  const message = err instanceof Error ? err.message : "Invalid password";
3105
3120
  setError(message);