mars-v2-frontend/hooks/useCreditAccountPositions.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

57 lines
1.2 KiB
TypeScript

import { Coin } from '@cosmjs/stargate'
import { useQuery } from '@tanstack/react-query'
import { useMemo } from 'react'
import { contractAddresses } from 'config/contracts'
import useWalletStore from 'stores/useWalletStore'
import { queryKeys } from 'types/query-keys-factory'
interface DebtAmount {
amount: string
denom: string
shares: string
}
interface VaultPosition {
locked: string
unlocked: string
}
interface Result {
account_id: string
coins: Coin[]
debts: DebtAmount[]
vaults: VaultPosition[]
}
const useCreditAccountPositions = (accountId: string) => {
const address = useWalletStore((s) => s.address)
const client = useWalletStore((s) => s.client)
const result = useQuery<Result>(
queryKeys.creditAccountsPositions(accountId),
async () =>
client?.queryContractSmart(contractAddresses.creditManager, {
positions: {
account_id: accountId,
},
}),
{
enabled: !!address && !!client,
refetchInterval: 30000,
staleTime: Infinity,
},
)
return {
...result,
data: useMemo(() => {
if (!address) return
return result?.data && { ...result.data }
}, [result.data, address]),
}
}
export default useCreditAccountPositions