Mp 3220 prevent making unnecessary calls from SwapForm (#338)
This commit is contained in:
parent
b6be254804
commit
69dece2600
@ -1,4 +1,4 @@
|
|||||||
import { useCallback, useEffect, useMemo, useState } from 'react'
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||||
|
|
||||||
import Divider from 'components/Divider'
|
import Divider from 'components/Divider'
|
||||||
import { DEFAULT_SETTINGS } from 'constants/defaultSettings'
|
import { DEFAULT_SETTINGS } from 'constants/defaultSettings'
|
||||||
@ -33,19 +33,20 @@ export default function SwapForm(props: Props) {
|
|||||||
const swap = useStore((s) => s.swap)
|
const swap = useStore((s) => s.swap)
|
||||||
const [slippage] = useLocalStorage(SLIPPAGE_KEY, DEFAULT_SETTINGS.slippage)
|
const [slippage] = useLocalStorage(SLIPPAGE_KEY, DEFAULT_SETTINGS.slippage)
|
||||||
const { computeMaxSwapAmount } = useHealthComputer(account)
|
const { computeMaxSwapAmount } = useHealthComputer(account)
|
||||||
|
const buyAmountEstimationTimeout = useRef<NodeJS.Timeout | undefined>(undefined)
|
||||||
|
|
||||||
const [isMarginChecked, setMarginChecked] = useState(false)
|
const [isMarginChecked, setMarginChecked] = useState(false)
|
||||||
const [buyAssetAmount, setBuyAssetAmount] = useState(BN_ZERO)
|
const [buyAssetAmount, setBuyAssetAmount] = useState(BN_ZERO)
|
||||||
const [sellAssetAmount, setSellAssetAmount] = useState(BN_ZERO)
|
const [sellAssetAmount, setSellAssetAmount] = useState(BN_ZERO)
|
||||||
const [focusedInput, setFocusedInput] = useState<'buy' | 'sell' | null>(null)
|
const focusedInput = useRef<'buy' | 'sell' | null>(null)
|
||||||
const [maxBuyableAmountEstimation, setMaxBuyableAmountEstimation] = useState(BN_ZERO)
|
const [maxBuyableAmountEstimation, setMaxBuyableAmountEstimation] = useState(BN_ZERO)
|
||||||
const [selectedOrderType, setSelectedOrderType] = useState<AvailableOrderType>('Market')
|
const [selectedOrderType, setSelectedOrderType] = useState<AvailableOrderType>('Market')
|
||||||
const [isTransactionExecuting, setTransactionExecuting] = useState(false)
|
const [isTransactionExecuting, setTransactionExecuting] = useState(false)
|
||||||
|
|
||||||
const maxSellAssetAmount = useMemo(
|
const [maxSellAssetAmount, maxSellAssetAmountStr] = useMemo(() => {
|
||||||
() => computeMaxSwapAmount(sellAsset.denom, buyAsset.denom, 'default'),
|
const amount = computeMaxSwapAmount(sellAsset.denom, buyAsset.denom, 'default')
|
||||||
[computeMaxSwapAmount, sellAsset.denom, buyAsset.denom],
|
return [amount, amount.toString()]
|
||||||
)
|
}, [computeMaxSwapAmount, sellAsset.denom, buyAsset.denom])
|
||||||
|
|
||||||
const [buyAssetValue, sellAssetValue] = useMemo(() => {
|
const [buyAssetValue, sellAssetValue] = useMemo(() => {
|
||||||
const buyAssetPrice = prices.find(byDenom(buyAsset.denom))?.amount ?? BN_ZERO
|
const buyAssetPrice = prices.find(byDenom(buyAsset.denom))?.amount ?? BN_ZERO
|
||||||
@ -66,29 +67,38 @@ export default function SwapForm(props: Props) {
|
|||||||
])
|
])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setFocusedInput(null)
|
focusedInput.current = null
|
||||||
setBuyAssetAmount(BN_ZERO)
|
setBuyAssetAmount(BN_ZERO)
|
||||||
setSellAssetAmount(BN_ZERO)
|
setSellAssetAmount(BN_ZERO)
|
||||||
}, [buyAsset.denom, sellAsset.denom])
|
}, [buyAsset.denom, sellAsset.denom])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
estimateExactIn(
|
estimateExactIn({ denom: sellAsset.denom, amount: maxSellAssetAmountStr }, buyAsset.denom).then(
|
||||||
{ denom: sellAsset.denom, amount: maxSellAssetAmount.toString() },
|
setMaxBuyableAmountEstimation,
|
||||||
buyAsset.denom,
|
)
|
||||||
).then(setMaxBuyableAmountEstimation)
|
}, [maxSellAssetAmountStr, buyAsset.denom, sellAsset.denom])
|
||||||
}, [maxSellAssetAmount, buyAsset.denom, sellAsset.denom])
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (focusedInput === 'sell') {
|
if (focusedInput.current === 'sell') {
|
||||||
estimateExactIn(
|
if (buyAmountEstimationTimeout.current) {
|
||||||
{ denom: sellAsset.denom, amount: sellAssetAmount.toString() },
|
clearTimeout(buyAmountEstimationTimeout.current)
|
||||||
buyAsset.denom,
|
}
|
||||||
).then(setBuyAssetAmount)
|
|
||||||
|
buyAmountEstimationTimeout.current = setTimeout(
|
||||||
|
() =>
|
||||||
|
estimateExactIn(
|
||||||
|
{ denom: sellAsset.denom, amount: sellAssetAmount.toString() },
|
||||||
|
buyAsset.denom,
|
||||||
|
)
|
||||||
|
.then(setBuyAssetAmount)
|
||||||
|
.then(() => clearTimeout(buyAmountEstimationTimeout?.current)),
|
||||||
|
250,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}, [buyAsset.denom, focusedInput, sellAsset.denom, sellAssetAmount])
|
}, [buyAsset.denom, focusedInput, sellAsset.denom, sellAssetAmount])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (focusedInput === 'buy') {
|
if (focusedInput.current === 'buy') {
|
||||||
estimateExactIn(
|
estimateExactIn(
|
||||||
{
|
{
|
||||||
denom: buyAsset.denom,
|
denom: buyAsset.denom,
|
||||||
@ -117,10 +127,11 @@ export default function SwapForm(props: Props) {
|
|||||||
}
|
}
|
||||||
}, [account?.id, buyAsset.denom, sellAsset.denom, sellAssetAmount, slippage, swap])
|
}, [account?.id, buyAsset.denom, sellAsset.denom, sellAssetAmount, slippage, swap])
|
||||||
|
|
||||||
const dismissInputFocus = useCallback(() => setFocusedInput(null), [])
|
const dismissInputFocus = useCallback(() => (focusedInput.current = null), [])
|
||||||
|
|
||||||
const handleRangeInputChange = useCallback(
|
const handleRangeInputChange = useCallback(
|
||||||
(value: number) => {
|
(value: number) => {
|
||||||
setFocusedInput('sell')
|
focusedInput.current = 'sell'
|
||||||
setSellAssetAmount(BN(value).shiftedBy(sellAsset.decimals).integerValue())
|
setSellAssetAmount(BN(value).shiftedBy(sellAsset.decimals).integerValue())
|
||||||
},
|
},
|
||||||
[sellAsset.decimals],
|
[sellAsset.decimals],
|
||||||
@ -144,7 +155,7 @@ export default function SwapForm(props: Props) {
|
|||||||
maxButtonLabel='Max Amount:'
|
maxButtonLabel='Max Amount:'
|
||||||
containerClassName='mx-3 my-6'
|
containerClassName='mx-3 my-6'
|
||||||
onBlur={dismissInputFocus}
|
onBlur={dismissInputFocus}
|
||||||
onFocus={() => setFocusedInput('buy')}
|
onFocus={() => (focusedInput.current = 'buy')}
|
||||||
disabled={isTransactionExecuting}
|
disabled={isTransactionExecuting}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
@ -167,7 +178,7 @@ export default function SwapForm(props: Props) {
|
|||||||
maxButtonLabel='Balance:'
|
maxButtonLabel='Balance:'
|
||||||
containerClassName='mx-3'
|
containerClassName='mx-3'
|
||||||
onBlur={dismissInputFocus}
|
onBlur={dismissInputFocus}
|
||||||
onFocus={() => setFocusedInput('sell')}
|
onFocus={() => (focusedInput.current = 'sell')}
|
||||||
disabled={isTransactionExecuting}
|
disabled={isTransactionExecuting}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user