mars-v2-frontend/hooks/mutations/useCreateCreditAccount.tsx
Linkie Link 27cdd1c954
Linter and prettier adjustments (#50)
* tidy: added eslintrc and prettierrc rules

* tidy: formated the files via ‚yarn format‘

* import sort improvements

* format script regex fix

* replace eslint import severity to warning

* remove staged file

Co-authored-by: Gustavo Mauricio <gustavo.mauricio58@gmail.com>
2022-11-09 10:04:06 +01:00

50 lines
1.4 KiB
TypeScript

import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-toastify'
import { contractAddresses } from 'config/contracts'
import useCreditManagerStore from 'stores/useCreditManagerStore'
import useWalletStore from 'stores/useWalletStore'
import { queryKeys } from 'types/query-keys-factory'
import { hardcodedFee } from 'utils/contants'
// 200000 gas used
const executeMsg = {
create_credit_account: {},
}
const useCreateCreditAccount = () => {
const signingClient = useWalletStore((s) => s.signingClient)
const setSelectedAccount = useCreditManagerStore((s) => s.actions.setSelectedAccount)
const address = useWalletStore((s) => s.address)
const queryClient = useQueryClient()
return useMutation(
async () =>
await signingClient?.execute(
address,
contractAddresses.creditManager,
executeMsg,
hardcodedFee,
),
{
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')
},
},
)
}
export default useCreateCreditAccount