[Ethereum] Send eth from one account to another using transfer() function

contract-debuggingcontract-developmentetherethereumjstransactions

I am trying to write a smart contract that will send eth from one account to another account :

Contract Send {

 event Sent(address from, address to, uint amount );

 function send(address _receiver, uint _amount) public payable{
    _receiver.transfer(_amount);
    emit Sent(msg.sender, _receiver, _amount);
 }
}

Here, the amount is deducted from the sender's account but not being added to receivers account.
I want to use transfer() function for this.

Thanks in advance!

Best Answer

This contract works and there is no need to change it.

The user interacting with this contract must send Ether in the transaction in order to trigger a transfer to the _receiver. The Ether gets sent to the contract, and lives there prior to transfer from the contract to the _receiver.

I would suggest removing _amount and simply using msg.value to send the entire value that is sent in the transaction.

Additionally, this can be done with a simple transaction and does not decessarily need a smart contract.

Related Topic