Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(fiatAmount): test UsdPricesUpdater #3091

Merged
merged 52 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
0436ad0
refactor: remove usd amount calc based ony CowSwap quote
shoom3301 Aug 22, 2023
76925b3
refactor: move useHigherUSDValue hook to fiatAmount module
shoom3301 Aug 22, 2023
3ff2993
refactor: calculate priceImpact only basing on Coingecko
shoom3301 Aug 22, 2023
36931aa
refactor: request fiat amounts only when trade is ready
shoom3301 Aug 23, 2023
315d15a
refactor: deprecate parsePrice
shoom3301 Aug 23, 2023
32affd6
feat: use native price api for fiat amounts
shoom3301 Aug 23, 2023
007afd6
feat: module for fiat prices
shoom3301 Aug 24, 2023
cc1298a
chore: remove unused file
shoom3301 Aug 24, 2023
3409abb
chore: simplify code
shoom3301 Aug 25, 2023
639f220
docs: fiat price module diagram
shoom3301 Aug 25, 2023
6e6eba5
chore: fix import
shoom3301 Aug 25, 2023
e8ab945
chore: rename
shoom3301 Aug 25, 2023
245409d
chore: remove error field from price impact
shoom3301 Aug 25, 2023
7f353e3
chore: fix build
shoom3301 Aug 25, 2023
4eb1f6d
chore: fix build
shoom3301 Aug 25, 2023
1b339f5
Merge branch 'fix/fiat-amount-api' of https://github.com/cowprotocol/…
shoom3301 Aug 25, 2023
d0019d6
fix: display price impact unknown warning only when fiat amounts are …
shoom3301 Aug 28, 2023
3795e7d
Merge branch 'develop' of https://github.com/cowprotocol/cowswap into…
shoom3301 Aug 28, 2023
7274ad1
Merge branch 'fix/fiat-amount-api' of https://github.com/cowprotocol/…
shoom3301 Aug 28, 2023
960c22b
chore: rename var
shoom3301 Aug 28, 2023
6e3b193
chore: rename const
shoom3301 Aug 28, 2023
da2b879
chore: clean up env files
shoom3301 Aug 28, 2023
22ed725
chore: clean up code
shoom3301 Aug 28, 2023
40ac98c
chore: refactor fetchCurrencyFiatPrice
shoom3301 Aug 28, 2023
3601d0f
fix: fix UnknownImpactWarning displaying conditions
shoom3301 Aug 28, 2023
e97a8e6
chore: fix naming
shoom3301 Aug 28, 2023
1e18bbd
fix: request USDC native price on demand
shoom3301 Aug 28, 2023
e5b0a4e
fix: fix calc
shoom3301 Aug 28, 2023
dc2b958
fix: fix errors catching in fetchCurrencyFiatPrice
shoom3301 Aug 28, 2023
1554ecd
chore: additional check for zero
shoom3301 Aug 29, 2023
b4e7224
chore: fix comment
shoom3301 Aug 29, 2023
c95e7de
chore: refactor useFiatValuePriceImpact
shoom3301 Aug 29, 2023
9fc2797
chore: rename getCoingeckoFiatPrice
shoom3301 Aug 29, 2023
0ca263b
chore: rename usdc -> usd
shoom3301 Aug 29, 2023
fa035e8
chore: simplify useUsdPrice
shoom3301 Aug 29, 2023
4955667
chore: rename fiat -> usd
shoom3301 Aug 29, 2023
04908d6
chore: rename fiat -> usd globally
shoom3301 Aug 29, 2023
ff84560
Merge branch 'fix/fiat-amount-api-unit-tests' of https://github.com/c…
shoom3301 Aug 29, 2023
355671f
chore: simplify useTradeUsdAmounts
shoom3301 Aug 29, 2023
45b0615
chore: coingecko fetch rate limitter and backOff
shoom3301 Aug 30, 2023
7dea62f
Merge branch 'fix/fiat-amount-api-2' of https://github.com/cowprotoco…
shoom3301 Aug 30, 2023
10cc765
test(fiatAmount): test UsdPricesUpdater
shoom3301 Aug 30, 2023
d5ef158
chore: revert
shoom3301 Aug 30, 2023
f24b689
chore: fix SWR cache cleaning
shoom3301 Aug 30, 2023
ed61077
chore: fix code style
shoom3301 Aug 30, 2023
1263764
chore: comment
shoom3301 Aug 30, 2023
3f4db54
chore: remove unused const
shoom3301 Aug 30, 2023
dddc86b
test: test useUsdAmount
shoom3301 Aug 30, 2023
2bc7e50
chore: fix circular dependency
shoom3301 Aug 30, 2023
04a1791
Merge branch 'develop' of https://github.com/cowprotocol/cowswap into…
shoom3301 Aug 31, 2023
6ac4d6f
Merge branch 'fix/fiat-amount-api-2' of https://github.com/cowprotoco…
shoom3301 Aug 31, 2023
3a56435
Merge branch 'develop' of https://github.com/cowprotocol/cowswap into…
shoom3301 Aug 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ All price feeds are enabled by default, but they can be individually disabled by
| **Paraswap** | `REACT_APP_PRICE_FEED_PARASWAP_ENABLED` | `boolean` (default = `true`) | [Paraswap](https://paraswap.io/) price estimation. Used for all price estimations. |
| **1inch** | `REACT_APP_PRICE_FEED_1INCH_ENABLED` | `boolean` (default = `true`) | [Paraswap](https://1inch.exchange) price estimation. Used for all price estimations. |
| **0x** | `REACT_APP_PRICE_FEED_0X_ENABLED` | `boolean` (default = `true`) | [0x](https://0x.org/) price estimation. Used for all price estimation. |
| **Coingecko** | `REACT_APP_PRICE_FEED_COINGECKO_ENABLED` | `boolean` (default = `true`) | [Coingecko](coingecko.com) price estimation. Used only for USD estimations to calculate the slippage on the trade. |

### Metadata attached to orders (AppData)

Expand Down
1 change: 0 additions & 1 deletion apps/cowswap-frontend/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ REACT_APP_PRICE_FEED_GP_ENABLED=true
REACT_APP_PRICE_FEED_PARASWAP_ENABLED=true
REACT_APP_PRICE_FEED_1INCH_ENABLED=true
REACT_APP_PRICE_FEED_0X_ENABLED=true
REACT_APP_PRICE_FEED_COINGECKO_ENABLED=true

# AppData, build yours at https://explorer.cow.fi/appdata
REACT_APP_FULL_APP_DATA_PRODUCTION='{"version":"0.7.0","appCode":"CoW Swap","environment":"production","metadata":{}}'
Expand Down
1 change: 0 additions & 1 deletion apps/cowswap-frontend/.env.production
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ REACT_APP_PRICE_FEED_GP_ENABLED=true
REACT_APP_PRICE_FEED_PARASWAP_ENABLED=true
REACT_APP_PRICE_FEED_1INCH_ENABLED=true
REACT_APP_PRICE_FEED_0X_ENABLED=true
REACT_APP_PRICE_FEED_COINGECKO_ENABLED=true
# INFURA KEY
REACT_APP_INFURA_KEY=586e7e6b7c7e437aa41f5da496a749f5

Expand Down
135 changes: 0 additions & 135 deletions apps/cowswap-frontend/src/api/coingecko/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useAtomValue } from 'legacy/state/application/atoms'
import { useAtomValue } from 'jotai'

import { cancellationModalContextAtom } from 'common/hooks/useCancelOrder/state'
import { CancellationModal as Pure } from 'common/pure/CancellationModal'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Nullish } from 'types'
import { MIN_FIAT_SURPLUS_VALUE, MIN_FIAT_SURPLUS_VALUE_MODAL, MIN_SURPLUS_UNITS } from 'legacy/constants'
import { Order } from 'legacy/state/orders/actions'

import { useCoingeckoUsdValue } from 'modules/fiatAmount'
import { useUsdAmount } from 'modules/usdAmount'

import { getExecutedSummaryData } from 'utils/getExecutedSummaryData'
import { ParsedOrder } from 'utils/orderUtils/parseOrder'
Expand All @@ -34,7 +34,7 @@ export function useGetSurplusData(order: Order | ParsedOrder | undefined): Outpu
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [JSON.stringify(order)])

const surplusFiatValue = useCoingeckoUsdValue(surplusAmount).value
const surplusFiatValue = useUsdAmount(surplusAmount).value
const showFiatValue = Number(surplusFiatValue?.toExact()) >= MIN_FIAT_SURPLUS_VALUE

const showSurplus = shouldShowSurplus(surplusFiatValue, surplusAmount)
Expand Down
14 changes: 7 additions & 7 deletions apps/cowswap-frontend/src/common/hooks/useRateInfoParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Currency, CurrencyAmount } from '@uniswap/sdk-core'

import { Nullish } from 'types'

import { useHigherUSDValue } from 'modules/fiatAmount'
import { useTradeUsdAmounts } from 'modules/usdAmount'
import { useWalletInfo } from 'modules/wallet'

import { usePrice } from 'common/hooks/usePrice'
Expand All @@ -29,13 +29,13 @@ export function useRateInfoParams(
[activeRate]
)

const activeRateFiatAmount = useHigherUSDValue(
const {
inputAmount: { value: invertedActiveRateFiatAmount },
outputAmount: { value: activeRateFiatAmount },
} = useTradeUsdAmounts(
tryParseCurrencyAmount(parseRate(true), inputCurrencyAmount?.currency || undefined),
tryParseCurrencyAmount(parseRate(false), outputCurrencyAmount?.currency || undefined)
).value

const invertedActiveRateFiatAmount = useHigherUSDValue(
tryParseCurrencyAmount(parseRate(true), inputCurrencyAmount?.currency || undefined)
).value
)

return useSafeMemoObject({
chainId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Currency, CurrencyAmount, Price } from '@uniswap/sdk-core'

import { useHigherUSDValue } from 'modules/fiatAmount'
import { useUsdAmount } from 'modules/usdAmount'

import { rawToTokenAmount } from 'utils/rawToTokenAmount'

Expand All @@ -19,5 +19,5 @@ export function useExecutionPriceFiat(
): CurrencyAmount<Currency> | null {
const amount = executionPrice ? getPriceQuoteAmount(executionPrice, isInverted) : undefined

return useHigherUSDValue(amount).value
return useUsdAmount(amount).value
}
7 changes: 7 additions & 0 deletions apps/cowswap-frontend/src/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Currency, CurrencyAmount } from '@uniswap/sdk-core'

/**
* https://github.com/rndlabs/composable-cow/blob/main/src/ComposableCoW.sol
* Information about ComposableCoW conditional orders
Expand All @@ -21,3 +23,8 @@ export type SafeTransactionParams = {
confirmations: number
safeTxHash: string
}

export interface TradeAmounts {
readonly inputAmount: CurrencyAmount<Currency>
readonly outputAmount: CurrencyAmount<Currency>
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { useAtomValue, useSetAtom } from 'jotai'
import { selectAtom } from 'jotai/utils'
import React, { useEffect, useMemo, useCallback, useRef, PropsWithChildren } from 'react'

import { useRecentActivityLastPendingOrder } from 'legacy/hooks/useRecentActivity'
import {
useSetAtom,
useAtomValue,
handleFollowPendingTxPopupAtom,
handleHidePopupPermanentlyAtom,
showFollowTxPopupAtom,
followPendingTxPopupAtom,
selectAtom,
handleCloseOrderPopupAtom,
} from 'legacy/state/application/atoms'
import { Order } from 'legacy/state/orders/actions'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Trans } from '@lingui/macro'

import { MouseoverTooltip } from 'legacy/components/Tooltip'

import { useHigherUSDValue } from 'modules/fiatAmount'
import { useUsdAmount } from 'modules/usdAmount'

import { FiatAmount } from 'common/pure/FiatAmount'

Expand All @@ -14,9 +14,9 @@ type FiatBalanceCellProps = {
balance: CurrencyAmount<Token> | undefined
}

export default function FiatBalanceCell({ balance }: FiatBalanceCellProps) {
export function FiatBalanceCell({ balance }: FiatBalanceCellProps) {
const hasBalance = balance?.greaterThan(0)
const fiatValue = useHigherUSDValue(balance).value
const fiatValue = useUsdAmount(balance).value

return (
<BalanceValue hasBalance={!!hasBalance}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { CardsSpinner, ExtLink } from 'pages/Account/styled'

import BalanceCell from './BalanceCell'
import FavouriteTokenButton from './FavouriteTokenButton'
import FiatBalanceCell from './FiatBalanceCell'
import { FiatBalanceCell } from './FiatBalanceCell'
import {
ApproveLabel,
BalanceValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useAtomValue } from 'legacy/state/application/atoms'
import { useAtomValue } from 'jotai'

import { useModalIsOpen, useToggleModal } from 'legacy/state/application/hooks'
import { ApplicationModal } from 'legacy/state/application/reducer'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useSetAtom } from 'jotai'
import React, { ReactNode, useCallback, useEffect } from 'react'

import { Currency } from '@uniswap/sdk-core'

import { getActivityState, useActivityDerivedState } from 'legacy/hooks/useActivityDerivedState'
import { useMultipleActivityDescriptors } from 'legacy/hooks/useRecentActivity'
import { handleFollowPendingTxPopupAtom, useSetAtom } from 'legacy/state/application/atoms'
import { handleFollowPendingTxPopupAtom } from 'legacy/state/application/atoms'

import { useSetIsConfirmationModalOpen } from 'modules/swap/state/surplusModal'
import { useWalletInfo } from 'modules/wallet'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { computeSlippageAdjustedAmounts } from 'legacy/utils/prices'

import { useHigherUSDValue } from 'modules/fiatAmount'
import { PriceUpdatedBanner } from 'modules/trade/pure/PriceUpdatedBanner'
import { useTradeUsdAmounts } from 'modules/usdAmount'

import { CurrencyLogo } from 'common/pure/CurrencyLogo'
import { FiatValue } from 'common/pure/FiatValue'
Expand Down Expand Up @@ -100,8 +101,10 @@ export default function SwapModalHeader({

const theme = useContext(ThemeContext)

const fiatValueInput = useHigherUSDValue(trade.inputAmountWithoutFee).value
const fiatValueOutput = useHigherUSDValue(trade.outputAmountWithoutFee).value
const {
inputAmount: { value: fiatValueInput },
outputAmount: { value: fiatValueOutput },
} = useTradeUsdAmounts(trade.inputAmountWithoutFee, trade.outputAmountWithoutFee)

const [slippageIn, slippageOut] = useMemo(
() => [slippageAdjustedAmounts[Field.INPUT], slippageAdjustedAmounts[Field.OUTPUT]],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
import { computeFiatValuePriceImpact } from 'legacy/utils/computeFiatValuePriceImpact'
import { Currency, CurrencyAmount, Percent } from '@uniswap/sdk-core'

import { useHigherUSDValue } from 'modules/fiatAmount'
import JSBI from 'jsbi'
import ms from 'ms.macro'

import { ONE_HUNDRED_PERCENT } from 'legacy/constants/misc'
import useDebounce from 'legacy/hooks/useDebounce'

import { useDerivedTradeState } from 'modules/trade/hooks/useDerivedTradeState'
import { useTradeUsdAmounts } from 'modules/usdAmount'

import { useSafeMemo } from 'common/hooks/useSafeMemo'

import { ParsedAmounts } from './types'
const FIAT_VALUE_LOADING_THRESHOLD = ms`0.1s`

export function useFiatValuePriceImpact() {
const { state } = useDerivedTradeState()
const { inputCurrencyAmount, outputCurrencyAmount, inputCurrency, outputCurrency } = state || {}

const isTradeSetUp = !!inputCurrency && !!outputCurrency

export function useFiatValuePriceImpact({ INPUT, OUTPUT }: ParsedAmounts) {
const areBothValuesPresent = !!INPUT && !!OUTPUT
// prevent querying any fiat estimation unless both values are filled in
const input = areBothValuesPresent ? INPUT : undefined
const output = areBothValuesPresent ? OUTPUT : undefined
const {
inputAmount: { value: fiatValueInput, isLoading: inputIsLoading },
outputAmount: { value: fiatValueOutput, isLoading: outputIsLoading },
} = useTradeUsdAmounts(inputCurrencyAmount, outputCurrencyAmount)

const { value: fiatValueInput, isLoading: inputIsLoading } = useHigherUSDValue(input)
const { value: fiatValueOutput, isLoading: outputIsLoading } = useHigherUSDValue(output)
// Consider the price impact loading if either the input or output amount is falsy
// Debounce the loading state to prevent the price impact from flashing
const isLoading = useDebounce(isTradeSetUp ? inputIsLoading || outputIsLoading : false, FIAT_VALUE_LOADING_THRESHOLD)

return useSafeMemo(() => {
const priceImpact = computeFiatValuePriceImpact(fiatValueInput, fiatValueOutput)

return { priceImpact, isLoading }
}, [fiatValueInput, fiatValueOutput, isLoading])
}

// Only compute price impact after BOTH finished loading
// This prevents the impact look like it's ready but still loading
const priceImpact =
!inputIsLoading && !outputIsLoading ? computeFiatValuePriceImpact(fiatValueInput, fiatValueOutput) : undefined
function computeFiatValuePriceImpact(
fiatValueInput: CurrencyAmount<Currency> | undefined | null,
fiatValueOutput: CurrencyAmount<Currency> | undefined | null
): Percent | undefined {
if (!fiatValueOutput || !fiatValueInput) return undefined
if (!fiatValueInput.currency.equals(fiatValueOutput.currency)) return undefined
if (JSBI.equal(fiatValueInput.quotient, JSBI.BigInt(0))) return undefined

const isLoading = areBothValuesPresent && (inputIsLoading || outputIsLoading)
const pct = ONE_HUNDRED_PERCENT.subtract(fiatValueOutput.divide(fiatValueInput))

return useSafeMemo(() => ({ priceImpact, isLoading }), [priceImpact, isLoading])
return new Percent(pct.numerator, pct.denominator)
}
Loading
Loading