Skip to content

Commit

Permalink
Merge branch '4.x' into ok/6801-bsc-gasprice-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
avkos authored May 22, 2024
2 parents 43dd4c7 + e0fc158 commit 2cec997
Show file tree
Hide file tree
Showing 10 changed files with 351 additions and 109 deletions.
6 changes: 5 additions & 1 deletion packages/web3-errors/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,8 @@ Documentation:

- Fixed grammar and spelling in `transactionTimeoutHint` (#6559)

## [Unreleased]
## [Unreleased]

### Added

- Added `InvalidIntegerError` error for fromWei and toWei (#7052)
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_INTEGER = 1015;

// Validation error codes
export const ERR_VALIDATION = 1100;
Expand Down
10 changes: 10 additions & 0 deletions packages/web3-errors/src/errors/utils_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
ERR_INVALID_TYPE,
ERR_INVALID_TYPE_ABI,
ERR_INVALID_UNIT,
ERR_INVALID_INTEGER,
ERR_INVALID_UNSIGNED_INTEGER,
} from '../error_codes.js';
import { InvalidValueError } from '../web3_error_base.js';
Expand Down Expand Up @@ -75,6 +76,15 @@ export class InvalidUnitError extends InvalidValueError {
}
}

export class InvalidIntegerError extends InvalidValueError {
public code = ERR_INVALID_INTEGER;

public constructor(value: unknown) {
super(value, 'not a valid unit. Must be a positive integer');

}
}

export class HexProcessingError extends InvalidValueError {
public code = ERR_INVALID_HEX;

Expand Down
3 changes: 2 additions & 1 deletion packages/web3-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,9 @@ Documentation:
### Added

- `toWei` add warning when using large numbers or large decimals that may cause precision loss (#6908)
- `toWei` and `fromWei` now supports integers as a unit. (#7053)

### Fixed

- `toWei` support numbers in scientific notation (#6908)
- `toWei` and `fromWei` trims according to ether unit successfuly (#7044)
- `toWei` and `fromWei` trims according to ether unit successfuly (#7044)
39 changes: 29 additions & 10 deletions packages/web3-utils/src/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
utils,
utils as validatorUtils,
validator,
bigintPower,
} from 'web3-validator';

import {
Expand All @@ -41,6 +42,7 @@ import {
InvalidBytesError,
InvalidNumberError,
InvalidUnitError,
InvalidIntegerError,
} from 'web3-errors';
import { isUint8Array } from './uint8array.js';

Expand Down Expand Up @@ -492,13 +494,22 @@ export const toBigInt = (value: unknown): bigint => {
* > 0.000000001
* ```
*/
export const fromWei = (number: Numbers, unit: EtherUnits): string => {
const denomination = ethUnitMap[unit];
export const fromWei = (number: Numbers, unit: EtherUnits | number): string => {
let denomination;
if (typeof unit === 'string') {
denomination = ethUnitMap[unit];

if (!denomination) {
throw new InvalidUnitError(unit);
if (!denomination) {
throw new InvalidUnitError(unit);
}
} else {
if (unit < 0 || !Number.isInteger(unit)) {
throw new InvalidIntegerError(unit);
}
denomination = bigintPower(BigInt(10),BigInt(unit));
}


// value in wei would always be integer
// 13456789, 1234
const value = String(toNumber(number));
Expand Down Expand Up @@ -551,14 +562,23 @@ export const fromWei = (number: Numbers, unit: EtherUnits): string => {
* ```
*/
// todo in 1.x unit defaults to 'ether'
export const toWei = (number: Numbers, unit: EtherUnits): string => {
export const toWei = (number: Numbers, unit: EtherUnits | number): string => {
validator.validate(['number'], [number]);

const denomination = ethUnitMap[unit];

if (!denomination) {
throw new InvalidUnitError(unit);
let denomination;
if (typeof unit === 'string') {
denomination = ethUnitMap[unit];
if (!denomination) {
throw new InvalidUnitError(unit);
}
} else {
if (unit < 0 || !Number.isInteger(unit)) {
throw new InvalidIntegerError(unit);
}

denomination = bigintPower(BigInt(10),BigInt(unit));
}

let parsedNumber = number;
if (typeof parsedNumber === 'number') {
if (parsedNumber < 1e-15) {
Expand Down Expand Up @@ -589,7 +609,6 @@ export const toWei = (number: Numbers, unit: EtherUnits): string => {

// join the value removing `.` from
// 24.56 -> 2456

const value = BigInt(`${integer}${fraction}`);

// multiply value with denomination
Expand Down
Loading

0 comments on commit 2cec997

Please sign in to comment.