DeFi Actions Integration
DeFi Actions is a composability framework that enables ALP to integrate seamlessly with other DeFi protocols like Flow Yield Vaults (FYV). This powerful abstraction allows for automated value flows and complex strategy compositions.
Understanding DeFi Actions
What are DeFi Actions?
DeFi Actions is a composability framework that provides standardized interfaces for DeFi protocols to interact. Think of it as "LEGO blocks" for DeFi - each protocol provides compatible pieces that snap together.
Key concepts:
- Source: An interface for withdrawing/pulling funds (like a faucet)
- Sink: An interface for depositing/pushing funds (like a drain)
- Composability: Ability to combine protocols seamlessly
Why DeFi Actions Matter
Without DeFi Actions, integrating protocols is complex:
- Withdraw from position manually
- Check balance
- Calculate amounts
- Approve tokens
- Call other protocol
- Handle errors
- Return funds
With DeFi Actions, it's simple and automated:
_10Position (Source) → Auto-flow → Yield Farm (Sink)
Benefits: DeFi Actions provides simplified integrations through standard interfaces for all protocols, automated flows for set-and-forget value movements, composable strategies that chain multiple protocols together, reduced errors via standardized error handling, and gas efficiency through optimized execution paths.
Core Concepts
The Sink Pattern (Push)
A Sink receives tokens - it's where funds flow TO.
How it works:
_10graph LR_10 ALP[ALP Position] -->|Pushes funds| Sink[Sink Interface]_10 Sink -->|Deposits to| External[External Protocol]_10_10 style Sink fill:#bbf,stroke:#333,stroke-width:2px
Common Sink examples: Common sink destinations include a user's wallet (simple, default), yield farming protocols (earn returns), liquidity pools (provide liquidity), and other ALP positions (leverage strategies).
What ALP's PositionSink does: The PositionSink receives tokens from external protocols, deposits them into your ALP position as collateral, updates your position balances, and may trigger rebalancing if the position becomes overcollateralized.
ALP implements the Sink interface via PositionSink:
_10// Create a sink that deposits into your position_10let sink = position.createSink(type: Type<@MOET.Vault>())_10_10// Any MOET sent to this sink goes to your position_10externalProtocol.send(to: sink, amount: 100.0)
See GitHub for full API documentation.
The Source Pattern (Pull)
A Source provides tokens - it's where funds flow FROM.
How it works:
_10graph LR_10 External[External Protocol] -->|Requests funds| Source[Source Interface]_10 Source -->|Pulls from| ALP[ALP Position]_10_10 style Source fill:#bfb,stroke:#333,stroke-width:2px
Common Source examples: Common source origins include a user's wallet (manual funding), yield farming protocols (auto-withdrawal), liquidity pools (exit liquidity), and other ALP positions (cross-position management).
What ALP's PositionSource does: The PositionSource provides tokens to external protocols, may borrow from ALP if withdrawing debt tokens, can pull from TopUpSource if configured, and updates your position balances accordingly.
Advanced: TopUpSource Integration
A PositionSource can be configured to pull from an external TopUpSource for automatic liquidation prevention:
- External protocol requests funds from PositionSource
- If position has insufficient funds, pulls from TopUpSource
- TopUpSource might be FYV (yield from your farming)
- Funds used to repay debt and restore health
- Result: Automatic liquidation protection using your yield!
Create a Source with TopUpSource integration:
_10// Create source that can pull from TopUpSource for rebalancing_10let source = position.createSourceWithOptions(_10 type: Type<@MOET.Vault>(),_10 pullFromTopUpSource: true // Enable auto-pull_10)
This enables the yield-powered liquidation prevention that makes FCM unique.
How ALP Uses DeFi Actions
DrawDownSink (When Overcollateralized)
When your position has excess borrowing capacity (health > 1.5), ALP can automatically push funds to a DrawDownSink.
The flow:
_11sequenceDiagram_11 participant Position_11 participant DrawDownSink_11 participant FYV_11_11 Position->>Position: Health = 1.8 (too high)_11 Position->>Position: Calculate excess: $200 MOET_11 Position->>Position: Auto-borrow $200 MOET_11 Position->>DrawDownSink: Push $200 MOET_11 DrawDownSink->>FYV: Deploy to yield strategy_11 FYV->>FYV: Generate returns
Common DrawDownSink configurations: Borrowed funds can flow to your wallet for manual control, be automatically deployed to FYV strategies for yield farming, be added as liquidity to LP pools, or be sent to another position to create leveraged strategies.
TopUpSource (When Undercollateralized)
When your position's health drops below minimum (health < 1.1), ALP can automatically pull funds from a TopUpSource.
The flow:
_12sequenceDiagram_12 participant FYV_12 participant TopUpSource_12 participant Position_12_12 Position->>Position: Price drops! Health = 1.05_12 Position->>Position: Calculate needed: $150 MOET_12 Position->>TopUpSource: Request $150 MOET_12 TopUpSource->>FYV: Withdraw from yield_12 FYV->>TopUpSource: Return $150 MOET_12 TopUpSource->>Position: Provide $150 MOET_12 Position->>Position: Repay debt, Health = 1.3 ✓
Common TopUpSource configurations: Funds can be pulled from your wallet for manual liquidation protection, from FYV strategies for automatic liquidation protection using yield, from LP pools to exit liquidity when needed, or from another position for cross-position risk management.
The TopUpSource from FYV is what enables FCM's unique yield-powered liquidation prevention. Your yield automatically protects your position without manual intervention!
Learn more: FCM Basics - Yield-Powered Liquidation Prevention
Integration Patterns
Pattern 1: Simple Auto-Borrowing
Use case: Borrow against collateral, receive funds in wallet.
Setup:
- DrawDownSink: Your wallet
- TopUpSource: None (manual management)
Flow:
_10Deposit FLOW → ALP auto-borrows MOET → Funds to your wallet
Best for: Users who want manual control over borrowed funds.
Pattern 2: Full FCM Integration
Use case: Maximum automation with yield-powered liquidation prevention.
Setup:
- DrawDownSink: FYV yield strategy
- TopUpSource: FYV yield strategy
Flow:
_10Deposit FLOW → Auto-borrow MOET → Deploy to FYV → Generate yield_10Price drops → FYV provides funds → Repay debt → Health restored
Best for: Users who want set-and-forget yield generation with automatic protection.
Pattern 3: Liquidity Provision
Use case: Automatically provide liquidity with borrowed funds.
Setup:
- DrawDownSink: DEX liquidity pool
- TopUpSource: DEX liquidity pool
Flow:
_10Deposit collateral → Borrow MOET → Add to LP → Earn trading fees_10Needs rebalancing → Exit LP position → Repay debt
Best for: Users wanting to earn trading fees on borrowed capital.
Pattern 4: Cross-Position Leverage
Use case: Lever position across multiple accounts.
Setup:
- Position 1 DrawDownSink: Position 2 Sink
- Position 2 TopUpSource: Position 1 Source
Flow:
_10Position 1 borrows → Flows to Position 2 → Position 2 borrows → Repeat_10Creates leveraged exposure with automatic risk management
Best for: Advanced users creating complex strategies.
Real-World Example: FCM with FYV
Let's see how a complete FCM setup works with DeFi Actions:
Initial Setup
You deposit: 1000 FLOW into ALP position
Configuration:
_10Position.DrawDownSink = FYV Strategy Sink_10Position.TopUpSource = FYV Strategy Source
Auto-Borrowing (Overcollateralized)
_101. ALP calculates: Can safely borrow 615 MOET_102. ALP auto-borrows: 615 MOET_103. ALP pushes via DrawDownSink: 615 MOET → FYV_104. FYV swaps: 615 MOET → 615 YieldToken_105. FYV holds: YieldToken, generating yield
Price Drop Response (Undercollateralized)
_101. FLOW price drops 20%_102. ALP detects: Health = 1.04 (below 1.1 minimum)_103. ALP calculates: Need to repay 123 MOET_104. ALP pulls via TopUpSource: Request 123 MOET from FYV_105. FYV swaps: 123 YieldToken → 123 MOET_106. FYV provides: 123 MOET to ALP_107. ALP repays: 123 MOET debt_108. Health restored: 1.3 ✓
Result: Your yield automatically prevented liquidation!
Safety & Best Practices
Built-in Safety Features
- Access Control: Only position owner can create Sources/Sinks
- Type Validation: Ensures token types match
- Balance Checks: Validates sufficient funds before operations
- Error Handling: Graceful failures with clear messages
Best Practices
Do:
- ✅ Always configure both DrawDownSink AND TopUpSource for full automation
- ✅ Ensure TopUpSource has sufficient liquidity
- ✅ Monitor your position health regularly
- ✅ Test with small amounts first
- ✅ Understand the external protocol you're integrating with
Don't:
- ❌ Leave TopUpSource empty if you want liquidation protection
- ❌ Assume TopUpSource has unlimited funds
- ❌ Create circular dependencies between positions
- ❌ Ignore gas costs of complex strategies
Common Pitfalls
-
Insufficient TopUpSource Liquidity
- Problem: TopUpSource runs dry during rebalancing
- Solution: Monitor TopUpSource balance, add buffer funds
-
Token Type Mismatches
- Problem: Sink expects MOET but receives FLOW
- Solution: Always verify token types match
-
Gas Limitations
- Problem: Complex DeFi Actions chains hit gas limits
- Solution: Simplify strategies or split into multiple transactions
Advanced Topics
Looking to build complex strategies? Here are advanced patterns:
Multi-Protocol Stacks
Chain multiple protocols together for sophisticated strategies:
_10ALP → Swap → Farm → Stake → Compound
Yield Optimization
Automatically rebalance between multiple positions based on yield:
_10Monitor yields → Move funds from low-yield → Deploy to high-yield
Flash Loan Integration
Use ALP with flash loans for arbitrage opportunities (advanced).
See GitHub examples for code samples.
Summary
DeFi Actions enables:
- 🔗 Seamless protocol integration
- 🤖 Automated value flows
- 🛡️ Liquidation protection via yield
- 🎯 Complex strategy composition
Key patterns:
- Sink: Where funds go (DrawDownSink)
- Source: Where funds come from (TopUpSource)
- Integration: Connect ALP with FYV, DEXs, farms, etc.
FCM's innovation: Using FYV as both DrawDownSink AND TopUpSource creates yield-powered liquidation prevention - the yield you earn automatically protects your position!
Mathematical Foundation
DeFi Actions enable automated position management based on mathematical rules:
- Auto-Borrowing Triggers: Auto-Borrowing Mathematics
- Rebalancing Calculations: Rebalancing Mathematics
- Health Factor Monitoring: Health Factor Formula
Next Steps
- Learn about MOET: MOET's Role
- Explore rebalancing: Rebalancing Mechanics
- See the big picture: FCM Architecture
- Understand position lifecycle: Position Lifecycle
DeFi Actions is the "glue" that makes FCM work. It connects ALP's automated lending with FYV's yield strategies, enabling the unique liquidation prevention mechanism that sets FCM apart from traditional lending protocols.