mars-v2-frontend/hooks/useCreditAccounts.tsx
Gustavo Mauricio 3022ae9a6a
MP-2017: Deposit Funds and Account stats (#21)
* style: fund account font size adjustments

* client instance. contract addresses updates. prices hook added

* persist lend assets value for every credit account

* feat: account stats and semi circular progress

* minor code cleanup

* display borrowed assets interest rate

* fallback screen when no wallet is connected

* fix: hydration mismatch

* update osmosis testnet endpoints

* style: body text color

* coin interface imported from cosmos package

* risk calculation from ltv assets comment added

* svgr setup. inline svg extracted to Icons folder

* address removed from local storage. wallet store improvements

* rename setAddress action to connect

* yield page renamed to earn

* refactor: accountStats using BigNumber

* update contract addresses

* update hardcoded fee

* update market mocked values

* current leverage added to useAccountStats hook return

* leverage naming disambiguation

* debt positions labels color update. negative sign before values

* remove prefers-color-scheme media query

* update redbank mock data
2022-10-12 16:41:03 +01:00

52 lines
1.4 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { useMemo } from 'react'
import useWalletStore from 'stores/useWalletStore'
import { contractAddresses } from 'config/contracts'
import useCreditManagerStore from 'stores/useCreditManagerStore'
import { queryKeys } from 'types/query-keys-factory'
type Result = {
tokens: string[]
}
const useCreditAccounts = () => {
const address = useWalletStore((s) => s.address)
const client = useWalletStore((s) => s.client)
const selectedAccount = useCreditManagerStore((s) => s.selectedAccount)
const creditManagerActions = useCreditManagerStore((s) => s.actions)
const queryMsg = useMemo(() => {
return {
tokens: {
owner: address,
},
}
}, [address])
const result = useQuery<Result>(
queryKeys.creditAccounts(address),
async () => client?.queryContractSmart(contractAddresses.accountNft, queryMsg),
{
staleTime: Infinity,
enabled: !!address && !!client,
onSuccess: (data) => {
if (!data.tokens.includes(selectedAccount || '') && data.tokens.length > 0) {
creditManagerActions.setSelectedAccount(data.tokens[0])
}
},
}
)
return {
...result,
data: useMemo(() => {
if (!address) return []
return result?.data && result.data.tokens
}, [address, result?.data]),
}
}
export default useCreditAccounts