fix up coin validation

This commit is contained in:
fmtabbara
2022-06-09 12:15:29 +01:00
parent 2a4b80062d
commit b15292e403
2 changed files with 21 additions and 17 deletions
+6 -11
View File
@@ -1,7 +1,7 @@
import { appWindow } from '@tauri-apps/api/window';
import bs58 from 'bs58';
import { valid } from 'semver';
import { basicRawCoinValueValidation, MajorAmountString } from '@nymproject/types';
import { isValidRawCoin, MajorAmountString } from '@nymproject/types';
import { getLockedCoins, getSpendableCoins, userBalance } from '../requests';
import { Console } from './console';
@@ -26,19 +26,14 @@ export const validateAmount = async (
return false;
}
try {
if (!basicRawCoinValueValidation(majorAmountAsString)) {
return false;
}
const majorValueFloat = parseInt(majorAmountAsString, Number(10));
return majorValueFloat >= parseInt(minimumAmountAsString, Number(10));
} catch (e) {
Console.error(e as string);
if (!isValidRawCoin(majorAmountAsString)) {
return false;
}
const majorValueFloat = parseInt(majorAmountAsString, Number(10));
return majorValueFloat >= parseInt(minimumAmountAsString, Number(10));
// this conversion seems really iffy but I'm not sure how to better approach it
};
+15 -6
View File
@@ -1,16 +1,25 @@
export const basicRawCoinValueValidation = (rawAmount: string): boolean => {
export const isValidRawCoin = (rawAmount: string): boolean => {
const amountFloat = parseFloat(rawAmount);
// it cannot have more than 6 decimal places
if (amountFloat !== parseInt(amountFloat.toFixed(6), Number(10))) {
return false;
// if value is a decimal it cannot have more than 6 decimal places
if (amountFloat % 1 > 0) {
const [_, numsAfterDecimal] = rawAmount.split('.');
if (+numsAfterDecimal.length > 6) {
return false;
}
}
// it cannot be larger than the total supply
if (amountFloat > 1_000_000_000_000_000) {
if (amountFloat > 1_000_000_000) {
return false;
}
console.log(amountFloat);
// it can't be lower than one micro coin
return amountFloat >= 0.000001;
if (amountFloat < 0.000001) {
return false;
}
return true;
};