DEV Community

Cover image for How Uniswap deployed contract to the same address?
Turbo Panumarch
Turbo Panumarch

Posted on

How Uniswap deployed contract to the same address?

The contract address is not random?

I thought all the Ethereum-based contract address was random, just like how we access the wallet from the randomized private key.

Until I found interesting notes from Uniswap v2 Docs.

  1. The Uniswap V2 router address is 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D for all networks.

  2. Moreover, Uniswap LP token address can be deterministicly computed offline.

Wow, These are such a nice dev experience ✨. So I want to explore how they do it.


1. Deploying same contract address to all networks

The address for an Ethereum contract is deterministically computed from the address of its creator (sender) and how many transactions the creator has sent (nonce)

So one simple way is to use the same wallet (address) to deploy all networks, and deploy with the first transaction of the wallet, so nonce will be the same. 🎉

But why?: I think this is quite convenient for the testnet development. The developers have no more need to google for the different testnet address, or make the config handle different addresses.

2. Deploying a pair of tokens LP to the same address.

A new opcode, CREATE2 was added in EIP-1014 that is another way that a contract can be created. For contract created by CREATE2 its address will be:
keccak256( 0xff ++ senderAddress ++ salt ++ keccak256(init_code))[12:]

Considering variable params needed...

  • init_code is a byte code from the deployed contract
  • salt is something which can be changed.

So Uniswap utilized salt to be a hash of token0 and token1 addresses. That's why it is always deterministic. 🎉

Which I think this is also helpful to Uniswap to avoid having a duplicate LP for the same pair of tokens.

But why?: This is quite useful to get the information of each LP without looking up LP address. We just need the addresses of 2 tokens, then compute the LP address to retrieve the information right away.


Thanks to a nice summary from: https://ethereum.stackexchange.com/a/761/76945

Top comments (0)