mars-v2-frontend/hooks/useDeleteCreditAccount.tsx
Gustavo Mauricio f709c12da2
Tooling improvements and minor refactor (#19)
* refactor: store selector callbacks less verbose

* chore: prettier tailwind plugin added and respective formatting

* disable metamask connection button
2022-09-30 13:50:16 +01:00

56 lines
1.5 KiB
TypeScript

import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { useEffect, useState } from 'react'
import { toast } from 'react-toastify'
import useWalletStore from 'stores/useWalletStore'
import { chain } from 'utils/chains'
import { contractAddresses } from 'config/contracts'
import { hardcodedFee } from 'utils/contants'
import { queryKeys } from 'types/query-keys-factory'
const useCreateCreditAccount = (accountId: string) => {
const [signingClient, setSigningClient] = useState<SigningCosmWasmClient>()
const address = useWalletStore((s) => s.address)
const queryClient = useQueryClient()
useEffect(() => {
;(async () => {
if (!window.keplr) return
const offlineSigner = window.keplr.getOfflineSigner(chain.chainId)
const clientInstance = await SigningCosmWasmClient.connectWithSigner(chain.rpc, offlineSigner)
setSigningClient(clientInstance)
})()
}, [address])
return useMutation(
async () =>
await signingClient?.execute(
address,
contractAddresses.accountNft,
{
burn: {
token_id: accountId,
},
},
hardcodedFee
),
{
onSettled: () => {
queryClient.invalidateQueries(queryKeys.creditAccounts(address))
},
onError: (err: Error) => {
toast.error(err.message)
},
onSuccess: () => {
toast.success('Credit Account Deleted')
},
}
)
}
export default useCreateCreditAccount