📢 Gate Square #MBG Posting Challenge# is Live— Post for MBG Rewards!
Want a share of 1,000 MBG? Get involved now—show your insights and real participation to become an MBG promoter!
💰 20 top posts will each win 50 MBG!
How to Participate:
1️⃣ Research the MBG project
Share your in-depth views on MBG’s fundamentals, community governance, development goals, and tokenomics, etc.
2️⃣ Join and share your real experience
Take part in MBG activities (CandyDrop, Launchpool, or spot trading), and post your screenshots, earnings, or step-by-step tutorials. Content can include profits, beginner-friendl
Uniswap Code Revealed: 7 Key Smart Contracts Development Tips Explained
Contract Development Techniques Learned from Uniswap Code
Recently, while writing a tutorial for developing a decentralized exchange, I referenced the code implementation of Uniswap V3 and learned many valuable knowledge points. As a developer attempting to develop Defi contracts for the first time, these techniques will be very helpful for beginners who want to learn contract development.
Predictable Contract Deployment Address
The address obtained from deploying contracts usually appears random due to its relation to nonce. However, in certain cases, we need to infer the contract address through transaction pairs and related information. Uniswap uses the CREATE2 method to create contracts, adding a salt parameter, making the generated contract address predictable. The address generation logic is: new address = hash("0xFF", creator address, salt, initcode).
Effectively Using Callback Functions
In certain scenarios, mutual calls and callbacks between contracts are very useful. For example, in Uniswap's swap method, it will call back swapCallback and pass in the actual required amount of Token. The caller needs to transfer the required Tokens into the pool during the callback to ensure the integrity and security of the entire transaction logic.
Use exception handling to estimate transactions with try catch
In Uniswap's Quoter contract, the swap method is executed within a try-catch block to estimate the transaction. By throwing a special error in the callback function and then capturing and parsing the error message, the transaction estimation functionality is achieved without the need to specifically modify the swap method for estimation purposes.
Big Number Solution to Precision Issues
The computational logic in Uniswap frequently uses a left shift of 96 bits (equivalent to multiplication by 2^96). This method ensures that normal transactions do not overflow while also maintaining precision. Although there is theoretically still a minimal loss of precision, it is acceptable in practical applications.
Share Mechanism to Calculate Earnings
In order to efficiently record the fee earnings of LPs, Uniswap adopts a share-like method. By recording the total fees and the fees to be allocated per unit of liquidity, LPs only need to calculate the withdrawable fees based on the liquidity they hold when withdrawing, which greatly reduces gas consumption.
Reasonable Use of Off-Chain Information
Considering the high costs of on-chain storage, not all information needs to be on-chain or retrieved from the chain. For example, transaction pool lists, pool information, etc. can be stored in traditional databases and periodically synchronized with on-chain data. This method can improve efficiency and reduce costs.
Contract Splitting and Standard Contract Reuse
A project may contain multiple actual deployed contracts. Even if only one contract is deployed, the code can be split into multiple contracts for maintenance through inheritance. At the same time, using existing standard contracts (such as ERC721) can improve development efficiency and enhance the reliability of the contracts.
Conclusion
Practice is the best way to learn. By attempting to implement a simplified version of a decentralized exchange, one can gain a deeper understanding of Uniswap's code implementation and learn valuable experience from real projects. For developers interested in diving deeper into Web3 and DeFi project development, participating in practical courses will be a great choice.