mars-v2-frontend/hooks/mutations/useBorrowFunds.tsx
Gustavo Mauricio 18c7547c00
MP-1638: Repay (#35)
* added repay action percentage buffer

* moved repay amount calculation to RepayFunds component

* refetch interval added to credit account positions query

* feat: revamp borrow and repay components to modals

* remove references to deleted components

* cosmo signing client added to wallet store

* amount decimal normalized for borrow action

* amount decimals conversion lifted for borrow and repay

* refactor: moved hooks to mutations folder

* update button disable condition and min input value

* refactor: reset modal states when reopened

* react-number-format dependency and borrow/repay modals revamp

* renamed borrow modal state variable

* style: borrow table ui revamp
2022-10-28 12:14:14 +01:00

89 lines
2.3 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { useMemo } from 'react'
import { toast } from 'react-toastify'
import useWalletStore from 'stores/useWalletStore'
import { contractAddresses } from 'config/contracts'
import { hardcodedFee } from 'utils/contants'
import useCreditManagerStore from 'stores/useCreditManagerStore'
import { queryKeys } from 'types/query-keys-factory'
const useBorrowFunds = (
amount: number,
denom: string,
withdraw = false,
options: Omit<UseMutationOptions, 'onError'>
) => {
const signingClient = useWalletStore((s) => s.signingClient)
const selectedAccount = useCreditManagerStore((s) => s.selectedAccount)
const address = useWalletStore((s) => s.address)
const queryClient = useQueryClient()
const executeMsg = useMemo(() => {
if (!withdraw) {
return {
update_credit_account: {
account_id: selectedAccount,
actions: [
{
borrow: {
denom: denom,
amount: String(amount),
},
},
],
},
}
}
return {
update_credit_account: {
account_id: selectedAccount,
actions: [
{
borrow: {
denom: denom,
amount: String(amount),
},
},
{
withdraw: {
denom: denom,
amount: String(amount),
},
},
],
},
}
}, [withdraw, selectedAccount, denom, amount])
return useMutation(
async () =>
await signingClient?.execute(
address,
contractAddresses.creditManager,
executeMsg,
hardcodedFee
),
{
onSettled: () => {
queryClient.invalidateQueries(queryKeys.creditAccountsPositions(selectedAccount ?? ''))
queryClient.invalidateQueries(queryKeys.redbankBalances())
// if withdrawing to wallet, need to explicility invalidate balances queries
if (withdraw) {
queryClient.invalidateQueries(queryKeys.tokenBalance(address, denom))
queryClient.invalidateQueries(queryKeys.allBalances(address))
}
},
onError: (err: Error) => {
toast.error(err.message)
},
...options,
}
)
}
export default useBorrowFunds