From this, I know how to send ether to contract address itself. But I have a function defined as payable, then how can I call and send ether at the same time?

for example, my contract is like this,

contract MyTest {
  function DepositEther(address _to, string _title) payable external {
     //do something inside..

Thank you.


Regular functions look like this in truffle:

var account = accounts[0];
myTestInstance.regularFunction(_arg1, _arg2, {from: account});

To make it payable, all you have to do is this:

var account = accounts[0];
var price = 1000000000000000000; //whatever your price is
myTestInstance.payableFunction(_arg1, _arg2, {value: price, from: account});

Is similar to the syntax proposed in the linked post, you just need to specify the method name also.

contractInstance.method.sendTransaction(to, title, { from: accounts[0], value: ..., gas: ... });

where method is your method name (DepositEther) and contractInstance is the instance of your contract

Your Answer

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

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