Skip to content

Commit

Permalink
add error
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Luu committed Mar 25, 2024
1 parent e64a3fe commit da99254
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/web3-errors/src/error_codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export const ERR_INVALID_LARGE_VALUE = 1011;
export const ERR_INVALID_BLOCK = 1012;
export const ERR_INVALID_TYPE_ABI = 1013;
export const ERR_INVALID_NIBBLE_WIDTH = 1014;
export const ERR_INVALID_NUMBER_DECIMAL_PRECISION_LOSS = 1015;

// Validation error codes
export const ERR_VALIDATION = 1100;
Expand Down
9 changes: 9 additions & 0 deletions packages/web3-errors/src/errors/utils_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
ERR_INVALID_TYPE_ABI,
ERR_INVALID_UNIT,
ERR_INVALID_UNSIGNED_INTEGER,
ERR_INVALID_NUMBER_DECIMAL_PRECISION_LOSS
} from '../error_codes.js';
import { InvalidValueError } from '../web3_error_base.js';

Expand Down Expand Up @@ -146,3 +147,11 @@ export class InvalidTypeAbiInputError extends InvalidValueError {
super(value, 'components found but type is not tuple');
}
}

export class InvalidNumberDecimalPrecisionLossError extends InvalidValueError {
public code = ERR_INVALID_NUMBER_DECIMAL_PRECISION_LOSS;

Check warning on line 152 in packages/web3-errors/src/errors/utils_errors.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L152 was not covered by tests

public constructor(value: number) {
super(value, 'value is too small to be represented accurately, use bigInt or string instead.');

Check warning on line 155 in packages/web3-errors/src/errors/utils_errors.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-errors/src/errors/utils_errors.ts#L154-L155

Added lines #L154 - L155 were not covered by tests
}
}
9 changes: 8 additions & 1 deletion packages/web3-utils/src/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
InvalidBytesError,
InvalidNumberError,
InvalidUnitError,
InvalidNumberDecimalPrecisionLossError
} from 'web3-errors';
import { isUint8Array } from './uint8array.js';

Expand Down Expand Up @@ -411,6 +412,7 @@ export const toHex = (
*/
export const toNumber = (value: Numbers): number | bigint => {
if (typeof value === 'number') {
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')
if (value > 1e+20) {
// JavaScript converts numbers >= 10^21 to scientific notation when coerced to strings,
// leading to potential parsing errors and incorrect representations.
Expand Down Expand Up @@ -551,10 +553,15 @@ export const toWei = (number: Numbers, unit: EtherUnits): string => {
if (!denomination) {
throw new InvalidUnitError(unit);
}
if (typeof number === 'number'){
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')
if (number < 1e-15){
throw new InvalidNumberDecimalPrecisionLossError(number);
}
}

// create error if decimal place is over 20 digits
const parsedNumber = typeof number === 'number' ? number.toLocaleString('fullwide', {useGrouping: false, maximumFractionDigits: 20}) : number;
// console.log(parsedNumber)
// if value is decimal e.g. 24.56 extract `integer` and `fraction` part
// to avoid `fraction` to be null use `concat` with empty string
const [integer, fraction] = String(
Expand Down
4 changes: 4 additions & 0 deletions packages/web3-utils/test/fixtures/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ export const toWeiValidData: [[Numbers, EtherUnits], Numbers][] = [
[['255', 'wei'], '0xFF'],
[['255', 'wei'], '0xFF'],
[['100000000000', 'ether'], 0.0000001],
[['1000000000', 'ether'], 0.000000001],
[['1000000', 'ether'], 0.000000000001]

];

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

1 comment on commit da99254

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: da99254 Previous: 6c075db Ratio
processingTx 9162 ops/sec (±3.93%) 9301 ops/sec (±4.81%) 1.02
processingContractDeploy 37694 ops/sec (±8.36%) 39129 ops/sec (±7.62%) 1.04
processingContractMethodSend 18606 ops/sec (±7.01%) 19443 ops/sec (±5.19%) 1.04
processingContractMethodCall 37809 ops/sec (±5.06%) 38971 ops/sec (±6.34%) 1.03
abiEncode 40754 ops/sec (±6.88%) 44252 ops/sec (±6.92%) 1.09
abiDecode 29874 ops/sec (±5.90%) 30419 ops/sec (±8.89%) 1.02
sign 1522 ops/sec (±3.64%) 1656 ops/sec (±4.08%) 1.09
verify 372 ops/sec (±0.46%) 373 ops/sec (±0.78%) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.