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
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 can call the receiver.transfer(amount)
to send the ether in the receiver
s account
This is how I solve it:
function sendMoney(address to, uint value) public {
address payable receiver = payable(to);
receiver.transfer(value);
}
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);