Get Your Own web3 .defi Domains Today!

Daily Defi News from Across the Web

Daily Defi News from Across the Web

How to Integrate the Curve.Fi Protocol Into Your DeFi Protocol | by Pavlo Horbonos


Power your DeFi application with an adapter to Curve.Fi liquidity pools

highway curving out of sight in a forest of fir trees
Photo by Nathan Anderson on Unsplash

Editor’s note: This article is provided for education and entertainment purposes only and is not in any way financial advice.

Hello, reader. There is a high probability that you have opened this piece because of some modern, hyped blockchain terms in the title. If so, you are in the right place. There are bunches and bunches of regular tutorials and articles about blockchain and especially smart contracts development. That’s why I decided to focus on the modern approach and pass all the experience I have gathered as a blockchain engineer. Welcome to the first of my Blockchain Everywhere series.

First of all, this piece is not about DeFi in general or Curve.Fi protocol in particular. There are far more informative sources with general information about decentralized finance. And, of course, you can read all you want about Curve.Fi protocol in their official docs or on Twitter. Okay then, what is this all about? The piece you are currently reading centers on the modern approaches in smart contracts development and crucial points for the integration of such a huge protocol into your own application.

Let’s dive into the code.

Yes, I have said that we will dive into the code. Nevertheless, we need to understand what we are connecting to, though it is not the talk about the math under AMM models or some other specific tricks within Curve.Fi contracts. And surely it is not a talk about successful DeFi development — I am not giving tips on that. This piece is a part of my experience, where I share some knowledge about the basic architecture of Curve smart contracts and the way we can integrate with those structures. Actually, this scheme contains all the information:

diagram of the basic architecture of Curve contracts
Image source: Author

We are looking at the Y-pool, which is organized in a very straightforward way. The Deposit contract takes your funds and wraps them into Y-tokens, which are in their turn sent to the Swap contract. Swap contract generates LP-tokens for the user. If you want to withdraw your shares together with the yield earned, contracts perform the same actions but in a reversed way. And this all about the financial contracts (of course, if we close our eyes at the complex logic behind pools balancing and shares calculation).

And we also have Gauge and Minter contracts, which are part of Curve.Fi DAO. They allow you to collect CRV-token rewards and stake them in order to get the voting power for the protocol’s governance.

Since we fully understand the contracts we need to interact with, we can start the development.

As you remember, we are dealing with external contracts, which are by the way written on Vyper. In order to interact with them, we need to create some interfaces. It is not the hardest job to perform: You just need to look closely at original contracts and pick up the methods you will call.

1. We will start with the Deposit contract. We need methods to move money in and out, and some view interfaces (for example, to see the registered coins and their Y-analogs).

2. The same method applies to the Swap contract (which, as you remember, performs actual minting of LP tokens for the user).

3. Now we have the main DAO contract — liquidity Gauge. It performs complex functionality but we need mostly the methods for checkpoints creation and view interface for minter and CRV-token addresses.

4. And there is the last Curve contract left (and the easiest one from the interface point of view) — the Minter. Actually, we need only the minter method itself and the one to see the available reward amount.

5. The last but not least interface we need is the YToken interface, since we are working with Curve’s Y-pool. It is a simple ERC20 interface with few additional methods.

Now we are ready to create some code to interact with this protocol.

The main goal of our experimental contract is to show how you can organize the funds’ flow with Curve protocol as a final deposit endpoint. So we will have two main methods: one for complete deposit flow (with all the benefits you can get from Curve.Fi) and another with withdrawal flow.

Let’s start with a deposit.

As you see, we perform several actions within our multiStepDeposit() method. First of all, we deposit our funds (the chosen stablecoins in our case) into the Deposit contract in order to receive Curve LP-tokens instead. The second step is to deposit LP-tokens into the Gauge in order to receive rewards in CRV-token. And the third step is up to you — you can do anything you want with your CRV-rewards.

Actually, the same scheme (but in reverse order) applies to multiStepWithdraw() method.

Yes, the solution is very straight, somehow stupid, and has several obvious vulnerabilities. Though it is used only for demonstration purposes. Now you can add all the checks you need, split the stages into separate methods, delegate the rights for methods calling, and do all the other stuff you need to make your DeFi project successful.

Of course, the last step is to add several unit tests to verify that the solution is working. Though here we face several circumstances. The first (and the main one) rests against Curve.Fi protocol. In order to test the solution, we need to emulate the complete Curve protocol on our local environment (Ganache in our case). Since it is a complicated set of contracts and is written on Vyper by the way, we cannot just compile them and deploy them into our Ganache node.

I have already done some work and prepared test stubs of the Curve protocol, which can be compiled together with your project and deployed on your testnet. They are totally equal to the originals, except for several simplifications for testing purposes. You can find all of them in my GitHub through the link at the end of the article.

So we can skip the boring part of turning Vyper code into testable Solidity contracts and see the output of our unit tests (available in the same repository on GitHub):

We have reached the point where we have earned the whole experience of integration of complex DeFi protocol. You have the foundations and can turn on your fantasy.

I find the blockchain world quite fascinating and exciting. Every day I keep searching for more interesting staff within this field.

Also, as always in my articles, you can find a complete working example on my GitHub.

I will return later with another modern approach to smart contracts’ development.





Source link

Recommended For You

blank

About the Author: admin

Main Menu

Subscribe for Daily DeFi News