0

there.

I'm coding kill function below:

function kill() public onlyOwner returns (bool) {
  selfdestruct(msg.sender);     
  return true;
}

and my unit test code:

function testOwnerCanKillContract() public {
  XToken xtn = XToken(DeployedAddresses.XToken());
 bool isKilled = nmt.kill();
 Assert.equal(isKilled, true, "Owner should have to kill contract with selfdestruct.");
}

However, test fails with revert error after $ truffle test:

Error: Returned error: VM Exception while processing transaction: revert

So does anyone know how to resolve this problem, or any idea?

and also, my enviromnent is here: Truffle v5.0.5 (core: 5.0.5) Solidity v0.5.0 (solc-js) Node v10.9.0

Thanks in advance!

1

I don't quite get the code. It looks it should be xtn.kill();

In any case, kill wipes out the state and the bytecode. If I'm not mistaken, the return step will never happen because the contract was obliterated.

If that case, any call to the contract should fail. That's one way to test, crudely.

  1. You could test it reverts as expected. A sort of "Now you see it, bu-bye, now you don't process". Your test would insist that the call/transaction must revert to call it a success. Same pattern you would use to confirm that only the owner can use an onlyOwner function, i.e. testing that things that should not happen do in fact never happen.
  2. You can confirm the msg.sender received the balance by comparing before and after balances of the sender and the contract. That's part of it.
  3. You can confirm that there is no bytecode at the address anymore. Have a look at the accepted answer here. So, response must be 0x. How to detect if an address is a contract?.

Unless you have a really good reason, I would incline to using a Pausable pattern to refrain from creating nasty voids on the blockchain. Selfdestruct is a brutal way to go that can lead to trouble.

Hope it helps.

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.