mars-v2-frontend/hooks/useRedbankBalances.tsx
Gustavo Mauricio d22de166da
Borrow improvements (#29)
* update token prices and market data to match smart contract

* feat: redbank balances query and respective rendering on ui

* query key for rb balances and respective invalidations

* update contracts config

* fix: avoid returning negative max borrow amounts

* fix: added deposit action to repay execute message

* add minus sign before apy on debt positions

* consider market liquidity on max borrow calculation

* hive url added to chain config

* update hardcoded token decimals
2022-10-24 16:15:26 +01:00

60 lines
1.3 KiB
TypeScript

import { useMemo } from 'react'
import { useQuery } from '@tanstack/react-query'
import { Coin } from '@cosmjs/stargate'
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