From 39e254bc701d3e448fa3aaea815a6e632ddc6920 Mon Sep 17 00:00:00 2001 From: alter-eggo Date: Fri, 8 Mar 2024 17:48:34 +0400 Subject: [PATCH] fix: fetch brc20 balance from hiro --- src/app/query/bitcoin/bitcoin-client.ts | 53 ++++++++++++++++--- .../ordinals/brc20/brc20-tokens.query.ts | 16 +++--- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/app/query/bitcoin/bitcoin-client.ts b/src/app/query/bitcoin/bitcoin-client.ts index 9ff0b71c6a7..5f4e5c31c35 100644 --- a/src/app/query/bitcoin/bitcoin-client.ts +++ b/src/app/query/bitcoin/bitcoin-client.ts @@ -1,5 +1,7 @@ import axios from 'axios'; +import { HIRO_API_BASE_URL_MAINNET } from '@shared/constants'; + class Configuration { constructor(public baseUrl: string) {} } @@ -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; } @@ -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( + `${this.url}/ordinals/v1/brc-20/balances/${address}` + ); + return resp.data; + } + + async getBrc20TickerData(ticker: string) { + const resp = await axios.get( + `${this.url}/ordinals/v1/brc-20/tokens?ticker=${ticker}` + ); + return resp.data; + } +} + class AddressApi { constructor(public configuration: Configuration) {} @@ -250,6 +285,7 @@ export class BitcoinClient { feeEstimatesApi: FeeEstimatesApi; transactionsApi: TransactionsApi; BestinslotApi: BestinslotApi; + HiroApi: HiroApi; constructor(basePath: string) { this.configuration = new Configuration(basePath); @@ -257,5 +293,6 @@ export class BitcoinClient { this.feeEstimatesApi = new FeeEstimatesApi(this.configuration); this.transactionsApi = new TransactionsApi(this.configuration); this.BestinslotApi = new BestinslotApi(this.configuration); + this.HiroApi = new HiroApi(); } } diff --git a/src/app/query/bitcoin/ordinals/brc20/brc20-tokens.query.ts b/src/app/query/bitcoin/ordinals/brc20/brc20-tokens.query.ts index de6a8f9c150..c16468beaa3 100644 --- a/src/app/query/bitcoin/ordinals/brc20/brc20-tokens.query.ts +++ b/src/app/query/bitcoin/ordinals/brc20/brc20-tokens.query.ts @@ -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(); @@ -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, }; });