From 743275e21449e7d9623c4e31c926eae924e43b39 Mon Sep 17 00:00:00 2001 From: pedromcunha Date: Wed, 29 Nov 2023 17:17:16 -0500 Subject: [PATCH] Batch Listing: implement price restrictions --- components/portfolio/BatchListings.tsx | 32 ++++++++++--- .../portfolio/BatchListingsTableRow.tsx | 45 ++++++++++++++++--- 2 files changed, 65 insertions(+), 12 deletions(-) diff --git a/components/portfolio/BatchListings.tsx b/components/portfolio/BatchListings.tsx index 9e207fb61..f207829bf 100644 --- a/components/portfolio/BatchListings.tsx +++ b/components/portfolio/BatchListings.tsx @@ -71,6 +71,7 @@ type Props = { } const MINIMUM_AMOUNT = 0.000001 +const MAXIMUM_AMOUNT = Infinity const BatchListings: FC = ({ selectedItems, @@ -227,12 +228,31 @@ const BatchListings: FC = ({ }, [listings, onChainRoyaltiesMap, globalPrice]) const listButtonDisabled = useMemo(() => { - const hasInvalidPrice = listings.some( - (listing) => - listing.price === undefined || - listing.price === '' || - Number(listing.price) < MINIMUM_AMOUNT - ) + const hasInvalidPrice = listings.some((listing) => { + const minimumAmount = listing.exchange?.minPriceRaw + ? Number( + formatUnits( + BigInt(listing.exchange.minPriceRaw), + listing.currency?.decimals || 18 + ) + ) + : MINIMUM_AMOUNT + const maximumAmount = listing.exchange?.maxPriceRaw + ? Number( + formatUnits( + BigInt(listing.exchange.maxPriceRaw), + listing.currency?.decimals || 18 + ) + ) + : MAXIMUM_AMOUNT + + const withinPricingBounds = + listing.price && + Number(listing.price) !== 0 && + Number(listing.price) <= maximumAmount && + Number(listing.price) >= minimumAmount + return !withinPricingBounds + }) return hasInvalidPrice }, [listings]) diff --git a/components/portfolio/BatchListingsTableRow.tsx b/components/portfolio/BatchListingsTableRow.tsx index ca9a2c3c8..7c066ed9c 100644 --- a/components/portfolio/BatchListingsTableRow.tsx +++ b/components/portfolio/BatchListingsTableRow.tsx @@ -26,6 +26,8 @@ import { UserToken } from 'pages/portfolio/[[...address]]' import CryptoCurrencyIcon from 'components/primitives/CryptoCurrencyIcon' import { BatchListing } from './BatchListings' import optimizeImage from 'utils/optimizeImage' +import { formatUnits } from 'viem' +import { formatNumber } from 'utils/numbers' type BatchListingsTableRowProps = { listing: BatchListing @@ -44,6 +46,7 @@ type BatchListingsTableRowProps = { } const MINIMUM_AMOUNT = 0.000001 +const MAXIMUM_AMOUNT = Infinity export const BatchListingsTableRow: FC = ({ listing, @@ -179,8 +182,30 @@ export const BatchListingsTableRow: FC = ({ ) const restrictCurrency = - listing?.exchange?.paymentTokens && - listing.exchange.paymentTokens.length > 0 + listing.exchange?.paymentTokens && listing.exchange.paymentTokens.length > 0 + + const minimumAmount = listing.exchange?.minPriceRaw + ? Number( + formatUnits( + BigInt(listing.exchange.minPriceRaw), + listing.currency?.decimals || 18 + ) + ) + : MINIMUM_AMOUNT + const maximumAmount = listing.exchange?.maxPriceRaw + ? Number( + formatUnits( + BigInt(listing.exchange.maxPriceRaw), + listing.currency?.decimals || 18 + ) + ) + : MAXIMUM_AMOUNT + + const withinPricingBounds = + price && + Number(price) !== 0 && + Number(price) <= maximumAmount && + Number(price) >= minimumAmount return ( = ({ ))} )} - + = ({ onChange={(e) => { handlePriceChange(e.target.value) }} - css={{ width: 100, '@bp1500': { width: 150 } }} /> {price !== undefined && price !== '' && Number(price) !== 0 && - Number(price) < MINIMUM_AMOUNT && ( + !withinPricingBounds && ( - Must exceed {MINIMUM_AMOUNT} + {maximumAmount !== Infinity + ? `Amount must be between ${formatNumber( + minimumAmount + )} - ${formatNumber(maximumAmount)}` + : `Amount must be higher than ${formatNumber( + minimumAmount + )}`} )}