Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
A simple module for handling Ethereum units (e.g. 'ether', 'wei', etc...)
The ethjs-unit npm package is a utility library for Ethereum that provides functions to convert between different Ethereum units. It is particularly useful for developers working with Ethereum smart contracts and dApps, as it simplifies the process of handling various units of Ether and other Ethereum-based tokens.
Convert Ether to Wei
This feature allows you to convert Ether to Wei, which is the smallest unit of Ether. The code sample demonstrates converting 1 Ether to Wei.
const ethjsUnit = require('ethjs-unit');
const weiValue = ethjsUnit.toWei(1, 'ether');
console.log(weiValue); // '1000000000000000000'
Convert Wei to Ether
This feature allows you to convert Wei back to Ether. The code sample demonstrates converting 1,000,000,000,000,000,000 Wei to 1 Ether.
const ethjsUnit = require('ethjs-unit');
const etherValue = ethjsUnit.fromWei('1000000000000000000', 'ether');
console.log(etherValue); // '1'
Convert between other units
This feature allows you to convert between various other units such as Gwei, Kwei, Mwei, etc. The code sample demonstrates converting 1 Gwei to Wei and back.
const ethjsUnit = require('ethjs-unit');
const gweiValue = ethjsUnit.toWei(1, 'gwei');
console.log(gweiValue); // '1000000000'
const etherFromGwei = ethjsUnit.fromWei('1000000000', 'gwei');
console.log(etherFromGwei); // '1'
The web3-utils package is part of the web3.js library, which is a comprehensive collection of modules that allow you to interact with the Ethereum blockchain. The web3-utils module includes functions for unit conversion similar to ethjs-unit, but it also provides a broader range of utility functions for Ethereum development.
The ethers library is a complete and compact library for interacting with the Ethereum blockchain and its ecosystem. It includes utilities for unit conversion, similar to ethjs-unit, but also offers a wide range of other functionalities such as interacting with smart contracts, managing wallets, and more.
The bignumber.js library is a JavaScript library for arbitrary-precision decimal and non-decimal arithmetic. While it is not specific to Ethereum, it is often used in conjunction with Ethereum libraries to handle large numbers, such as those involved in unit conversions. It provides a more general-purpose solution compared to ethjs-unit.
A simple module for handling Ethereum unit convertion.
npm install --save ethjs-unit
const unit = require('ethjs-unit');
var val1 = unit.toWei(249824778, 'ether');
// result <BN ...> 249824778000000000000000000
var val2 = unit.fromWei('249824778000000000000000000', 'ether');
// result '249824778'
A port from the web3.js
library, that just handles the unit convertion between the various types of Ethereum currency units.
Note, the toWei
returns a BN instance while fromWei
always returns a string number.
ethjs-unit
uses the number-to-bn module to format all number values (hex or otherwise) into digestable BN.js number instances.
unitMap { unitName: singleUnitWeiValue, ... }
getValueOfUnit <Function (unit) : (BN)>
toWei <Function (value, unit) : (BN)>
fromWei <Function (value, unit) : (String)>
'wei': '1',
'kwei': '1000',
'Kwei': '1000',
'babbage': '1000',
'femtoether': '1000',
'mwei': '1000000',
'Mwei': '1000000',
'lovelace': '1000000',
'picoether': '1000000',
'gwei': '1000000000',
'Gwei': '1000000000',
'shannon': '1000000000',
'nanoether': '1000000000',
'nano': '1000000000',
'szabo': '1000000000000',
'microether': '1000000000000',
'micro': '1000000000000',
'finney': '1000000000000000',
'milliether': '1000000000000000',
'milli': '1000000000000000',
'ether': '1000000000000000000',
'kether': '1000000000000000000000',
'grand': '1000000000000000000000',
'mether': '1000000000000000000000000',
'gether': '1000000000000000000000000000',
'tether': '1000000000000000000000000000000'
ethjs
has made a policy of using BN.js
accross all of our modules. Here are some reasons why:
ethereumjs
repositorieselliptic
Please help better the ecosystem by submitting issues and pull requests to default. We need all the help we can get to build the absolute best linting standards and utilities. We follow the AirBNB linting standard and the unix philosophy.
You'll find more detailed information on using ethjs-unit
and tailoring it to your needs in our guides:
ethjs-unit
, writing coverage and updates.There is always a lot of work to do, and will have many rules to maintain. So please help out in any way that you can:
ethjs-unit
better, and pull requests to show us how your idea works.Please consult our Code of Conduct docs before helping out.
We communicate via issues and pull requests.
This project is licensed under the MIT license, Copyright (c) 2016 Nick Dodson. For more information see LICENSE.md.
The MIT License
Copyright (c) 2016 Nick Dodson. nickdodson.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
FAQs
A simple module for handling Ethereum units (e.g. 'ether', 'wei', etc...)
We found that ethjs-unit demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.