[Ethereum] How to transfer eth to specific address in smart contract

etheretherscanremix

I want to send eth to specific amount by calling smart contract.

This is my contract.

pragma solidity ^0.6.7;

contract MyContract {   
  address payable receiver;

    constructor(address _icb_address) public {
        receiver = msg.sender;
    }

    function sendEther(uint _amount) public returns(bool success) {
        receiver.transfer(_amount);
        success = true;
    }

}

I called endEther from an address which is not same as receiver, but I got this error.
gas required exceeds allowance (10000000) or always failing transaction

This is failed transaction on etherscan.
https://rinkeby.etherscan.io/tx/0x5652cad647ee5146cdb1e2e97f8376075a630e98021e0927cb629ed0efbe1099

It seems like transfer function is called from contract.
I couldn't understand why this function is executed by contract address not msg.sender.

How to send eth to spesific address on smart contract?
Could you give me any advise, please?

Best Answer

The transaction failed because your contract does not have any ETH balance and in your transaction to sendEther you also did not include any ETH.

Try either to send ETH to the contract and afterwards call sendEther or directly call sendEther with enough ETH in the transaction.

Related Topic