Solidity – Fixing ‘Too Many Decimal Places’ Error

javascriptmochasolidity

So I am putting together a test for my contract and this is the it() statement:

it('allows one account to enter', async () => {
    await lottery.methods.enter().send({
      from: accounts[0],
      value: web3.utils.toWei('0.02', 'wei')
    });

I am unclear as to why I am getting this error:

Error: [ethjs-unit] while converting number 0.02 to wei, too many
decimal places

Does web3.utils no longer take care of the conversion or did I calculate the decimal places wrong?

Here is the snippet of the actual contract that assigns the amount of money:

function enter() public payable {
        require(msg.value > .01 ether);
        players.push(msg.sender);
    }

Best Answer

wei is the smallest unit of Ether and cannot be subdivided. This line:

web3.utils.toWei('0.02', 'wei')

says to convert .02 wei to wei, which obviously cannot be done because .02 wei cannot exist to begin with, and it doesn't make sense to convert to and from the same units...

You probably meant to put:

web3.utils.toWei('0.02', 'ether')

Which converts .02 ether to wei which is: 20000000000000000 wei or 20 * 10^15 wei

Related Topic