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 useCreditManagerStore from 'stores/useCreditManagerStore'
|
|
|
|
import { queryKeys } from 'types/query-keys-factory'
|
|
|
|
|
|
|
|
type Result = {
|
|
|
|
tokens: string[]
|
|
|
|
}
|
|
|
|
|
|
|
|
const useCreditAccounts = () => {
|
|
|
|
const [signingClient, setSigningClient] = useState<SigningCosmWasmClient>()
|
2022-09-30 12:50:16 +00:00
|
|
|
const address = useWalletStore((s) => s.address)
|
|
|
|
const selectedAccount = useCreditManagerStore((s) => s.selectedAccount)
|
|
|
|
const creditManagerActions = useCreditManagerStore((s) => s.actions)
|
2022-09-29 19:21:31 +00:00
|
|
|
|
|
|
|
const queryMsg = useMemo(() => {
|
|
|
|
return {
|
|
|
|
tokens: {
|
|
|
|
owner: address,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}, [address])
|
|
|
|
|
|
|
|
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.creditAccounts(address),
|
|
|
|
async () => signingClient?.queryContractSmart(contractAddresses.accountNft, queryMsg),
|
|
|
|
{
|
|
|
|
enabled: !!address && !!signingClient,
|
|
|
|
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
|