2022-10-28 11:14:14 +00:00
|
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
2022-09-29 19:21:31 +00:00
|
|
|
import { toast } from 'react-toastify'
|
|
|
|
|
|
|
|
import { contractAddresses } from 'config/contracts'
|
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
|
|
|
|
2022-10-25 10:31:36 +00:00
|
|
|
const useDepositCreditAccount = (
|
|
|
|
accountId: string,
|
|
|
|
denom: string,
|
|
|
|
amount: number,
|
|
|
|
options?: {
|
|
|
|
onSuccess?: () => void
|
2022-11-09 09:04:06 +00:00
|
|
|
},
|
2022-10-25 10:31:36 +00:00
|
|
|
) => {
|
2022-10-28 11:14:14 +00:00
|
|
|
const signingClient = useWalletStore((s) => s.signingClient)
|
2022-09-30 12:50:16 +00:00
|
|
|
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,
|
|
|
|
{
|
|
|
|
update_credit_account: {
|
|
|
|
account_id: accountId,
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
deposit: {
|
|
|
|
denom,
|
|
|
|
amount: String(amount),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
hardcodedFee,
|
|
|
|
undefined,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
denom,
|
|
|
|
amount: String(amount),
|
|
|
|
},
|
2022-11-09 09:04:06 +00:00
|
|
|
],
|
2022-09-29 19:21:31 +00:00
|
|
|
),
|
|
|
|
{
|
|
|
|
onError: (err: Error) => {
|
|
|
|
toast.error(err.message)
|
|
|
|
},
|
|
|
|
onSuccess: () => {
|
|
|
|
queryClient.invalidateQueries(queryKeys.allBalances(address))
|
|
|
|
queryClient.invalidateQueries(queryKeys.tokenBalance(address, denom))
|
|
|
|
queryClient.invalidateQueries(queryKeys.creditAccountsPositions(accountId))
|
|
|
|
|
2022-10-25 10:31:36 +00:00
|
|
|
options?.onSuccess && options.onSuccess()
|
2022-09-29 19:21:31 +00:00
|
|
|
},
|
2022-11-09 09:04:06 +00:00
|
|
|
},
|
2022-09-29 19:21:31 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default useDepositCreditAccount
|