eth-helpers
Kitchensink of ethereum utils
Last updated 4 months ago by chm-diederichs .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install eth-helpers 
SYNC missed versions from official npm registry.

eth-helpers

Kitchensink of ethereum utils

Usage

Units

const units = require('eth-helpers/units')
const { Wei, Ether, GWei } = units

const gasPrice = 16n * GWei
const value = units.parse('2.7811', { from: Ether }) // implicit `to: Wei`

const tx = // Send tx

console.log(units.format(address.balance, { to: Ether, from: Wei, decimals: 4 }))

API

Utils

const str = utils.format(nullish|string|Buffer|number|BigInt)

Format any of the above types as a eth hex string. Also has type specific functions to avoid guessing wrong:

  • const hex = utils.format.boolean(bool)
  • const hex = utils.format.number(number)
  • const hex = utils.format.bigint(bigint)
  • const hex = utils.format.bytes(buf)
  • const hex = utils.format.address(buf)

Parsing

Utilities exist for converting from hex strings to native data types:

  • const bool = utils.parse.boolean(hex)
  • const number = utils.parse.number(hex)
  • const bigint = utils.parse.bigint(hex)
  • const buf = utils.parse.bytes(hex)
  • const buf = utils.parse.address(hex)
  • const buf = utils.parse.uint256(hex)
  • const str = utils.parse.string(hex)

const receipt = async utils.mined(txHash, eth)

Wait for a transaction to be mined

Units

The units module contains a list of constants as well as helper functions to convert to and from strings and do lossless conversion.

Unit constants include as BigInt:

  • Wei
  • KWei / babbage
  • MWei / lovelace
  • GWei / shannon
  • µEther / microether / szabo
  • mEther / milliether / finney
  • Ether
  • KEther
  • MEther
  • GEther
  • TEther

const bigint = units.convert(value, from, [to = Wei])

Lossless unit conversion to BigInt. Asserts that no precision is lost.

const number = units.convertLossy(value, from, [to = Wei])

Lossy unit conversion to Number. Not suitable to exact calculations.

const str = units.format(value, { from = Wei, to = Wei, decimals = null })

Format BigInt as a string with from basis to to basis. Optional set the number of decimals similar to toFixed

const bigint = units.parse(string, { from, to = Wei })

Parse a string number into BigInt with specified basis.

Install

npm install eth-helpers

License

ISC

Current Tags

  • 1.3.1                                ...           latest (4 months ago)

6 Versions

  • 1.3.1                                ...           4 months ago
  • 1.3.0                                ...           4 months ago
  • 1.2.0                                ...           4 months ago
  • 1.1.0                                ...           4 months ago
  • 1.0.1                                ...           5 months ago
  • 1.0.0                                ...           5 months ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (2)
Dependents (2)

Copyright 2014 - 2016 © taobao.org |