On Ethereum, the user (Externally-owned account, EOA) can create smart contracts, and a smart contract can also create new smart contracts. The decentralized exchange Uniswap creates an infinite number of Pair contracts with its Factory contract. In this lecture, I will explain how to create new smart contracts in an existed smart contract by using a simplified version of Uniswap.
create and create2
There are two ways to create a new contract in an existing contract, create and create2, this lecture will introduce create, next lecture will introduce create2.
The usage of create is very simple, creating a contract with new keyword, and passing the arguments required by the constructor of the new smart contract:
Contract is the name of the smart contract to be created, x is the smart contract object (address), and if the constructor is payable, the creator can transfer _value ETH to the new smart contract, params are the parameters of the constructor of the new smart contract.
Simplified Uniswap
The core smart contracts of Uniswap V2 include 2 smart contracts:
- UniswapV2Pair: Pair contract, used to manage token addresses, liquidity, and swap.
- UniswapV2Factory: Factory contract, used to create new Pair contracts, and manage Pair address.
Below we will implement a simplified Uniswap with create: Pair contract is used to manage token addresses, PairFactory contract is used to create new Pair contracts and manage Pair addresses.
Pair contract
Pair contract is very simple, including 3 state variables: factory, token0 and token1.
The constructor assigns the Factory contract's address to factory at the time of deployment. initialize function will be called once by the Factory contract when the Pair contract is created, and update token0 and token1 with the addresses of 2 tokens in the token pair.
Ask: Why doesn't
Uniswapset the addresses oftoken0andtoken1in theconstructor?Answer: Because
Uniswapusescreate2to create new smart contracts, parameters are not allowed in the constructor when using create2. When usingcreate, it is allowed to have parameters inPaircontract, and you can set the addresses oftoken0andtoken1in theconstructor.
PairFactory
Factory contract (PairFactory) has 2 state variables, getPair is a map of 2 token addresses and Pair contract address, and is used to find Pair contract address based on 2 token addresses. allPairs is an array of Pair contract addresses, which is used to store all Pair contract addresses.
There's only one function in PairFactory, createPair, which creates a new Pair contract based on 2 token addresses tokenA and tokenB.
The above code is used to create a new smart contract, very straightforward. You can deploy PairFactory contract first, then call createPair with the following 2 addresses as arguments, and find out what is the address of the new Pair contract.
Verify on Remix
- Call
createPairwith the arguments of the addresses ofWBNBandPEOPLE, we will have the address ofPaircontract: 0x5C9eb5D6a6C2c1B3EFc52255C0b356f116f6f66D

- Check the state variables of
Paircontract

- Use debug to check
createopcode

Summary
In this lecture, we introduce how to create a new smart contract in an existing smart contract with create method by using a simplified version of Uniswap, in the next lecture we will introduce how to implement a simplified Uniswap with create2.