Pre-compute contract deployment address using CREATE2
In this article lets see how we can calculate the deployment address of our contract on the EVM blockchain before deploying. Here we make use of CREATE2 opcode of EVM which is similar to CREATE opcode.
Let us deploy “TestContract” and match the address of deployment with our precomputed address. So lets switch to remix… You can refer to the code on Github here.

This is the simplest contract to be deployed(notice it does not have any constructor args… Further in the article we shall see, if it had constructor args, how to insert the args and get bytecode)
Lets create a contract named “preComputeAddress” which pre-computes the address and deploys this “TestContract”.
- It has a function to compute address with the following signature.It takes the bytecode of the contract whose address needs to be calculated with a salt(think of it as a random number).
function computeAddress(bytes memory _byteCode, uint256 _salt)public view returns (address )
2. To calculate the bytecode of the contract lets add another function with the following function signature. In this function, we use “.creationCode” (check the end of the article for difference between runtimebytecode and initbytecode. creationcode is same as initbytecode)
function getBytecode() public pure returns (bytes memory)
3. and then finally we shall deploy our contract and emit an event with the deployed address
function deploy(bytes memory _byteCode, uint256 _salt)public payable
event signature
event Deployed(address indexed preComputedAddress);
lets deploy our PreComputedAddress contract in remix. check this Github repo for the code.

We can see the three functions of our contract exposed. Lets get the bytecode of the “Test contract” which, we wish to deploy. If the contract has any constructor args, they are encoded and appended to the bytecode of the contract.

Now lets calculate the deployment address using “computeAddress” function. here it takes the above calculated bytecode and a salt. computeAddress() performs keccak256 of a)the contract address from which it is getting deployed, b)salt passed (salt is just a random number used), c) and the keccak256 of the byte code. these params are prefixed with “0xff” and packed together in the order as shown below. Finally the address is the last 20 bytes of the resulting hash.
keccak256(abi.encodePacked(bytes1(0xff),address(this),_salt,keccak256(_byteCode)));
the calculated address in remix(using salt = 121) is “0x578e2361Cfa345158e4635Dfb52484db60c37cf6” as shown below.

Now let us deploy the TestContract and check the event log for the address. The deploy function uses CREATE2 opcode of the EVM to deploy the contract.This will be using assembly language of EVM(yul).

Lets dig-in to the event log and find the deployment address.

as you can see in the code, event Deployed has an indexed param called preComputedAddress. this can be seen in the event log. Our precompiled address is the same as the address in the event Deployed.
More about CREATE2
CREATE2 is an EVM opcode. it can be called using create2(v,p,n,s).
where v is the value of the ether passed in the call, p is the starting pointer to bytecode passed, n is the size of th e bytecode i.e from p to p+n is the bytecode. s is the salt passed. this function returns the address of the contract deployed(contract is defined by the bytecode p to p+n).
More about initbytecode and runtimebytecode
The code stored on chain is different from the code which we get after compiling the smart contract.
Contract creation happens when we send datapayload to a null/0 address in the EVM. The code(bytecode) what we send is init-code which runs and constructs the runtime-code. And then this runtime-code is stored on the EVM using “sstore” opcode (without the constructor code and private variables). So whenever we fetch the contract code using “extcodecopy” opcode, the runtimebytecode is returned.
Further Reading
Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing
Also, Read
- Bookmap Review | 5 Best Crypto Exchanges in the USA
- The Best Crypto Hardware wallet | Bitbns Review
- 10 Best Crypto Exchange in Singapore | Buy AXS
- Red Dog Casino Review | Swyftx Review | CoinGate Review
- Best Crypto to Invest in India | WazirX P2P | Hi Dollar Review
- Best Crypto Trading bots in Canada | KuCoin Review
- Crypto Trading Signals for Huobi | HitBTC Review