Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit da99254

Browse files
author
Alex Luu
committed
add error
1 parent e64a3fe commit da99254

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

packages/web3-errors/src/error_codes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export const ERR_INVALID_LARGE_VALUE = 1011;
155155
export const ERR_INVALID_BLOCK = 1012;
156156
export const ERR_INVALID_TYPE_ABI = 1013;
157157
export const ERR_INVALID_NIBBLE_WIDTH = 1014;
158+
export const ERR_INVALID_NUMBER_DECIMAL_PRECISION_LOSS = 1015;
158159

159160
// Validation error codes
160161
export const ERR_VALIDATION = 1100;

packages/web3-errors/src/errors/utils_errors.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
ERR_INVALID_TYPE_ABI,
3333
ERR_INVALID_UNIT,
3434
ERR_INVALID_UNSIGNED_INTEGER,
35+
ERR_INVALID_NUMBER_DECIMAL_PRECISION_LOSS
3536
} from '../error_codes.js';
3637
import { InvalidValueError } from '../web3_error_base.js';
3738

@@ -146,3 +147,11 @@ export class InvalidTypeAbiInputError extends InvalidValueError {
146147
super(value, 'components found but type is not tuple');
147148
}
148149
}
150+
151+
export class InvalidNumberDecimalPrecisionLossError extends InvalidValueError {
152+
public code = ERR_INVALID_NUMBER_DECIMAL_PRECISION_LOSS;
153+
154+
public constructor(value: number) {
155+
super(value, 'value is too small to be represented accurately, use bigInt or string instead.');
156+
}
157+
}

packages/web3-utils/src/converters.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
InvalidBytesError,
4242
InvalidNumberError,
4343
InvalidUnitError,
44+
InvalidNumberDecimalPrecisionLossError
4445
} from 'web3-errors';
4546
import { isUint8Array } from './uint8array.js';
4647

@@ -411,6 +412,7 @@ export const toHex = (
411412
*/
412413
export const toNumber = (value: Numbers): number | bigint => {
413414
if (typeof value === 'number') {
415+
console.warn('Warning: Using type `number` with values that are large or contain many decimals may cause loss of precision, it is recommended to use type `string` or `BigInt` when using conversion methods')
414416
if (value > 1e+20) {
415417
// JavaScript converts numbers >= 10^21 to scientific notation when coerced to strings,
416418
// leading to potential parsing errors and incorrect representations.
@@ -551,10 +553,15 @@ export const toWei = (number: Numbers, unit: EtherUnits): string => {
551553
if (!denomination) {
552554
throw new InvalidUnitError(unit);
553555
}
556+
if (typeof number === 'number'){
557+
console.warn('Warning: The type `numbers` that are large or contain many decimals may cause loss of precision, it is recommended to use type `string` or `BigInt` when using conversion methods')
558+
if (number < 1e-15){
559+
throw new InvalidNumberDecimalPrecisionLossError(number);
560+
}
561+
}
554562

555563
// create error if decimal place is over 20 digits
556564
const parsedNumber = typeof number === 'number' ? number.toLocaleString('fullwide', {useGrouping: false, maximumFractionDigits: 20}) : number;
557-
// console.log(parsedNumber)
558565
// if value is decimal e.g. 24.56 extract `integer` and `fraction` part
559566
// to avoid `fraction` to be null use `concat` with empty string
560567
const [integer, fraction] = String(

packages/web3-utils/test/fixtures/converters.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ export const toWeiValidData: [[Numbers, EtherUnits], Numbers][] = [
290290
[['255', 'wei'], '0xFF'],
291291
[['255', 'wei'], '0xFF'],
292292
[['100000000000', 'ether'], 0.0000001],
293+
[['1000000000', 'ether'], 0.000000001],
294+
[['1000000', 'ether'], 0.000000000001]
295+
293296
];
294297

295298
export const fromWeiInvalidData: [[any, any], string][] = [
@@ -312,6 +315,7 @@ export const toWeiInvalidData: [[any, any], string][] = [
312315
[[{}, 'kwei'], 'value "{}" at "/0" must pass "number" validation'],
313316
[['data', 'kwei'], 'value "data" at "/0" must pass "number" validation'],
314317
[['1234', 'uwei'], 'Invalid value given "uwei". Error: invalid unit.'],
318+
[[0.0000000000000000000001, 'ether'], 'value is too small to be represented accurately, use bigInt or string instead.'],
315319
];
316320
export const toCheckSumValidData: [string, string][] = [
317321
['0x0089d53f703f7e0843953d48133f74ce247184c2', '0x0089d53F703f7E0843953D48133f74cE247184c2'],

0 commit comments

Comments
 (0)