From 6e900b87cb237dac5e56782778a3ee4133e2638a Mon Sep 17 00:00:00 2001 From: Ben Goldberg Date: Tue, 19 Mar 2024 15:44:44 -0700 Subject: [PATCH] NFT Offers: crash fix (#5539) * fix * revert rename --- src/resources/reservoir/nftOffersQuery.ts | 24 +++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/resources/reservoir/nftOffersQuery.ts b/src/resources/reservoir/nftOffersQuery.ts index c51e26dd48e..c86ddff616e 100644 --- a/src/resources/reservoir/nftOffersQuery.ts +++ b/src/resources/reservoir/nftOffersQuery.ts @@ -98,23 +98,27 @@ export function useNFTOffers({ walletAddress }: { walletAddress: string }) { const offerVarianceArray = nftOffers.map(offer => offer.floorDifferencePercentage / 100); offerVarianceArray.sort((a, b) => a - b); - // calculate median offer variance + // calculate median floor difference percentage const middleIndex = Math.floor(offerVarianceArray.length / 2); - let medianVariance; - if (offerVarianceArray.length % 2 === 0) { - medianVariance = (offerVarianceArray[middleIndex - 1] + offerVarianceArray[middleIndex]) / 2; - } else { - medianVariance = offerVarianceArray[middleIndex]; + let medianFloorDifferencePercentage; + if (offerVarianceArray.length) { + if (offerVarianceArray.length % 2 === 0) { + medianFloorDifferencePercentage = (offerVarianceArray[middleIndex - 1] + offerVarianceArray[middleIndex]) / 2; + } else { + medianFloorDifferencePercentage = offerVarianceArray[middleIndex]; + } } - // calculate mean offer variance - const meanVariance = offerVarianceArray.reduce((acc, cur) => acc + cur, 0) / offerVarianceArray.length; + // calculate mean floor difference percentage + const meanFloorDifferencePercentage = offerVarianceArray.length + ? offerVarianceArray.reduce((acc, cur) => acc + cur, 0) / offerVarianceArray.length + : undefined; analyticsV2.identify({ nftOffersAmount: nftOffers.length, nftOffersUSDValue: totalUSDValue, - nftOffersMedianOfferVariance: medianVariance, - nftOffersMeanOfferVariance: meanVariance, + nftOffersMedianOfferVariance: medianFloorDifferencePercentage, + nftOffersMeanOfferVariance: meanFloorDifferencePercentage, }); }, [query.data?.nftOffers]);