A contract after selfdestruct cannot manage transactions, and as such any ether sent to it is lost. Wouldn't be better to switch to a contract defined state "dismissed" and reject any ether sent by mistake when in such a state?
2 Answers
In my opinion, no, self-destruct is usually not a good practice. As you say, it creates dangerous voids on the blockchain where:
- no one has a signing key,
- there is no contract code, and
- possibly users believe there is a contract there.
Any funds sent to such an address will be unrecoverable which is the same as destroyed.
A conscientious developer can completely disable a contract without creating such a void.
contract Pausable is Ownable {
bool public isRunning;
modifier onlyWhenRunning {
require(isRunning);
_;
}
function stopContract() public onlyOwner {
isRunning = false;
}
}
Check out OpenZeppelin for a more complete implementation of the pattern.
Hope it helps.
-
Actually OpenZeppelin has a Destructible contract which calls selfdestruct(...)– Davide CCommented Oct 2, 2018 at 9:59
-
I'm not against using it with extreme caution such as in the context of upgradable contracts. What concerns me is using it as a standard operating procedure. It's often not the optimal solution. Commented Oct 7, 2018 at 23:00
-
You can implement this functionality by yourself. You can implement a modifier before the selfdestruct instruction to check the quantity of ether owned by the contract. This way, the contract could be "destroyed" only if it does not own ether.
-
I don't think this answers the question. The question was about best practice and what happens to sent Ether after the contract has been destroyed - own implementation doesn't help in this case. Commented Oct 1, 2018 at 8:32
-
True. But can we talk about good practice if the functionality described is the only one available? Commented Oct 1, 2018 at 8:36
-
Davide is saying something different: he thinks to leave a “reject ether” function in place, fully working, instead of self destruction. I can say that self destructing is useful to clean the blockchain, freeing space. If you restrict the functionality of a contract only, all the code space remains occupied... anyway I understand the point. Commented Oct 1, 2018 at 8:39
-