I would like to create a web3-based JS function like this:

function hasSelfDestructed(address) { if (... address was a contract, but has called selfdestruct ...) return true else return false }

If a contract has called selfdestruct, its code will be empty, or web3.eth.getCode(address) === '0x'. However, non-contract addresses also have empty code.

You can see a contract's code at some point in the past with web3.eth.getCode(address, blockNumber) (if you have a non-pruned full node). But short of querying all previous blocks, how would I know what past block to examine?

I suppose I could scan every transaction since block 0 and see if any transaction creates the contract. The transaction receipt has the contractAddress field to help with this. Whenever I see an attempt at contract creation, I could see if it succeeded and yielded any code at the address.

Is there another way?

  • I don't think there is a way without querying the previous blocks. Maybe you can use Etherscan's API to query the transactions faster. – jeff Jun 7 '17 at 23:49

Your Answer

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

Browse other questions tagged or ask your own question.