Skip to content

Commit

Permalink
Merge pull request #654 from syscoin/fix/evm-amount-validation
Browse files Browse the repository at this point in the history
fix: solve amount validation bug
  • Loading branch information
lucasgabrielgsp authored Oct 16, 2024
2 parents d478cb8 + 3a1e9d2 commit d4c4873
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions source/pages/Send/SendEth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { useSelector } from 'react-redux';

import { isValidEthereumAddress } from '@pollum-io/sysweb3-utils';

import errorIcon from 'assets/icons/errorIcon.svg';
import successIcon from 'assets/icons/successIcon.svg';
import { Card, Layout, Button } from 'components/index';
import { useUtils } from 'hooks/index';
import { useController } from 'hooks/useController';
Expand Down Expand Up @@ -64,9 +66,22 @@ export const SendEth = () => {
const validAddress = isValidEthereumAddress(value);
setIsValidAddress(validAddress);
} else if (name === 'amount') {
const balance = selectedAsset
? selectedAsset.balance
: Number(activeAccount?.balances.ethereum);

const validAmount =
value <= Number(activeAccount?.balances.ethereum) && value > 0;
setIsValidAmount(validAmount);
Number(value) > 0 && parseFloat(value) <= parseFloat(balance);

const isValidForSelectedAsset = selectedAsset
? selectedAsset.isNft || (!selectedAsset.isNft && validAmount)
: validAmount;

if (isValidForSelectedAsset) {
setIsValidAmount(true);
} else {
setIsValidAmount(false);
}
} else {
setIsValidAddress(null);
}
Expand Down Expand Up @@ -276,14 +291,10 @@ export const SendEth = () => {
/>
{isValidAddress !== null && (
<img
src={
isValidAddress === true
? '/assets/icons/successIcon.svg'
: '/assets/icons/errorIcon.svg'
}
alt={isValidAddress === true ? 'Success' : 'Error'}
src={isValidAddress ? successIcon : errorIcon}
alt={isValidAddress ? 'Success' : 'Error'}
className={`absolute right-8 ${
isValidAmount === true ? 'top-[12.5px]' : 'top-[11.5px]'
isValidAmount ? 'top-[12.5px]' : 'top-[11.5px]'
}`}
/>
)}
Expand Down Expand Up @@ -398,28 +409,32 @@ export const SendEth = () => {
? selectedAsset.balance
: Number(activeAccount?.balances.ethereum);

if (
!selectedAsset &&
parseFloat(value) <= parseFloat(balance) &&
Number(value) > 0
) {
const isValueLowerThanBalance = selectedAsset
? parseFloat(value) <= parseFloat(selectedAsset.balance)
: parseFloat(value) <= parseFloat(balance);

const isToken = !!selectedAsset;

const isValidValue = Number(value) > 0;

const isAssetSelectedWithValidBalance =
isToken && isValidValue;

const isNFT = selectedAsset.isNft;

const isValidNFTValue =
isAssetSelectedWithValidBalance && isNFT;

const isValidTokenValue =
isAssetSelectedWithValidBalance &&
!isNFT &&
isValueLowerThanBalance;

if (!isToken && isValueLowerThanBalance && isValidValue) {
return Promise.resolve();
}

if (
Boolean(
selectedAsset &&
selectedAsset.isNft &&
Number(value) > 0
) ||
Boolean(
selectedAsset &&
!selectedAsset.isNft &&
parseFloat(value) <=
parseFloat(selectedAsset.balance) &&
Number(value) > 0
)
) {
if (isValidNFTValue || isValidTokenValue) {
return Promise.resolve();
}

Expand Down

0 comments on commit d4c4873

Please sign in to comment.