🎉 [Gate 30 Million Milestone] Share Your Gate Moment & Win Exclusive Gifts!
Gate has surpassed 30M users worldwide — not just a number, but a journey we've built together.
Remember the thrill of opening your first account, or the Gate merch that’s been part of your daily life?
📸 Join the #MyGateMoment# campaign!
Share your story on Gate Square, and embrace the next 30 million together!
✅ How to Participate:
1️⃣ Post a photo or video with Gate elements
2️⃣ Add #MyGateMoment# and share your story, wishes, or thoughts
3️⃣ Share your post on Twitter (X) — top 10 views will get extra rewards!
👉
Uniswap Source Code Unveiled: 7 Smart Contract Development Tips to Help DeFi Newbies To da moon
Contract Development Tips: Learning from Uniswap Source Code
Recently, while writing a tutorial on developing a decentralized exchange, I referred to the code implementation of Uniswap V3 and learned many valuable insights. As a developer trying to develop Defi contracts for the first time, these tips will be very helpful for beginners who want to learn about contract development.
Here are some little tips I've learned, some of which can even be considered clever tricks.
Predictable Contract Deployment Address
The contract deployed usually gets a seemingly random address, because it is related to the nonce, making the contract address difficult to predict. However, in certain scenarios, we can derive the contract address through the trading pair and related information. This is useful in situations such as determining trading permissions or obtaining pool addresses.
Uniswap creates contracts by adding a salt parameter and using CREATE2, making the created contract address predictable. The address generation logic is: new address = hash("0xFF", creator address, salt, initcode).
https://img-cdn.gateio.im/webp-social/moments-b0c3d4eb7e8ca88cc4cfc9476a34437a.webp(
Using Big Numbers to Solve Precision Issues
The Uniswap code contains a large amount of computational logic, such as calculating the exchanged tokens based on the current price and liquidity. To avoid precision loss during division operations, the calculation process often uses the "<< FixedPoint96.RESOLUTION" operation, which is equivalent to shifting left by 96 bits, or multiplying by 2^96. After the left shift, division operations are performed to ensure precision under normal trading conditions without overflow.
Calculate Earnings Using Share Method
In Uniswap, it is necessary to record the fee income of LP) liquidity provider (. Obviously, it is not feasible to record the fees for each LP for every transaction, as this would consume a large amount of Gas.
Uniswap's solution is to record feeGrowthInside0LastX128 and feeGrowthInside1LastX128 in the Position structure, indicating the fees that each liquidity should receive at the last time they withdrew fees. It only needs to record the total fees and the fees to be allocated to each liquidity, which can be calculated based on the liquidity held when the LP withdraws. This is similar to holding company stocks, where you only need to know the company's historical earnings per share and the earnings at the last withdrawal to extract the profits.
![Web3 Beginner Series: Contract Development Tips I Learned from Uniswap Code])https://img-cdn.gateio.im/webp-social/moments-45e66af69435e6d4412ae506e77ab893.webp(
Non-essential information does not need to be retrieved from the chain
On-chain storage is relatively expensive, and not all information needs to be on-chain or retrieved from the chain. Many interfaces called by the Uniswap front-end website are traditional Web2 interfaces.
The list of trading pools, trading pool information, etc. can be stored in a regular database. Some may need to be periodically synchronized from the blockchain, but there is no need to call the RPC interface of the chain or node services in real time to obtain relevant data.
Of course, key transactions must be conducted on-chain.
Reasonable Split of Contracts, Utilizing Existing Standard Contracts
A project may contain multiple contracts that are actually deployed. Even if only one contract is actually deployed, the code can be split into multiple contracts for maintenance through inheritance.
For example, Uniswap's NonfungiblePositionManager contract inherits from multiple contracts. When looking at the implementation of the ERC721Permit contract, it is found that it directly uses the @openzeppelin/contracts/token/ERC721/ERC721.sol contract. This not only facilitates managing positions through NFTs but also leverages existing standard contracts to improve development efficiency.
Summary
Practice is the best method of learning. Trying to implement a simplified version of a decentralized exchange by yourself can help you gain a deeper understanding of the code implementation of Uniswap and learn more practical knowledge points from real projects.
![Web3 Beginner Series: Tips on Contract Development I Learned from Uniswap Code])https://img-cdn.gateio.im/webp-social/moments-f95ddc9d89809cf11dbe65b9bafda157.webp(