DEV Community

Cover image for Using one smart contract address across multiple Blockchains.
Ayo Solomon
Ayo Solomon

Posted on • Edited on

Using one smart contract address across multiple Blockchains.

Using smart contracts with the same address across different Blockchains makes the smart contract super developer-friendly.

It removes the huddle of extra lines of code in trying to maintain addresses for different environments (mainnet vs. testnet) and blockchains (Ethereum, Polygon, Avalanche.). Another great benefit is that social campaigns are also very effective since users need to know or have just one address to interact with.

A typical example is Uniswap. Uniswap usually has one smart contract address that’s maintained across different blockchains. This contract address 0x1F98431c8aD98523631AE4a59f267346ea31F984 is UniswapV3Factory smart contract address across all main nets such as Ethereum, Polygon, Optimism, Arbitrum, and Avalanche.

How is this possible?

This is what I’ll show you. In this article, you’ll get to know how to get and use the same contract address across multiple Blockchains

A smart contract address is deterministic. What this means is that even before deployment, we can determine the smart contract address that will be generated using the wallet address from which the user will deploy the contract and their most recent nonce incremented by one.

For clarity, I’ll briefly explain what nonce and wallets are.

On the EVM(Ethereum Virtual Machine), there are two types of accounts: Externally owned accounts (EOA) and smart contract accounts.

Externally owned accounts are accounts generated as user wallets. They contain a 42-character hexadecimal string, random letters, and numbers.

A smart contract address is also a 42-character hexadecimal string attached to computer programs called smart contracts. Smart contracts are immutable scripts that are executed on the EVM.

Nonce also known as Number Used Only Once is used to record the number of transactions a user has performed on a given Blockchain. It is also used to validate and invalidate a transaction to monitor double-spending attacks.

So now that you understand what Nonce and wallets are, let’s go into the main purpose of this article. How is the address of a smart contract generated?

The smart contract’s address is derived on the Blockchain from two values: a user’s wallet address and the count on the number of transactions the user has sent. So simply put, if you monitor the nonce you can always know what the address of the smart contract will be even before deploying to the Blockchain.

This is done by just computing your nonce and your wallet address.

Let’s look at this…

//this imports the ethers.js library
const ethers = require('ethers')

/**
 * this is your new wallet address, this address shoukd never be used to send or purchase 
 * crypto before deploying smart contract
 */
const walletAddress = "0x331C0A6Bf81A16d366cc73f01Bf86DEfdC8E626A";
//here we are defining the nonce
 const nonce = 0;

 //This deployedAddress veriable uses the ethers.js library to get the contract address
 //by computing the nonce and the wallet address
let deployedAddress = ethers.utils.getAddress(ethers.utils.getContractAddress({from: walletAddress, nonce}));

console.log(deployedAddress);
//Console will print 0x5489e3bF61dde8A04E53b3Ae54E3710E06B3E9ED
Enter fullscreen mode Exit fullscreen mode

When you increase the nonce you’ll notice that the address changes, you can do this by changing the nonce from being 0 to 1 and you should get a different deployment address like this

Image description

We can verify that we get this same address for our contract by deploying with Remix IDE.

Image description

Image description

Image description

Finally, when you send a smart contract creation transaction in all the blockchains with this same nonce it will result in the same smart contract address everywhere.

I hope you found this article useful. You could drop feedback or ask your question via the comments, I’ll be glad to help in any way I can.

Here's my ERC20 wallet for tip
0xD48a3323E0349912185Ae4522F083bcc011CEa07

Top comments (0)