Created
June 6, 2020 09:32
-
-
Save Sowmayjain/3f44f0aec47d55b88575006ba74ead76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.6.6; | |
interface IERC20 { | |
function balanceOf(address) external view returns (uint); | |
function transfer(address, uint) external returns (bool); | |
} | |
contract Wallet { | |
function flush(address token) public { | |
address payable treasury = 0x0000000000000000000000000000000000000000; | |
if (token != address(0x0)) IERC20(token).transfer(treasury, IERC20(token).balanceOf(address(this))); | |
if (address(this).balance > 0) treasury.transfer(address(this).balance); | |
} | |
} | |
contract Deployer { | |
function calculateAddress(uint256 seed) public view returns (address) { | |
bytes32 codeHash = keccak256(type(Wallet).creationCode); | |
bytes32 salt = bytes32(seed); | |
bytes32 rawAddress = keccak256( | |
abi.encodePacked( | |
bytes1(0xff), | |
address(this), | |
salt, | |
codeHash | |
) | |
); | |
return address(bytes20(rawAddress << 96)); | |
} | |
function deploy(uint256 seed, address token) public returns (address wallet) { | |
bytes memory code = type(Wallet).creationCode; | |
bytes32 salt = bytes32(seed); | |
assembly { | |
wallet := create2(0, add(code, 0x20), mload(code), salt) | |
if iszero(extcodesize(wallet)) {revert(0, 0)} | |
} | |
Wallet(wallet).flush(token); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment