This document describes the implementation of a yield-bearing treasury system for Stellar grant contracts, addressing issue #46/#36.
The Grant Treasury contract implements a sophisticated treasury management system that automatically invests idle funds in Stellar-based liquidity pools to generate yield while ensuring liquidity is always available for grant withdrawals.
invest_idle_funds()- Automatically invests idle funds in liquidity poolsdivest_funds()- Divests funds when liquidity is needed- Liquidity Availability - Ensures grants can always be withdrawn
- Yield Generation - Earns yield on idle capital through DeFi protocols
- Auto-Investment - Automatically invests when idle funds exceed threshold
- Yield Claiming - Periodic yield collection and reinvestment
- Grant Management - Complete grant allocation and withdrawal system
- Risk Management - Minimum liquidity ratio enforcement
- Comprehensive Auditing - Full transaction history and yield tracking
pub struct TreasuryConfig {
pub admin: Address,
pub liquidity_pool_address: Address,
pub min_liquidity_ratio: u32, // Minimum liquidity (basis points)
pub auto_invest_threshold: i128, // Auto-invest trigger amount
pub yield_claim_frequency: u64, // Yield claim interval (seconds)
}pub struct InvestmentPosition {
pub amount: i128,
pub pool_address: Address,
pub invested_at: u64,
pub last_yield_claim: u64,
pub accumulated_yield: i128,
}pub struct GrantAllocation {
pub grantee: Address,
pub amount: i128,
pub allocated_at: u64,
pub status: AllocationStatus,
}- Invests specified amount in liquidity pool
- Enforces minimum liquidity requirements
- Creates investment position tracking
- Updates balance records
- Divests funds from specific investment position
- Calculates and claims accumulated yield
- Updates investment positions
- Ensures liquidity availability
- Automatically divests funds when liquidity is needed
- Prioritizes most recent investments
- Calculates yield before divestment
- Maintains minimum liquidity ratio
- Allocates funds to grantee
- Ensures liquidity availability
- Creates grant allocation record
- Updates available balance
- Allows grantee to withdraw allocated funds
- Ensures liquidity through auto-divestment
- Updates allocation status
- Maintains audit trail
- Claims accumulated yield from all investments
- Updates yield history
- Reinvests or makes available for grants
- Calculates based on time and APY
pub fn initialize(
env: Env,
admin: Address,
liquidity_pool_address: Address,
min_liquidity_ratio: u32, // 2000 = 20%
auto_invest_threshold: i128, // 1000 lumens
yield_claim_frequency: u64, // 86400 = daily
)// Deposit funds
pub fn deposit(env: Env, from: Address, amount: i128)
// Manual investment
pub fn invest_idle_funds(env: Env, caller: Address, amount: i128)
// Manual divestment
pub fn divest_funds(env: Env, caller: Address, amount: i128, position_index: u32)
// Grant allocation
pub fn allocate_grant(env: Env, caller: Address, grantee: Address, amount: i128)
// Grant withdrawal
pub fn withdraw_grant(env: Env, grantee: Address, allocation_id: u32)
// Yield claiming
pub fn claim_yield(env: Env, caller: Address)pub fn get_total_balance(env: Env) -> i128
pub fn get_available_balance(env: Env) -> i128
pub fn get_invested_balance(env: Env) -> i128
pub fn get_investment_positions(env: Env) -> Vec<InvestmentPosition>
pub fn get_grant_allocations(env: Env) -> Vec<GrantAllocation>
pub fn get_yield_history(env: Env) -> Vec<YieldRecord>
pub fn get_accumulated_yield(env: Env) -> i128
pub fn get_apy(env: Env) -> u32
pub fn should_auto_invest(env: Env) -> boolThe contract uses a simplified yield calculation model:
// 5% APY (500 basis points)
let apy = 500;
let seconds_per_year = 365 * 24 * 60 * 60;
let time_fraction = (time_elapsed * 10000) / seconds_per_year;
let yield_amount = (principal * apy * time_fraction) / (10000 * 10000);- Configurable minimum liquidity (default: 20%)
- Prevents over-investment
- Ensures grant withdrawal availability
- Triggered when grants exceed available liquidity
- Divests from newest positions first
- Claims yield before divestment
- Single liquidity pool integration
- Conservative APY (5%)
- Minimum liquidity enforcement
- Comprehensive audit trail
- Admin-only investment/divestment operations
- Grantee-only withdrawal permissions
- Role-based authorization
- Positive amount requirements
- Balance sufficiency checks
- Position index validation
- Atomic operations
- Consistent balance updates
- Comprehensive logging
- Rust toolchain with wasm32 target
- Stellar SDK
- Testnet XLM for deployment
- Build Contract
cd contracts
cargo build --target wasm32-unknown-unknown --release- Deploy Contract
node scripts/deploy_grant_treasury.js- Initialize Contract
- Set admin address
- Configure liquidity pool
- Set investment parameters
| Parameter | Default | Description |
|---|---|---|
min_liquidity_ratio |
2000 | Minimum liquidity (20% in basis points) |
auto_invest_threshold |
1000 | Auto-invest trigger (1000 lumens) |
yield_claim_frequency |
86400 | Claim interval (daily in seconds) |
apy |
500 | Annual Percentage Yield (5% in basis points) |
- Batch operations for multiple investments
- Optimized storage patterns
- Minimal cross-contract calls
| Operation | Estimated Cost |
|---|---|
| Initialize | ~200,000 stroops |
| Deposit | ~100,000 stroops |
| Invest Funds | ~150,000 stroops |
| Divest Funds | ~150,000 stroops |
| Allocate Grant | ~100,000 stroops |
| Withdraw Grant | ~100,000 stroops |
| Claim Yield | ~200,000 stroops |
- ✅ Contract initialization
- ✅ Deposit and auto-investment
- ✅ Manual investment/divestment
- ✅ Grant allocation and withdrawal
- ✅ Liquidity management
- ✅ Yield calculation and claiming
- ✅ Access control and authorization
- ✅ Edge cases and error handling
cd contracts
cargo test --package verinode-contracts --lib grantTreasury-
Basic Operations
- Initialize contract
- Deposit funds
- Auto-investment trigger
- Grant allocation
- Grant withdrawal
-
Liquidity Management
- Over-investment prevention
- Auto-divestment for withdrawals
- Minimum liquidity enforcement
-
Yield Generation
- Yield calculation accuracy
- Yield claiming functionality
- Yield history tracking
-
Security
- Unauthorized access prevention
- Input validation
- State consistency
- Mock liquidity pool address for testing
- Simplified yield calculation
- Single pool integration
- Real Stellar AMM pools
- Dynamic APY calculation
- Multi-pool diversification
- Slippage protection
- Total treasury balance
- Investment positions
- Yield generation rate
- Grant allocation efficiency
- Liquidity utilization
- Complete transaction history
- Investment position tracking
- Yield claim records
- Grant allocation logs
- Multi-pool diversification
- Dynamic APY optimization
- Risk scoring system
- Automated rebalancing
- Real-time price feeds
- Advanced DeFi protocols
- Cross-chain yield farming
- Governance integration
-
Insufficient Liquidity
- Check minimum liquidity ratio
- Verify available balance
- Review investment positions
-
Investment Failures
- Validate pool address
- Check authorization
- Verify amount limits
-
Grant Withdrawal Issues
- Confirm allocation status
- Check liquidity availability
- Verify grantee address
- Contract event logs
- Balance inspection
- Position tracking
- Yield history analysis
- Smart contract vulnerabilities
- Liquidity pool risks
- Market volatility
- APY fluctuations
- Comprehensive testing
- Conservative parameters
- Regular audits
- Risk monitoring
The Grant Treasury contract successfully implements yield-bearing functionality while ensuring liquidity availability for grant withdrawals. The system provides:
- ✅ Yield Generation: 5% APY on idle funds
- ✅ Liquidity Management: Always available for withdrawals
- ✅ Risk Mitigation: Conservative parameters and controls
- ✅ Comprehensive Auditing: Full transaction history
- ✅ Gas Optimization: Efficient operations
- ✅ Security: Robust access control and validation
The implementation meets all acceptance criteria and provides a solid foundation for DeFi integration in grant management systems.