Introducing LeveragedPosition
LeveragedPosition is a decentralized finance (DeFi) protocol designed to empower users with the ability to create and manage leveraged positions seamlessly. By integrating with Uniswap V3 for token swaps and lending platforms like Aave V3 and Compound V3 for borrowing and collateral management, LeveragedPosition enables users to amplify their exposure to various assets while maintaining control over their positions.
This project builds upon my experience in developing a leveraged trading tool that allowed users to create long or short positions on assets using FlashLoans and lending protocols. The original process involved initiating a FlashLoan to acquire the collateral asset, supplying it to a lending protocol, borrowing shortable assets, swapping them for collateral on a DEX, and repaying the FlashLoan. LeveragedPosition refines this approach by replacing FlashLoans with exact output swaps on Uniswap V3 pools, significantly reducing gas costs and transaction fees by streamlining the process.
Core Features of LeveragedPosition
Permissionless Deployment
LeveragedPosition supports permissionless deployment, allowing users to create their own leveraged positions without requiring any centralized authorization. This ensures decentralization and accessibility for all participants.
Transient Storage for Efficiency and Security
The protocol leverages transient storage to cache temporary states. This enables:
- Prevention of reentrancy attacks.
- Validation of callbacks securely.
- Efficient use of gas.
Comprehensive State Management
For every trade or iteration, LeveragedPosition manages the following key states:
- Principal: The principal balance of collateral deposited to the contract by the owner.
- Liquidity from Lending Protocol: Encodes details like the principal collateral or borrow balance (
int104
), reserve index (uint104
), and accrual time (uint40
). - Checkpoint: Encodes price feed data from sources like ChainLink, including price (
uint128
), round ID (uint80
), and timestamp (uint40
). - LTV Bounds: Encodes upper, lower, and median loan-to-value bounds, used for calculating supply, borrow amounts, leverage, and collateral utilization.
Exact Output Swaps for Gas Optimization
By using exact output swaps on Uniswap V3 pools instead of FlashLoans, the protocol reduces gas consumption and minimizes fees by consolidating multiple transactions into a streamlined workflow.
Human-Readable Metadata
The PositionDescriptor contract generates intuitive tickers and descriptions for positions, providing transparency and ease of use for dashboards and analytics tools.
Workflow: How LeveragedPosition Works
Increase Liquidity
- User Action: Deposit collateral (e.g., ETH) and specify a swap path (e.g., ETH → USDC).
- Protocol Steps:
- Calculate the supply, borrow, and flash amounts.
- Execute an exact output swap to obtain additional collateral if needed.
- Supply collateral and borrow assets via the lending protocol.
- Result: Increased collateral and leverage with safe collateralization.
Decrease Liquidity
- User Action: Withdraw collateral (e.g., ETH) and specify a swap path (e.g., USDC → ETH).
- Protocol Steps:
- Calculate the repay, redeem, and flash amounts.
- Execute an exact output swap to repay liabilities.
- Redeem collateral from the lending protocol.
- Result: Reduced position size and liabilities with partial collateral withdrawal.
Security and Risk Management
LTV Bounds
Loan-to-value bounds ensure that every action maintains safe collateralization. The upper, lower, and median bounds dynamically calculate supply and borrow amounts while safeguarding user funds.
Reentrancy Protection
Transient storage is used to validate callbacks and prevent reentrant attacks, ensuring robust security during position modifications.
Checkpoints
The checkpoint
state is used to store the price of the collateral or liability asset, its corresponding round ID, and the timestamp when the price was last updated. This ensures accurate tracking of asset values and provides a historical record for validation during position updates.
Liquidity State Management
The liquidity
state tracks the principal liquidity of the position, the reserve index of the lending pool, and the accrual time. This allows the protocol to accurately compute changes in the position’s value and maintain synchronization with the lending protocol’s state.
Example Use Cases
Scenario 1: Amplifying Exposure
- Objective: A user wants to increase leverage on their ETH/USDC position.
- Action: Call
increaseLiquidity
with 5 ETH and a path for swapping USDC to ETH. - Result: Supplies collateral, borrows USDC, swaps it for ETH, and increases leverage.
Scenario 2: Partial Position Closure
- Objective: A user wants to withdraw 3 ETH while reducing their position size.
- Action: Call
decreaseLiquidity
with the withdrawal amount and a swap path for ETH to USDC. - Result: Redeems collateral, repays liabilities, and returns 3 ETH to the user.
Conclusion
LeveragedPosition combines the power of DeFi’s composability with an emphasis on security, gas efficiency, and user flexibility. By integrating seamlessly with Uniswap V3, Aave V3, and Compound V3, the protocol delivers a robust solution for managing leveraged positions for traders and liquidity providers alike.
You can explore the source code and tests here.