Deploying Smart Contracts to the Same Address Across Multiple Networks with Foundry

·

2 min read

Deploying Smart Contracts to the Same Address Across Multiple Networks with Foundry

Quick Summary

Deploying a smart contract to the same address on different blockchain networks is straightforward: use a dedicated wallet that's solely for deploying a specific smart contract. This method, utilized by Multicall3, ensures consistent contract addresses across networks.

Step-by-Step Guide Using Foundry

Step 1: Prepare a Dedicated Wallet

  • Use a unique wallet address for deploying your smart contract. In this tutorial, we'll use:

      0x000006b92fb31c6854e9f598f2d0096da4d02de0
    
  • Fund this wallet for deployment and ensure it's never been used on your target networks.

Step 2: Set Up Foundry

  • Install Foundry following the official guide.

  • Check your Forge version:

      ✦ forge --version
      forge 0.2.0 (8517446 2022-04-12T12:36:16.992513+00:00)
    
  • Update to the latest version if needed:

      foundryup -b master
    

Step 3: Clone and Update the Example Project

  • Clone the tutorial repository:

      git clone git@github.com:pyk/foundry-tutorial-same-address.git
      cd foundry-tutorial-same-address/
      forge update
    

Step 4: Deploy to Multiple Networks

Deploy to Rinkeby

  • Run the deployment command:

      forge create --chain=rinkeby \
          --rpc-url <RPC_URL> \
          --optimize \
          --optimize-runs 200 \
          --private-key <PRIVATE_KEY> \
          src/SameAddress.sol:SameAddress
    
  • Note the deployment result:

      Deployed to: 0x01bb3f43c855b80dd82ad6468c378aae73decc84
    

Verify on Rinkeby

  • Verify the contract:

      forge verify-contract --chain-id=rinkeby \
          --compiler-version v0.8.13+commit.abaa5c0e \
          --num-of-optimizations 200 \
          0x01bb3f43c855b80dd82ad6468c378aae73decc84 \
          src/SameAddress.sol:SameAddress \
          <ETHERSCAN_API_KEY>
    
  • Contract on Rinkeby: Etherscan Link

Deploy to Kovan

  • Repeat the deployment process for Kovan:

      forge create --chain=kovan \
          --rpc-url <RPC_URL> \
          --optimize \
          --optimize-runs 200 \
          --private-key <PRIVATE_KEY> \
          src/SameAddress.sol:SameAddress
    
  • Confirm the deployment result:

      Deployed to: 0x01bb3f43c855b80dd82ad6468c378aae73decc84
    

Step 5: Repeat and Confirm

  • Repeat the deployment steps for other networks.

  • You'll notice the contract address remains the same across networks:

      0x01bb3f43c855b80dd82ad6468c378aae73decc84
    

Conclusion

Deploying to multiple networks with the same address is quite simple using Foundry and a dedicated wallet.

Explore the tutorial repository here for more details. Happy deploying!