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

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

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);

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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