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 = const balance = await web3eth // returns BN.js instance // assume balance is 20000000000000000 wei (=0.02eth) console // "0.04"
Use it calculate the Wei/Gwei/Eth equivalent of a given value:
const v = '1.234' 'eth' console // "1234000000"
You can also output hex and binary strings:
const v = 255 console // "0xff"console // "11111111"
Basic arithmetic supported fully:
const v = 255const b = await web3eth // assume balance is 100 wei console // 0x12c (= 300 in base-10)
Comparisons too:
const toBN = const v = 255const v2 = '0.2' 'eth' console // falseconsole // trueconsole // true
It also ensures units are aligned when performing operations:
const v = 25500const v2 = '0.2' 'eth' console // trueconsole // "200000000000025500"console // "0.2000000000000255"
API
new EthVal(input, unit = 'wei')
Constructs a new EthVal
instance.
input
- can be aNumber
, astring
(in base-10 or base-16/hex format), anotherEthVal
instance, or aBN
instance.unit
- must be one ofeth
,gwei
orwei
(default).
.toWei()
Convert the value to its Wei equivalent and return a new EthVal
instance.
.toGwei()
Convert the value to its Gwei equivalent and return a new EthVal
instance.
.toEth()
Convert the value to its Eth equivalent and return a new EthVal
instance.
.add(input)
Add input
to this value and return a new EthVal
instance.
input
- same as for theEthVal
constructor
.sub(input)
Subtract input
from this value and return a new EthVal
instance.
input
- same as for theEthVal
constructor
.mul(input)
Multiply input
with this value and return a new EthVal
instance.
input
- same as for theEthVal
constructor
.div(input)
Divide this value by input
and return a new EthVal
instance.
input
- same as for theEthVal
constructor
.round()
Round this value to the nearest integer (i.e 0 decimal places) and return a new EthVal
instance.
.eq(input)
Get whether this value equals input
. Returns true
or false
.
input
- same as for theEthVal
constructor
.lt(input)
Get whether this value is less than input
. Returns true
or false
.
input
- same as for theEthVal
constructor
.lte(input)
Get whether this value is less than or equal to input
. Returns true
or false
.
input
- same as for theEthVal
constructor
.gt(input)
Get whether this value is greater than input
. Returns true
or false
.
input
- same as for theEthVal
constructor
.gte(input)
Get whether this value is greater than or equal to input
. Returns true
or false
.
input
- same as for theEthVal
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.
.toNumber()
Return base-10 Number
representation of this value.
.toFixed(precision)
Return base-10 decimal-point representation of this value to the given precision.
precision
- maximum no. of numbers after the decimal point.
.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