mars-v2-frontend/hooks/mutations/useTradeAsset.tsx
Gustavo Mauricio 1deba2059e
MP-1699: Trade on Margin Account (#52)
* update generated types

* added CRO to token info

* update contract addresses to match latest deployment

* feat: token prices fetched from oracle contract

* trade page initial commit

* trade asset action hook

* extract max swap amount logic into custom hook

* trade component ui adjustments

* trade container min-width and some styling improvements

* trade success message and loading indicator

* normalize naming conventions on trading

* max swap amount formula adjustments

* trade execute msg with borrow. code cleanup

* fix: click max update tokenOut amount. remove wallet from fund denom

* delay token amount decimal conversion. input formatting

* increase hardcoded gas

* renamed max swappable amount hook

* display token prices and market information on landing page

* reset trade amounts when selected account change

* max trade amount cleanup and minor performance optimizations

* fix: liabilities value with 1 hour interest buffer for trade action

* add token symbol to wallet and account labels

* swap trade pairs icon and basic functionality

* remove unnecessary optional chaining. comment adjusted

* refactor useTokenPrices to build query dynamically on tokens data

* extracted trade container and respective functionality into separate file

* fix: properly calculate positions after full swap

* mp-1218: trading using wallet
2022-11-22 10:14:12 +01:00

79 lines
2.1 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { useMemo } from 'react'
import { toast } from 'react-toastify'
import useCreditManagerStore from 'stores/useCreditManagerStore'
import useWalletStore from 'stores/useWalletStore'
import { queryKeys } from 'types/query-keys-factory'
import { hardcodedFee } from 'utils/contants'
import { Action } from 'types/generated/mars-credit-manager/MarsCreditManager.types'
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 = useCreditManagerStore((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