Skip to content

Commit

Permalink
fix: fetch brc20 balance from hiro
Browse files Browse the repository at this point in the history
  • Loading branch information
alter-eggo committed Mar 8, 2024
1 parent 6702100 commit 39e254b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
53 changes: 45 additions & 8 deletions src/app/query/bitcoin/bitcoin-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import axios from 'axios';

import { HIRO_API_BASE_URL_MAINNET } from '@shared/constants';

class Configuration {
constructor(public baseUrl: string) {}
}
Expand Down Expand Up @@ -65,16 +67,17 @@ export interface Brc20Token extends Brc20TokenResponse {
}

interface Brc20TokenTicker {
decimals: number;
deploy_incr_number: number;
deploy_ts: string;
holder_count: number;
image_url: string | null;
limit_per_mint: string;
id: string;
number: number;
block_height: number;
tx_id: string;
address: string;
ticker: string;
max_supply: string;
mint_progress: number;
mint_limit: string;
decimals: number;
deploy_timestamp: number;
minted_supply: string;
ticker: string;
tx_count: number;
}

Expand Down Expand Up @@ -139,6 +142,38 @@ class BestinslotApi {
}
}

interface HiroApiBrc20AddressBalanceResponse {
limit: number;
offset: number;
total: number;
results: Brc20TokenResponse[];
}

interface HiroApiBrc20TickerResponse {
limit: number;
offset: number;
total: number;
results: Brc20TokenTicker[];
}

class HiroApi {
url = HIRO_API_BASE_URL_MAINNET;

async getBrc20Balance(address: string) {
const resp = await axios.get<HiroApiBrc20AddressBalanceResponse>(
`${this.url}/ordinals/v1/brc-20/balances/${address}`
);
return resp.data;
}

async getBrc20TickerData(ticker: string) {
const resp = await axios.get<HiroApiBrc20TickerResponse>(
`${this.url}/ordinals/v1/brc-20/tokens?ticker=${ticker}`
);
return resp.data;
}
}

class AddressApi {
constructor(public configuration: Configuration) {}

Expand Down Expand Up @@ -250,12 +285,14 @@ export class BitcoinClient {
feeEstimatesApi: FeeEstimatesApi;
transactionsApi: TransactionsApi;
BestinslotApi: BestinslotApi;
HiroApi: HiroApi;

constructor(basePath: string) {
this.configuration = new Configuration(basePath);
this.addressApi = new AddressApi(this.configuration);
this.feeEstimatesApi = new FeeEstimatesApi(this.configuration);
this.transactionsApi = new TransactionsApi(this.configuration);
this.BestinslotApi = new BestinslotApi(this.configuration);
this.HiroApi = new HiroApi();
}
}
16 changes: 9 additions & 7 deletions src/app/query/bitcoin/ordinals/brc20/brc20-tokens.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { useCurrentNetwork } from '@app/store/networks/networks.selectors';

import { Brc20Token } from '../../bitcoin-client';

const addressesSimultaneousFetchLimit = 5;
const stopSearchAfterNumberAddressesWithoutBrc20Tokens = 5;
const addressesSimultaneousFetchLimit = 3;
const stopSearchAfterNumberAddressesWithoutBrc20Tokens = 3;

export function useGetBrc20TokensQuery() {
const network = useCurrentNetwork();
Expand Down Expand Up @@ -51,16 +51,18 @@ export function useGetBrc20TokensQuery() {
}

const brc20TokensPromises = addressesData.map(async address => {
const brc20Tokens = await client.BestinslotApi.getBrc20Balance(address);
const brc20Tokens = await client.HiroApi.getBrc20Balance(address);

const tickerPromises = await Promise.all(
brc20Tokens.data.map(token => {
return client.BestinslotApi.getBrc20TickerData(token.ticker);
brc20Tokens.results.map(token => {
return client.HiroApi.getBrc20TickerData(token.ticker);
})
);
return brc20Tokens.data.map((token, index) => {

return brc20Tokens.results.map((token, index) => {
return {
...token,
decimals: tickerPromises[index].data.decimals,
decimals: tickerPromises[index].results[0].decimals,
holderAddress: address,
};
});
Expand Down

0 comments on commit 39e254b

Please sign in to comment.