Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More optimized mk_contract_address function #15

Open
k06a opened this issue May 10, 2019 · 0 comments
Open

More optimized mk_contract_address function #15

k06a opened this issue May 10, 2019 · 0 comments

Comments

@k06a
Copy link

k06a commented May 10, 2019

Estimated gas: 526

function mk_contract_address(uint _nonce) private view returns(address) {
    if(_nonce == 0x00)     return address(uint256(keccak256(abi.encodePacked(byte(0xd6), byte(0x94), address(this), byte(0x80)))));
    if(_nonce <= 0x7f)     return address(uint256(keccak256(abi.encodePacked(byte(0xd6), byte(0x94), address(this), uint8(_nonce)))));
    if(_nonce <= 0xff)     return address(uint256(keccak256(abi.encodePacked(byte(0xd7), byte(0x94), address(this), byte(0x81), uint8(_nonce)))));
    if(_nonce <= 0xffff)   return address(uint256(keccak256(abi.encodePacked(byte(0xd8), byte(0x94), address(this), byte(0x82), uint16(_nonce)))));
    if(_nonce <= 0xffffff) return address(uint256(keccak256(abi.encodePacked(byte(0xd9), byte(0x94), address(this), byte(0x83), uint24(_nonce)))));
    return address(uint256(keccak256(abi.encodePacked(byte(0xda), byte(0x94), address(this), byte(0x84), uint32(_nonce))))); // more than 2^32 nonces not realistic
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant