As a contract owner, how do I remove the ethers from the contract address automatically to my wallet?Can I have a function that when the contract receives the ethers from the costumer, it automatically sends the ethers to the owner wallet address?
Asked
Active
Viewed 156 times
1 Answers
0
It is possible to forward ether from receive function to a wallet of your choice.
address owner;
receive() external payable {
payable(owner).transfer(msg.amount);
}
Just be aware that
- When invoked from another contract
receivefunction has a maximum gas stipend of 2300 gas so transfer will fail. It will work without issues if ether is sent from a non-contract account. - A contract can receive ether without invoking its receive function, for example by being the recipient of self destructing contract.
- If owner is a contract it has to have a payable receive or fallback function.
The alternative is to have a withdraw function
function withdraw() onlyOwner {
payable(owner).transfer(address(this).balance);
}
Ismael
- 30,570
- 21
- 53
- 96