2

I'm quite new at smart contracts/Solidity, which I'm learning for a postgraduate course.

In one of the assignments, we have to implement a short example of dutch auction (wiki link). Now my question is that in one of methods, specifically finalize(), the owner of contract must destroy it and collect its funds. And here is my code:

    function finalize() public creatorOnly biddingClosedOnly {
      selfdestruct(_creator);
      if(finalPrice > 0) {
        _creator.transfer(finalPrice);
      }
    }

The rest of contract's methods are running well in Remix but when I call finalize method, the smart contract is not destroyed and still exists so that anyone can call the other functions as before. What am I doing wrong ?

  • 1
    Doesn't it feel to you like you should be self-destructing the contract after you've transferred the funds? – goodvibration May 23 at 18:22
3

All you need to do is have the selfdestruct(address payable recipient) function. selfdestruct takes a single parameter that sends all ETH in the contract to that address. In your case, you can do:

function finalize() public creatorOnly biddingClosedOnly {
    selfdestruct(_creator);
}

From the docs:

selfdestruct(address payable recipient): destroy the current contract, sending its funds to the given address


The reason you can still call the function after the contract has been selfdestructed is because technically the address is still valid. However, no contract (data) lives there any more.

Because of this, you can still send ETH to the address and you can still send transactions with data to the address, but the EVM will not execute the function as it would with a non-selfdestructed address.

Edit: You can use the get_code RPC method to verify that the contract was, in fact, destroyed.

Using ethers.js, the following output will be given:

// Deploy contract

ethersProvider.getCode('0xDCcd6331401b62ebcE7F3a22e966b26ACe27559d').then(console.log)
> 0x608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680634bb278f3146044575b600080fd5b348015604f57600080fd5b5060566058565b005b3373ffffffffffffffffffffffffffffffffffffffff16ff00a165627a7a7230582077ca7684f4d93293e360c5c695d0e416f54dde89713426cc4d6fddb9f9963faa0029

// Self destruct contract

ethersProvider.getCode('0xDCcd6331401b62ebcE7F3a22e966b26ACe27559d').then(console.log)
> 0x
| improve this answer | |

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.