2022-12-01 15:38:41 -08:00
|
|
|
import Tooltip from '@components/shared/Tooltip'
|
|
|
|
import mangoStore from '@store/mangoStore'
|
|
|
|
import useMarkPrice from 'hooks/useMarkPrice'
|
|
|
|
import useSelectedMarket from 'hooks/useSelectedMarket'
|
|
|
|
import { useTranslation } from 'next-i18next'
|
|
|
|
import { useMemo } from 'react'
|
2023-01-02 11:50:09 -08:00
|
|
|
// import { notify } from 'utils/notifications'
|
2022-12-01 15:38:41 -08:00
|
|
|
import { calculateSlippage } from 'utils/tradeForm'
|
|
|
|
|
|
|
|
const Slippage = () => {
|
|
|
|
const { t } = useTranslation('trade')
|
|
|
|
const markPrice = useMarkPrice()
|
|
|
|
const tradeForm = mangoStore((s) => s.tradeForm)
|
|
|
|
const { selectedMarket } = useSelectedMarket()
|
|
|
|
|
|
|
|
const slippage = useMemo(() => {
|
2022-12-08 11:58:54 -08:00
|
|
|
try {
|
|
|
|
if (tradeForm.tradeType === 'Market' && markPrice && selectedMarket) {
|
|
|
|
const orderbook = mangoStore.getState().selectedMarket.orderbook
|
|
|
|
return calculateSlippage(
|
|
|
|
orderbook,
|
|
|
|
Number(tradeForm.baseSize),
|
|
|
|
tradeForm.side,
|
2023-07-21 11:47:53 -07:00
|
|
|
markPrice,
|
2022-12-08 11:58:54 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2023-01-02 11:50:09 -08:00
|
|
|
console.error({ type: 'info', title: 'Unable to calculate slippage' })
|
|
|
|
return 100_000
|
2022-12-01 15:38:41 -08:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}, [tradeForm, markPrice, selectedMarket])
|
|
|
|
|
|
|
|
return slippage ? (
|
2022-12-13 18:23:01 -08:00
|
|
|
<div className="flex justify-between text-xs">
|
2022-12-01 15:38:41 -08:00
|
|
|
<Tooltip content={t('trade:tooltip-slippage')}>
|
2022-12-13 18:23:01 -08:00
|
|
|
<p className="tooltip-underline mr-4">{t('trade:est-slippage')}</p>
|
2022-12-01 15:38:41 -08:00
|
|
|
</Tooltip>
|
|
|
|
<p
|
2023-07-03 06:17:17 -07:00
|
|
|
className={`font-mono ${
|
2022-12-01 15:38:41 -08:00
|
|
|
slippage <= 1
|
|
|
|
? 'text-th-success'
|
|
|
|
: slippage <= 3
|
|
|
|
? 'text-th-warning'
|
|
|
|
: 'text-th-error'
|
|
|
|
}`}
|
|
|
|
>
|
2023-01-15 17:19:01 -08:00
|
|
|
{slippage === 100_000 ? 'Unavailable' : `${slippage.toFixed(2)}%`}
|
2022-12-01 15:38:41 -08:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
) : null
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Slippage
|