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