Here is my onSubmit function:

onSubmitModifyAllocation = async event => {

  const accounts = await web3.eth.getAccounts();

  await fund.methods.modifyAllocation(this.state.address, this.state.allocation).send({
    from: accounts[0],
    allocation: web3.utils.toWei(this.state.value, 'ether') // Here is an issue

And the form:

<form onSubmit={this.onSubmitModifyAllocation}>
  <h4>Modifies the max investment limit allowed for an investor</h4>
    <label>Address </label>
      onChange={event => this.setState({ address: event.target.value })}
    <label>Allocation, denominated in ether </label>
      onChange={event => this.setState({ allocation: event.target.value })}
  <button>Change allocation</button>

I've tried to convert the allocation value to BN, but no luck. What's the best approach to managing the error?

  • Strange. web3.utils.toWei returns a string or a BN, see here (I'm assuming that you're using web3.js v1.x). – goodvibration Jan 13 '19 at 11:47
  • Oh, you know what? Possibly you need to convert the BN to a string or a BigNumber. Tell you what - add .toFixed() at the end of that expression, see if it resolves the problem. – goodvibration Jan 13 '19 at 11:49
  • Make sure this.state.value is a string. (Your code doesn't show this being set anywhere, so it's hard to tell you where the issue is.) – user19510 Jan 13 '19 at 22:01

Function web3.utils.toWei returns a String if a string is given as input, and a BN otherwise (see here).

Since this.state.value is not a string, web3.utils.toWei(this.state.value, 'ether') returns a BN.

But web3.js wants you to pass to function modifyAllocation either a String or a BigNumber, and a BN is not a BigNumber.

So you can resolve this problem in either one of two ways:

  • web3.utils.toWei(this.state.value.toString(), 'ether')
  • web3.utils.toWei(this.state.value, 'ether').toFixed()

Note that I've used toFixed in the second option, because toString translates large values to scientific format (you know, with the "E" thing), which could technically yield precision-loss.

  • The first way throws the error 'Unhandled Rejection (TypeError): Cannot read property 'toString' of undefined' - why is that? And the second one doesn't work as well "Unhandled Rejection (Error): Please pass numbers as strings or BigNumber objects to avoid precision errors." What am I doing wrong? – Vladimir B. Jan 13 '19 at 15:38
  • @VladimirB.: Reading just the first line of your comment, proves that this.state.value is undefined. Now, I suggest that you first resolve this issue, before we can proceed. By the way, you could (and should) have conducted this basic check before posting the question. – goodvibration Jan 13 '19 at 16:07
  • It works well when I put the value in wei, but throws an error when I'm using web3.utils.toWei in any form. – Vladimir B. Jan 13 '19 at 16:23
  • @VladimirB.: Your description suggest that this.state.value === undefined, and I suggest that you fix this problem to begin with. Even if it is somehow accepted by some function, it doesn't mean that this function operates as you hope it will. – goodvibration Jan 13 '19 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.