1

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
3

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) 
0x6080604052348015600f57600080fd5b506004361060285760003560e01c8063b9554c5914602d575b600080fd5b60336035565b005b600073ffffffffffffffffffffffffffffffffffffffff16fffea265627a7a723158203c431b72fff6bd29b3dd6d5b1c73f87aa200fb5a5029cbc24f45dac0e030d52664736f6c63430005110032

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

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.