Skip to content

Commit

Permalink
Merge pull request #55 from lidofinance/develop
Browse files Browse the repository at this point in the history
Develop to main
  • Loading branch information
DiRaiks authored May 12, 2023
2 parents 9b130f3 + a6fb500 commit 55a2a04
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/http/nft/nft.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
14 changes: 8 additions & 6 deletions src/http/nft/nft.utils.ts
Original file line number Diff line number Diff line change
@@ -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)' : ''}`;
}
Expand All @@ -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` };
Expand Down

0 comments on commit 55a2a04

Please sign in to comment.