1

I want my clients to fill in a number in ethers instead of Wei (as I don't what them to go through the hassle of putting 18 zeroes in every time.)

I'm trying to do this in the following way in JavaScript, but for some reason, it triggers the transaction, but translates the amount 1-1 into wei (so input 1 gives 1 wei instead of 1000000000000000000 wei.)

$("#BetalenRekeningMI").click(function() {
  contractMI.testmultiinput(web3.toWei($("#bedrag").val(), 'ether'),
                            $("#addressMI").val(), {value: 0},
                            function(err, result) {
                          console.log(err, result);
                            })
});

Here some more details. This is the corresponding HTML (without the comment tags of course:

      <tr>
        <td>
          <input type="text" name="bedrag" id="bedrag" placeholder="Bedrag in Ether"/>
          <input type="text" name="addressMI" id="addressMI" placeholder="Adres van de leverancier"/>
          <button id="BetalenRekeningMI">Klik hier om te betalen</button>
        </td>
      </tr>

This is the Solidity function that the JS triggers:

 function testmultiinput(uint256 bedrag, address klant) payable{
        klant.transfer(bedrag);
    }
  • 1
    Isolate the layers of code to find the issue. Is contractMI.testmultiinput("1000000000000000000", 0xDEAD) giving you the expected result? After entering 1 into the HTML Input, does web3.toWei($("#bedrag").val(), 'ether') == "1000000000000000000" return true? – carver Aug 10 '17 at 17:38
  • What versions of web3 did you test with? How does your browser load web3? Test a page with only this code. How did you determine that the contract received only 1 Wei? – dcorking Aug 12 '17 at 11:51
  • please open a javascript console and post these properties web3.version.api and web3.version.node ; also check that there is no other code on the page that could alter the value of $("#bedrag"); edit your question with the results – dcorking Aug 14 '17 at 12:03
1

I suggest you test your code with another Ethereum implementation, or update your copy of Ethereum.

You convert Ether to Wei correctly.

Here are some examples of using the toWei() function of web3.

web3.toWei(1)
"1000000000000000000"
web3.toWei('1')
"1000000000000000000"
web3.toWei('1.1')
"1100000000000000000"
web3.toWei('1.1', 'ether')
"1100000000000000000"

Your code works correctly for me. I entered '1' and an address of '0x00' into your web form:

enter image description here

and the Parity signer received 1,000,000,000,000,000,000 wei to send to your contract:

enter image description here

I have a master version of Parity that is a few weeks old, with these version strings:

>
11:58:56.081
web3.version.api
<
11:58:56.089
"0.17.0-alpha"
>
11:59:02.934
web3.version.node
<
11:59:02.947
"Parity//v1.7.0-unstable-02f2c61-20170711/x86_64-macos/rustc1.18.0"
  • sorry for the misunderstanding guys. With 1-1 I meant one on one. So if I fill in 1, my code reads it as 1 wei, instead of 1 ether (or 1000000000000000000 wei). Don't know why it is not converting to Wei, but keeps thinking the input (someone puts in in HTML) is Wei instead ether. – Olivier Rikken Aug 10 '17 at 9:21
  • I'd prefer not to install your contract to test your code. Please break the code up into smaller pieces so you can show us the exact output of the toWei function? It might also help to also post the HTML snippet and screenshot of what your user enters. – dcorking Aug 10 '17 at 9:27
  • sure, no problem. this is the corresponding HTML: – Olivier Rikken Aug 10 '17 at 9:40
  • Please paste extra detail into your question, not as comments. – dcorking Aug 10 '17 at 9:42
0

Here some more details. This is the corresponding HTML (without the comment tags of course:

<tr><td><input type="text" name="bedrag" id="bedrag" placeholder="Bedrag in Ether"/><input type="text" name="addressMI" id="addressMI" placeholder="Adres van de leverancier"/><button id="BetalenRekeningMI">Klik hier om te betalen</button></td></tr>

The corresponding js is above and this is the solidity function is triggers:

 function testmultiinput(uint256 bedrag, address klant) payable{
        klant.transfer(bedrag);
    }
  • ok, for some reason it does not show my html code – Olivier Rikken Aug 10 '17 at 9:48
  • <tr><td><input type="text" name="bedrag" id="bedrag" placeholder="Bedrag in Ether"/><input type="text" name="addressMI" id="addressMI" placeholder="Adres van de leverancier"/><button id="BetalenRekeningMI">Klik hier om te betalen</button></td></tr> – Olivier Rikken Aug 10 '17 at 9:48
  • here it is! above my html code. 2 fields that can be filled in, one for the address, other for the amount. If i fill in the amount in wei it transfers the right amount, if i just fill in 1, it thinks i want to transfer 1 wei instead of 1 ether.... – Olivier Rikken Aug 10 '17 at 9:50
0

I had the exact same issue and it can be easily solved by converting the value parameter in the transfer method to ether.

See below for example:

destinationAddress.transfer(value * 1 ether);

Note the multiplication by 1 ether, without which the transfer will be done in default type Wei.

Hope this helps.

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.