Skip to content

Instantly share code, notes, and snippets.

@k06a
Created May 18, 2019 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k06a/4c29391e3056f4460e93721bcc602f31 to your computer and use it in GitHub Desktop.
Save k06a/4c29391e3056f4460e93721bcc602f31 to your computer and use it in GitHub Desktop.
GasRefund
contract GasRefund {
uint256 private _begin;
uint256 private _end;
function refundSupply() public view returns(uint256 supply) {
return _end - _begin;
}
function burn(uint256 n) internal {
uint256 begin = _begin;
require(begin + n <= _end);
for (uint i = begin; i < begin + n; i++) {
_addressFrom(i).call("");
}
_begin = begin + n;
}
function mint(uint256 n) internal {
for (uint i = 0; i < n; i++) {
_makeChild();
}
_end += n;
}
function _makeChild() private returns (address addr) {
assembly {
// EVM assembler of runtime portion of child contract:
// ;; Pseudocode: if (msg.sender != 0x0000000000b3f879cb30fe243b4dfee438691c04) { throw; }
let mem_ptr := mload(0x40)
mstore(mem_ptr, 0x00756eb3f879cb30fe243b4dfee438691c043318585733ff6000526016600af3)
addr := create(0, add(mem_ptr, 1), 31)
}
}
function _addressFrom(uint _nonce) private view returns(address) {
if(_nonce == 0x00) return address(bytes20(keccak256(abi.encodePacked(byte(0xd6), byte(0x94), address(this), byte(0x80)))));
if(_nonce <= 0x7f) return address(bytes20(keccak256(abi.encodePacked(byte(0xd6), byte(0x94), address(this), uint8(_nonce)))));
if(_nonce <= 0xff) return address(bytes20(keccak256(abi.encodePacked(byte(0xd7), byte(0x94), address(this), byte(0x81), uint8(_nonce)))));
if(_nonce <= 0xffff) return address(bytes20(keccak256(abi.encodePacked(byte(0xd8), byte(0x94), address(this), byte(0x82), uint16(_nonce)))));
if(_nonce <= 0xffffff) return address(bytes20(keccak256(abi.encodePacked(byte(0xd9), byte(0x94), address(this), byte(0x83), uint24(_nonce)))));
return address(bytes20(keccak256(abi.encodePacked(byte(0xda), byte(0x94), address(this), byte(0x84), uint32(_nonce))))); // more than 2^32 nonces not realistic
}
function addressFrom() public view returns(address) {
return _addressFrom(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment