2022-10-20 15:39:21 +00:00
|
|
|
import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate'
|
|
|
|
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
|
|
|
import { useEffect, useMemo, useState } from 'react'
|
|
|
|
import { toast } from 'react-toastify'
|
|
|
|
import BigNumber from 'bignumber.js'
|
|
|
|
|
|
|
|
import useWalletStore from 'stores/useWalletStore'
|
|
|
|
import { chain } from 'utils/chains'
|
|
|
|
import { contractAddresses } from 'config/contracts'
|
|
|
|
import { hardcodedFee } from 'utils/contants'
|
|
|
|
import useCreditManagerStore from 'stores/useCreditManagerStore'
|
|
|
|
import { queryKeys } from 'types/query-keys-factory'
|
2022-10-24 15:15:26 +00:00
|
|
|
import { getTokenDecimals } from 'utils/tokens'
|
2022-10-20 15:39:21 +00:00
|
|
|
|
|
|
|
const useRepayFunds = (
|
|
|
|
amount: string | number,
|
|
|
|
denom: string,
|
|
|
|
options: Omit<UseMutationOptions, 'onError'>
|
|
|
|
) => {
|
|
|
|
const [signingClient, setSigningClient] = useState<SigningCosmWasmClient>()
|
|
|
|
|
|
|
|
const selectedAccount = useCreditManagerStore((s) => s.selectedAccount)
|
|
|
|
const address = useWalletStore((s) => s.address)
|
|
|
|
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
;(async () => {
|
|
|
|
if (!window.keplr) return
|
|
|
|
|
|
|
|
const offlineSigner = window.keplr.getOfflineSigner(chain.chainId)
|
|
|
|
const clientInstance = await SigningCosmWasmClient.connectWithSigner(chain.rpc, offlineSigner)
|
|
|
|
|
|
|
|
setSigningClient(clientInstance)
|
|
|
|
})()
|
|
|
|
}, [address])
|
|
|
|
|
2022-10-24 15:15:26 +00:00
|
|
|
const tokenDecimals = getTokenDecimals(denom)
|
|
|
|
|
2022-10-20 15:39:21 +00:00
|
|
|
const executeMsg = useMemo(() => {
|
|
|
|
return {
|
|
|
|
update_credit_account: {
|
|
|
|
account_id: selectedAccount,
|
|
|
|
actions: [
|
2022-10-24 15:15:26 +00:00
|
|
|
{
|
|
|
|
deposit: {
|
|
|
|
denom: denom,
|
|
|
|
amount: BigNumber(amount)
|
|
|
|
.times(10 ** tokenDecimals)
|
|
|
|
.toString(),
|
|
|
|
},
|
|
|
|
},
|
2022-10-20 15:39:21 +00:00
|
|
|
{
|
|
|
|
repay: {
|
|
|
|
denom: denom,
|
|
|
|
amount: BigNumber(amount)
|
2022-10-24 15:15:26 +00:00
|
|
|
.times(10 ** tokenDecimals)
|
2022-10-20 15:39:21 +00:00
|
|
|
.toString(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
2022-10-24 15:15:26 +00:00
|
|
|
}, [amount, denom, selectedAccount, tokenDecimals])
|
2022-10-20 15:39:21 +00:00
|
|
|
|
|
|
|
return useMutation(
|
|
|
|
async () =>
|
|
|
|
await signingClient?.execute(
|
|
|
|
address,
|
|
|
|
contractAddresses.creditManager,
|
|
|
|
executeMsg,
|
2022-10-24 15:15:26 +00:00
|
|
|
hardcodedFee,
|
|
|
|
undefined,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
denom,
|
|
|
|
amount: BigNumber(amount)
|
|
|
|
.times(10 ** tokenDecimals)
|
|
|
|
.toString(),
|
|
|
|
},
|
|
|
|
]
|
2022-10-20 15:39:21 +00:00
|
|
|
),
|
|
|
|
{
|
|
|
|
onSettled: () => {
|
|
|
|
queryClient.invalidateQueries(queryKeys.creditAccountsPositions(selectedAccount ?? ''))
|
|
|
|
queryClient.invalidateQueries(queryKeys.tokenBalance(address, denom))
|
|
|
|
queryClient.invalidateQueries(queryKeys.allBalances(address))
|
2022-10-24 15:15:26 +00:00
|
|
|
queryClient.invalidateQueries(queryKeys.redbankBalances())
|
2022-10-20 15:39:21 +00:00
|
|
|
},
|
|
|
|
onError: (err: Error) => {
|
|
|
|
toast.error(err.message)
|
|
|
|
},
|
|
|
|
...options,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default useRepayFunds
|