web3-utils
Advanced tools
Comparing version 4.2.4-dev.93296c2.0 to 4.2.4-dev.ac2e180.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. |
@@ -60,2 +60,3 @@ "use strict"; | ||
}; | ||
const PrecisionLossWarning = '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'; | ||
/** | ||
@@ -373,2 +374,3 @@ * Convert a value from bytes to Uint8Array | ||
if (value > 1e+20) { | ||
console.warn(PrecisionLossWarning); | ||
// JavaScript converts numbers >= 10^21 to scientific notation when coerced to strings, | ||
@@ -443,6 +445,15 @@ // leading to potential parsing errors and incorrect representations. | ||
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 = (0, web3_validator_1.bigintPower)(BigInt(10), BigInt(unit)); | ||
} | ||
// value in wei would always be integer | ||
@@ -474,3 +485,4 @@ // 13456789, 1234 | ||
} | ||
return `${integer}.${fraction}`; | ||
const updatedValue = `${integer}.${fraction}`; | ||
return updatedValue.slice(0, integer.length + numberOfZerosInDenomination + 1); | ||
}; | ||
@@ -494,9 +506,37 @@ 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 = (0, web3_validator_1.bigintPower)(BigInt(10), BigInt(unit)); | ||
} | ||
let parsedNumber = number; | ||
if (typeof parsedNumber === 'number') { | ||
if (parsedNumber < 1e-15) { | ||
console.warn(PrecisionLossWarning); | ||
} | ||
if (parsedNumber > 1e20) { | ||
console.warn(PrecisionLossWarning); | ||
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, | ||
}); | ||
} | ||
} | ||
// 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 number === 'string' && !(0, web3_validator_1.isHexStrict)(number) ? number : (0, exports.toNumber)(number)) | ||
const [integer, fraction] = String(typeof parsedNumber === 'string' && !(0, web3_validator_1.isHexStrict)(parsedNumber) | ||
? parsedNumber | ||
: (0, exports.toNumber)(parsedNumber)) | ||
.split('.') | ||
@@ -510,12 +550,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 +559,0 @@ exports.toWei = toWei; |
@@ -22,4 +22,4 @@ /* | ||
import { bytesToUtf8, utf8ToBytes as ecUtf8ToBytes } from 'ethereum-cryptography/utils.js'; | ||
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 { isAddress, isHex, isHexStrict, isInt, isUInt, isNullish, utils, utils as validatorUtils, validator, bigintPower, } from 'web3-validator'; | ||
import { HexProcessingError, InvalidAddressError, InvalidBooleanError, InvalidBytesError, InvalidNumberError, InvalidUnitError, InvalidIntegerError, } from 'web3-errors'; | ||
import { isUint8Array } from './uint8array.js'; | ||
@@ -58,2 +58,3 @@ // Ref: https://ethdocs.org/en/latest/ether.html | ||
}; | ||
const PrecisionLossWarning = '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'; | ||
/** | ||
@@ -359,2 +360,3 @@ * Convert a value from bytes to Uint8Array | ||
if (value > 1e+20) { | ||
console.warn(PrecisionLossWarning); | ||
// JavaScript converts numbers >= 10^21 to scientific notation when coerced to strings, | ||
@@ -427,6 +429,15 @@ // leading to potential parsing errors and incorrect representations. | ||
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 = bigintPower(BigInt(10), BigInt(unit)); | ||
} | ||
// value in wei would always be integer | ||
@@ -458,3 +469,4 @@ // 13456789, 1234 | ||
} | ||
return `${integer}.${fraction}`; | ||
const updatedValue = `${integer}.${fraction}`; | ||
return updatedValue.slice(0, integer.length + numberOfZerosInDenomination + 1); | ||
}; | ||
@@ -477,9 +489,37 @@ /** | ||
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) { | ||
console.warn(PrecisionLossWarning); | ||
} | ||
if (parsedNumber > 1e20) { | ||
console.warn(PrecisionLossWarning); | ||
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, | ||
}); | ||
} | ||
} | ||
// 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 number === 'string' && !isHexStrict(number) ? number : toNumber(number)) | ||
const [integer, fraction] = String(typeof parsedNumber === 'string' && !isHexStrict(parsedNumber) | ||
? parsedNumber | ||
: toNumber(parsedNumber)) | ||
.split('.') | ||
@@ -493,12 +533,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); | ||
}; | ||
@@ -505,0 +542,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.93296c2.0+93296c2", | ||
"version": "4.2.4-dev.ac2e180.0+ac2e180", | ||
"description": "Collection of utility functions used in web3.js.", | ||
@@ -34,3 +34,3 @@ "main": "./lib/commonjs/index.js", | ||
"build:check": "node -e \"require('./lib')\"", | ||
"lint": "eslint --ext .js,.ts .", | ||
"lint": "eslint --cache --cache-strategy content --ext .ts .", | ||
"lint:fix": "eslint --fix --ext .js,.ts .", | ||
@@ -69,7 +69,7 @@ "format": "prettier --write '**/*'", | ||
"eventemitter3": "^5.0.1", | ||
"web3-errors": "1.1.5-dev.93296c2.0+93296c2", | ||
"web3-types": "1.6.1-dev.93296c2.0+93296c2", | ||
"web3-validator": "2.0.6-dev.93296c2.0+93296c2" | ||
"web3-errors": "1.1.5-dev.ac2e180.0+ac2e180", | ||
"web3-types": "1.6.1-dev.ac2e180.0+ac2e180", | ||
"web3-validator": "2.0.6-dev.ac2e180.0+ac2e180" | ||
}, | ||
"gitHead": "93296c2e0ec299c506591d790236e5dd96291757" | ||
"gitHead": "ac2e180c3db5e734c40f994edc39382542afc289" | ||
} |
@@ -35,2 +35,3 @@ /* | ||
validator, | ||
bigintPower, | ||
} from 'web3-validator'; | ||
@@ -45,2 +46,3 @@ | ||
InvalidUnitError, | ||
InvalidIntegerError, | ||
} from 'web3-errors'; | ||
@@ -82,2 +84,4 @@ import { isUint8Array } from './uint8array.js'; | ||
const PrecisionLossWarning = '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'; | ||
export type EtherUnits = keyof typeof ethUnitMap; | ||
@@ -421,3 +425,4 @@ /** | ||
if (typeof value === 'number') { | ||
if (value > 1e+20) { | ||
if (value > 1e+20) { | ||
console.warn(PrecisionLossWarning) | ||
// JavaScript converts numbers >= 10^21 to scientific notation when coerced to strings, | ||
@@ -496,9 +501,18 @@ // leading to potential parsing errors and incorrect representations. | ||
*/ | ||
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 | ||
@@ -537,4 +551,5 @@ // 13456789, 1234 | ||
} | ||
const updatedValue = `${integer}.${fraction}`; | ||
return `${integer}.${fraction}`; | ||
return updatedValue.slice(0, integer.length + numberOfZerosInDenomination + 1); | ||
}; | ||
@@ -556,9 +571,35 @@ | ||
// 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 = bigintPower(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 (parsedNumber > 1e20) { | ||
console.warn(PrecisionLossWarning); | ||
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, | ||
}); | ||
} | ||
} | ||
@@ -569,3 +610,5 @@ | ||
const [integer, fraction] = String( | ||
typeof number === 'string' && !isHexStrict(number) ? number : toNumber(number), | ||
typeof parsedNumber === 'string' && !isHexStrict(parsedNumber) | ||
? parsedNumber | ||
: toNumber(parsedNumber), | ||
) | ||
@@ -583,8 +626,4 @@ .split('.') | ||
// 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) { | ||
@@ -594,5 +633,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); | ||
}; | ||
@@ -599,0 +637,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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
529230
10041