2

Using web3 0.20.3

I want to call function and send some Etherium at the same time.

JavaScript

Contract = web3.eth.contract(ABI);

Instance = Contract.at('0x162b74ea16da6ae9d7f7b349eaeab2fcadf4e835');

Instance.setData("name", "url", function(err, result) {
  console.log(err, result);
});

Solidity

  function setData(string name, string imageUrl) public payable {
    accounts[msg.sender].name = name;
    accounts[msg.sender].imageUrl = imageUrl;
    accounts[msg.sender].bid = accounts[msg.sender].bid + msg.value;
    emit Bid(msg.sender, accounts[msg.sender].bid);
  }

  function() public payable { // fallback function
    accounts[msg.sender].bid = accounts[msg.sender].bid + msg.value;
    emit Bid(msg.sender, accounts[msg.sender].bid);
  }

Ideally something like that:

Instance.setData("url", "name", {value: "1000000000000000000"}, function(err, result) { console.log(err, result); });

(note adding extra value parameter)

More involved syntax I've observed in the wild, but they involve manually creating the whole transaction. I just want to add sending Ether to something that I have.
  • Did you check your code? It looks fine. You might want to add from: web3.eth.accounts[0] to the object you put the value in to make sure web3 will try to send from the 'current' account. – JBrouwer Mar 31 '18 at 12:24
  • Rubber duck debugging. It works. Here is a great example: github.com/ethereum/wiki/wiki/JavaScript-API#example-51 myContractInstance.myStateChangingMethod('someParam1', 23, {value: 200, gas: 2000}); Can I have my time back? How do we mark the question as pointless, resolved, it was always working, maybe I was so tired and forgot to save the file or reload the page... – Mars Robertson Mar 31 '18 at 12:30
  • Haha I know this kind of issue, don't worry about it. – JBrouwer Mar 31 '18 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.