Decentralized Event-Driven Oracles to fuse onchain and offchain signals with AI for automated DeFi
Presentation: https://drive.google.com/file/d/1YlZoie_UHjRm1GfKRE4hkFUGU2_9xfs4/view?usp=sharing DeFi Vault Risk Sentinel (the selected use case to display DED0) is an automated system that continuously monitors collateralized vaults by merging on-chain data (live prices, historical collateral ratios) with off-chain inputs (tweets, news, social-sentiment), each anchored by cryptographic proofs. A GPT- model assesses these multi-source signals and, when risk thresholds are met, triggers a Solidity smart contract (VaultProtector.sol) to protect the vault (by SELL or BUY actions)—no manual intervention needed (but always possible). This ensures earlier, trustless vault protection and reduces liquidation losses.
Frontend • Vanilla HTML/CSS/JS with Poppins font and a light-blue→violet gradient theme. • Builder UI: lets users select modular feeds—Price (Pyth), Volume (Blockscout), Sentiment (Flare FDC), News/Twitter (API + vlayer Web Proof), Collateral (vlayer Time Travel), Custom HTTP. • Dynamic sub-forms appear per feed (e.g. Pyth feed ID, Flare topic key, lookback blocks). • Chart.js draws real-time charts of selected feed values. • Axios calls the FastAPI backend; Web3.js connects MetaMask for on-chain previews.
Backend (FastAPI + SQLAlchemy + Alembic) • PostgreSQL automations table stores each automation’s list of feed configs as JSON. • Fetchers (app/fetchers/):
price_fetcher.py (Pyth) – Uses Web3.py pointed at an Ethereum or Flow endpoint to call the Pyth price-feed contract. – Fetches up-to-date price for a given feed ID (e.g. BTC/USD) directly from Pyth on-chain oracle.
volume_fetcher.py (Blockscout) – Calls Blockscout (or Etherscan) REST API to gather ERC-20 transfer counts or on-chain volume for a token contract. – Summarizes recent token transfers to detect unusual trading activity.
sentiment_fetcher.py (Flare FDC) – Uses Web3.py on the Flare network’s RPC endpoint to call the Flare FDC contract’s getAttestation(topic_key). – Retrieves signed social-sentiment scores (e.g. an integer scaled by 100) for on-chain–verified market sentiment.
news_fetcher.py / from hackernews, coindesk, reddit (vlayer Web Proof) – Performs a standard REST call to a News API endpoint or Twitter v2 API (with bearer token). – Immediately calls vlayer’s server-side JSON-RPC to generate a zero-knowledge “Web Proof” of that JSON response (no browser extension needed). – Returns proofHex
collateral_fetcher.py (vlayer Time Travel) – Computes pastBlock = currentBlock − lookbackBlocks. – Calls vlayer Time Travel contract (getCollateralRatioAt(vault, pastBlock)) via Web3.py (pointed at Ethereum or Flow EVM). – Obtains a verifiable historical collateral ratio, proving “at block X, the vault was Y% collateralized.”
custom_fetcher.py – Allows arbitrary HTTP GET/POST with JSON-path extraction; optionally notarizes via vlayer if proof is required. • feed_utils.py maps each feed type to (fetch_function, PydanticConfigModel) so the runner dynamically loads only chosen feeds. • automation_runner.py : every 5 s, it:
Loads active automations from PostgreSQL.
Validates each feed’s JSON against its Pydantic model (e.g. PriceFeedConfig).
Invokes the appropriate fetcher: Pyth for price, Blockscout for volume, Flare for sentiment, vlayer for Web Proofs and Time Travel (on Ethereum or Flow EVM).
Aggregates results into a JSON object and calls llm_service.py. • llm_service.py wraps OpenAI GPT-4: given {feeds, timestamp}, returns {risk_score, should_trigger, reason}. • If should_trigger == true, send_protect_tx() in contract_service.py uses Web3.py to call VaultProtector.protectVault(vault, proofHex, histBlock) on the configured EVM (Ethereum or Flow).
Smart Contracts (Solidity, Hardhat) • VaultProtector.sol (Solidity 0.8): – Constructor receives IVLayerProver and IVLayerStateConnector addresses (already deployed on Ethereum or Flow). – protectVault(address vault, bytes calldata proof, uint256 histBlock): • Calls prover.verifyWebProof(proof) (verifies vlayer Web Proof on-chain). • Calls stateConnector.getCollateralRatioAt(vault, histBlock) (vlayer Time Travel) to fetch the archived collateral ratio. • Requires collateral ≥ 9000 (≥ 90%) and valid proof, then emits VaultProtected. • Deployed to an EVM-compatible chain—on Flow EVM, the same bytecode and ABI work without modification. • Hardhat handles compilation, deployment, and unit tests (Mocha/Chai).
vlayer Integration • Server-Side Web Proof: vlayer’s headless JSON-RPC endpoint automates notarizing any REST response (news or Twitter JSON) into a ZK-proof (proofHex). • Time Travel: vlayer’s on-chain State Connector archives historical state, enabling trustless retrieval of past collateral ratios on Ethereum or Flow EVM.
LLM & AI • GPT OpenAI API acts as a risk aggregator: parses multi-feed JSON → outputs {risk_score, should_trigger, reason}. • Prompts are designed so the model consistently returns only those three fields.