Metricore
Autonomous risk monitoring and circuit-breaker for under-collateralized DeFi lending protocols.
What it is
Metricore is a decentralized risk controller built on Chainlink CRE that continuously
monitors under-collateralized DeFi lending protocols and autonomously enforces protective
actions when risk thresholds are breached.
I'm building Invalend Protocol an early-stage under-collateralized lending protocol on
Base that enables traders to access 5x leverage with only 20% collateral. As I was
designing the risk architecture, I realized the protocol's core mechanic creates a
critical vulnerability: a market crash of just 20% can trigger liquidation cascades
faster than any human operator can respond. I built Metricore as the autonomous
monitoring and circuit-breaker layer that Invalend needs before going to production.
The problem it solves: existing solutions rely on centralized monitoring, manual
intervention, or binary circuit breakers with no nuance. Metricore provides graduated,
autonomous response proportional to actual risk conditions.
It works in three layers:
Monitoring
A CRE Workflow runs every 5 minutes, reading live position health factors
and pool utilization directly from Base Sepolia, while fetching external market signals
(ETH price via CoinGecko, sentiment via Alternative.me Fear & Greed Index) through
NodeRuntime with consensus median aggregation across the DON.Risk Classification
A three-tier engine (ELEVATED / HIGH / CRITICAL) aggregates all
signals and determines the appropriate response: alert only, reduce leverage, or pause
new positions.Trustless Execution
The workflow submits a DON-signed report via evmClient.writeReport()
to a Chainlink KeystoneForwarder, which delivers it to MetricoreGateway.onReport(). The
gateway independently re-verifies all thresholds from on-chain state before executing, no direct authority is granted to the workflow itself.
For this hackathon submission, I'm using mock contracts that simulate Invalend's lending
interface. Phase 2 replaces these with Invalend's production contracts as the protocol
moves toward mainnet launch.
How it Works
Stack:
- CRE Workflow: TypeScript, @chainlink/cre-sdk , Bun runtime
- Smart Contracts: Solidity ^0.8.20, Foundry, OpenZeppelin
- EVM Encoding: viem (encodeFunctionData, encodeAbiParameters, decodeFunctionResult)
- Blockchain: Base Sepolia (chainId 84532)
- External APIs: CoinGecko (ETH/USD price), Alternative.me (Fear & Greed Index)
- Alerting Backend: Express.js + TypeScript webhook receiver
Architecture:
The CRE Workflow uses CronCapability (every 5 minutes) to orchestrate three monitoring modules. All EVM reads use EVMClient.callContract() with viem-encoded calldata. External HTTP calls use NodeRuntime with consensusMedianAggregation — price and sentiment data reach DON-level consensus before influencing any risk decision.
For EVM writes, the workflow uses the correct CRE pattern: runtime.report() generates a DON-signed report containing the ABI-encoded action type, then evmClient.writeReport() submits it to the MockKeystoneForwarder (simulation) or KeystoneForwarder (production), which validates signatures and calls MetricoreGateway.onReport(). The gateway decodes the report, re-verifies thresholds independently from on-chain state, checks a 30-minute cooldown, and executes on the protocol only if all conditions are confirmed.
MetricoreGateway also stores a PriceSnapshot (price + timestamp) after each workflow run — solving CRE's stateless execution model by using the contract itself as persistent memory for the next run's hourly price delta calculation.
Links
Created by
- Muhammad Aris