I am studying Uniswap source code and there is something I do not understand in this file:


This factory can create new pairs (createPair function):

    bytes memory bytecode = type(UniswapV2Pair).creationCode;
    bytes32 salt = keccak256(abi.encodePacked(token0, token1));
    assembly {
        pair := create2(0, add(bytecode, 32), mload(bytecode), salt)

for me, these 4 lines do the same thing than:

pair = new UniswapV2Pair()

I do not understand what they want to do with the 4 lines of codes. Do you have any idea ?

Thanks a lot

  • Here is another source code for flattener + EtherScan verification API: Jan 19, 2021 at 9:02

2 Answers 2


The lines you showed are using the new CREATE2 opcode, instead of the regular CREATE opcode (which would be invoked by using pair = new UniswapV2Pair()).

The benefit of using CREATE2 is that the contract address doesn't depend on the transaction nonce, but on other values which do not automatically change. This way the address can be predetermined.

One of the main advantages of predetermined addresses is that the contract only needs to be created if needed - this is the case especially for Layer 2 disputes where the contract is actually created only if a dispute arises and it needs to be resolved with the contract. This way we save in gas costs.

In the case of Uniswap, using CREATE2 enables the address of any pair to be determined without an on-chain lookup. https://docs.uniswap.org/protocol/V2/guides/smart-contract-integration/getting-pair-addresses

In newer Solidity versions assembly is not needed but CREATE2 can be invoked directly from pure Solidity. Here's some more information: https://docs.openzeppelin.com/cli/2.8/deploying-with-create2

  • Thanks a lot but what is the value-add to predict the contract address ?
    – Bob5421
    Jan 19, 2021 at 8:01
  • edited answer to answer your question Jan 19, 2021 at 9:47
  • Creation itself is not cheaper vs CREATE according to: eips.ethereum.org/EIPS/eip-1014 "The CREATE2 has the same gas schema as CREATE, but also an extra hashcost of GSHA3WORD * ceil(len(init_code) / 32), to account for the hashing that must be performed."
    – wcislo
    Jul 3, 2022 at 13:06
  • The advantage for Uniswap is that they are able to determine the pair address with keccak256 instead of using an on-chain lookup. docs.uniswap.org/protocol/V2/guides/smart-contract-integration/…
    – Milk
    Aug 29, 2022 at 21:21

Create2 used to have ability get pair address without any on-chain requests to save gas etc.

Thanks to some fancy footwork in the factory, we can also compute pair addresses without any on-chain lookups because of CREATE2. The following values are required for this technique

Details: https://docs.uniswap.org/protocol/V2/guides/smart-contract-integration/getting-pair-addresses

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.