1

I'm going to deploy the Uniswap contract on my customized Ethereum.

Could I replace fancy footwork in the factory by Solidity code?

I need to update this code assembly { pair := create2(0, add(bytecode, 32), mload(bytecode), salt)} into general solidity code.

I tried this, but not working well.

hobbydev
  • 1,513
  • 19
  • 31
  • Stack Overflow questions can link external code for context, but the code you're directly asking about changing or fixing should always be in the question directly. – Peter Cordes Mar 05 '22 at 11:59

2 Answers2

1

I found the solution

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

I updated above code into

UniswapV2Pair newPair = new UniswapV2Pair();
IUniswapV2Pair(pair).initialize(token0, token1);
hobbydev
  • 1,513
  • 19
  • 31
  • I don't think you can calculate the pair address offchain without using `create2`. However, it seems like with Solidity version 0.8, you can do it without using assembly https://stackoverflow.com/questions/71121396/how-is-uniswap-assembly-create2-function-working – Relaxed Leaf Oct 01 '22 at 01:04
0

Uniswap version 1 uses library contracts with hardcoded pair contract addresses.

  • First deploy the factory contract
  • Then call Factory.pairCodeHash - the hardcoded hex string of the init code
  • Replace the pair code hash in the source code, compile the rest of the contracts

More tips in this Python module.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435