I was going through uniswap code trying to understand the code and most of it is pretty clear but I do have a few questions.
in this function:
function createPair(address tokenA, address tokenB) external returns (address pair) {
require(tokenA != tokenB, 'UniswapV2: IDENTICAL_ADDRESSES');
(address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
require(token0 != address(0), 'UniswapV2: ZERO_ADDRESS');
require(getPair[token0][token1] == address(0), 'UniswapV2: PAIR_EXISTS'); // single check is sufficient
bytes memory bytecode = type(UniswapV2Pair).creationCode;
bytes32 salt = keccak256(abi.encodePacked(token0, token1));
assembly {
pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
}
IUniswapV2Pair(pair).initialize(token0, token1);
getPair[token0][token1] = pair;
getPair[token1][token0] = pair; // populate mapping in the reverse direction
allPairs.push(pair);
emit PairCreated(token0, token1, pair, allPairs.length);
There is the assembly line. According to solidity docs this deploys a new contract but I don't understand how it works where it gets the code from and so on.
So is it possible to "translate" this into solidity somehow? Thanks a lot!
It makes use of the create2
opcode allowing you to deploy a contract to an address determinable by its bytecode and salt.
Uniswap V2 was written in Solidity 0.5 that didn't have a way to produce the create2
opcode directly from the language. So you had to use a low-lewel assemly to actually use this opcode.
The current version 0.8 allows passing the salt
param producing the create2
(instead of the regular create
) opcode.
pragma solidity ^0.8;
contract UniswapV2Pair {
}
contract MyContract {
function createPair() external {
bytes32 salt = 0x1234567890123456789012345678901234567890123456789012345678901234;
address pair = address(
new UniswapV2Pair{salt: salt}()
);
}
}
Uniswap uses a combination of the pair token addresses as the salt, and the bytecode is always the same. Which effectively allows to deploy just one contract for each unique pair combination.
Example:
0x123
and 0x456
would always result in UniswapV2Pair
contract address 0xabc
.0x123
and 0x789
would always result in UniswapV2Pair
contract address 0xdef
.