Why are selfdestructs used in contract programming?

  • I'm reading bits and pieces that selfdestructs within contracts are a good thing. Can someone explain what the benefits are of doing a contract selfdestruct when it comes to Ethereum programming? Is there a difference if you're using different clients or implementations, or languages?

    This question is very unclear. Are you asking about contract suicides, or clients or what?

    Sorry about that - contract.

    The last sentence is the confusing part, because the go client is a client, solidity is a language, and python could be referring to the python client, or possibly to serpent, the python-like Ethereum language

    tried again - let me know if it's more clear - just trying to see if there are any differences within each implementation?

  • Tjaden Hess

    Tjaden Hess Correct answer

    5 years ago

    Selfdestructs in Ethereum are an operation (an OPCODE actually) at the EVM level, independent of what language or client you are using.

    For example, calling selfdestruct(address) sends all of the contract's current balance to address.

    This is useful when you are finished with a contract, because it costs far less gas than just sending the balance with address.send(this.balance).

    In fact, the SELFDESTRUCT opcode uses negative gas because the operation frees up space on the blockchain by clearing all of the contract's data.

    This negative gas deducts from the total gas cost of the transaction, so if you're doing some clean-up operations first, SELFDESTRUCT can reduce your gas costs.

    What happens if you try to call suicided contract? Is it state back to zero or does EVM set some flag telling this contract is no longer here?

    What happens if you send ether to the contract which is suicided?

    The balance of the address increases, but since there is no code at the address anymore, the ETH just gets stuck. That ETH is then essentially burned.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM