mars-v2-frontend/hooks/mutations/useTradeAsset.tsx
Linkie Link c4f8f4eab0
Mp 1757 wallet connect (#66)
* MP-1757: implemented the WalletProvider and connect buttons

* tidy: tidy up the search

* MP-1691: moved modals outside of the DOM

* MP-1691: changed CreditManager into AccountDetails

* fix: fixed the naming

* MP-1691: UX approvements

* MP-1691: global confirm and delete modal added

* fix: merged the credit-account and wallet branch

* MP-1757: added the status store

* fix: updated the store interaction

* MP-1757: major cleanup of stores

* tidy: format
2022-12-08 21:14:38 +01:00

78 lines
2.1 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { useMemo } from 'react'
import { toast } from 'react-toastify'
import { useAccountDetailsStore, useWalletStore } from 'stores'
import { Action } from 'types/generated/mars-credit-manager/MarsCreditManager.types'
import { queryKeys } from 'types/query-keys-factory'
import { hardcodedFee } from 'utils/contants'
const useTradeAsset = (
amount: number,
borrowAmount: number,
depositAmount: number,
tokenIn: string,
tokenOut: string,
slippage: number,
options?: Omit<UseMutationOptions, 'onError'>,
) => {
const creditManagerClient = useWalletStore((s) => s.clients.creditManager)
const selectedAccount = useAccountDetailsStore((s) => s.selectedAccount ?? '')
const queryClient = useQueryClient()
// actions need to be executed in order deposit -> borrow -> swap
// first two are optional
const actions = useMemo(() => {
const actionsBase = [
{
swap_exact_in: {
coin_in: { amount: String(amount), denom: tokenIn },
denom_out: tokenOut,
slippage: String(slippage),
},
},
] as Action[]
if (borrowAmount > 0) {
actionsBase.unshift({
borrow: {
denom: tokenIn,
amount: String(borrowAmount),
},
})
}
if (depositAmount > 0) {
actionsBase.unshift({
deposit: {
denom: tokenIn,
amount: String(depositAmount),
},
})
}
return actionsBase
}, [amount, tokenIn, tokenOut, slippage, borrowAmount, depositAmount])
return useMutation(
async () =>
await creditManagerClient?.updateCreditAccount(
{ accountId: selectedAccount, actions },
hardcodedFee,
),
{
onSettled: () => {
queryClient.invalidateQueries(queryKeys.creditAccountsPositions(selectedAccount))
queryClient.invalidateQueries(queryKeys.redbankBalances())
},
onError: (err: Error) => {
toast.error(err.message)
},
...options,
},
)
}
export default useTradeAsset