Back to Hub
CREATED BY CHAINLINK LABS

Automated Liquidation Protection

Automatically protect DeFi lending positions by continuously monitoring liquidation risk and executing collateral management, debt repayment, position reduction, or hedging strategies while preserving the confidentiality of centralized exchange as well as LLM API keys, proprietary risk management thresholds, and execution preferences.

This standalone CRE project implements a confidential liquidation-defense workflow for DeFi lending positions.

Description

The workflow continuously evaluates borrower risk and takes defensive action before a position becomes unsafe. During periods of high volatility, it can increase collateral, reduce debt, or combine both strategies based on policy constraints. Sensitive operational data remains protected in confidential execution, including exchange credentials, model credentials, and user-defined risk thresholds.

Target Customer

  • Professional retail traders
  • Developer shops
  • Founders building trading products

Structure

  • project.yaml: project-level target settings
  • secrets.yaml: secret ID mappings used by the workflow
  • mock-server.js: local deterministic API server
  • automated-liquidation-protection-ts/: TypeScript workflow implementation
  • automated-liquidation-protection-go/: Go workflow implementation

Private Inputs

The following inputs are treated as confidential:

  • Exchange API credentials used to fetch account context such as stablecoin reserves and available cash balance.
  • LLM reasoning API credentials and policy parameters used to govern defense behavior, including minimum and target health factors, reserve deployment caps, minimum reserve balance, and collateral allocation limits.
  • Execution preferences that define how defense actions should be sequenced.

Workflow Notes

  1. Observe liquidation risk signals. The workflow tracks collateral and debt asset pricing, health factor, liquidation proximity, LTV, liquidation threshold, and market volatility.
  2. Enforce user policy constraints. Confidential reasoning evaluates how much capital can be deployed, whether debt reduction should be prioritized, which reserve assets are eligible, and what execution sequence is preferred.
  3. Select defense actions. The workflow builds a response plan that may include collateral-focused moves (deposit, bridge, swap-then-deposit) and debt-focused moves (repay, swap-then-repay, partial payoff, full payoff).
  4. Execute the approved defense plan.

Note: Every reasoning stage can be implemented with deterministic rule-based logic instead of an LLM, if your deployment requires a fully rules-driven policy engine.

Required Environment Variables

Copy .env.example to .env and provide values for:

  • CRE_ETH_PRIVATE_KEY (optional for local simulate)
  • MOCK_PORT
  • MOCK_EXCHANGE_API_KEY
  • MOCK_OPENAI_API_KEY
  • MOCK_LIQUIDATION_WARNING_ACTION_THRESHOLD
  • MOCK_LIQUIDATION_MINIMUM_HEALTH_FACTOR
  • MOCK_LIQUIDATION_TARGET_HEALTH_FACTOR
  • MOCK_LIQUIDATION_MAX_STABLECOIN_RESERVE_DEPLOYMENT
  • MOCK_LIQUIDATION_MIN_STABLECOIN_RESERVE_BALANCE
  • MOCK_LIQUIDATION_MAX_COLLATERAL_ALLOCATION
  • MOCK_LIQUIDATION_MAX_PARTIAL_DEBT_REPAYMENT
  • MOCK_LIQUIDATION_DEFENSIVE_ACTION_SEQUENCING_PREFERENCE
  • MOCK_LIQUIDATION_PREFERRED_VENUES

The local mock server for this project only exposes routes under /liquidation/*.

Quick navigation:

TypeScript Quick Start

  1. Install dependencies
bun install
  1. Create environment file
cp .env.example .env
  1. Start mock server
bun run mock:server
  1. In another terminal, run checks
bun run typecheck
bun run test
  1. Simulate workflow
cre workflow simulate ./automated-liquidation-protection-ts --target=staging-settings

Go Quick Start

  1. Create environment file (at the shared project root)
cp ../.env.example ../.env
  1. Start the mock server (requires Node or Bun)
bun mock-server.js
  1. In another terminal, run checks
go vet ./...
go test ./...
  1. Simulate workflow
cd .. && cre workflow simulate ./automated-liquidation-protection-go --target=staging-settings

Get the latest Chainlink content straight to your inbox.