From 70362c68e136d27fc0fd71822b96935ddaa25383 Mon Sep 17 00:00:00 2001 From: andrewprog97 Date: Sun, 20 Oct 2024 19:03:20 +0300 Subject: [PATCH] add validateTokensInput shared function --- package-lock.json | 17 ++++++++- package.json | 9 ++++- shared/src/index.ts | 1 + shared/src/utils.ts | 89 ++++++++++++++++++++++++++++++++++++++++++++ tsconfig.shared.json | 10 +++++ 5 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 shared/src/index.ts create mode 100644 shared/src/utils.ts create mode 100644 tsconfig.shared.json diff --git a/package-lock.json b/package-lock.json index a912651..98d3162 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "@types/uuid": "^10.0.0", "axios": "^1.7.2", "big.js": "^6.2.1", + "decimal.js": "^10.4.3", "react": "^18.3.1", "typescript": "^5.5.4", "uuid": "^10.0.0" @@ -96,6 +97,12 @@ "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz", "integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==" }, + "node_modules/decimal.js": { + "version": "10.4.3", + "resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.4.3.tgz", + "integrity": "sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA==", + "license": "MIT" + }, "node_modules/delayed-stream": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", @@ -139,12 +146,14 @@ "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", - "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", + "license": "MIT" }, "node_modules/loose-envify": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", + "license": "MIT", "dependencies": { "js-tokens": "^3.0.0 || ^4.0.0" }, @@ -180,6 +189,7 @@ "version": "18.3.1", "resolved": "https://registry.npmjs.org/react/-/react-18.3.1.tgz", "integrity": "sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==", + "license": "MIT", "dependencies": { "loose-envify": "^1.1.0" }, @@ -283,6 +293,11 @@ "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz", "integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==" }, + "decimal.js": { + "version": "10.4.3", + "resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.4.3.tgz", + "integrity": "sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA==" + }, "delayed-stream": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", diff --git a/package.json b/package.json index d5fbbbd..cce5fd4 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "scripts": { "build:web": "tsc --project tsconfig.web.json", "build:server": "tsc --project tsconfig.server.json", - "build": "npm run build:web && npm run build:server", + "build:shared": "tsc --project tsconfig.shared.json", + "build": "npm run build:web && npm run build:server && npm run build:shared", "postinstall": "npm run build" }, "repository": { @@ -29,6 +30,7 @@ "@types/uuid": "^10.0.0", "axios": "^1.7.2", "big.js": "^6.2.1", + "decimal.js": "^10.4.3", "react": "^18.3.1", "typescript": "^5.5.4", "uuid": "^10.0.0" @@ -46,6 +48,11 @@ "import": "./server/dist/index.js", "require": "./server/dist/index.js", "types": "./server/dist/index.d.ts" + }, + "./shared": { + "import": "./shared/dist/index.js", + "require": "./shared/dist/index.js", + "types": "./shared/dist/index.d.ts" } }, "homepage": "https://github.com/hyle-team/zano_web3#readme" diff --git a/shared/src/index.ts b/shared/src/index.ts new file mode 100644 index 0000000..037b90c --- /dev/null +++ b/shared/src/index.ts @@ -0,0 +1 @@ +export * from "./utils"; \ No newline at end of file diff --git a/shared/src/utils.ts b/shared/src/utils.ts new file mode 100644 index 0000000..86bc8c4 --- /dev/null +++ b/shared/src/utils.ts @@ -0,0 +1,89 @@ +import Decimal from "decimal.js"; + +export function validateTokensInput(input: string | number, decimal_point: number = 12) { + + if (typeof input === 'number') { + input = input.toString(); + } + + if (input === "") { + return { + valid: false, + error: 'Invalid input', + }; + } + + input = input.replace(/[^0-9.,]/g, ''); + + const MAX_NUMBER = new Decimal(2).pow(64).minus(1); + + if (decimal_point < 0 || decimal_point > 18) { + return { + valid: false, + error: 'Invalid decimal point', + }; + } + + const dotInput = input.replace(/,/g, ''); + + const decimalDevider = new Decimal(10).pow(decimal_point); + + const maxAllowedNumber = MAX_NUMBER.div(decimalDevider); + + const minAllowedNumber = new Decimal(1).div(decimalDevider); + + const rounded = (() => { + if (dotInput.replace('.', '').length > 20) { + const decimalParts = dotInput.split('.'); + + if (decimalParts.length === 2 && decimalParts[1].length > 1) { + const beforeDotLength = decimalParts[0].length; + const roundedInput = new Decimal(dotInput).toFixed(Math.max(20 - beforeDotLength, 0)); + + if (roundedInput.replace(/./g, '').length <= 20) { + return roundedInput; + } + } + + return false; + } else { + return dotInput; + } + })(); + + const decimalsAmount = dotInput.split('.')[1]?.length || 0; + + if (decimalsAmount > decimal_point) { + return { + valid: false, + error: 'Invalid amount - too many decimal points', + }; + } + + if (rounded === false) { + return { + valid: false, + error: 'Invalid amount - number is too big or has too many decimal points', + }; + } + + const dotInputDecimal = new Decimal(rounded); + + if (dotInputDecimal.gt(maxAllowedNumber)) { + return { + valid: false, + error: 'Invalid amount - number is too big', + }; + } + + if (dotInputDecimal.lt(minAllowedNumber)) { + return { + valid: false, + error: 'Invalid amount - number is too small', + }; + } + + return { + valid: true + }; +} \ No newline at end of file diff --git a/tsconfig.shared.json b/tsconfig.shared.json new file mode 100644 index 0000000..54819c1 --- /dev/null +++ b/tsconfig.shared.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "./shared/dist", + "declarationDir": "./shared/dist", + "rootDir": "./shared/src", + }, + "include": ["shared/src/**/*"], + "exclude": ["node_modules"], + } \ No newline at end of file