Skip to content

Commit

Permalink
remove error message for decimal values
Browse files Browse the repository at this point in the history
  • Loading branch information
gsteenkamp89 committed Feb 12, 2024
1 parent 2ac60ba commit a40a517
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/plugins/oSnap/components/Input/Amount.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ onMounted(() => {
watch(input, () => {
const value = format(input.value);
isValid.value = !!value;
const valid = !!value;
isValid.value = valid;
if (props.enforcePositiveValue) {
isValid.value = amountPositive(input.value);
const isPositive = amountPositive(input.value, props.decimals);
isValid.value = isPositive;
}
});
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
createTransferFundsTransaction,
getERC20TokenTransferTransactionData,
getNativeAsset,
isTransferFundsValid,
amountPositive
isTransferFundsValid
} from '../../utils';
import AddressInput from '../Input/Address.vue';
import AmountInput from '../Input/Amount.vue';
Expand Down
13 changes: 10 additions & 3 deletions src/plugins/oSnap/utils/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { extractMethodArgs, getABIWriteFunctions } from './abi';
import { isNativeAsset } from './coins';
import { FunctionFragment } from '@ethersproject/abi';
import { parseUnits } from '@ethersproject/units';

/**
* Validates that the given `address` is a valid Ethereum address
Expand Down Expand Up @@ -87,7 +88,7 @@ export function isTransferFundsValid(params: {
recipient: string;
amount: string;
}): boolean {
if (!amountPositive(params.amount)) {
if (!amountPositive(params.amount, params.token.decimals)) {
return false;
}
if (!params.recipient || !isAddress(params.recipient)) {
Expand Down Expand Up @@ -119,8 +120,14 @@ export function isTransferNftValid(params: {
return true;
}

export function amountPositive(amount: string) {
return isBigNumberish(amount) && parseFloat(amount) > 0;
export function amountPositive(amount: string, decimals = 18) {
try {
const isBigNumber = isBigNumberish(parseUnits(amount, decimals)); // checks for underflow
const isPositive = parseFloat(amount) > 0;
return isBigNumber && isPositive;
} catch {
return false;
}
}

export function allTransactionsValid(transactions: Transaction[]): boolean {
Expand Down

0 comments on commit a40a517

Please sign in to comment.