Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: solve amount validation bug #654

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading