Skip to content

Instantly share code, notes, and snippets.

@tinchoabbate
Last active May 29, 2020 17:56
Show Gist options
  • Save tinchoabbate/6b8ca6b2b7cf137e3d9171211c549de5 to your computer and use it in GitHub Desktop.
Save tinchoabbate/6b8ca6b2b7cf137e3d9171211c549de5 to your computer and use it in GitHub Desktop.
Calling a contract after being selfdestructed
pragma solidity ^0.6.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.1/contracts/utils/Address.sol";
contract Test {
using Address for address;
event Log(uint256 result);
function execute() public payable {
// Deploy target contract
SelfDestructible c = new SelfDestructible{value: msg.value}();
require(address(c).isContract()); // OK
require(address(c).balance == msg.value);
// Selfdestruct the target contract
c.explode();
// After selfdestructing, balance is set to zero, but code can still be executed
require(address(c).balance == 0);
require(address(c).isContract()); // OK
require(c.onePlusOnePure() == 2); // OK - Still able to execute pure functions
require(c.onePlusOneView() == 2); // OK - Still able to read from storage
require(c.onePlusOne() == 2); // OK - Still able to read and write storage
// Now with low-level calls
(bool success, bytes memory returndata) = address(c).call(abi.encodeWithSignature("onePlusOnePure()"));
require(success); // OK
(success, returndata) = address(c).call(abi.encodeWithSignature("onePlusOneView()"));
require(success); // OK
(success, returndata) = address(c).call(abi.encodeWithSignature("onePlusOne()"));
require(success); // OK
uint256 result = abi.decode(returndata, (uint256));
emit Log(result); // 3 (OK)
}
}
contract SelfDestructible {
uint256 private number = 1;
constructor() public payable {}
function explode() public {
selfdestruct(msg.sender);
}
function onePlusOnePure() public pure returns (uint256) {
return 1+1;
}
function onePlusOneView() public view returns (uint256) {
return number + 1;
}
function onePlusOne() public returns (uint256) {
number += 1;
return number;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment