diff --git a/src/http/nft/nft.constants.ts b/src/http/nft/nft.constants.ts index 6578bcc..2b1097f 100644 --- a/src/http/nft/nft.constants.ts +++ b/src/http/nft/nft.constants.ts @@ -4,3 +4,4 @@ export const META_DATA_DESC = export const SVG_ID_LENGTH = 17; export const MAX_AMOUNT_IN_ETH = 1000; +export const MIN_AMOUNT_IN_WEI = 100; diff --git a/src/http/nft/nft.utils.ts b/src/http/nft/nft.utils.ts index 7988fb4..fdf804f 100644 --- a/src/http/nft/nft.utils.ts +++ b/src/http/nft/nft.utils.ts @@ -1,16 +1,16 @@ import { formatUnits } from 'ethers'; -import { MAX_AMOUNT_IN_ETH } from './nft.constants'; +import { MAX_AMOUNT_IN_ETH, MIN_AMOUNT_IN_WEI } from './nft.constants'; export const convertFromWei = (amountInWei: string, prefix?: string): string => { const convertedInWei = parseFloat(formatUnits(amountInWei.toString(), 'wei')); - const amountInGwei = parseFloat(formatUnits(amountInWei.toString(), 'gwei')); - const amountInEth = parseFloat(formatUnits(amountInWei.toString(), 'ether')); + const amountInGwei = Math.floor(Number(formatUnits(amountInWei.toString(), 'gwei')) * 10000) / 10000; + const amountInEth = Math.floor(Number(formatUnits(amountInWei.toString(), 'ether').slice(0, 16)) * 10000) / 10000; if (amountInEth > 0.00009) { - return `${parseFloat(String(Math.floor(amountInEth * 10000) / 10000))} ${prefix ? prefix : ''}ETH`; - } else if (amountInGwei >= 1) { - return `${parseFloat(amountInGwei.toFixed(2))} GWEI${prefix ? '(STETH)' : ''}`; + return `${parseFloat(String(amountInEth))} ${prefix ? prefix : ''}ETH`; + } else if (amountInGwei > 0.00009) { + return `${parseFloat(String(amountInGwei))} GWEI${prefix ? '(STETH)' : ''}`; } else { return `${convertedInWei} WEI${prefix ? '(STETH)' : ''}`; } @@ -19,8 +19,10 @@ export const convertFromWei = (amountInWei: string, prefix?: string): string => export const validateWeiAmount = (amount: string, key: string): { isValid: boolean; message?: string } => { try { const amountInEth = parseFloat(formatUnits(amount.toString(), 'ether')); + const amountInWei = parseFloat(formatUnits(amount.toString(), 'wei')); if (amountInEth > MAX_AMOUNT_IN_ETH) return { isValid: false, message: `${key} is too big` }; + if (amountInWei < MIN_AMOUNT_IN_WEI) return { isValid: false, message: `${key} is too small` }; else return { isValid: true }; } catch (error) { return { isValid: false, message: `${key} is not valid` };