2022-10-20 15:39:21 +00:00
|
|
|
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
2022-10-28 11:14:14 +00:00
|
|
|
import { useMemo } from 'react'
|
2022-10-20 15:39:21 +00:00
|
|
|
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 = (
|
2022-10-28 11:14:14 +00:00
|
|
|
amount: number,
|
2022-10-20 15:39:21 +00:00
|
|
|
denom: string,
|
|
|
|
withdraw = false,
|
|
|
|
options: Omit<UseMutationOptions, 'onError'>
|
|
|
|
) => {
|
2022-10-28 11:14:14 +00:00
|
|
|
const signingClient = useWalletStore((s) => s.signingClient)
|
2022-10-20 15:39:21 +00:00
|
|
|
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,
|
2022-10-28 11:14:14 +00:00
|
|
|
amount: String(amount),
|
2022-10-20 15:39:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
update_credit_account: {
|
|
|
|
account_id: selectedAccount,
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
borrow: {
|
|
|
|
denom: denom,
|
2022-10-28 11:14:14 +00:00
|
|
|
amount: String(amount),
|
2022-10-20 15:39:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
withdraw: {
|
|
|
|
denom: denom,
|
2022-10-28 11:14:14 +00:00
|
|
|
amount: String(amount),
|
2022-10-20 15:39:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
2022-10-24 15:15:26 +00:00
|
|
|
}, [withdraw, selectedAccount, denom, amount])
|
2022-10-20 15:39:21 +00:00
|
|
|
|
|
|
|
return useMutation(
|
|
|
|
async () =>
|
|
|
|
await signingClient?.execute(
|
|
|
|
address,
|
|
|
|
contractAddresses.creditManager,
|
|
|
|
executeMsg,
|
|
|
|
hardcodedFee
|
|
|
|
),
|
|
|
|
{
|
|
|
|
onSettled: () => {
|
|
|
|
queryClient.invalidateQueries(queryKeys.creditAccountsPositions(selectedAccount ?? ''))
|
2022-10-24 15:15:26 +00:00
|
|
|
queryClient.invalidateQueries(queryKeys.redbankBalances())
|
2022-10-20 15:39:21 +00:00
|
|
|
|
|
|
|
// 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
|