In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces how to integrate the Curve protocol in DeFi applications, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Curve.Fi is a large set of DEFi protocols. The purpose of this tutorial is to help you focus on modern methods of intelligent contract development and the key points of how to integrate large protocols like Curve.Fi into your Defi applications.
1. The organization of Curve.Fi development tutorials
Yes, I have already said that we will delve into the code. Although we are not talking about the mathematical operations of the AMM model, nor are we talking about some other specific techniques used in the Curve.Fi contract, we still need to know something. Of course, this is not about successful DeFi development-I'm not giving a hint. This is part of my experience, and here I will share some knowledge about the basic architecture of Curve smart contracts and how we integrate with these architectures. In fact, the following figure contains the entire content of this tutorial:
We are looking at Y-Pool, which is organized in a simple and straightforward way. The Deposit contract packages your money into Y-Token and sends it to the Swap contract, which generates LP tokens for the user. If you want to extract the liquidity share and the income earned, the contract does the same, but in the opposite way. It's all about financial contracts (of course, if we turn a blind eye to the complex logical choices behind pool balance and share count).
In addition, we have Gauge and Minter contracts, which are part of Curve.Fi DAO. These contracts allow you to receive CRV tokens and mortgage them in order to gain voting rights under agreed governance.
After we fully understand these contracts that require interaction, we can start our development.
2. Start with the Curve.Fi user interface
As you may recall, we are working on some external contracts developed using Vyper. To interact with these contracts, we need to create some interfaces. This is not the most difficult task: just take a closer look at the original contract and pick out the method to be called.
Let's start with the deposit contract. We need a way to get in and out of funds, as well as some data viewing interfaces (for example, to view registered tokens and their Y counterparts).
Contract ICurveFi_DepositY {function add_liquidity (uint256 [4] calldata uamounts, uint256 min_mint_amount) external; function remove_liquidity (uint256 _ amount, uint256 [4] calldata min_uamounts) external; function remove_liquidity_imbalance (uint256 [4] calldata uamounts, uint256 max_burn_amount) external; function coins (int128 I) external view returns (address); function underlying_coins (int128 I) external view returns (address); function underlying_coins () external view returns (address [4] memory) Function curve () external view returns (address); function token () external view returns (address);}
Then deal with the exchange contract in the same way (which, as you may recall, minted LP tokens for the user).
Contract ICurveFi_Gauge {function lp_token () external view returns (address); function crv_token () external view returns (address); function balanceOf (address addr) external view returns (uint256); function deposit (uint256 _ value) external; function withdraw (uint256 _ value) external; function claimable_tokens (address addr) external returns (uint256); function minter () external view returns (address) / / use minter () .mint (gauge_addr) to claim CRV function integrate_fraction (address _ for) external view returns (uint256); function user_checkpoint (address _ for) external returns (bool);}
Now it's time to deal with DAO's main contract, the liquidity meter contract. It performs complex functions, but we mainly need methods for creating checkpoints and an interface for viewing Minter and CRV token addresses.
Contract ICurveFi_Gauge {function lp_token () external view returns (address); function crv_token () external view returns (address); function balanceOf (address addr) external view returns (uint256); function deposit (uint256 _ value) external; function withdraw (uint256 _ value) external; function claimable_tokens (address addr) external returns (uint256); function minter () external view returns (address) / / use minter () .mint (gauge_addr) to claim CRV function integrate_fraction (address _ for) external view returns (uint256); function user_checkpoint (address _ for) external returns (bool);}
And from a user interface point of view, it's actually the simplest contract-Minter. In fact, all we need is the minter method itself, and a way to view valid awards.
Contract ICurveFi_Minter {function mint (address gauge_addr) external; function minted (address _ for, address gauge_addr) external view returns (uint256); function toggle_approve_mint (address minting_user) external; function token () external view returns (address);}
The last one is the YToken interface, but it's not the least important, because it's the Y pool of Curve that we want to interact with. YToken is a simple ERC20 interface with few additional methods.
Contract IYERC20 {/ / ERC20 functions / / Y-token functions function deposit (uint256 amount) external; function withdraw (uint256 shares) external; function getPricePerFullShare () external view returns (uint256); function token () external returns (address);}
Now we are ready to create some code to interact with this protocol.
3. Transfer funds in the correct order based on Curve.Fi
The main goal of our experimental contract is to show how to use the Curve protocol as an end point to organize the flow of funds. Therefore, we will have two main methods: one for the complete deposit process (with all the benefits available from Curve.Fi), and the other for the withdrawal process.
Let's start with savings.
Function multiStepDeposit (uint256 [4] memory _ amounts) public {address [4] memory stablecoins = ICurveFi_DepositY (curveFi_Deposit). Underlying_coins (); for (uint256 I = 0; I
< stablecoins.length; i++) { IERC20(stablecoins[i]).safeTransferFrom(_msgSender(), address(this), _amounts[i]); IERC20(stablecoins[i]).safeApprove(curveFi_Deposit, _amounts[i]); } //Step 1 - deposit stablecoins and get Curve.Fi LP tokens ICurveFi_DepositY(curveFi_Deposit).add_liquidity(_amounts, 0); //0 to mint all Curve has to //Step 2 - stake Curve LP tokens into Gauge and get CRV rewards uint256 curveLPBalance = IERC20(curveFi_LPToken).balanceOf(address(this)); IERC20(curveFi_LPToken).safeApprove(curveFi_LPGauge, curveLPBalance); ICurveFi_Gauge(curveFi_LPGauge).deposit(curveLPBalance); //Step 3 - get all the rewards (and make whatever you need with them) crvTokenClaim(); uint256 crvAmount = IERC20(curveFi_CRVToken).balanceOf(address(this)); IERC20(curveFi_CRVToken).safeTransfer(_msgSender(), crvAmount);} 正像你看见的,我们在multiStepDeposit()方法中执行了几个动作。首先,将我们的资金(在本例中为选定的稳定币)存入Deposit合约,以便接收Curve LP代币。第二步是将LP代币存入Gauge合约,以便获得CRV代币奖励。第三步取决于你自己 -- 可以使用CRV奖励做任何想做的事情。 实际上,相同的方案也适用于multiStepWithdraw()方法,但顺序相反。 function multiStepWithdraw(uint256[4] memory _amounts) public { address[4] memory stablecoins = ICurveFi_DepositY(curveFi_Deposit).underlying_coins(); //Step 1 - Calculate amount of Curve LP-tokens to unstake uint256 nWithdraw; uint256 i; for (i = 0; i < stablecoins.length; i++) { nWithdraw = nWithdraw.add(normalize(stablecoins[i], _amounts[i])); } uint256 withdrawShares = calculateShares(nWithdraw); //Check if you can re-use unstaked LP tokens uint256 notStaked = curveLPTokenUnstaked(); if (notStaked >0) {withdrawShares = withdrawShares.sub (notStaked);} / / Step 2-Unstake Curve LP tokens from Gauge ICurveFi_Gauge (curveFi_LPGauge) .requests (withdrawShares); / / Step 3-Withdraw stablecoins from CurveDeposit IERC20 (curveFi_LPToken) .creative Appliance (curveFi_Deposit, withdrawShares); ICurveFi_DepositY (curveFi_Deposit). Remove_liquidity_imbalance (_ amounts, withdrawShares); / / Step 4-Send stablecoins to the requestor / /}
Yes, the solution is very simple, looks clumsy to some extent, and has several obvious vulnerabilities-- although the code is for demonstration purposes only. You can add all the checks you need, split the phase into different methods, set different permissions for the method, and perform all the other work needed to make your DeFi project successful.
4. Test the Curve.Fi solution
The final step of the tutorial is to add several unit tests to verify that the solution is effective. Although we are faced with several problems here. The first (and major) problem is the Curve.Fi protocol environment. To test the solution, we need to simulate the complete Curve protocol on the local environment (Ganache in our case). Since this is a complex set of contracts and is written on Vyper, we cannot just compile and deploy them to our Ganache node.
I have done some work and prepared a test stub for the Curve protocol that can be compiled and deployed with your project. Except for several simplifications for testing purposes, these test stubs are exactly the same as the original Curve protocol. You can download it here.
Therefore, we can skip the boring part of converting Vyper code into a testable Solidity contract and look directly at the output of the unit test:
On how to integrate the Curve protocol in DeFi applications to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.