2022-09-29 19:21:31 +00:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
|
|
import { toast } from 'react-toastify'
|
|
|
|
|
|
|
|
import { contractAddresses } from 'config/contracts'
|
|
|
|
import useCreditManagerStore from 'stores/useCreditManagerStore'
|
2022-11-09 09:04:06 +00:00
|
|
|
import useWalletStore from 'stores/useWalletStore'
|
2022-09-29 19:21:31 +00:00
|
|
|
import { queryKeys } from 'types/query-keys-factory'
|
2022-11-09 09:04:06 +00:00
|
|
|
import { hardcodedFee } from 'utils/contants'
|
2022-09-29 19:21:31 +00:00
|
|
|
|
|
|
|
// 200000 gas used
|
|
|
|
const executeMsg = {
|
|
|
|
create_credit_account: {},
|
|
|
|
}
|
|
|
|
|
|
|
|
const useCreateCreditAccount = () => {
|
2022-10-28 11:14:14 +00:00
|
|
|
const signingClient = useWalletStore((s) => s.signingClient)
|
2022-09-30 12:50:16 +00:00
|
|
|
const setSelectedAccount = useCreditManagerStore((s) => s.actions.setSelectedAccount)
|
|
|
|
const address = useWalletStore((s) => s.address)
|
2022-09-29 19:21:31 +00:00
|
|
|
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
|
|
|
|
return useMutation(
|
|
|
|
async () =>
|
|
|
|
await signingClient?.execute(
|
|
|
|
address,
|
|
|
|
contractAddresses.creditManager,
|
|
|
|
executeMsg,
|
2022-11-09 09:04:06 +00:00
|
|
|
hardcodedFee,
|
2022-09-29 19:21:31 +00:00
|
|
|
),
|
|
|
|
{
|
|
|
|
onSettled: () => {
|
|
|
|
queryClient.invalidateQueries(queryKeys.creditAccounts(address))
|
|
|
|
},
|
|
|
|
onError: (err: Error) => {
|
|
|
|
toast.error(err.message)
|
|
|
|
},
|
|
|
|
onSuccess: (data) => {
|
|
|
|
if (!data) return
|
|
|
|
|
|
|
|
// TODO: is there some better way to parse response to extract token id???
|
|
|
|
const createdID = data.logs[0].events[2].attributes[6].value
|
|
|
|
setSelectedAccount(createdID)
|
|
|
|
toast.success('New account created')
|
|
|
|
},
|
2022-11-09 09:04:06 +00:00
|
|
|
},
|
2022-09-29 19:21:31 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default useCreateCreditAccount
|