Blog

Predictable Contract Addresses in Solidity: Understanding Deterministic Deployment and the Role of CREATE2

When you are deploying a contract using the new keyword, a contract is deployed with a new unique address that is based on a deterministic process. This address is based on the deployer's address and the nonce (a number that increments with each transaction from the address).

This means that, if you deploy a contract multiple times from the same address in sequence, its address will be predictable.

The new contract's address is generated deterministically from:

The deployer’s address (msg.sender).

The deployer’s nonce (how many transactions the deployer has made).

While deterministic addresses can be useful, there are cases where predictability is not desirable. For instance:

Contract logic dependency: Some applications might rely on unique addresses, and deterministic deployment could cause unintended conflicts or vulnerabilities

To prevent deterministic contract address generation when it is undesirable, we can introduce a custom salt using the CREATE2 opcode in Solidity.

The CREATE2 opcode allows you to deploy a contract with a custom salt, making the contract's address deterministic based on the salt, but customizable. By using msg.sender as the salt, we can ensure that each deployment address is uniquely tied to the deployer's address, even if the deployment happens multiple times.