Skip to content

Commit

Permalink
refactor: add ns addresses for runes balances
Browse files Browse the repository at this point in the history
  • Loading branch information
fbwoolf committed Apr 15, 2024
1 parent 4e1de2c commit 059f75d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
10 changes: 7 additions & 3 deletions src/app/components/loaders/runes-loader.tsx
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function BitcoinFungibleTokenAssetList({
<Src20TokensLoader address={btcAddressNativeSegwit}>
{src20Tokens => <Src20TokenAssetList src20Tokens={src20Tokens} />}
</Src20TokensLoader>
<RunesLoader address={btcAddressTaproot}>
<RunesLoader addresses={[btcAddressNativeSegwit, btcAddressTaproot]}>
{runes => <RunesAssetList runes={runes} />}
</RunesLoader>
</>
Expand Down
25 changes: 16 additions & 9 deletions src/app/query/bitcoin/runes/runes-wallet-balances.query.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -7,19 +7,26 @@ import { useCurrentNetwork } from '@app/store/networks/networks.selectors';

import type { RuneBalance } from '../bitcoin-client';

export function useGetRunesWalletBalancesQuery<T extends unknown = RuneBalance[]>(
address: string,
export function useGetRunesWalletBalancesByAddressesQuery<T extends unknown = RuneBalance[]>(
addresses: string[],
options?: AppUseQueryConfig<RuneBalance[], T>
) {
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,
};
}),
});
}
23 changes: 12 additions & 11 deletions src/app/query/bitcoin/runes/runes.hooks.ts
Original file line number Diff line number Diff line change
@@ -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),
});
}
4 changes: 2 additions & 2 deletions src/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 059f75d

Please sign in to comment.