1

I'm trying to have some sort of confirmation after I kill a contract by returning true, but when I run my kill command it always returns false. Is there any reasoning why, or is the contract not being killed?

Here is my contract code:

function killContract() constant returns (bool) {
    require(msg.sender == owner);
    selfdestruct(owner);
    return true;
}

Here is my truffle code that I'm trying to have return true, but it keeps returning false:

MyContract.at('0x...')
    .then(instance => {
        c = instance;
        return c.getOwner()
    })
    .then(owner => {
        console.log(`Owner: ${owner}`);
        return c.killContract()
    })
    .then(killed => {
        console.log(`If killed return true: ${killed}`);
    })
    .catch(error => {
        console.log(`${error}`);
    })
2

There's 2 things going on in here:

1- The killContract function modifies the state of the blockchain, thus, marking it a constant/view won't work. Constant/view functions can only read state.

Given that this would NOT be a constant function you will not be able to return any values out of solidity.

2- Once selfdestruct is invoked, it seems like further instructions will not process.

What you can do is to fire an event before calling selfdestruct to let web3 know the function was called. If selfdestruct failed, the function would revert and the event would not fire.

pragma solidity 0.4.19;

contract A{

    event debug(bool destroyed);

    function destroyMeAndLog() onlyOwner{
        debug(true);
        selfdestruct(this);


    }
}

Another thing you could do is add a dummy function that returns true when called. If you called it after the contract was selfdestructed, then it would return false.

0

Unless I'm mistaken, the contract is killed off and there is no one/nothing left to return true;. It obliterated the next step.

I believe you can emit an event before selfdestruct and the event will appear in the transaction log. That would give a watcher a positive confirmation that selfdestruct took place. Something like:

event LogSelfDestruct(address sender, uint amount);

function kill() public onlyOwner {
  LogSelfDestruct(msg.sender, this.balance);
  selfdestruct(owner);
}

Then, watch that event log to observe self-destruction events.

Hope it helps.

p.s. I agree with Pablo. constant doesn't belong.

  • I want to return a true, because I'm trying to trigger another event upon the contract being killed. Is there any way to do that? – JorahFriendzone Dec 7 '17 at 23:01
  • I really don't think so, but possibly someone while chime in with an insight. – Rob Hitchens - B9lab Dec 7 '17 at 23:25

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.