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 ?
Best Answer
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:From the docs:
The reason you can still call the function after the contract has been
selfdestruct
ed 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-
selfdestruct
ed 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: