mars-v2-frontend/hooks/mutations/useCreateCreditAccount.tsx
Linkie Link c4f8f4eab0
Mp 1757 wallet connect (#66)
* MP-1757: implemented the WalletProvider and connect buttons

* tidy: tidy up the search

* MP-1691: moved modals outside of the DOM

* MP-1691: changed CreditManager into AccountDetails

* fix: fixed the naming

* MP-1691: UX approvements

* MP-1691: global confirm and delete modal added

* fix: merged the credit-account and wallet branch

* MP-1757: added the status store

* fix: updated the store interaction

* MP-1757: major cleanup of stores

* tidy: format
2022-12-08 21:14:38 +01:00

48 lines
1.3 KiB
TypeScript

import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-toastify'
import { contractAddresses } from 'config/contracts'
import { useAccountDetailsStore, useWalletStore } from 'stores'
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 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
useAccountDetailsStore.setState({ selectedAccount: createdID })
toast.success('New account created')
},
},
)
}
export default useCreateCreditAccount