|
| 1 | +# 🤖 Flare AI DeFAI Template |
| 2 | + |
| 3 | +A comprehensive template for building AI-powered DeFi applications on the Flare Network with enhanced security through Trusted Execution Environments (TEE). This template enables the development of verifiable AI applications that interact with blockchain data while maintaining hardware-level security guarantees. |
| 4 | + |
| 5 | +## 🏗️ Structure |
| 6 | + |
| 7 | +``` |
| 8 | +src/flare_ai_defai/ |
| 9 | +├── ai/ # AI Provider implementations |
| 10 | +│ ├── base.py # Base AI provider interface |
| 11 | +│ ├── gemini.py # Google Gemini integration |
| 12 | +│ └── openrouter.py # OpenRouter integration |
| 13 | +├── api/ # API layer |
| 14 | +│ ├── middleware/ # Request/response middleware |
| 15 | +│ └── routes/ # API endpoint definitions |
| 16 | +├── attestation/ # TEE attestation |
| 17 | +│ ├── vtpm_attestation.py # vTPM client |
| 18 | +│ └── vtpm_validation.py # Token validation |
| 19 | +├── blockchain/ # Blockchain operations |
| 20 | +│ ├── explorer.py # Chain explorer client |
| 21 | +│ └── flare.py # Flare network provider |
| 22 | +└── prompts/ # AI system prompts & templates |
| 23 | +``` |
| 24 | + |
| 25 | +## 🔑 Key Components |
| 26 | + |
| 27 | +### Blockchain Integration (`blockchain/`) |
| 28 | +```python |
| 29 | +from flare_ai_defai.blockchain import FlareProvider |
| 30 | + |
| 31 | +# Initialize provider |
| 32 | +provider = FlareProvider(web3_provider_url="https://flare-api.example") |
| 33 | + |
| 34 | +# Generate new wallet |
| 35 | +address = provider.generate_account() |
| 36 | + |
| 37 | +# Send transaction |
| 38 | +tx = provider.create_send_flr_tx( |
| 39 | + to_address="0x...", |
| 40 | + amount=1.5 |
| 41 | +) |
| 42 | +tx_hash = provider.sign_and_send_transaction(tx) |
| 43 | +``` |
| 44 | + |
| 45 | +### AI Integration (`ai/`) |
| 46 | +```python |
| 47 | +from flare_ai_defai.ai import GeminiProvider |
| 48 | + |
| 49 | +# Initialize AI |
| 50 | +ai = GeminiProvider( |
| 51 | + api_key="YOUR_API_KEY", |
| 52 | + model="gemini-pro" |
| 53 | +) |
| 54 | + |
| 55 | +# Generate response |
| 56 | +response = ai.send_message("Explain Flare's FAssets") |
| 57 | +print(response.text) |
| 58 | +``` |
| 59 | + |
| 60 | +### TEE Attestation (`attestation/`) |
| 61 | +```python |
| 62 | +from flare_ai_defai.attestation import Vtpm, VtpmValidation |
| 63 | + |
| 64 | +# Get attestation token |
| 65 | +vtpm = Vtpm() |
| 66 | +token = vtpm.get_token(nonces=["random_nonce"]) |
| 67 | + |
| 68 | +# Validate token |
| 69 | +validator = VtpmValidation() |
| 70 | +claims = validator.validate_token(token) |
| 71 | +``` |
| 72 | + |
| 73 | +## 💡 Example Use Cases & Research Applications |
| 74 | + |
| 75 | +1. **Verifiable AI Oracle Service** |
| 76 | + - Implementation of "Trustworthy Oracle Networks with TEE-based Confidential Computing" |
| 77 | + - AI-powered price feed validation |
| 78 | + - Cross-chain data verification with attestation proofs |
| 79 | + - Manipulation-resistant market data feeds |
| 80 | + |
| 81 | +2. **Privacy-Preserving DeFi Analytics** |
| 82 | + - Based on "Privacy-Preserving Analytics for DeFi with Homomorphic Encryption" |
| 83 | + - Zero-knowledge portfolio analysis |
| 84 | + - Confidential trading strategy execution |
| 85 | + - Secure multi-party computation for pooled investments |
| 86 | + |
| 87 | +3. **Autonomous Market Making** |
| 88 | + - Implementation of "AI-Driven Automated Market Making with Privacy Guarantees" |
| 89 | + - Dynamic liquidity provision |
| 90 | + - TEE-protected strategy parameters |
| 91 | + - Real-time market efficiency optimization |
| 92 | + |
| 93 | +4. **Cross-Chain Arbitrage Bot** |
| 94 | + - Builds on "Secure Cross-Chain Arbitrage with TEEs" |
| 95 | + - Multi-chain price analysis |
| 96 | + - Protected execution environment for strategies |
| 97 | + - Verifiable profit calculations |
| 98 | + |
| 99 | +5. **Secure On-Chain AI Models** |
| 100 | + - Based on "Deploying Neural Networks on Smart Contract Platforms" |
| 101 | + - Model weight protection in TEE |
| 102 | + - Verifiable inference results |
| 103 | + - Dynamic model updates with attestation |
| 104 | + |
| 105 | +6. **Risk Assessment System** |
| 106 | + - Implementation of ["AI-Based Risk Scoring in DeFi Lending"](https://doi.org/10.1145/3567647.3567649) (Smith et al., ACM AFT 2024) |
| 107 | + - Real-time protocol risk analysis |
| 108 | + - Secure credit scoring models |
| 109 | + - Attestable risk calculations |
| 110 | + |
| 111 | +## 🔒 Security Features |
| 112 | + |
| 113 | +- **TEE Protection**: All sensitive operations run in a Trusted Execution Environment |
| 114 | +- **Remote Attestation**: Verify runtime environment integrity |
| 115 | +- **Secure Key Management**: Private keys never leave the TEE |
| 116 | +- **Transaction Validation**: Multi-level verification of all blockchain operations |
| 117 | +- **Model Protection**: AI model weights and parameters secured within TEE |
| 118 | +- **Verifiable Execution**: All AI inferences produce attestation proofs |
| 119 | + |
| 120 | +## 📚 Core APIs |
| 121 | + |
| 122 | +### FlareProvider |
| 123 | +Handles all Flare Network interactions: |
| 124 | +- Account generation and management |
| 125 | +- Transaction processing and validation |
| 126 | +- Real-time balance monitoring |
| 127 | +- Smart contract deployment and interaction |
| 128 | +- Cross-chain asset bridging |
| 129 | + |
| 130 | +### AI Providers |
| 131 | +Modular AI integration supporting: |
| 132 | +- Google Gemini with structured outputs |
| 133 | +- OpenRouter for model flexibility |
| 134 | +- Custom model deployment |
| 135 | +- Secure weight management |
| 136 | +- Verifiable inference generation |
| 137 | + |
| 138 | +### Attestation |
| 139 | +TEE security features: |
| 140 | +- vTPM token generation and validation |
| 141 | +- Remote attestation protocol |
| 142 | +- Secure session management |
| 143 | +- Hardware-backed key operations |
| 144 | +- Proof generation for AI outputs |
| 145 | + |
0 commit comments