Skip to content

Instantly share code, notes, and snippets.

@luoqeng
Forked from PavelCore/create2.sol
Created March 26, 2021 09:48
Show Gist options
  • Save luoqeng/a276398fce6614b238b8f738991ad75c to your computer and use it in GitHub Desktop.
Save luoqeng/a276398fce6614b238b8f738991ad75c to your computer and use it in GitHub Desktop.
// Note that this is not the production code
pragma solidity 0.5.6;
import "./IERC20.sol";
contract Wallet {
address internal token = 0x123...<hot_wallet_addr>;
address internal hotWallet = 0x321...<hot_wallet_addr>;
constructor() public {
// send all tokens from this contract to hotwallet
IERC20(token).transfer(
hotWallet,
IERC20(token).balanceOf(address(this))
);
// selfdestruct to receive gas refund and reset nonce to 0
selfdestruct(address(0x0));
}
}
contract Fabric {
function createContract(uint256 salt) public {
// get wallet init_code
bytes memory bytecode = type(Wallet).creationCode;
assembly {
let codeSize := mload(bytecode) // get size of init_bytecode
let newAddr := create2(
0, // 0 wei
add(bytecode, 32), // the bytecode itself starts at the second slot. The first slot contains array length
codeSize, // size of init_code
salt // salt from function arguments
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment