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? – Tjaden Hess Jan 22 '16 at 4:43
  • Sorry about that - contract. – high110 Jan 22 '16 at 4:46
  • 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 – Tjaden Hess Jan 22 '16 at 4:51
  • 1
    tried again - let me know if it's more clear - just trying to see if there are any differences within each implementation? – high110 Jan 22 '16 at 4:57
up vote 40 down vote accepted

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.

  • 2
    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? – Mikko Ohtamaa Feb 9 '17 at 20:53
  • What happens if you send ether to the contract which is suicided? – Jossie Calderon Jun 12 '17 at 19:21
  • 1
    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. – Tjaden Hess Jun 12 '17 at 19:23

Note: this question originally asked about the suicide opcode, which has become selfdestruct in the meantime as per EIP6.

selfdestruct is the encouraged term and may be found in newer Solidity and Serpent code. It is the same API and behavior, as described in the other answers, and is an alias for suicide.

EIP6 describes the motivation:

The primary reason for us to change the term suicide is to show that people matter more than code and Ethereum is a mature enough of a project to recognize the need for a change. Suicide is a heavy subject and we should make every effort possible to not affect those in our development community who suffer from depression or who have recently lost someone to suicide.

  • 1
    selfdestruct alias has been applied to the codebases of Solidity and Serpent and is the preferred term to use. – Hudson Jameson Feb 14 '16 at 6:10

The selfdestruct() function and the relative OPCODE are used when you have a contract that has a bug or an unwanted behaviour and you want to get rid of it.

The selfdestruct(address) is a better version as @tjaden-hess pointed out, when you have a payable contracts (contracts that receive ethers) the ethers will be redirected to address which is a big win.

When you interact with a selfdestructed contract NOTE this: If you send a transaction and/or funds to it then your funds are LOST. Be sure to not send funds or other transactions to it after suiciding a contract.

That's an issue you always have to keep in mind when developing apps that use contracts with a selfdestruct function, be sure the address of the contract is removed from your app after selfdestruct() has been called on the contract, maybe you can always call a getter method to see if the contract responds with a valid value before sending a transaction and/or transferring ethers to it.

The name of the method is now selfdestruct

We have some description of it in the new solidity docs: http://solidity.readthedocs.io/en/v0.4.21/units-and-global-variables.html?highlight=selfdestruct#contract-related

Here's a post from r/ethereum about suicide with more infos and a small discussion about it.

  • This should be the accepted answer. – Jossie Calderon Jun 12 '17 at 19:22

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.