Skip to content

0xshubhs/Policy2Protocol

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Policy2Protocol

Natural-language compliance for cross-chain DeFi treasuries


"If utilization exceeds 85% or stablecoin deviation is above 2%, immediately withdraw and rebalance to the safest chain."

That sentence is all you need. Policy2Protocol turns it into a live, autonomous, cross-chain enforcement workflow.


The Problem

Multi-chain treasuries are everywhere. The tooling isn't.

  • Passive dashboards show numbers but don't act.
  • Governance is slow — by the time a vote passes, the opportunity (or the risk) is gone.
  • No verifiable compliance layer exists that autonomously monitors utilization, deviation, TVL, and APY across chains and acts when thresholds are breached.

The Solution

Policy2Protocol lets DAOs and protocols describe treasury risk policies in plain English and converts them into autonomous, consensus-backed, cross-chain enforcement.

Plain English Policy
        |
   AI Parser (Claude)
        |
  Structured JSON Config
        |
  CRE Workflow (DON consensus)
        |
  On-chain Execution (CCIP cross-chain)
        |
  Verifiable Compliance Logs

How It Works

1. Define Policy

A DAO risk manager types a policy in plain English:

"Monitor every 5 minutes. Rebalance to the highest-APR chain when the difference exceeds 50 basis points."

2. AI Parsing

The AI layer (Claude) parses the natural language into a structured CRE workflow config — extracting schedule, thresholds, metrics, and whitelisted actions. Falls back to a deterministic rule-based parser if no API key is set.

3. CRE Workflow Execution

The generated config drives a Chainlink Runtime Environment workflow:

  • Cron-triggered on a schedule (e.g., every 5 minutes)
  • Reads currentLiquidityRate from lending pools on each configured chain
  • DON consensus evaluates whether policy conditions are met
  • Encodes a RebalanceParams report when thresholds are breached

4. Cross-Chain Enforcement

The ProtocolSmartWallet receives the CRE report via Keystone forwarder:

  • Withdraws funds from the source pool
  • Sends cross-chain via CCIP to the destination chain
  • Destination PSW automatically deposits into the higher-yield pool

5. Verifiable Audit Trail

Every action emits on-chain events (ReportReceived, MessageSent, MessageReceived) — queryable, immutable compliance logs.


Architecture

                    +-------------------+
                    |   Next.js Frontend |
                    |   (Policy Input)   |
                    +--------+----------+
                             |
                    POST /api/parse-policy
                             |
                    +--------v----------+
                    |   AI Parser        |
                    |   (Claude / Rules) |
                    +--------+----------+
                             |
                      CRE Config JSON
                             |
              +--------------+--------------+
              |                             |
     +--------v--------+          +--------v--------+
     | CRE Workflow     |          | POST /api/deploy |
     | (DON Consensus)  |          | (cre CLI)        |
     +--------+---------+          +-----------------+
              |
     Read APR per chain
     Evaluate thresholds
     Encode RebalanceParams
              |
     +--------v---------+
     | PSW.onReport()    |
     | Withdraw from Pool|
     +--------+----------+
              |
        CCIP Transfer
              |
     +--------v-----------+
     | Destination PSW     |
     | Deposit into Pool   |
     +---------------------+

Project Structure

chainlink-cre/
├── frontend/           Next.js app — policy input, AI parse, deploy, logs, settings
├── contracts/          Foundry — MockPool, ProtocolSmartWallet, CCIP integration
├── workflow-ts/        CRE TypeScript workflow — cron, metrics, cross-chain rebalance
└── DEMO_GUIDE.md       Step-by-step demo runbook

Tech Stack

Layer Technology
Frontend Next.js 16, TypeScript, Tailwind CSS v4, RainbowKit, Wagmi
AI Anthropic Claude (structured policy parsing)
Workflow Chainlink CRE SDK, Bun, Viem, Zod
Contracts Solidity 0.8.26, Foundry, OpenZeppelin 5.x
Cross-chain Chainlink CCIP (token transfers + messaging)
Testnets Ethereum Sepolia, Base Sepolia

Quick Start

Frontend

cd frontend
npm install
npm run dev
# Open http://localhost:3000

Set environment variables in frontend/.env.local:

ANTHROPIC_API_KEY=sk-ant-...                    # optional — enables AI parsing
NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID=...        # required — get one at cloud.walletconnect.com

Contracts

cd contracts
forge test -vv

Deploy to testnets:

export PRIVATE_KEY=0x...
forge script scripts/multi-chain-token-manager/DeployTokenManagerContracts.s.sol \
  --multi --broadcast --verify

CRE Workflow

# Install CRE CLI
curl -sSL https://cre.chain.link/install.sh | bash

cd workflow-ts/workflow && bun install
cd ..
cre workflow simulate ./workflow --target staging-settings   # test
cre workflow deploy ./workflow --target staging-settings     # deploy

Deployed Contracts (Testnet)

Chain MockPool ProtocolSmartWallet
Ethereum Sepolia 0xC0a2EF140487C69B46eEC6e150018b82F791984e 0x4561aAB2113F52Ae15063720fd771E17BF505B29
Base Sepolia 0xB4467F6331f0228Af9985f6bA2AF788FFdB880d9 0xb1D927779797E085De790f4bdf6Cb02da1fC8760

Asset: CCIP-BnM token | Deployer: 0xE2b39f4cfFA5B17434e47Ab5F54b984155e4b7aD


Frontend Pages

Page Route What It Does
Landing / Hero, protocol flow visualization, architecture cards, feature breakdown
Get Started /get-started 3-step flow: type policy in English -> review generated CRE config -> deploy workflow
Compliance Logs /logs Live on-chain events from PSW contracts (ReportReceived, MessageSent, MessageReceived)
Settings /settings Toggle chains and metric thresholds (persisted to localStorage)

API Routes

Endpoint Method Purpose
/api/parse-policy POST Parses natural language policy into CRE workflow config JSON
/api/policy-to-strategy POST Converts structured policy JSON into CRE config with implementation steps
/api/logs GET Fetches real on-chain events from PSW contracts on all configured chains
/api/deploy-workflow POST Writes config and runs cre workflow deploy (local dev only)

What Makes This Different

Feature Traditional Policy2Protocol
Policy definition Code or governance proposals Plain English
Monitoring Manual dashboards Autonomous CRE cron
Decision making Human or centralized bot DON consensus (decentralized)
Execution Single-chain scripts Cross-chain via CCIP
Audit trail Off-chain logs On-chain events (verifiable)
Speed Hours/days (governance) Minutes (automated)

Security Considerations

This is a hackathon project / educational demo. Not production-ready.

  1. Contracts are unaudited examples — use audited contracts for production
  2. Keep API keys and private keys out of version control
  3. Use your own RPC endpoints for stable CRE deployment
  4. The AI parser is constrained to whitelisted metrics and actions only

License

MIT


Built with Chainlink CRE, CCIP, and Keystone

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors