Search code examples
ethereumsolidity

How do I send ether from an EOA to a smart contract?


I am trying to write a simple smart contract that will share any ether it gets between two EOAs. However, after searching online for some time, I am unable to find the command I need to run from the geth console to send the ether "to the share function". What is the syntax for this command? It would also be nice if I could get a link to documentation that would help me find this command and other similar ones.

To clarify, I am aware of how to call a function normally, but not sure how to call the function and include the ether payment.

pragma solidity ^0.4.0;
contract sharer {
    address owner;
    address A;
    address B;

    function sharer (address _A, address _B) public {
        A = _A;
        B = _B;
    }

    function share () payable public {
        A.transfer(msg.value/2);
        B.transfer(msg.value/2);
    }
}

Solution

  • You include ether to send in the transactionObject for the method call.

    const txObject = {
      value: web3.toWei(amtInEther, 'ether')
    };
    
    shareContractInstance.share.sendTransaction(txObject);
    

    The other options for the transaction object can be found here.