fix trigger price when changing tokens

This commit is contained in:
saml33 2023-09-07 23:59:24 +10:00
parent e4b3e95d8f
commit ba859597ef
3 changed files with 20 additions and 6 deletions

View File

@ -54,6 +54,7 @@ const priceToDisplayString = (price: number | Decimal | string): string => {
}
type LimitSwapFormProps = {
showTokenSelect: SwapFormTokenListType
setShowTokenSelect: Dispatch<SetStateAction<SwapFormTokenListType>>
}
@ -94,7 +95,10 @@ const getOrderTypeMultiplier = (
}
}
const LimitSwapForm = ({ setShowTokenSelect }: LimitSwapFormProps) => {
const LimitSwapForm = ({
showTokenSelect,
setShowTokenSelect,
}: LimitSwapFormProps) => {
const { t } = useTranslation(['common', 'swap', 'trade'])
const { mangoAccountAddress } = useMangoAccount()
const { ipAllowed, ipCountry } = useIpAddress()
@ -166,14 +170,21 @@ const LimitSwapForm = ({ setShowTokenSelect }: LimitSwapFormProps) => {
// set default trigger price
useEffect(() => {
if (!quotePrice || triggerPrice) return
if (!quotePrice || triggerPrice || showTokenSelect) return
const multiplier = getOrderTypeMultiplier(
OrderTypes.STOP_LOSS,
orderType,
flipPrices,
isReducingShort,
)
setTriggerPrice(priceToDisplayString(quotePrice * multiplier))
}, [flipPrices, quotePrice, triggerPrice, isReducingShort])
}, [
flipPrices,
isReducingShort,
orderType,
quotePrice,
showTokenSelect,
triggerPrice,
])
// flip trigger price and set amount out when chart direction is flipped
useLayoutEffect(() => {

View File

@ -178,7 +178,10 @@ const SwapForm = () => {
{swapOrLimit === 'swap' ? (
<MarketSwapForm setShowTokenSelect={setShowTokenSelect} />
) : (
<LimitSwapForm setShowTokenSelect={setShowTokenSelect} />
<LimitSwapForm
showTokenSelect={showTokenSelect}
setShowTokenSelect={setShowTokenSelect}
/>
)}
{inputBank ? (
<TokenVaultWarnings bank={inputBank} type="swap" />

View File

@ -295,7 +295,7 @@ const SwapFormTokenList = ({
</div>
<div className="flex justify-between rounded bg-th-bkg-2 p-2">
<p className="text-xs text-th-fgd-4">{t('token')}</p>
{type !== 'output' ? (
{!type?.includes('output') ? (
<p className="text-xs text-th-fgd-4">{t('max')}</p>
) : null}
</div>