Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
[ABA impact] Show eager loader (#1985)
Browse files Browse the repository at this point in the history
* commonTypes > types

* show loader on amount change
  • Loading branch information
W3stside authored Dec 9, 2021
1 parent 80d2447 commit 50a32a1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/custom/hooks/usePriceImpact/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Percent } from '@uniswap/sdk-core'
import useFiatValuePriceImpact from './useFiatValuePriceImpact'
import useFallbackPriceImpact from './useFallbackPriceImpact'
import { FallbackPriceImpactParams, ParsedAmounts } from './commonTypes'
import { FallbackPriceImpactParams, ParsedAmounts } from './types'
import { QuoteError } from 'state/price/actions'

type PriceImpactParams = Omit<FallbackPriceImpactParams, 'fiatPriceImpact'> & {
Expand Down
2 changes: 1 addition & 1 deletion src/custom/hooks/usePriceImpact/useFallbackPriceImpact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Percent } from '@uniswap/sdk-core'
import { useSwapState } from 'state/swap/hooks'

import useExactInSwap, { useCalculateQuote } from './useQuoteAndSwap'
import { FallbackPriceImpactParams } from './commonTypes'
import { FallbackPriceImpactParams } from './types'
import { calculateFallbackPriceImpact, FeeQuoteParams } from 'utils/price'
import TradeGp from 'state/swap/TradeGp'
import { QuoteInformationObject } from 'state/price/reducer'
Expand Down
2 changes: 1 addition & 1 deletion src/custom/hooks/usePriceImpact/useFiatValuePriceImpact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Field } from 'state/swap/actions'
import { useHigherUSDValue } from 'hooks/useUSDCPrice'

import { computeFiatValuePriceImpact } from 'utils/computeFiatValuePriceImpact'
import { ParsedAmounts } from './commonTypes'
import { ParsedAmounts } from './types'

export default function useFiatValuePriceImpact(parsedAmounts: ParsedAmounts) {
const fiatValueInput = useHigherUSDValue(parsedAmounts[Field.INPUT])
Expand Down
15 changes: 15 additions & 0 deletions src/custom/hooks/usePriceImpact/useQuoteAndSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { SupportedChainId } from 'constants/chains'
import { DEFAULT_DECIMALS } from 'constants/index'
import { QuoteError } from 'state/price/actions'
import { isWrappingTrade } from 'state/swap/utils'
import { useSwapState } from 'state/swap/hooks'

type ExactInSwapParams = {
parsedAmount: CurrencyAmount<Currency> | undefined
Expand All @@ -34,6 +35,14 @@ type GetQuoteParams = {

type FeeQuoteParamsWithError = FeeQuoteParams & { error?: QuoteError }

function useOnSwapParamChange<T>({ onChangeCb, predicate }: { predicate: T; onChangeCb: (...params: any[]) => any }) {
const { typedValue, INPUT, OUTPUT, independentField } = useSwapState()

useEffect(() => {
Boolean(predicate) && onChangeCb
}, [typedValue, INPUT.currencyId, OUTPUT.currencyId, independentField, predicate, onChangeCb])
}

export function useCalculateQuote(params: GetQuoteParams) {
const {
amountAtoms: amount,
Expand All @@ -48,6 +57,12 @@ export function useCalculateQuote(params: GetQuoteParams) {
const [quote, setLocalQuote] = useState<QuoteInformationObject | FeeQuoteParamsWithError | undefined>()
const [loading, setLoading] = useState(false)

// listens to changed swap params and calls onChangeCb based on predicate Boolean(predicate)
useOnSwapParamChange({
onChangeCb: () => setLoading(true),
predicate: amount,
})

useEffect(() => {
const chainId = supportedChainId(preChain)
// bail out early - amount here is undefined if usd price impact is valid
Expand Down

0 comments on commit 50a32a1

Please sign in to comment.