Skip to content

Commit

Permalink
chore: use market data service for STX balances
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-watters committed Nov 19, 2024
1 parent 8bb0059 commit 25bc879
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
10 changes: 7 additions & 3 deletions apps/mobile/src/queries/balance/stacks-balance.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import {
createGetStacksAccountBalanceQueryOptions,
createStxCryptoAssetBalance,
createStxMoney,
useCryptoCurrencyMarketDataMeanAverage,
useCurrentNetworkState,
useStacksClient,
} from '@leather.io/query';
import { baseCurrencyAmountInQuote, createMoney, sumMoney } from '@leather.io/utils';

import { useStxMarketDataQuery } from '../market-data/stx-market-data.query';

interface StxBalances {
totalStxBalance: Money;
}
Expand Down Expand Up @@ -67,7 +68,10 @@ function useStxBalancesQueries(addresses: string[]) {

export function useStxBalance(addresses: string[]) {
const { totalStxBalance } = useGetStxBalanceByAddresses(addresses);
const stxMarketData = useCryptoCurrencyMarketDataMeanAverage('STX');
const stxBalanceUsd = baseCurrencyAmountInQuote(totalStxBalance, stxMarketData);

const { data: stxMarketData } = useStxMarketDataQuery();
const stxBalanceUsd = stxMarketData
? baseCurrencyAmountInQuote(totalStxBalance, stxMarketData)
: createMoney(0, 'USD');
return { availableBalance: totalStxBalance, fiatBalance: stxBalanceUsd };
}
21 changes: 21 additions & 0 deletions apps/mobile/src/queries/market-data/stx-market-data.query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { QueryFunctionContext, useQuery } from '@tanstack/react-query';

import { getMarketDataService } from '@leather.io/services';
import { oneMinInMs } from '@leather.io/utils';

export function createStxMarketDataQueryOptions() {
return {
queryKey: ['market-data-service-get-stx-market-data'],
queryFn: ({ signal }: QueryFunctionContext) => getMarketDataService().getStxMarketData(signal),
refetchOnReconnect: false,
refetchOnWindowFocus: false,
refetchOnMount: false,
retryOnMount: false,
staleTime: oneMinInMs,
gcTime: oneMinInMs,
} as const;
}

export function useStxMarketDataQuery() {
return useQuery(createStxMarketDataQueryOptions());
}

0 comments on commit 25bc879

Please sign in to comment.