2

I'm trying to send some Ether to the payable function of my contract and I'm unable to find/understand a resource to do so.

Here's my attempt below:

Web3 App file (simplified, without initialisation functions) :

App = {
  sendEther: function(_value) {

    // Enter details to send transaction;
    web3.eth.getAccounts(function(error, accounts) {
      if (error) {
        console.log(error);
      }

      var account = accounts[1]; // send from account index 1

      App.contracts.Plant.deployed().then(function(instance) {

        return instance.sendTransaction({ // Something like this?!
          from: account,
          gas: 5000,
          value: _value
        });
      }).then(function(result) {
        console.log(result);
        return App.UpdateContractValues();
      }).catch(function(err) {
        console.log(err.message);
      });
    });
  }
};

Simplified smart contract:

contract Plant {

    address public creator;

    uint public finney_balance; 

    function () payable public {
        finney_balance += msg.value/1000000000000000;
        depositedFunds(msg.value, finney_balance);
    }
}
2
  • What error/s are you getting with the code above? Nov 24, 2017 at 13:02
  • plantInstance.sendTransaction is not a function which is expected as I wrote it as a placeholder.
    – Charles
    Nov 24, 2017 at 13:24

1 Answer 1

0

If you are using truffle you can do it exactly as you wrote above:

instance.sendTransaction({
  from: account,
  gas: _gas,
  value: _value
}).then(function(result) {
  // 
});

Here's the truffle reference: https://github.com/trufflesuite/truffle-contract#sending-ether--triggering-the-fallback-function

1
  • Does this work for payable function? Where do you mention what function is being called? Mar 6, 2018 at 16:39

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.