ethjs-unit icon indicating copy to clipboard operation
ethjs-unit copied to clipboard

#toWei breaks for decimal inputs of "wei" and "noether"

Open RyanRHall opened this issue 6 years ago • 3 comments

Issue Type

  • [x] Bug (https://github.com/ethjs/ethjs-unit/blob/master/.github/CONTRIBUTING.md#bug-reports)
  • [ ] Feature (https://github.com/ethjs/ethjs-unit/blob/master/.github/CONTRIBUTING.md#feature-requests)

Description

#toWei breaks for decimal inputs of "wei" and "noether"

Steps to reproduce

// these *should* throw errors
unit.toWei(0.1, 'noether') // 1
unit.toWei(0.1, 'wei') // 1

Note that larger denominations work fine

// these work as expected
unit.toWei(0.1, 'kwei') // 100
unit.toWei(0.001, 'kwei') // 1
unit.toWei(0.0001, 'kwei') // throws error

RyanRHall avatar Jun 11 '19 05:06 RyanRHall

const baseLength = unitMap[unit].length - 1 || 1;

wei and noether are treated the same as if the unit was '10'

imo noether should always return BN(0) and wei should just return the formatted input and throw if the input is not an integer.

junderw avatar Dec 03 '20 02:12 junderw

Fixed in #3

junderw avatar Dec 04 '20 01:12 junderw

@junderw will review the PR. Thank you. In future, if you want a better maintained lib for this, I would use ethers JS.

SilentCicero avatar Dec 05 '20 17:12 SilentCicero