EatTheBlocks Forum

How can I check if a contract has self-destructed in Solidity?

Hi everyone. I need your help to figure out an unusual error in my contract. Below is the contract. The contract revives Ether and when the kill() is called, all Ether should be sent to the owner and the contract should be destructed. I used a modifier contractStatus to prevent sending Ether to the contract after the contract is destructed, but it does not work !! When I try to check the destroyed value after calling kill() I got this error “error: Failed to decode output: TypeError: Cannot read property ‘length’ of undefined

pragma solidity 0.5.11;

contract Test {

bool public destroyed = false;
uint256 balance;
address payable owner; 

modifier contractStatus { 
    require(!destroyed, "This contract has been destroyed"); 

modifier onlyOwner() {
    require(msg.sender == owner, "Only contract owner can call this function");

constructor ()  public payable {
    owner = msg.sender;
    balance = balance + uint256(msg.value);

function () payable external  contractStatus onlyOwner  {
    balance = balance+ uint256(msg.value);

function kill() external  contractStatus {
    destroyed = true;

This is normal. Once you kill the contract the data is destroyed and is not accessible anymore.

To answer your original question, to check if the contract has self-destructed you need to check the size of the code of the address. With web3 you can do it with web3.eth.getCode(). More info here.