4

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.

Kronos
  • 896
  • 1
  • 10
  • 21

2 Answers2

3

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

qbsp
  • 4,367
  • 2
  • 15
  • 26
3

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});
Teleporting Goat
  • 1,516
  • 1
  • 13
  • 33