logo
Deploying a contract to identical addresses on multiple networks
by John Oba - AfrodevMarch 19, 2023 • 3 min read
Image Banner

While dabbling with dApps and smart contracts, you may have noticed some contracts with the same addresses across testnets and mainnets. It provides a good developer experience for developers building with your smart contracts. A clean example is Uniswap router smart contract.

uniswap router

In this article, we will deploy a smart contract across evm chains using Remix IDE.

Remix IDE, is a no-setup tool with a GUI for developing smart contracts.

How are contract addresses generated?

A contract address is derived from these three main components.

  • Deployer’s address
  • Deployer’s nonce
  • Contract's bytecode

This ensures that a smart contract deployed to the network has a unique address.

For a contract address to remain the same across the target EVM chains, the contract must be deployed with the same wallet address, the wallet's nonce is equivalent on each network, and the contract’s bytecode remains the same.

What Is a Nonce?

This is the (sequential) number of transactions sent from a given address. In English, a nonce is a number that can only be used once. It helps to keep transactions and transaction ids unique.

Let’s go down the rabbit hole

A very quick way to play around with this convention is to use remix. Here's a simple Storage contract.

    // SPDX-License-Identifier: GPL-3.0
    
    pragma solidity >=0.8.2 <0.9.0;
    
    /**
     * @title Storage
     * @dev Store & retrieve value in a variable
     */
    contract Storage {
    
        uint256 number;
    
        /**
         * @dev Store value in variable
         * @param num value to store
         */
        function store(uint256 num) public {
            number = num;
        }
    
        /**
         * @dev Return value 
         * @return value of 'number'
         */
        function retrieve() public view returns (uint256){
            return number;
        }
    }

We will utilize a single virtual account within the Remix IDE to deploy the contract onto two EVM virtual networks, the Remix VM (London) and Remix VM (Berlin).

Compile the Storage Contract

To deploy our compiled contract onto different virtual machines in the Remix IDE's test environments, we can select one out of the 15 available virtual accounts with 100ETH. However, it's important to take note of the selected account and ensure that it has not been used on the specific Remix VM we plan to deploy the contract to.

Deployment

Selected account is 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4

Remix VM (London)

Resulting Contract address is 0xd9145CCE52D386f254917e481eB44e9943F39138

Remix VM (Berlin)

and the resulting Contract address remains the same 0xd9145CCE52D386f254917e481eB44e9943F39138

Key concepts

  • The bytecode was the same (compiled contract did not change)
  • Same Wallet address
  • The nonces was identical across the two VMs. simply 0

Conclusion

In real world, keeping track of nonces count can be a bit of a mess, and as result other conventions are employed. One major one is Create2. It utilizes a salt (a randomly generated value) instead of a sequential nonce. Here's a guide to using Create2

Cheers 🍻


More Stories from Afrodev

2023 AfroDev