8

I'm trying to make a function in my Dapps front end that requests Ether from the user, I know I have to use web3.eth.sendTransaction() but am unsure of how to correctly implement and call this function on the front end of my Dapp, thanks!

current code is:

   $("#button").click(function() {
    Raffle.main(function (err, result) {
      web3.eth.sendTransaction({from:0x627306090abaB3A6e1400e9345bC60c78a8BEf57 ,to:0x086912faa7f6598d28d80c448c8d1e9dae5a4dee, value:web3.toWei(1, "ether")});
      console.log(result);
      });
5
  • Please post your contract code.
    – lunr
    Jan 31, 2018 at 1:58
  • @lunr to invoke the "send" function do I not just call it on an event in the front end?
    – JAG
    Jan 31, 2018 at 2:01
  • Oh, I got it now. There are examples in the documentation: web3js.readthedocs.io/en/1.0/web3-eth.html#sendtransaction (this is for web3.js 1.0 version)
    – lunr
    Jan 31, 2018 at 2:09
  • @lunr yea, I'm already aware of that, I'm having trouble setting it up and calling in my contract
    – JAG
    Jan 31, 2018 at 2:12
  • Please post what you have now, so someone can help. Your question is very broad now.
    – lunr
    Jan 31, 2018 at 2:13

2 Answers 2

5

I found this to work to call a function and send Ether with it, to is my contracts address, value is the amount in wei I want to send with it and data is the address of my function that I want to call when sending the data

  function mainEnter() {
        web3.eth.getAccounts(function(error, result) {
        web3.eth.sendTransaction(
            {from:web3.eth.accounts[0],
            to:"0x943",
            value:  "1000000000000000000", 
            data: "0xdf"
                }, function(err, transactionHash) {
          if (!err)
            console.log(transactionHash + " success"); 
        });
    });

    }
3

Addresses should be strings:

web3.eth.sendTransaction({
    from: "0x627306090abaB3A6e1400e9345bC60c78a8BEf57",
    to: "0x086912faa7f6598d28d80c448c8d1e9dae5a4dee", 
    value: web3.toWei(1, "ether"), 
}, function(err, transactionHash) {
    if (err) { 
        console.log(err); 
    } else {
        console.log(transactionHash);
    }
});
5
  • Even when in string format the function still doesn't call
    – JAG
    Jan 31, 2018 at 2:31
  • How do you know it's not called?
    – lunr
    Jan 31, 2018 at 2:38
  • when I can the function that the sendTransaction function is in the value on metamask doesn't go to 1 ETH
    – JAG
    Jan 31, 2018 at 2:42
  • Please check the callback values.
    – lunr
    Jan 31, 2018 at 2:43
  • web3.utils.toWei(string, string) web3js.readthedocs.io/en/v1.3.4/…
    – Christian
    May 11, 2021 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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