mars-v2-frontend/hooks/mutations/useRepayFunds.tsx
Gustavo Mauricio 56bfea8ed0
Update account stats on pending action (#51)
* feat: update account stats on pending action

* lift repay amount with buffer calculation to repay modal

* refactor navigation component to properly format account stats

* update repay funds hook using credit manager client

* consider market liquidity when calculating max withdraw amount

* refactor useMaxBorrowAmount hook to be consistent with withdraw

* format

* remove duplicated import statements
2022-11-09 13:47:36 +01:00

68 lines
1.8 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { useMemo } from 'react'
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'
const useRepayFunds = (
amount: number,
denom: string,
options: Omit<UseMutationOptions, 'onError'>,
) => {
const creditManagerClient = useWalletStore((s) => s.clients.creditManager)
const selectedAccount = useCreditManagerStore((s) => s.selectedAccount ?? '')
const address = useWalletStore((s) => s.address)
const queryClient = useQueryClient()
const actions = useMemo(() => {
return [
{
deposit: {
denom: denom,
amount: String(amount),
},
},
{
repay: {
denom: denom,
amount: String(amount),
},
},
]
}, [amount, denom])
return useMutation(
async () =>
await creditManagerClient?.updateCreditAccount(
{ accountId: selectedAccount, actions },
hardcodedFee,
undefined,
[
{
denom,
amount: String(amount),
},
],
),
{
onSettled: () => {
queryClient.invalidateQueries(queryKeys.creditAccountsPositions(selectedAccount))
queryClient.invalidateQueries(queryKeys.tokenBalance(address, denom))
queryClient.invalidateQueries(queryKeys.allBalances(address))
queryClient.invalidateQueries(queryKeys.redbankBalances())
},
onError: (err: Error) => {
toast.error(err.message)
},
...options,
},
)
}
export default useRepayFunds