header-langage
简体中文
繁體中文
English
Tiếng Việt
한국어
日本語
ภาษาไทย
Türkçe
Scan to Download the APP

Simple Introduction to Solidity|Lecture 24: Create a new contract in a contract

Read this article in 7 Minutes
This article uses a minimalist Uniswap example to introduce how to use the create method to create a contract in a contract.
Original title: "Introduction to Solidity Minimalist: 24. Create a new contract in the contract "
Original author: 0xAA 


I am re-learning solidity recently, consolidate the details, and write a "Solidity minimalist introduction" , for beginners (programmers can find another tutorial), update 1-3 lectures every week.


All codes and tutorials are open source on github: github.com/AmazingAng/WTFSolidity


In On the Ethereum chain, users (external accounts, EOA) can create smart contracts, and smart contracts can also create new smart contracts. The decentralized trading platform uniswap uses the factory contract (Factory) to create countless currency pair contracts (Pair). In this talk, I will use a simplified version of uniswap to talk about how to create a contract through a contract.


create and create2


There are two ways to create a new contract in a contract, create and create2, here we talk about create, and create2 will be introduced in the next lecture.


The use of create is very simple, it is to create a new contract, and pass in the parameters required by the new contract constructor:



Where Contract is to be created Contract name, x is the contract object (address), if the constructor is payable, you can transfer _value amount of ETH when creating, params is the parameter of the new contract constructor.


Simple Uniswap


The Uniswap V2 core contract contains two contracts:


UniswapV2Pair: currency pair contract, used to manage currency pair address, liquidity, buying and selling.

UniswapV2Factory: Factory contract, used to create new currency pairs and manage currency pair addresses.


Next, we use the create method to implement a minimal version of Uniswap: the Pair currency pair contract is responsible for managing the currency pair address, and the PairFactory factory contract is used to create new currency pairs , and manage currency pair addresses.



The Pair contract is very simple and contains 3 state variables: factory, token0 and token1.


The constructor assigns factory as the address of the factory contract when it is deployed. The initialize function will be called once by the factory contract when the Pair contract is created, and token0 and token1 will be updated to the addresses of the two Tokens in the pair.


Question: Why doesn’t uniswap update the addresses of token0 and token1 in the constructor?


PairFactory  



The factory contract (PairFactory) has two state variables getPair is two Token The map from the address to the currency pair address is convenient according to Token Find the currency pair address; allPairs is an array of currency pair addresses, storing all Token address.


The PairFactory contract has only one createPair function, according to the two input Token Address tokenA and tokenB to create a new Pair contract. where



is the code to create the contract, which is very simple. You can deploy the PairFactory contract, and then use the following two addresses as parameters to call createPair to see what the address of the created currency pair is:  



Summary


In this lecture, we use the minimalist Uniswap example to introduce how to use the create method to create a contract in the contract, and we will introduce it in the next lecture How to use the create2 method to implement minimalist Uniswap.


Original link


Welcome to join the official BlockBeats community:

Telegram Subscription Group: https://t.me/theblockbeats

Telegram Discussion Group: https://t.me/BlockBeats_App

Official Twitter Account: https://twitter.com/BlockBeatsAsia

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit