-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(usd): further refactor SpotPrice updater (#5297)
* feat: further refactor SpotPrice updater Use UsdPriceUpdater instead of triggering new requests from here * fix: lint * fix: prices must be in Fraction before division
- Loading branch information
1 parent
2a7a51d
commit 85e4679
Showing
4 changed files
with
113 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 18 additions & 64 deletions
82
apps/cowswap-frontend/src/modules/limitOrders/hooks/useGetInitialPrice.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,44 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
import { useIsWindowVisible } from '@cowprotocol/common-hooks' | ||
import { getWrappedToken } from '@cowprotocol/common-utils' | ||
import { useWalletInfo } from '@cowprotocol/wallet' | ||
import { Currency, Fraction } from '@uniswap/sdk-core' | ||
import { FractionUtils, getWrappedToken } from '@cowprotocol/common-utils' | ||
import { Fraction } from '@uniswap/sdk-core' | ||
|
||
import ms from 'ms.macro' | ||
import { useAsyncMemo } from 'use-async-memo' | ||
|
||
import { useLimitOrdersDerivedState } from 'modules/limitOrders/hooks/useLimitOrdersDerivedState' | ||
|
||
import { useSafeMemo } from 'common/hooks/useSafeMemo' | ||
|
||
import { fetchCurrencyUsdPrice, usdcPriceLoader } from '../../usdAmount' | ||
|
||
const PRICE_UPDATE_INTERVAL = ms`10sec` | ||
|
||
export async function requestPrice( | ||
chainId: number | undefined, | ||
inputCurrency: Currency | null, | ||
outputCurrency: Currency | null, | ||
): Promise<Fraction | null> { | ||
if (!chainId || !inputCurrency || !outputCurrency) { | ||
return null | ||
} | ||
|
||
const inputToken = getWrappedToken(inputCurrency) | ||
const outputToken = getWrappedToken(outputCurrency) | ||
|
||
// Only needed for the fallback CoW price, which needs to know the USDC price | ||
const getUsdPrice = usdcPriceLoader(chainId) | ||
|
||
return Promise.all([ | ||
fetchCurrencyUsdPrice(inputToken, getUsdPrice), | ||
fetchCurrencyUsdPrice(outputToken, getUsdPrice), | ||
]).then(([inputPrice, outputPrice]) => { | ||
if (!inputPrice || !outputPrice) { | ||
return null | ||
} | ||
|
||
const result = inputPrice.divide(outputPrice) | ||
|
||
console.debug('Updated limit orders initial price: ', result.toSignificant(18)) | ||
|
||
return result | ||
}) | ||
} | ||
import { useUsdPrice } from '../../usdAmount' | ||
|
||
// Fetches the INPUT and OUTPUT price and calculates initial Active rate | ||
// When return null it means we failed on price loading | ||
export function useGetInitialPrice(): { price: Fraction | null; isLoading: boolean } { | ||
const { chainId } = useWalletInfo() | ||
const { inputCurrency, outputCurrency } = useLimitOrdersDerivedState() | ||
const [isLoading, setIsLoading] = useState(false) | ||
const [updateTimestamp, setUpdateTimestamp] = useState(Date.now()) | ||
const isWindowVisible = useIsWindowVisible() | ||
|
||
const inputToken = inputCurrency && getWrappedToken(inputCurrency) | ||
const outputToken = outputCurrency && getWrappedToken(outputCurrency) | ||
const inputUsdPrice = useUsdPrice(inputToken) | ||
const outputUsdPrice = useUsdPrice(outputToken) | ||
|
||
useEffect(() => { | ||
setIsLoading(!!inputUsdPrice?.isLoading || !!outputUsdPrice?.isLoading) | ||
}, [inputUsdPrice?.isLoading, outputUsdPrice?.isLoading]) | ||
|
||
const price = useAsyncMemo( | ||
async () => { | ||
setIsLoading(true) | ||
|
||
console.debug('[useGetInitialPrice] Fetching price') | ||
try { | ||
return await requestPrice(chainId, inputCurrency, outputCurrency) | ||
} finally { | ||
setIsLoading(false) | ||
if (!inputUsdPrice?.price || !outputUsdPrice?.price) { | ||
return null | ||
} | ||
const inputFraction = FractionUtils.fractionLikeToFraction(inputUsdPrice.price) | ||
const outputFraction = FractionUtils.fractionLikeToFraction(outputUsdPrice.price) | ||
return inputFraction.divide(outputFraction) | ||
}, | ||
[chainId, inputCurrency, outputCurrency, updateTimestamp], | ||
[inputUsdPrice?.price, outputUsdPrice?.price], | ||
null, | ||
) | ||
|
||
// Update initial price every 10 seconds | ||
useEffect(() => { | ||
if (!isWindowVisible) { | ||
console.debug('[useGetInitialPrice] No need to fetch quotes') | ||
return | ||
} | ||
|
||
console.debug('[useGetInitialPrice] Periodically fetch price') | ||
const interval = setInterval(() => { | ||
setUpdateTimestamp(Date.now()) | ||
}, PRICE_UPDATE_INTERVAL) | ||
|
||
return () => clearInterval(interval) | ||
}, [isWindowVisible]) | ||
|
||
return useSafeMemo(() => ({ price, isLoading }), [price, isLoading]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters