Skip to content

Commit

Permalink
fix: btcscan api url & apiUrl cached item
Browse files Browse the repository at this point in the history
  • Loading branch information
soumyaRouterP committed Nov 11, 2024
1 parent 872f757 commit 944c74e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
47 changes: 25 additions & 22 deletions packages/react/src/actions/bitcoin/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,36 @@ import { APIs, CACHE_EXPIRATION_TIME, tryAPI } from './bitcoinApiConfig.js';
export const getBalance = async (address: string): Promise<BalanceApiResponse> => {
const lastUsedApiData = localStorage.getItem('lastUsedApi-bitcoin');
if (lastUsedApiData) {
const { apiName, apiUrl, timestamp } = JSON.parse(lastUsedApiData);
const { apiName, timestamp } = JSON.parse(lastUsedApiData);

if (timestamp + CACHE_EXPIRATION_TIME > Date.now()) {
try {
let response: BalanceApiResponse;
switch (apiName) {
case 'btcscan': {
const apiResponse = await tryAPI<BtcScanBalanceResponse>(apiName, apiUrl);
response = { source: 'btcscan', data: apiResponse.data };
break;
}
case 'blockchain.info': {
const apiResponse = await tryAPI<BlockchainInfoBalanceResponse>(apiName, apiUrl);
response = { source: 'blockchain.info', data: apiResponse.data };
break;
}
case 'blockcypher': {
const apiResponse = await tryAPI<BlockcypherBalanceResponse>(apiName, apiUrl);
response = { source: 'blockcypher', data: apiResponse.data };
break;
}
default: {
throw new Error(`Unknown API: ${apiName}`);
const api = APIs.find((api) => api.name === apiName);
if (api) {
const apiUrl = api.url.balance(address);
let response: BalanceApiResponse;
switch (apiName) {
case 'btcscan': {
const apiResponse = await tryAPI<BtcScanBalanceResponse>(apiName, apiUrl);
response = { source: 'btcscan', data: apiResponse.data };
break;
}
case 'blockchain.info': {
const apiResponse = await tryAPI<BlockchainInfoBalanceResponse>(apiName, apiUrl);
response = { source: 'blockchain.info', data: apiResponse.data };
break;
}
case 'blockcypher': {
const apiResponse = await tryAPI<BlockcypherBalanceResponse>(apiName, apiUrl);
response = { source: 'blockcypher', data: apiResponse.data };
break;
}
default: {
throw new Error(`Unknown API: ${apiName}`);
}
}
return response;
}
return response;
} catch (error) {
console.warn(`Last used API ${apiName} failed, trying all APIs...`);
}
Expand Down Expand Up @@ -68,7 +72,6 @@ export const getBalance = async (address: string): Promise<BalanceApiResponse> =
'lastUsedApi-bitcoin',
JSON.stringify({
apiName: api.name,
apiUrl,
timestamp: Date.now(),
}),
);
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/actions/bitcoin/bitcoinApiConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const APIs: ApiConfig[] = [
name: 'btcscan',
url: {
balance: (address: string) => `https://btcscan.org/api/address/${address}`,
transaction: (txHash: string) => `https://btcscan.org/api//tx/${txHash}/status`,
transaction: (txHash: string) => `https://btcscan.org/api/tx/${txHash}/status`,
},
},
{
Expand Down

0 comments on commit 944c74e

Please sign in to comment.