This commit is contained in:
jejolare 2024-11-06 20:33:56 +07:00
commit 6a90dc0dd8
5 changed files with 124 additions and 2 deletions

17
package-lock.json generated
View file

@ -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",

View file

@ -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"

1
shared/src/index.ts Normal file
View file

@ -0,0 +1 @@
export * from "./utils";

89
shared/src/utils.ts Normal file
View file

@ -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
};
}

10
tsconfig.shared.json Normal file
View file

@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./shared/dist",
"declarationDir": "./shared/dist",
"rootDir": "./shared/src",
},
"include": ["shared/src/**/*"],
"exclude": ["node_modules"],
}