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;
}
Comments
web3.eth.contract(your-abi-definition).yourFunction.sendTransaction('function parameters..', {from: web3.eth.coinbase, value:web3.toWei(your-amount,'ether')});
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.
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: 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...
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.
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...
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