zano_web3 7.0.0 → 7.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -2
- package/shared/src/index.ts +1 -0
- package/shared/src/utils.ts +89 -0
- package/tsconfig.shared.json +10 -0
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zano_web3",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.1.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"build:web": "tsc --project tsconfig.web.json",
|
|
8
8
|
"build:server": "tsc --project tsconfig.server.json",
|
|
9
|
-
"build": "
|
|
9
|
+
"build:shared": "tsc --project tsconfig.shared.json",
|
|
10
|
+
"build": "npm run build:web && npm run build:server && npm run build:shared",
|
|
10
11
|
"postinstall": "npm run build"
|
|
11
12
|
},
|
|
12
13
|
"repository": {
|
|
@@ -29,6 +30,7 @@
|
|
|
29
30
|
"@types/uuid": "^10.0.0",
|
|
30
31
|
"axios": "^1.7.2",
|
|
31
32
|
"big.js": "^6.2.1",
|
|
33
|
+
"decimal.js": "^10.4.3",
|
|
32
34
|
"react": "^18.3.1",
|
|
33
35
|
"typescript": "^5.5.4",
|
|
34
36
|
"uuid": "^10.0.0"
|
|
@@ -46,6 +48,11 @@
|
|
|
46
48
|
"import": "./server/dist/index.js",
|
|
47
49
|
"require": "./server/dist/index.js",
|
|
48
50
|
"types": "./server/dist/index.d.ts"
|
|
51
|
+
},
|
|
52
|
+
"./shared": {
|
|
53
|
+
"import": "./shared/dist/index.js",
|
|
54
|
+
"require": "./shared/dist/index.js",
|
|
55
|
+
"types": "./shared/dist/index.d.ts"
|
|
49
56
|
}
|
|
50
57
|
},
|
|
51
58
|
"homepage": "https://github.com/hyle-team/zano_web3#readme"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./utils";
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import Decimal from "decimal.js";
|
|
2
|
+
|
|
3
|
+
export function validateTokensInput(input: string | number, decimal_point: number = 12) {
|
|
4
|
+
|
|
5
|
+
if (typeof input === 'number') {
|
|
6
|
+
input = input.toString();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (input === "") {
|
|
10
|
+
return {
|
|
11
|
+
valid: false,
|
|
12
|
+
error: 'Invalid input',
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
input = input.replace(/[^0-9.,]/g, '');
|
|
17
|
+
|
|
18
|
+
const MAX_NUMBER = new Decimal(2).pow(64).minus(1);
|
|
19
|
+
|
|
20
|
+
if (decimal_point < 0 || decimal_point > 18) {
|
|
21
|
+
return {
|
|
22
|
+
valid: false,
|
|
23
|
+
error: 'Invalid decimal point',
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const dotInput = input.replace(/,/g, '');
|
|
28
|
+
|
|
29
|
+
const decimalDevider = new Decimal(10).pow(decimal_point);
|
|
30
|
+
|
|
31
|
+
const maxAllowedNumber = MAX_NUMBER.div(decimalDevider);
|
|
32
|
+
|
|
33
|
+
const minAllowedNumber = new Decimal(1).div(decimalDevider);
|
|
34
|
+
|
|
35
|
+
const rounded = (() => {
|
|
36
|
+
if (dotInput.replace('.', '').length > 20) {
|
|
37
|
+
const decimalParts = dotInput.split('.');
|
|
38
|
+
|
|
39
|
+
if (decimalParts.length === 2 && decimalParts[1].length > 1) {
|
|
40
|
+
const beforeDotLength = decimalParts[0].length;
|
|
41
|
+
const roundedInput = new Decimal(dotInput).toFixed(Math.max(20 - beforeDotLength, 0));
|
|
42
|
+
|
|
43
|
+
if (roundedInput.replace(/./g, '').length <= 20) {
|
|
44
|
+
return roundedInput;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return false;
|
|
49
|
+
} else {
|
|
50
|
+
return dotInput;
|
|
51
|
+
}
|
|
52
|
+
})();
|
|
53
|
+
|
|
54
|
+
const decimalsAmount = dotInput.split('.')[1]?.length || 0;
|
|
55
|
+
|
|
56
|
+
if (decimalsAmount > decimal_point) {
|
|
57
|
+
return {
|
|
58
|
+
valid: false,
|
|
59
|
+
error: 'Invalid amount - too many decimal points',
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (rounded === false) {
|
|
64
|
+
return {
|
|
65
|
+
valid: false,
|
|
66
|
+
error: 'Invalid amount - number is too big or has too many decimal points',
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const dotInputDecimal = new Decimal(rounded);
|
|
71
|
+
|
|
72
|
+
if (dotInputDecimal.gt(maxAllowedNumber)) {
|
|
73
|
+
return {
|
|
74
|
+
valid: false,
|
|
75
|
+
error: 'Invalid amount - number is too big',
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (dotInputDecimal.lt(minAllowedNumber)) {
|
|
80
|
+
return {
|
|
81
|
+
valid: false,
|
|
82
|
+
error: 'Invalid amount - number is too small',
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
valid: true
|
|
88
|
+
};
|
|
89
|
+
}
|