2022-09-29 19:21:31 +00:00
|
|
|
import { useQuery } from '@tanstack/react-query'
|
2022-10-12 15:41:03 +00:00
|
|
|
import { useMemo } from 'react'
|
|
|
|
import { Coin } from '@cosmjs/stargate'
|
2022-09-29 19:21:31 +00:00
|
|
|
|
|
|
|
import useWalletStore from 'stores/useWalletStore'
|
|
|
|
import { contractAddresses } from 'config/contracts'
|
|
|
|
import { queryKeys } from 'types/query-keys-factory'
|
|
|
|
|
2022-10-12 15:41:03 +00:00
|
|
|
interface DebtAmount {
|
2022-09-29 19:21:31 +00:00
|
|
|
amount: string
|
|
|
|
denom: string
|
|
|
|
shares: string
|
|
|
|
}
|
|
|
|
|
2022-10-12 15:41:03 +00:00
|
|
|
interface VaultPosition {
|
2022-09-29 19:21:31 +00:00
|
|
|
locked: string
|
|
|
|
unlocked: string
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Result {
|
|
|
|
account_id: string
|
2022-10-12 15:41:03 +00:00
|
|
|
coins: Coin[]
|
|
|
|
debts: DebtAmount[]
|
|
|
|
vaults: VaultPosition[]
|
2022-09-29 19:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const useCreditAccountPositions = (accountId: string) => {
|
2022-09-30 12:50:16 +00:00
|
|
|
const address = useWalletStore((s) => s.address)
|
2022-10-12 15:41:03 +00:00
|
|
|
const client = useWalletStore((s) => s.client)
|
2022-09-29 19:21:31 +00:00
|
|
|
|
|
|
|
const result = useQuery<Result>(
|
|
|
|
queryKeys.creditAccountsPositions(accountId),
|
|
|
|
async () =>
|
2022-10-12 15:41:03 +00:00
|
|
|
client?.queryContractSmart(contractAddresses.creditManager, {
|
2022-09-29 19:21:31 +00:00
|
|
|
positions: {
|
|
|
|
account_id: accountId,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
{
|
2022-10-12 15:41:03 +00:00
|
|
|
enabled: !!address && !!client,
|
2022-09-29 19:21:31 +00:00
|
|
|
staleTime: Infinity,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
...result,
|
|
|
|
data: useMemo(() => {
|
2022-10-12 15:41:03 +00:00
|
|
|
return result?.data && { ...result.data }
|
2022-09-29 19:21:31 +00:00
|
|
|
}, [result.data]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default useCreditAccountPositions
|