I created a new Destroyable
contract (which in fact derives from Ownable
because only the owner should be able to destroy the contract):
import "./Ownable.sol";
pragma solidity 0.5.12;
contract Destroyable is Ownable {
function close() public onlyOwner {
selfdestruct(owner);
}
}
Then I made our HelloWorld
contract Destroyable
(I initially inherited from Ownable
and Destroyable
but then realized that Destroyable
was already Ownable
:
contract HelloWorld is Destroyable {
//...
}
Compiled, deployed and executed the close
function:
[vm]from:0x147...c160cto:HelloWorld.close() 0x459...d8f30value:0 weidata:0x43d...726d6logs:0hash:0x178...4dce6
Debug
status 0x1 Transaction mined and execution succeed
...
I also needed to make the following change in the type of the ower
variable of the Ownable
contract:
address public owner;
changed to:
address payable public owner;
Thatâs because when destroying a contract, all itâs funds will be transferred to the ownerâs address.