8

I created a contract with a payable function but the ether is stored in the contract I am looking for a way to transfer the ether to another address

4 Answers 4

13

If the ether is already in the contract and you do not have the functionality to transfer ether already implemented in it, the ether will be forever in the contract.

A simple contract that receives ether and allows for transferring is shown below. Only the person that deploys the contract will be able to transfer the ether.

pragma solidity ^0.4.24; 

contract myContract{

    address public owner;

    constructor() public {
        owner = msg.sender;
    }

    function transfer(address to, uint256 amount) public {
        require(msg.sender==owner);
        to.transfer(amount);
    }

    function () public payable {}
}

Hope this helps

4

You can call the receiver.transfer(amount) to send the ether in the receivers account

3

This is how I solve it:

function sendMoney(address to, uint value) public {
   address payable receiver = payable(to);
   receiver.transfer(value);
}
1

You must have a function in the contract that send ether out of the contract as well. This function does not have to be payable, but would look something like:

myAddress.transfer(address(this).balance);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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