Solidity – How to Call Selfdestruct Without Writing It in Contract

ethereumjselfdestructsolidity

as mentioned in documentation "Even if a contract’s code does not contain a call to selfdestruct, it can still perform that operation using delegatecall or callcode." so my question is how does it possible , my purpose is to release Eth which stuck in a contract due to code mistake, and there is no way to withdraw now

Best Answer

If your contract has a function to run delegatecall with some way to provide the function to call, you can in make it run a function in another contract, which calls selfdestruct. Since functions ran by delegatecall will execute in the context of the first contract, this will self destruct the first contract. For example:

contract A {
  function kill (address payable to) public {
    selfdestruct(to);
  }
}

contract B {
  function run (address target, string calldata func, address to) public {
    target.delegatecall(abi.encodeWithSignature(func, to));
  }
}

Calling run with the address of contract A, kill(address) as func and any address as to, will cause contract B to self destruct and send the Ether to address to.

This run function is not very secure, as you can see. If your contract does not have such a function built-in, you're out of luck, and your ETH is unfortunately stuck.

Related Topic