I'm using web3.py to interact with contracts. I was wondering, after calling a function with selfdestruct or suicide(address), how do I find out that the contract is in fact dead?

  • Try to call any read-only function of that contract, and verifies that it raises an exception. – goodvibration May 3 at 14:18
  • @goodvibration Is there any smarter ways other than relying on exceptions? – Huadong Feng May 3 at 14:29
  • It is indeed not a very reliable method, because the call can fail for other reasons (for example, you're not even connected to the node). So you're bound to check the error message, which may vary depending on your web3 provider. – goodvibration May 3 at 14:36
  • Why not just check if there is a contract at the address? ethereum.stackexchange.com/a/15642/31933 – Lauri Peltonen May 3 at 17:23
  • @LauriPeltonen Can we do something like this in web3 or evm manager API instead of from another contract? – Huadong Feng May 3 at 17:41

You can call eth.getCode to see if there is code at the address. If there is no code, there is no contract. If there is code, there is a contract associated with the address.

Using ether.js, you can see how it works below. The first call was after the contract was deployed and the second call was after it was selfdestructed.

> kovanEthersProvider.getCode('0x761f887ea907DB3FBc13f55867Db2c2c9BDB34F9').then(console.log) 

> kovanEthersProvider.getCode('0x761f887ea907DB3FBc13f55867Db2c2c9BDB34F9').then(console.log)

Follow the same pattern with web3.py.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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