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?

Shane Fontaine
  • 18,036
  • 20
  • 54
  • 82
Huadong Feng
  • 211
  • 2
  • 8

1 Answers1

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.

Shane Fontaine
  • 18,036
  • 20
  • 54
  • 82