Socket
Socket
Sign inDemoInstall

web3-utils

Package Overview
Dependencies
Maintainers
4
Versions
449
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

web3-utils - npm Package Compare versions

Comparing version 4.2.4-dev.32b6b29.0 to 4.2.4-dev.3904a46.0

4

lib/commonjs/converters.d.ts

@@ -254,3 +254,3 @@ import { utf8ToBytes as ecUtf8ToBytes } from 'ethereum-cryptography/utils.js';

*/
export declare const fromWei: (number: Numbers, unit: EtherUnits) => string;
export declare const fromWei: (number: Numbers, unit: EtherUnits | number) => string;
/**

@@ -269,3 +269,3 @@ * Takes a number of a unit and converts it to wei.

*/
export declare const toWei: (number: Numbers, unit: EtherUnits) => string;
export declare const toWei: (number: Numbers, unit: EtherUnits | number) => string;
/**

@@ -272,0 +272,0 @@ * Will convert an upper or lowercase Ethereum address to a checksum address.

@@ -443,6 +443,15 @@ "use strict";

const fromWei = (number, unit) => {
const denomination = exports.ethUnitMap[unit];
if (!denomination) {
throw new web3_errors_1.InvalidUnitError(unit);
let denomination;
if (typeof unit === 'string') {
denomination = exports.ethUnitMap[unit];
if (!denomination) {
throw new web3_errors_1.InvalidUnitError(unit);
}
}
else {
if (unit < 0 || !Number.isInteger(unit)) {
throw new web3_errors_1.InvalidIntegerError(unit);
}
denomination = BigInt(10) ** BigInt(unit);
}
// value in wei would always be integer

@@ -474,3 +483,4 @@ // 13456789, 1234

}
return `${integer}.${fraction}`;
const updatedValue = `${integer}.${fraction}`;
return updatedValue.slice(0, integer.length + numberOfZerosInDenomination + 1);
};

@@ -494,6 +504,15 @@ exports.fromWei = fromWei;

web3_validator_1.validator.validate(['number'], [number]);
const denomination = exports.ethUnitMap[unit];
if (!denomination) {
throw new web3_errors_1.InvalidUnitError(unit);
let denomination;
if (typeof unit === 'string') {
denomination = exports.ethUnitMap[unit];
if (!denomination) {
throw new web3_errors_1.InvalidUnitError(unit);
}
}
else {
if (unit < 0 || !Number.isInteger(unit)) {
throw new web3_errors_1.InvalidIntegerError(unit);
}
denomination = BigInt(10) ** BigInt(unit);
}
let parsedNumber = number;

@@ -504,3 +523,3 @@ if (typeof parsedNumber === 'number') {

}
if (parsedNumber > 1e+20) {
if (parsedNumber > 1e20) {
console.warn(PrecisionLossWarning);

@@ -511,3 +530,6 @@ parsedNumber = BigInt(parsedNumber);

// in case there is a decimal point, we need to convert it to string
parsedNumber = parsedNumber.toLocaleString('fullwide', { useGrouping: false, maximumFractionDigits: 20 });
parsedNumber = parsedNumber.toLocaleString('fullwide', {
useGrouping: false,
maximumFractionDigits: 20,
});
}

@@ -517,3 +539,5 @@ }

// to avoid `fraction` to be null use `concat` with empty string
const [integer, fraction] = String(typeof parsedNumber === 'string' && !(0, web3_validator_1.isHexStrict)(parsedNumber) ? parsedNumber : (0, exports.toNumber)(parsedNumber))
const [integer, fraction] = String(typeof parsedNumber === 'string' && !(0, web3_validator_1.isHexStrict)(parsedNumber)
? parsedNumber
: (0, exports.toNumber)(parsedNumber))
.split('.')

@@ -527,12 +551,9 @@ .concat('');

const updatedValue = value * denomination;
// count number of zeros in denomination
const numberOfZerosInDenomination = denomination.toString().length - 1;
// check which either `fraction` or `denomination` have lower number of zeros
const decimals = Math.min(fraction.length, numberOfZerosInDenomination);
// check if whole number was passed in
const decimals = fraction.length;
if (decimals === 0) {
return updatedValue.toString();
}
// Add zeros to make length equal to required decimal points
// If string is larger than decimal points required then remove last zeros
return updatedValue.toString().padStart(decimals, '0').slice(0, -decimals);
// trim the value to remove extra zeros
return updatedValue.toString().slice(0, -decimals);
};

@@ -539,0 +560,0 @@ exports.toWei = toWei;

@@ -23,3 +23,3 @@ /*

import { isAddress, isHex, isHexStrict, isInt, isUInt, isNullish, utils, utils as validatorUtils, validator, } from 'web3-validator';
import { HexProcessingError, InvalidAddressError, InvalidBooleanError, InvalidBytesError, InvalidNumberError, InvalidUnitError, } from 'web3-errors';
import { HexProcessingError, InvalidAddressError, InvalidBooleanError, InvalidBytesError, InvalidNumberError, InvalidUnitError, InvalidIntegerError, } from 'web3-errors';
import { isUint8Array } from './uint8array.js';

@@ -427,6 +427,15 @@ // Ref: https://ethdocs.org/en/latest/ether.html

export const fromWei = (number, unit) => {
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 = BigInt(10) ** BigInt(unit);
}
// value in wei would always be integer

@@ -458,3 +467,4 @@ // 13456789, 1234

}
return `${integer}.${fraction}`;
const updatedValue = `${integer}.${fraction}`;
return updatedValue.slice(0, integer.length + numberOfZerosInDenomination + 1);
};

@@ -477,6 +487,15 @@ /**

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 = BigInt(10) ** BigInt(unit);
}
let parsedNumber = number;

@@ -487,3 +506,3 @@ if (typeof parsedNumber === 'number') {

}
if (parsedNumber > 1e+20) {
if (parsedNumber > 1e20) {
console.warn(PrecisionLossWarning);

@@ -494,3 +513,6 @@ parsedNumber = BigInt(parsedNumber);

// in case there is a decimal point, we need to convert it to string
parsedNumber = parsedNumber.toLocaleString('fullwide', { useGrouping: false, maximumFractionDigits: 20 });
parsedNumber = parsedNumber.toLocaleString('fullwide', {
useGrouping: false,
maximumFractionDigits: 20,
});
}

@@ -500,3 +522,5 @@ }

// to avoid `fraction` to be null use `concat` with empty string
const [integer, fraction] = String(typeof parsedNumber === 'string' && !isHexStrict(parsedNumber) ? parsedNumber : toNumber(parsedNumber))
const [integer, fraction] = String(typeof parsedNumber === 'string' && !isHexStrict(parsedNumber)
? parsedNumber
: toNumber(parsedNumber))
.split('.')

@@ -510,12 +534,9 @@ .concat('');

const updatedValue = value * denomination;
// count number of zeros in denomination
const numberOfZerosInDenomination = denomination.toString().length - 1;
// check which either `fraction` or `denomination` have lower number of zeros
const decimals = Math.min(fraction.length, numberOfZerosInDenomination);
// check if whole number was passed in
const decimals = fraction.length;
if (decimals === 0) {
return updatedValue.toString();
}
// Add zeros to make length equal to required decimal points
// If string is larger than decimal points required then remove last zeros
return updatedValue.toString().padStart(decimals, '0').slice(0, -decimals);
// trim the value to remove extra zeros
return updatedValue.toString().slice(0, -decimals);
};

@@ -522,0 +543,0 @@ /**

@@ -254,3 +254,3 @@ import { utf8ToBytes as ecUtf8ToBytes } from 'ethereum-cryptography/utils.js';

*/
export declare const fromWei: (number: Numbers, unit: EtherUnits) => string;
export declare const fromWei: (number: Numbers, unit: EtherUnits | number) => string;
/**

@@ -269,3 +269,3 @@ * Takes a number of a unit and converts it to wei.

*/
export declare const toWei: (number: Numbers, unit: EtherUnits) => string;
export declare const toWei: (number: Numbers, unit: EtherUnits | number) => string;
/**

@@ -272,0 +272,0 @@ * Will convert an upper or lowercase Ethereum address to a checksum address.

{
"name": "web3-utils",
"sideEffects": false,
"version": "4.2.4-dev.32b6b29.0+32b6b29",
"version": "4.2.4-dev.3904a46.0+3904a46",
"description": "Collection of utility functions used in web3.js.",

@@ -68,7 +68,7 @@ "main": "./lib/commonjs/index.js",

"eventemitter3": "^5.0.1",
"web3-errors": "1.1.5-dev.32b6b29.0+32b6b29",
"web3-types": "1.6.1-dev.32b6b29.0+32b6b29",
"web3-validator": "2.0.6-dev.32b6b29.0+32b6b29"
"web3-errors": "1.1.5-dev.3904a46.0+3904a46",
"web3-types": "1.6.1-dev.3904a46.0+3904a46",
"web3-validator": "2.0.6-dev.3904a46.0+3904a46"
},
"gitHead": "32b6b296fffc7084f7688a136c6a0eaf6e369012"
"gitHead": "3904a4644e4e06c7192b5f39be8f7df63d0f7e4a"
}

@@ -44,2 +44,3 @@ /*

InvalidUnitError,
InvalidIntegerError,
} from 'web3-errors';

@@ -496,9 +497,18 @@ import { isUint8Array } from './uint8array.js';

*/
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 = BigInt(10)**BigInt(unit);
}
// value in wei would always be integer

