From 059f75d540a8e7611e6c63e9e45c48e8ab7deb0e Mon Sep 17 00:00:00 2001 From: Fara Woolf Date: Mon, 15 Apr 2024 10:42:18 -0500 Subject: [PATCH] refactor: add ns addresses for runes balances --- src/app/components/loaders/runes-loader.tsx | 10 +++++--- .../bitcoin-fungible-tokens-asset-list.tsx | 2 +- .../runes/runes-wallet-balances.query.ts | 25 ++++++++++++------- src/app/query/bitcoin/runes/runes.hooks.ts | 23 +++++++++-------- src/shared/constants.ts | 4 +-- 5 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/app/components/loaders/runes-loader.tsx b/src/app/components/loaders/runes-loader.tsx index 0d5e985fd6b..333be639ba7 100644 --- a/src/app/components/loaders/runes-loader.tsx +++ b/src/app/components/loaders/runes-loader.tsx @@ -1,11 +1,15 @@ +import { isDefined } from '@shared/utils'; + import type { RuneToken } from '@app/query/bitcoin/bitcoin-client'; import { useRuneTokens } from '@app/query/bitcoin/runes/runes.hooks'; interface RunesLoaderProps { - address: string; + addresses: string[]; children(runes: RuneToken[]): React.ReactNode; } -export function RunesLoader({ address, children }: RunesLoaderProps) { - const { data: runes = [] } = useRuneTokens(address); +export function RunesLoader({ addresses, children }: RunesLoaderProps) { + const runes = useRuneTokens(addresses) + .flatMap(query => query.data) + .filter(isDefined); return children(runes); } diff --git a/src/app/features/asset-list/components/bitcoin-fungible-tokens-asset-list.tsx b/src/app/features/asset-list/components/bitcoin-fungible-tokens-asset-list.tsx index 8f0edcc0b45..4acf6b79c35 100644 --- a/src/app/features/asset-list/components/bitcoin-fungible-tokens-asset-list.tsx +++ b/src/app/features/asset-list/components/bitcoin-fungible-tokens-asset-list.tsx @@ -21,7 +21,7 @@ export function BitcoinFungibleTokenAssetList({ {src20Tokens => } - + {runes => } diff --git a/src/app/query/bitcoin/runes/runes-wallet-balances.query.ts b/src/app/query/bitcoin/runes/runes-wallet-balances.query.ts index f4837de55c3..c501100df88 100644 --- a/src/app/query/bitcoin/runes/runes-wallet-balances.query.ts +++ b/src/app/query/bitcoin/runes/runes-wallet-balances.query.ts @@ -1,4 +1,4 @@ -import { useQuery } from '@tanstack/react-query'; +import { useQueries } from '@tanstack/react-query'; import { useConfigRunesEnabled } from '@app/query/common/remote-config/remote-config.query'; import type { AppUseQueryConfig } from '@app/query/query-config'; @@ -7,19 +7,26 @@ import { useCurrentNetwork } from '@app/store/networks/networks.selectors'; import type { RuneBalance } from '../bitcoin-client'; -export function useGetRunesWalletBalancesQuery( - address: string, +export function useGetRunesWalletBalancesByAddressesQuery( + addresses: string[], options?: AppUseQueryConfig ) { const client = useBitcoinClient(); const network = useCurrentNetwork(); const runesEnabled = useConfigRunesEnabled(); - return useQuery({ - enabled: !!address && (network.chain.bitcoin.bitcoinNetwork === 'testnet' || runesEnabled), - queryKey: ['runes-wallet-balances', address], - queryFn: () => - client.BestinslotApi.getRunesWalletBalances(address, network.chain.bitcoin.bitcoinNetwork), - ...options, + return useQueries({ + queries: addresses.map(address => { + return { + enabled: !!address && (network.chain.bitcoin.bitcoinNetwork === 'testnet' || runesEnabled), + queryKey: ['runes-wallet-balances', address], + queryFn: () => + client.BestinslotApi.getRunesWalletBalances( + address, + network.chain.bitcoin.bitcoinNetwork + ), + ...options, + }; + }), }); } diff --git a/src/app/query/bitcoin/runes/runes.hooks.ts b/src/app/query/bitcoin/runes/runes.hooks.ts index 0d614b94607..1d74ee1b94c 100644 --- a/src/app/query/bitcoin/runes/runes.hooks.ts +++ b/src/app/query/bitcoin/runes/runes.hooks.ts @@ -1,16 +1,17 @@ import { createMoney } from '@shared/models/money.model'; -import type { RuneToken } from '../bitcoin-client'; -import { useGetRunesWalletBalancesQuery } from './runes-wallet-balances.query'; +import type { RuneBalance, RuneToken } from '../bitcoin-client'; +import { useGetRunesWalletBalancesByAddressesQuery } from './runes-wallet-balances.query'; -export function useRuneTokens(address: string) { - return useGetRunesWalletBalancesQuery(address, { - select: resp => - resp.map(rune => { - return { - ...rune, - balance: createMoney(Number(rune.total_balance), rune.rune_name, 0), - } as RuneToken; - }), +function makeRuneToken(rune: RuneBalance): RuneToken { + return { + ...rune, + balance: createMoney(Number(rune.total_balance), rune.rune_name, 0), + }; +} + +export function useRuneTokens(addresses: string[]) { + return useGetRunesWalletBalancesByAddressesQuery(addresses, { + select: resp => resp.map(makeRuneToken), }); } diff --git a/src/shared/constants.ts b/src/shared/constants.ts index 071eda6f524..56e50f59cf1 100644 --- a/src/shared/constants.ts +++ b/src/shared/constants.ts @@ -75,8 +75,8 @@ export interface NetworkConfiguration { }; } -export const BESTINSLOT_API_BASE_URL_MAINNET = 'https://api.bestinslot.xyz/v3'; -export const BESTINSLOT_API_BASE_URL_TESTNET = 'https://testnet.api.bestinslot.xyz/v3'; +export const BESTINSLOT_API_BASE_URL_MAINNET = 'https://leatherapi.bestinslot.xyz/v3'; +export const BESTINSLOT_API_BASE_URL_TESTNET = 'https://testnet.leatherapi.bestinslot.xyz/v3'; export const HIRO_API_BASE_URL_MAINNET = 'https://api.hiro.so'; export const HIRO_API_BASE_URL_TESTNET = 'https://api.testnet.hiro.so';