more safeguards

This commit is contained in:
Matthias Kretschmann 2024-09-21 02:06:44 +01:00
parent ca9a2c37f9
commit 92c53bb068
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 57 additions and 46 deletions

View File

@ -16,7 +16,7 @@ export function Price({ price }: { price: PriceCoingecko }) {
return ( return (
<p className={styles.price}> <p className={styles.price}>
<span className={`${styles.fiat} ${feedbackClasses}`}> <span className={`${styles.fiat} ${feedbackClasses}`}>
{formatFiat(price.usd, 'USD', locale)} {formatFiat(price?.usd, 'USD', locale)}
</span> </span>
{price?.usd_24h_change ? ( {price?.usd_24h_change ? (
<PriceChange priceChange={price.usd_24h_change} /> <PriceChange priceChange={price.usd_24h_change} />

View File

@ -24,7 +24,7 @@ export function usePrices(): {
isValidating: boolean isValidating: boolean
isLoading: boolean isLoading: boolean
} { } {
const { data, isValidating, isLoading } = useSWR( const { data, error, isValidating, isLoading } = useSWR(
`/api/prices?tokens=${tokenAddresses}`, `/api/prices?tokens=${tokenAddresses}`,
fetcher fetcher
) )
@ -34,24 +34,33 @@ export function usePrices(): {
const agixAddress = getTokenAddressBySymbol('AGIX') const agixAddress = getTokenAddressBySymbol('AGIX')
const cudosAddress = getTokenAddressBySymbol('CUDOS') const cudosAddress = getTokenAddressBySymbol('CUDOS')
if (!data || !oceanAddress || !fetAddress || !agixAddress || !cudosAddress) const pricesEmpty = {
return {
prices: {
ocean: { usd: 0, usd_24h_change: 0 }, ocean: { usd: 0, usd_24h_change: 0 },
fet: { usd: 0, usd_24h_change: 0 }, fet: { usd: 0, usd_24h_change: 0 },
agix: { usd: 0, usd_24h_change: 0 }, agix: { usd: 0, usd_24h_change: 0 },
cudos: { usd: 0, usd_24h_change: 0 }, cudos: { usd: 0, usd_24h_change: 0 },
asi: { usd: 0, usd_24h_change: 0 } asi: { usd: 0, usd_24h_change: 0 }
}
const isError =
!data ||
error ||
!oceanAddress ||
!fetAddress ||
!agixAddress ||
!cudosAddress
return {
prices: isError
? pricesEmpty
: {
ocean: data[oceanAddress],
fet: data[fetAddress],
agix: data[agixAddress],
cudos: data[cudosAddress],
asi: data[fetAddress]
}, },
isValidating, isValidating,
isLoading isLoading
} }
const ocean = data[oceanAddress]
const fet = data[fetAddress]
const agix = data[agixAddress]
const cudos = data[cudosAddress]
const asi = fet
return { prices: { ocean, fet, agix, cudos, asi }, isValidating, isLoading }
} }

View File

@ -26,44 +26,46 @@ export function Buy() {
</h3> </h3>
<Result <Result
token={getTokenBySymbol('OCEAN')} token={getTokenBySymbol('OCEAN')}
amount={debouncedAmount / prices.ocean.usd} amount={debouncedAmount / prices.ocean?.usd}
amountAsi={(debouncedAmount / prices.ocean.usd) * ratioOceanToAsi} amountAsi={(debouncedAmount / prices.ocean?.usd) * ratioOceanToAsi}
amountFiat={ amountFiat={
(debouncedAmount / prices.ocean.usd) * (debouncedAmount / prices.ocean?.usd) *
ratioOceanToAsi * ratioOceanToAsi *
prices.asi.usd prices.asi?.usd
} }
isValidating={isValidating} isValidating={isValidating}
isLoading={isLoading} isLoading={isLoading}
/> />
<Result <Result
token={getTokenBySymbol('AGIX')} token={getTokenBySymbol('AGIX')}
amount={debouncedAmount / prices.agix.usd} amount={debouncedAmount / prices.agix?.usd}
amountAsi={(debouncedAmount / prices.agix.usd) * ratioAgixToAsi} amountAsi={(debouncedAmount / prices.agix?.usd) * ratioAgixToAsi}
amountFiat={ amountFiat={
(debouncedAmount / prices.agix.usd) * ratioAgixToAsi * prices.asi.usd (debouncedAmount / prices.agix?.usd) *
ratioAgixToAsi *
prices.asi?.usd
} }
isValidating={isValidating} isValidating={isValidating}
isLoading={isLoading} isLoading={isLoading}
/> />
<Result <Result
token={getTokenBySymbol('CUDOS')} token={getTokenBySymbol('CUDOS')}
amount={debouncedAmount / prices.cudos.usd} amount={debouncedAmount / prices.cudos?.usd}
amountAsi={(debouncedAmount / prices.cudos.usd) * ratioCudosToAsi} amountAsi={(debouncedAmount / prices.cudos?.usd) * ratioCudosToAsi}
amountFiat={ amountFiat={
(debouncedAmount / prices.cudos.usd) * (debouncedAmount / prices.cudos?.usd) *
ratioCudosToAsi * ratioCudosToAsi *
prices.asi.usd prices.asi?.usd
} }
isValidating={isValidating} isValidating={isValidating}
isLoading={isLoading} isLoading={isLoading}
/> />
<Result <Result
token={getTokenBySymbol('FET')} token={getTokenBySymbol('FET')}
amount={debouncedAmount / prices.fet.usd} amount={debouncedAmount / prices.fet?.usd}
amountAsi={(debouncedAmount / prices.fet.usd) * ratioFetToAsi} amountAsi={(debouncedAmount / prices.fet?.usd) * ratioFetToAsi}
amountFiat={ amountFiat={
(debouncedAmount / prices.fet.usd) * ratioFetToAsi * prices.asi.usd (debouncedAmount / prices.fet?.usd) * ratioFetToAsi * prices.asi?.usd
} }
isValidating={isValidating} isValidating={isValidating}
isLoading={isLoading} isLoading={isLoading}

View File

@ -46,9 +46,9 @@ export function SwapResults({
const tokenSelected = tokenSymbol.toLowerCase() as keyof Prices const tokenSelected = tokenSymbol.toLowerCase() as keyof Prices
const amountInUsd = amount * prices[tokenSelected]?.usd const amountInUsd = amount * prices[tokenSelected]?.usd
const amountToOcean = amountInUsd / prices.ocean.usd const amountToOcean = amountInUsd / prices.ocean?.usd
const amountToAgix = amountInUsd / prices.agix.usd const amountToAgix = amountInUsd / prices.agix?.usd
const amountToCudos = amountInUsd / prices.cudos.usd const amountToCudos = amountInUsd / prices.cudos?.usd
// As of July 1st, use fixed ratios instead of FET market price // As of July 1st, use fixed ratios instead of FET market price
// for Migration Tool scenario // for Migration Tool scenario
@ -60,7 +60,7 @@ export function SwapResults({
: tokenSelected === 'cudos' : tokenSelected === 'cudos'
? amount * ratioCudosToAsi ? amount * ratioCudosToAsi
: amount : amount
: amountInUsd / prices.fet.usd : amountInUsd / prices.fet?.usd
const showOcean = !isMigration || (isMigration && tokenSelected === 'ocean') const showOcean = !isMigration || (isMigration && tokenSelected === 'ocean')
const showAgix = !isMigration || (isMigration && tokenSelected === 'agix') const showAgix = !isMigration || (isMigration && tokenSelected === 'agix')
@ -77,10 +77,10 @@ export function SwapResults({
amountFiat={ amountFiat={
(amountToOceanUniswap || amountToOcean) * (amountToOceanUniswap || amountToOcean) *
ratioOceanToAsi * ratioOceanToAsi *
prices.asi.usd prices.asi?.usd
} }
amountOriginalFiat={ amountOriginalFiat={
(amountToOceanUniswap || amountToOcean) * prices.ocean.usd (amountToOceanUniswap || amountToOcean) * prices.ocean?.usd
} }
isValidating={isValidatingToOcean || isValidatingPrices} isValidating={isValidatingToOcean || isValidatingPrices}
isLoading={isLoadingToOcean || isLoadingPrices} isLoading={isLoadingToOcean || isLoadingPrices}
@ -95,10 +95,10 @@ export function SwapResults({
amountFiat={ amountFiat={
(amountToAgixUniswap || amountToAgix) * (amountToAgixUniswap || amountToAgix) *
ratioAgixToAsi * ratioAgixToAsi *
prices.asi.usd prices.asi?.usd
} }
amountOriginalFiat={ amountOriginalFiat={
(amountToAgixUniswap || amountToAgix) * prices.agix.usd (amountToAgixUniswap || amountToAgix) * prices.agix?.usd
} }
isValidating={isValidatingToAgix || isValidatingPrices} isValidating={isValidatingToAgix || isValidatingPrices}
isLoading={isLoadingToAgix || isLoadingPrices} isLoading={isLoadingToAgix || isLoadingPrices}
@ -113,10 +113,10 @@ export function SwapResults({
amountFiat={ amountFiat={
(amountToCudosUniswap || amountToCudos) * (amountToCudosUniswap || amountToCudos) *
ratioCudosToAsi * ratioCudosToAsi *
prices.asi.usd prices.asi?.usd
} }
amountOriginalFiat={ amountOriginalFiat={
(amountToCudosUniswap || amountToCudos) * prices.cudos.usd (amountToCudosUniswap || amountToCudos) * prices.cudos?.usd
} }
isValidating={isValidatingToCudos || isValidatingPrices} isValidating={isValidatingToCudos || isValidatingPrices}
isLoading={isLoadingToCudos || isLoadingPrices} isLoading={isLoadingToCudos || isLoadingPrices}
@ -128,9 +128,9 @@ export function SwapResults({
token={getTokenBySymbol('FET')} token={getTokenBySymbol('FET')}
amount={amountToFetUniswap || amountToFet} amount={amountToFetUniswap || amountToFet}
amountAsi={(amountToFetUniswap || amountToFet) * ratioFetToAsi} amountAsi={(amountToFetUniswap || amountToFet) * ratioFetToAsi}
amountFiat={(amountToFetUniswap || amountToFet) * prices.asi.usd} amountFiat={(amountToFetUniswap || amountToFet) * prices.asi?.usd}
amountOriginalFiat={ amountOriginalFiat={
(amountToFetUniswap || amountToFet) * prices.asi.usd (amountToFetUniswap || amountToFet) * prices.asi?.usd
} }
isValidating={isValidatingToFet || isValidatingPrices} isValidating={isValidatingToFet || isValidatingPrices}
isLoading={isLoadingToFet || isLoadingPrices} isLoading={isLoadingToFet || isLoadingPrices}