Developer Contributions
This documentation explains how to create your own protocol adapter to integrate additional yield sources into the Polystream ecosystem.
Polystream is designed with a modular architecture that allows for easy integration of new yield-generating protocols. This is accomplished through the IProtocolAdapter interface, enabling developers to add support for any DeFi protocol that provides yield opportunities.
Protocol Adapter Interface
Protocol adapters serve as standardized bridges between Polystream's core functionality and external DeFi protocols. All adapters implement the IProtocolAdapter.sol interface.
interface IProtocolAdapter {
function supply(address asset, uint256 amount) external returns (uint256);
function withdraw(address asset, uint256 amount) external returns (uint256);
function withdrawToUser(address asset, uint256 amount, address user) external returns (uint256);
function harvest(address asset) external returns (uint256);
function convertFeeToReward(address asset, uint256 fee) external;
function getAPY(address asset) external view returns (uint256);
function getBalance(address asset) external view returns (uint256);
function getTotalPrincipal(address asset) external view returns (uint256);
function isAssetSupported(address asset) external view returns (bool);
function getProtocolName() external view returns (string memory);
}Implementing a Protocol Adapter
Step 1: Create a new adapter contract
Create a new contract file in the src/adapters directory that implements the IProtocolAdapter interface:
Step 2: Implement key functions
2.1 Supply Function
The supply function allows users to deposit assets into the external protocol:
2.2 Withdraw Function
The withdraw function allows users to withdraw assets from the protocol:
2.3 Harvest Function
The harvest function collects and compounds accumulated yield:
2.4 APY Calculation Function
The getAPY function provides the current annual percentage yield:
Step 3: Implement additional helper functions
Additional functions include:
Example Adapter Implementation: AaveAdapter
The AaveAdapter.sol provides a comprehensive implementation example for integrating with the Aave protocol:
Key implementation patterns from the AaveAdapter:
Asset Management: Track both principal deposits and yield separately
Yield Harvesting: Withdraw all assets, calculate yield, claim rewards, and redeposit
Principal Protection: Ensure withdrawals don't exceed the tracked principal
Reward Handling: Claim and potentially swap additional reward tokens
Testing Your Protocol Adapter
Create a test file in the test directory to verify your adapter's functionality:
Examples
For more comprehensive examples, refer to the existing adapter implementations:
AaveAdapter.sol: Integrates with Aave for lending-based yieldSyncSwapAdapter.sol: Integrates with SyncSwap for LP-based yield
Last updated