From 92c53bb068841f3332ac39ad8235075060d76870 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Sat, 21 Sep 2024 02:06:44 +0100 Subject: [PATCH] more safeguards --- features/prices/components/Price/Price.tsx | 2 +- features/prices/hooks/use-prices.tsx | 47 +++++++++++-------- features/strategies/components/Buy.tsx | 30 ++++++------ .../strategies/components/Swap/Results.tsx | 24 +++++----- 4 files changed, 57 insertions(+), 46 deletions(-) diff --git a/features/prices/components/Price/Price.tsx b/features/prices/components/Price/Price.tsx index fbd7d73..15a3619 100644 --- a/features/prices/components/Price/Price.tsx +++ b/features/prices/components/Price/Price.tsx @@ -16,7 +16,7 @@ export function Price({ price }: { price: PriceCoingecko }) { return (

- {formatFiat(price.usd, 'USD', locale)} + {formatFiat(price?.usd, 'USD', locale)} {price?.usd_24h_change ? ( diff --git a/features/prices/hooks/use-prices.tsx b/features/prices/hooks/use-prices.tsx index b0e3e9b..ffeecc4 100644 --- a/features/prices/hooks/use-prices.tsx +++ b/features/prices/hooks/use-prices.tsx @@ -24,7 +24,7 @@ export function usePrices(): { isValidating: boolean isLoading: boolean } { - const { data, isValidating, isLoading } = useSWR( + const { data, error, isValidating, isLoading } = useSWR( `/api/prices?tokens=${tokenAddresses}`, fetcher ) @@ -34,24 +34,33 @@ export function usePrices(): { const agixAddress = getTokenAddressBySymbol('AGIX') const cudosAddress = getTokenAddressBySymbol('CUDOS') - if (!data || !oceanAddress || !fetAddress || !agixAddress || !cudosAddress) - return { - prices: { - ocean: { usd: 0, usd_24h_change: 0 }, - fet: { usd: 0, usd_24h_change: 0 }, - agix: { usd: 0, usd_24h_change: 0 }, - cudos: { usd: 0, usd_24h_change: 0 }, - asi: { usd: 0, usd_24h_change: 0 } - }, - isValidating, - isLoading - } + const pricesEmpty = { + ocean: { usd: 0, usd_24h_change: 0 }, + fet: { usd: 0, usd_24h_change: 0 }, + agix: { usd: 0, usd_24h_change: 0 }, + cudos: { usd: 0, usd_24h_change: 0 }, + asi: { usd: 0, usd_24h_change: 0 } + } - const ocean = data[oceanAddress] - const fet = data[fetAddress] - const agix = data[agixAddress] - const cudos = data[cudosAddress] - const asi = fet + const isError = + !data || + error || + !oceanAddress || + !fetAddress || + !agixAddress || + !cudosAddress - return { prices: { ocean, fet, agix, cudos, asi }, isValidating, isLoading } + return { + prices: isError + ? pricesEmpty + : { + ocean: data[oceanAddress], + fet: data[fetAddress], + agix: data[agixAddress], + cudos: data[cudosAddress], + asi: data[fetAddress] + }, + isValidating, + isLoading + } } diff --git a/features/strategies/components/Buy.tsx b/features/strategies/components/Buy.tsx index 243bcc7..74864c4 100644 --- a/features/strategies/components/Buy.tsx +++ b/features/strategies/components/Buy.tsx @@ -26,44 +26,46 @@ export function Buy() {