Skip to content

Commit

Permalink
fix: reset market rate when switching assets
Browse files Browse the repository at this point in the history
  • Loading branch information
woodenfurniture committed Nov 13, 2024
1 parent 64fd7ed commit 2f7e1ab
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,13 @@ export const LimitOrderInput = ({
} = useQuoteLimitOrderQuery(limitOrderQuoteParams)

const marketPriceBuyAsset = useMemo(() => {
if (!data) return '0'
// RTK query returns stale data when `skipToken` is used, so we need to handle that case here.
if (!data || limitOrderQuoteParams === skipToken) return '0'

return bnOrZero(fromBaseUnit(data.quote.buyAmount, buyAsset.precision))
.div(fromBaseUnit(data.quote.sellAmount, sellAsset.precision))
.toFixed()
}, [buyAsset.precision, data, sellAsset.precision])
}, [buyAsset.precision, data, sellAsset.precision, limitOrderQuoteParams])

// Reset the limit price when the market price changes.
// TODO: If we introduce polling of quotes, we will need to add logic inside `LimitOrderConfig` to
Expand Down Expand Up @@ -277,7 +279,7 @@ export const LimitOrderInput = ({
sellAmountUserCurrency={inputSellAmountUserCurrency}
sellAsset={sellAsset}
sellAccountId={sellAccountId}
handleSwitchAssets={switchAssets}
onSwitchAssets={switchAssets}
onChangeIsInputtingFiatSellAmount={setIsInputtingFiatSellAmount}
onChangeSellAmountCryptoPrecision={setSellAmountCryptoPrecision}
setSellAsset={setSellAsset}
Expand Down Expand Up @@ -314,13 +316,13 @@ export const LimitOrderInput = ({
sellAccountId,
sellAmountCryptoPrecision,
sellAsset,
setBuyAsset,
setBuyAccountId,
setBuyAsset,
setIsInputtingFiatSellAmount,
setLimitPriceBuyAsset,
setSellAccountId,
setSellAmountCryptoPrecision,
setSellAsset,
setSellAccountId,
switchAssets,
])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type SharedTradeInputBodyProps = {
sellAmountUserCurrency: string | undefined
sellAsset: Asset
sellAccountId: AccountId | undefined
handleSwitchAssets: () => void
onSwitchAssets: () => void
onChangeIsInputtingFiatSellAmount: (isInputtingFiatSellAmount: boolean) => void
onChangeSellAmountCryptoPrecision: (sellAmountCryptoPrecision: string) => void
setSellAsset: (asset: Asset) => void
Expand All @@ -53,7 +53,7 @@ export const SharedTradeInputBody = ({
sellAmountUserCurrency,
sellAsset,
sellAccountId,
handleSwitchAssets,
onSwitchAssets,
onChangeIsInputtingFiatSellAmount,
onChangeSellAmountCryptoPrecision,
setSellAsset,
Expand Down Expand Up @@ -163,7 +163,7 @@ export const SharedTradeInputBody = ({
justifyContent='center'
>
<IconButton
onClick={handleSwitchAssets}
onClick={onSwitchAssets}
isRound
size='sm'
position='relative'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ export const TradeInput = ({ isCompact, tradeInputRef, onChangeTab }: TradeInput
sellAsset={sellAsset}
sellAmountCryptoPrecision={sellAmountCryptoPrecision}
sellAmountUserCurrency={sellAmountUserCurrency}
handleSwitchAssets={handleSwitchAssets}
onSwitchAssets={handleSwitchAssets}
setSellAsset={setSellAsset}
setSellAccountId={setSellAssetAccountId}
onChangeIsInputtingFiatSellAmount={handleIsInputtingFiatSellAmountChange}
Expand Down

0 comments on commit 2f7e1ab

Please sign in to comment.