How to initiate a contract transaction from JavaScript that also sends ether to the contract?

ThePiachuThePiachu Member Posts: 16
edited June 2015 in web3-js
I am currently reading the JavaScript API tutorial (https://dappsforbeginners.wordpress.com/tutorials/javascript-api-2/), and I see there are options for initiating contract transactions from the script:

contract.sendCoin('0x878965387afd76fd008e7668','500')

as well as ways to initiate payments with ether:

web3.eth.transact({to: document.querySelector('#recipient').value ,value: document.querySelector('#value').value});

however, what should I call in order to do both in the same transaction? I want to send ether to a given contract AND pass some data at the same time. This would be similar to the "contribute" function from https://dappsforbeginners.wordpress.com/tutorials/contracts-that-send-transactions/ :
//function to contributes to the campaign
    function contribute(uint campaignID) {
        Campaign c = campaigns[campaignID];
        Funder f = c.funders[c.numFunders++];
        f.addr = msg.sender;
        f.amount = msg.value;
        c.amount += f.amount;
    }
Post edited by StephanTual on

Comments

  • kvhnukekvhnuke Member Posts: 15
    edited June 2015
    i think this should work:

    web3.eth.contract(your-abi-definition).yourFunction.sendTransaction('function parameters..', {from: web3.eth.coinbase, value:web3.toWei(your-amount,'ether')});
    Post edited by StephanTual on
  • StephanTualStephanTual London, EnglandMember, Moderator Posts: 1,282 mod
    Please use the <code> tags guys :) Updated your posts, please spread the word :)
  • PranayPranay Member Posts: 58
    Thank you for information @kvhnuke .
  • ThePiachuThePiachu Member Posts: 16
    Hmm, I tried your solution @kvhnuke , but I don't think it worked.

    Here is my exact test code:

    Ethereum contract:

    contract Simple { string32 message; function Simple() { message = "Hello world!"; } function SetMessage(string32 newMessage) { if (msg.value < 1 finney) { message = "Cheapskate!"; } else { message = newMessage; } } }

    HTML code:

    <!DOCTYPE html> <head> <meta charset="UTF-8"> <script type="text/javascript"> var contractDesc = [ { "name": "SetMessage(string32)", "type": "function", "inputs": [ { "name": "newMessage", "type": "string32" } ] } ]; function cheap() { contract = web3.eth.contract('0x642ae5e1e213e15a6a4b3c58a0c6eaac2859440b', contractDesc); contract.SetMessage(document.getElementById('message').value); }; function message() { contract = web3.eth.contract('0x642ae5e1e213e15a6a4b3c58a0c6eaac2859440b', contractDesc); contract.SetMessage.sendTransaction(document.getElementById('message').value, {from: web3.eth.coinbase, value:web3.toWei('2','ether')}); }; </script> </head> <body> <div> <div> <h3>Set message:</h3> </div> <div> <input id="message" class="form-control" type="text" placeholder="Message"></input><br> </div> <button onclick="cheap();">Be Cheap</button> <button onclick="message();">Set message</button> </div> </body> </html>

    Calling `cheap()` works as expected - the transaction is sent and the contract changes `message` to "Cheapskate!". Calling `message` does nothing - it doesn't even bring up the notification that a transaction will be sent.
  • innovator256innovator256 Member Posts: 5
    If you look at the api : web3.eth.sendTransaction(transactionObject [, callback]) here :
    https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendtransaction
    the transaction object needs to have some properties most of which are optional :

    {from , to, value, gas, gasPrice, data, nonce}

    In your case you have the "from" and the "value" properties of the object ie:

    {from, value} :: {from: web3.eth.coinbase, value:web3.toWei('2','ether')}

    from your code:
    function message() {
            contract = web3.eth.contract('0x642ae5e1e213e15a6a4b3c58a0c6eaac2859440b', contractDesc);
            contract.SetMessage.sendTransaction(document.getElementById('message').value, {from: web3.eth.coinbase, value:web3.toWei('2','ether')});
    };
    you are leaving some properties blank which in your case defaults to contract creation. if you want to send to a specific contract you can specify the "to" property like so

    {from: web3.eth.coinbase, value:web3.toWei('2','ether'), to: "contractAddress"}

    if you want to send data with it also :

    {from: web3.eth.coinbase, value:web3.toWei('2','ether'), to: "contractAddress", data:"dataIWantToSend"}

    Hope it helps...
  • ThePiachuThePiachu Member Posts: 16
    So the correct use would be something like

    web3.eth.sendTransaction({from: web3.eth.coinbase, value:web3.toWei('2','ether'), to: contractAddress, data: document.getElementById('message').value});

    ? That doesn't appear to work. Looking at https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendtransaction isn't all that useful since it shows how to submit a contract, not send data to a contract.
  • innovator256innovator256 Member Posts: 5
    Sorry, in the data field try :

    data: web3.fromAscii( document.getElementById('message').value')

    you are trying to pass ascii characters by default. Needs to be hex format for parsing..

    I will do some vid tutorials, along side other things coming up...
  • ThePiachuThePiachu Member Posts: 16
    Still doesn't work.

    function message() { contract = web3.eth.contract(contractAddress, contractDesc); //contract.SetMessage.sendTransaction(document.getElementById('message').value, {from: web3.eth.coinbase, value:web3.toWei('2','ether')}); web3.eth.sendTransaction( { from: web3.eth.coinbase, value:web3.toWei('2','ether'), to: contractAddress, //data: web3.fromAscii( document.getElementById('message').value ) //data: web3.fromAscii( 'TEST' ) data: '0x0000000000000000000000000000000000000000' }); }

    Tried with the data being fetched, simple text being converted, and simple hex string, still nothing - not even a notification warning about transaction being sent.

    The exact code I'm using is at:

    https://github.com/ThePiachu/EtherTest/blob/master/Simple.html
  • PranayPranay Member Posts: 58
    any solution you get?
  • jesus666jesus666 Member Posts: 62 ✭✭
    I think the actual problem in your case is that you're not specifying gas or gasPrice. Try the same sendTransation with
    
    gas: "1000000"
    gasPrice: web3.eth.gasPrice
    
Sign In or Register to comment.