@@ -537,4 +547,5 @@ // 13456789, 1234

}
const updatedValue = `${integer}.${fraction}`;
return `${integer}.${fraction}`;
return updatedValue.slice(0, integer.length + numberOfZerosInDenomination + 1);
};

@@ -556,29 +567,43 @@

// 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];
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 = BigInt(10)**BigInt(unit);
}
if (!denomination) {
throw new InvalidUnitError(unit);
}
let parsedNumber = number;
if (typeof parsedNumber === 'number'){
if (parsedNumber < 1e-15){
console.warn(PrecisionLossWarning)
if (typeof parsedNumber === 'number') {
if (parsedNumber < 1e-15) {
console.warn(PrecisionLossWarning);
}
if (parsedNumber > 1e+20) {
console.warn(PrecisionLossWarning)
if (parsedNumber > 1e20) {
console.warn(PrecisionLossWarning);
parsedNumber = BigInt(parsedNumber);
parsedNumber = BigInt(parsedNumber);
} else {
// in case there is a decimal point, we need to convert it to string
parsedNumber = parsedNumber.toLocaleString('fullwide', {useGrouping: false, maximumFractionDigits: 20})
parsedNumber = parsedNumber.toLocaleString('fullwide', {
useGrouping: false,
maximumFractionDigits: 20,
});
}
}
// 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(
typeof parsedNumber === 'string' && !isHexStrict(parsedNumber) ? parsedNumber : toNumber(parsedNumber),
typeof parsedNumber === 'string' && !isHexStrict(parsedNumber)
? parsedNumber
: toNumber(parsedNumber),
)

@@ -590,3 +615,3 @@ .split('.')

// 24.56 -> 2456
const value = BigInt(`${integer}${fraction}`);

@@ -598,8 +623,4 @@

// count number of zeros in denomination
const numberOfZerosInDenomination = denomination.toString().length - 1;
// check which either `fraction` or `denomination` have lower number of zeros
const decimals = Math.min(fraction.length, numberOfZerosInDenomination);
// check if whole number was passed in
const decimals = fraction.length;
if (decimals === 0) {

@@ -609,5 +630,4 @@ return updatedValue.toString();

// Add zeros to make length equal to required decimal points
// If string is larger than decimal points required then remove last zeros
return updatedValue.toString().padStart(decimals, '0').slice(0, -decimals);
// trim the value to remove extra zeros
return updatedValue.toString().slice(0, -decimals);
};

@@ -614,0 +634,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc