Package Exports
- ethval
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ethval) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ethval
Easier calculation and formatting of Ethereum values.
- Supports floating point numbers
- Easily convert between Wei, Gwei and Eth
- Output binary, decimal and hexadecimal strings
- Output fixed-precision floating-point values (
toFixed()
) - Parse and generate BN.js instances
- Uses decimal.js under the hood
Install
- NPM/Yarn:
ethval
Example Usage
You can feed it BN.js instances returned from web3:
const EthVal = require('ethval')
const balance = await web3.eth.getBalance(account) // returns BN.js instance
// assume balance is 20000000000000000 wei (=0.02eth)
console.log( new EthVal(balance).toEth().mul(2).toFixed(2) ) // "0.04"
Use it calculate the Wei/Gwei/Eth equivalent of a given value:
const v = new EthVal('1.234', 'eth')
console.log( b.toGwei().toString() ) // "1234000000000000"
You can also output hex and binary strings:
const v = new EthVal(255)
console.log( b.toString(16) ) // "0xff"
console.log( b.toString(2) ) // "11111111"
Basic arithmetic supported fully:
const v = new EthVal(255)
const b = await web3.eth.getBalance('0x...') // assume balance is 100 wei
console.log( v.div(5).add(b).mul(2).sub(2).toString(16) ) // 0x64 (=100 in base-10)
API
new EthValue(input, unit = 'wei')
Constructs a new EthValue
instance.
input
- can be aNumber
, astring
(in base-10 or base-16/hex format), anotherEthValue
instance, or aBN
instance.unit
- must be one ofeth
,gwei
orwei
(default).
.toWei()
Convert the value to its Wei equivalent and return a new EthValue
instance.
.toGwei()
Convert the value to its Gwei equivalent and return a new EthValue
instance.
.toEth()
Convert the value to its Eth equivalent and return a new EthValue
instance.
.add(input)
Add input
to this value and return a new EthValue
instance.
input
- same as for theEthValue
constructor
.sub(input)
Subtract input
from this value and return a new EthValue
instance.
input
- same as for theEthValue
constructor
.mul(input)
Multiply input
with this value and return a new EthValue
instance.
input
- same as for theEthValue
constructor
.div(input)
Divide this value by input
and return a new EthValue
instance.
input
- same as for theEthValue
constructor
.toString(base)
Return string representation of this value according to the given number base.
For example, if base
is 2 then a binary string representation is returned, if
16
then a hexadecimal string representation is returned.
base
- either 2, 16 or 10.
.toFixed(precision)
Return base-10 decimal-point representation of this value to the given precision.
precision
- maximum no. of numbers after the decimal point.
.toWeiBN()
Return a BN
instance representing this value when converted to its wei
equivalent.
.isWei
Whether the current unit is wei
.
.isGwei
Whether the current unit is gwei
.
.isEth
Whether the current unit is eth
.
Dev guide
- Install deps:
npm i
- Tests:
npm test
- Tests with coverage:
npm run test:coverage
- Build final lib:
npm run build
- Lint:
npm run lint
License
MIT