mars-v2-frontend/hooks/useRedbankBalances.tsx
Linkie Link 27cdd1c954
Linter and prettier adjustments (#50)
* tidy: added eslintrc and prettierrc rules

* tidy: formated the files via ‚yarn format‘

* import sort improvements

* format script regex fix

* replace eslint import severity to warning

* remove staged file

Co-authored-by: Gustavo Mauricio <gustavo.mauricio58@gmail.com>
2022-11-09 10:04:06 +01:00

60 lines
1.3 KiB
TypeScript

import { Coin } from '@cosmjs/stargate'
import { useQuery } from '@tanstack/react-query'
import { useMemo } from 'react'
import { contractAddresses } from 'config/contracts'
import { queryKeys } from 'types/query-keys-factory'
import { chain } from 'utils/chains'
interface Result {
data: {
bank: {
balance: Coin[]
}
}
}
const fetchBalances = () => {
return fetch(chain.hive, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
query RedbankBalances {
bank {
balance(
address: "${contractAddresses.redBank}"
) {
amount
denom
}
}
}
`,
}),
}).then((res) => res.json())
}
const useRedbankBalances = () => {
const result = useQuery<Result>(queryKeys.redbankBalances(), fetchBalances)
return {
...result,
data: useMemo(() => {
if (!result.data) return
return result.data?.data.bank.balance.reduce(
(acc, coin) => ({
...acc,
[coin.denom]: coin.amount,
}),
{},
) as { [key in string]: string }
}, [result.data]),
}
}
export default useRedbankBalances