Building the Blend Strategy
⏱️ 5 min read
Introduction
Welcome to this guide on implementing the Blend Strategy for DeFindex. This tutorial is designed to provide a comprehensive walkthrough of the Blend Strategy smart contract, which integrates with the Blend Protocol, a lending and borrowing platform.
Note: This guide is for understanding how to create a strategy. The actual strategy may differ from this example. Do not use this code as is, but rather use it as a reference to create your own strategy.
The Blend Strategy implements the DeFindexStrategyTrait from the core strategy module, providing a standardized interface for interacting with the DeFindex vault while managing positions in the Blend Protocol.
Why a Strategy?
A strategy in DeFindex acts as a proxy between the Vault and an external protocol. This design is essential because:
Protocol-specific Authorization: The Vault cannot directly authorize interactions with external protocols like Blend.
Position Management: The Strategy holds positions for each interacting vault and tracks them using shares.
Standardized Outputs: The Strategy always converts internal shares to underlying asset balances for the Vault to ensure consistency.
Getting Started
To implement the Blend Strategy, you need to be familiar with Soroban smart contract development and Rust. If you're new to Soroban, start with the official Soroban Getting Started Guide.
Cargo.toml
Here's the Cargo.toml for the Blend Strategy:
Project Setup
Below, we'll break the Blend Strategy into its components, explaining each part with the corresponding code.
1. lib.rs: Core Logic
The Blend Strategy implements the DeFindexStrategyTrait and provides all core functionality, including deposits, withdrawals, rewards harvesting, and balance tracking.
Code:
2. Storage Module
The storage.rs file is fundamental to the Blend Strategy as it handles the configuration, reserves, and vault position data. This module is the first introduced into the contract, as it's initialized by the constructor to store the strategy's configuration.
Purpose
Configuration Management:
Stores essential information like the underlying asset, Blend Pool address, and reserve ID.
Used to retrieve the configuration during operations like deposits and withdrawals.
Vault Position Tracking:
Tracks the number of shares each vault or user owns.
Shares represent a user's proportionate stake in the strategy's reserves.
Reserves Management:
Maintains the total shares, bTokens, and bRate (exchange rate) for the strategy.
Code Walkthrough
Here's the complete storage.rs file with detailed explanations:
Key Points
Configuration:
The Config struct holds all necessary parameters for the strategy.
The constructor uses set_config to initialize these values.
Vault Positions:
Shares are stored with the VaultPos key and are specific to each vault or user.
Precision is managed with 7 decimal places to ensure accuracy.
Reserves:
Reserves track the strategy's overall state, including total shares, bTokens, and the current exchange rate (bRate).
If reserves are missing, default values are used.
3. Blend Pool Interactions
The blend_pool.rs file is responsible for managing all interactions with the Blend Pool smart contract. This includes supplying and withdrawing assets, claiming rewards, and reinvesting harvested tokens.
Purpose
Supply and Withdraw Assets:
Handles depositing and withdrawing the underlying asset to/from the Blend Pool.
Tracks
bTokensreceived or burned during these operations.
Claim Rewards:
Retrieves rewards (e.g., BLND tokens) accrued in the Blend Pool.
Reinvest Rewards:
Converts rewards into the underlying asset and reinvests them into the Blend Pool.
Code Walkthrough
Key Points
Supply and Withdraw:
Use RequestType to define the operation.
Ensure accurate tracking of bTokens for precise position management.
Claim:
Hardcoded reserve token IDs are used as placeholders for now
Reinvest:
Converts rewards to maximize returns.
Leverages Soroswap to swap BLND for the underlying asset.
4. Token Swapping with Soroswap
This module handles token swaps, converting rewards (e.g., BLND tokens) into the underlying asset during the harvest process to reinvest them into the Blend Pool.
Code Walkthrough
Key Points
Swaps BLND tokens into the underlying asset during harvest.
Uses the Soroswap Router contract.
References
The Blend Strategy for DeFindex showcases the power of modular architecture in decentralized finance. By acting as a proxy between the Vault and external protocols, the strategy ensures seamless integration while maintaining flexibility and security.
This guide provides a complete walkthrough for implementing the Blend Strategy, covering:
Initialization and storage management
Interactions with the Blend Pool
Reinvestment logic using Soroswap
With this foundation, you can build custom strategies tailored to specific protocols and assets, expanding DeFindex's utility. Remember to follow best practices, rigorously test your strategies, and monitor deployments to ensure optimal performance.
If you have questions or need help, join the conversation on our DeFindex Discord or connect with us on the PaltaLabs Discord. We're here to help you build and innovate. Happy coding! 🚀
Last updated
