2022-09-29 19:21:31 +00:00
|
|
|
import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate'
|
|
|
|
import { useQuery } from '@tanstack/react-query'
|
|
|
|
import { useEffect, useMemo, useState } from 'react'
|
|
|
|
|
|
|
|
import useWalletStore from 'stores/useWalletStore'
|
|
|
|
import { chain } from 'utils/chains'
|
|
|
|
import { contractAddresses } from 'config/contracts'
|
|
|
|
import { queryKeys } from 'types/query-keys-factory'
|
|
|
|
|
|
|
|
interface CoinValue {
|
|
|
|
amount: string
|
|
|
|
denom: string
|
|
|
|
price: string
|
|
|
|
value: string
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DebtSharesValue {
|
|
|
|
amount: string
|
|
|
|
denom: string
|
|
|
|
price: string
|
|
|
|
shares: string
|
|
|
|
value: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface VaultPosition {
|
|
|
|
locked: string
|
|
|
|
unlocked: string
|
|
|
|
}
|
|
|
|
|
|
|
|
interface VaultPositionWithAddr {
|
|
|
|
addr: string
|
|
|
|
position: VaultPosition
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Result {
|
|
|
|
account_id: string
|
|
|
|
coins: CoinValue[]
|
|
|
|
debt: DebtSharesValue[]
|
|
|
|
vault_positions: VaultPositionWithAddr[]
|
|
|
|
}
|
|
|
|
|
|
|
|
const useCreditAccountPositions = (accountId: string) => {
|
|
|
|
const [signingClient, setSigningClient] = useState<SigningCosmWasmClient>()
|
2022-09-30 12:50:16 +00:00
|
|
|
const address = useWalletStore((s) => s.address)
|
2022-09-29 19:21:31 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
;(async () => {
|
|
|
|
if (!window.keplr) return
|
|
|
|
|
|
|
|
const offlineSigner = window.keplr.getOfflineSigner(chain.chainId)
|
|
|
|
const clientInstance = await SigningCosmWasmClient.connectWithSigner(chain.rpc, offlineSigner)
|
|
|
|
|
|
|
|
setSigningClient(clientInstance)
|
|
|
|
})()
|
|
|
|
}, [address])
|
|
|
|
|
|
|
|
const result = useQuery<Result>(
|
|
|
|
queryKeys.creditAccountsPositions(accountId),
|
|
|
|
async () =>
|
|
|
|
signingClient?.queryContractSmart(contractAddresses.creditManager, {
|
|
|
|
positions: {
|
|
|
|
account_id: accountId,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
enabled: !!address && !!signingClient,
|
|
|
|
staleTime: Infinity,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
...result,
|
|
|
|
data: useMemo(() => {
|
|
|
|
return result?.data
|
|
|
|
}, [result.data]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default useCreditAccountPositions
|