fix limit and trigger price decimals

This commit is contained in:
saml33 2023-08-01 15:13:12 +10:00
parent c35ccd54ae
commit 9013cd8722
1 changed files with 15 additions and 8 deletions

View File

@ -117,30 +117,37 @@ const LimitSwapForm = ({ setShowTokenSelect }: LimitSwapFormProps) => {
const initialQuotePrice = useMemo(() => { const initialQuotePrice = useMemo(() => {
if (!baseBank || !quoteBank) return if (!baseBank || !quoteBank) return
return baseBank.uiPrice / quoteBank.uiPrice return floorToDecimal(
baseBank.uiPrice / quoteBank.uiPrice,
quoteBank.mintDecimals,
)
}, [baseBank, quoteBank]) }, [baseBank, quoteBank])
// set default limit and trigger price // set default limit and trigger price
useEffect(() => { useEffect(() => {
if (!initialQuotePrice) return if (!initialQuotePrice) return
if (!triggerPrice) { if (!triggerPrice) {
setTriggerPrice((initialQuotePrice * 0.9).toString()) setTriggerPrice(
initialQuotePrice.mul(0.9).toFixed(quoteBank?.mintDecimals),
)
} }
if (!limitPrice) { if (!limitPrice) {
set((s) => { set((s) => {
s.swap.limitPrice = (initialQuotePrice * 0.8).toString() s.swap.limitPrice = initialQuotePrice
.mul(0.8)
.toFixed(quoteBank?.mintDecimals)
}) })
} }
}, [initialQuotePrice, limitPrice, triggerPrice]) }, [initialQuotePrice, limitPrice, quoteBank, triggerPrice])
const [limitPriceDifference, triggerPriceDifference] = useMemo(() => { const [limitPriceDifference, triggerPriceDifference] = useMemo(() => {
if (!initialQuotePrice) return [0, 0] if (!initialQuotePrice) return [0, 0]
const initialPrice = initialQuotePrice.toNumber()
const limitDifference = limitPrice const limitDifference = limitPrice
? ((parseFloat(limitPrice) - initialQuotePrice) / initialQuotePrice) * 100 ? ((parseFloat(limitPrice) - initialPrice) / initialPrice) * 100
: 0 : 0
const triggerDifference = triggerPrice const triggerDifference = triggerPrice
? ((parseFloat(triggerPrice) - initialQuotePrice) / initialQuotePrice) * ? ((parseFloat(triggerPrice) - initialPrice) / initialPrice) * 100
100
: 0 : 0
return [limitDifference, triggerDifference] return [limitDifference, triggerDifference]
}, [initialQuotePrice, limitPrice, triggerPrice]) }, [initialQuotePrice, limitPrice, triggerPrice])
@ -169,7 +176,7 @@ const LimitSwapForm = ({ setShowTokenSelect }: LimitSwapFormProps) => {
if ( if (
orderType.includes('stop') && orderType.includes('stop') &&
initialQuotePrice && initialQuotePrice &&
triggerPriceNumber > initialQuotePrice triggerPriceNumber > initialQuotePrice.toNumber()
) { ) {
invalidFields.triggerPrice = invalidFields.triggerPrice =
'Trigger price must be less than current price' 'Trigger price must be less than current price'