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

Simple Introduction to Solidity|Lecture 25: Create2

Read this article in 9 Minutes
This article introduces the principle and usage of the CREATE2 opcode, and uses it to complete the minimal version of Uniswap and calculate the currency pair contract address in advance.
Original title: " A minimalist introduction to Solidity: 25. Create2 》
Original author: 0xAA   


I am re-learning solidity recently, consolidate the details, and write a "Solidity minimalist "Getting Started" is for novices (programmers can find other tutorials), 1-3 lectures are updated every week.


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


CREATE2


The CREATE2  opcode allows us to predict the address of the smart contract before it is deployed on the Ethereum network. Uniswap uses CREATE2 to create Pair contracts. In this lecture, I will introduce the usage of CREATE2.


How CREATE calculates the address


Smart contracts can be operated by other contracts and ordinary accounts using CREATE code creation. In both cases, the address of the new contract is calculated in the same way: a hash of the creator's address and the nonce (the total number of transactions sent by that address).



Creator The address will not change, but the nonce may change over time, so the address of the contract created with CREATE is not predictable.


How CREATE2 calculates the address


The purpose of CREATE2 is to make the contract address independent of future event. No matter what happens on the blockchain in the future, you can deploy the contract on the pre-calculated address. The contract address created with CREATE2 is determined by 4 parts:


0xFF: a constant to avoid conflict with CREATE

Creator address 

salt: a value given by the creator

The bytecode of the contract to be deployed  



CREATE2  ensures that if the creator uses  CREATE2  with the supplied  salt  Deploy the given contract bytecode, which will be stored at the  new address .


How to use CREATE2


The usage of CREATE2 is similar to the Create mentioned before, and it is also new A contract, and pass in the parameters required by the new contract constructor, but you need to pass one more salt parameter:


< p style="text-align:center;">

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


Simple Uniswap2


Similar to the previous lecture, we use Create2 to achieve minimalism Uniswap.


Pair  



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.


PairFactory2  



The factory contract (PairFactory2) 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 PairFactory2 contract has only one createPair2 function, use CREATE2 according to the two input Token Address tokenA and tokenB to create a new Pair contract. where



is the code to create a contract using CREATE2, which is very simple, and the salt is the hash of token1 and token2:

  


Pre-calculate Pair address  



We wrote a calculateAddr function to calculate in advance the Pair address that tokenA and tokenB will generate. Through it, we can verify whether the address we calculated beforehand and the actual address are the same.


You can deploy the PairFactory2 contract, and then use the following two addresses as parameters to call createPair2 to see what the created currency pair address is, and whether it is the same as the pre-calculated Same address:



Summary


In this lecture, we introduced the CREATE2 opcode The principle, usage method, and use it to complete the minimal version of Uniswap and calculate the currency pair contract address in advance. CREATE2 allows us to determine the contract address of the contract before deploying it, which is also the basis of the counterfactual system and many layer2.


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