-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
99 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { type UseQueryResult, useQueries } from '@tanstack/react-query'; | ||
|
||
import { useConfigRunesEnabled } from '@app/query/common/remote-config/remote-config.query'; | ||
import { useBitcoinClient } from '@app/store/common/api-clients.hooks'; | ||
import { useCurrentNetwork } from '@app/store/networks/networks.selectors'; | ||
|
||
import type { RuneTickerInfo } from '../bitcoin-client'; | ||
|
||
export function useGetRunesTickerInfoQuery(runeNames: string[]): UseQueryResult<RuneTickerInfo>[] { | ||
const client = useBitcoinClient(); | ||
const network = useCurrentNetwork(); | ||
const runesEnabled = useConfigRunesEnabled(); | ||
|
||
return useQueries({ | ||
queries: runeNames.map(runeName => { | ||
return { | ||
enabled: !!runeName && (network.chain.bitcoin.bitcoinNetwork === 'testnet' || runesEnabled), | ||
queryKey: ['runes-ticker-info', runeName], | ||
queryFn: () => | ||
client.BestinslotApi.getRunesTickerInfo(runeName, network.chain.bitcoin.bitcoinNetwork), | ||
}; | ||
}), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,38 @@ | ||
import { logger } from '@shared/logger'; | ||
import { createMoney } from '@shared/models/money.model'; | ||
import { isDefined } from '@shared/utils'; | ||
|
||
import type { RuneBalance, RuneToken } from '../bitcoin-client'; | ||
import type { RuneBalance, RuneTickerInfo, RuneToken } from '../bitcoin-client'; | ||
import { useGetRunesTickerInfoQuery } from './runes-ticker-info.query'; | ||
import { useGetRunesWalletBalancesByAddressesQuery } from './runes-wallet-balances.query'; | ||
|
||
function makeRuneToken(rune: RuneBalance): RuneToken { | ||
function makeRuneToken(runeBalance: RuneBalance, tickerInfo: RuneTickerInfo): RuneToken { | ||
return { | ||
...rune, | ||
balance: createMoney(Number(rune.total_balance), rune.rune_name, 0), | ||
...runeBalance, | ||
...tickerInfo, | ||
balance: createMoney( | ||
Number(runeBalance.total_balance), | ||
tickerInfo.rune_name, | ||
tickerInfo.decimals | ||
), | ||
}; | ||
} | ||
|
||
export function useRuneTokens(addresses: string[]) { | ||
return useGetRunesWalletBalancesByAddressesQuery(addresses, { | ||
select: resp => resp.map(makeRuneToken), | ||
const runesBalances = useGetRunesWalletBalancesByAddressesQuery(addresses) | ||
.flatMap(query => query.data) | ||
.filter(isDefined); | ||
|
||
const runesTickerInfo = useGetRunesTickerInfoQuery(runesBalances.map(r => r.rune_name)) | ||
.flatMap(query => query.data) | ||
.filter(isDefined); | ||
|
||
return runesBalances.map(r => { | ||
const tickerInfo = runesTickerInfo.find(t => t.rune_name === r.rune_name); | ||
if (!tickerInfo) { | ||
logger.error('No ticker info found for Rune'); | ||
return; | ||
} | ||
return makeRuneToken(r, tickerInfo); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters