From 9bbd97111a1252fb0930edd2287d10b31d4436ce Mon Sep 17 00:00:00 2001 From: semi-collab Date: Thu, 27 Feb 2025 11:31:20 +0100 Subject: [PATCH 1/7] Add initial implementation of BitLend protocol - Define protocol parameters and error codes - Implement data variables for protocol state and price oracle data - Create maps for loan data structure and balance tracking - Add private functions for amount validation and authorization checks This commit sets up the foundational elements of the BitLend protocol, enabling secure, non-custodial lending/borrowing against BTC collateral on Stacks L2. --- Clarinet.toml | 30 ++++++++--------- contracts/bitlend.clar | 74 ++++++++++++++++++++++++++++++++++++++++++ tests/bitlend.test.ts | 21 ++++++++++++ 3 files changed, 109 insertions(+), 16 deletions(-) create mode 100644 contracts/bitlend.clar create mode 100644 tests/bitlend.test.ts diff --git a/Clarinet.toml b/Clarinet.toml index 650d09f..6306063 100644 --- a/Clarinet.toml +++ b/Clarinet.toml @@ -1,21 +1,19 @@ [project] -name = "BitLend" -description = "" +name = 'BitLend' +description = '' authors = [] telemetry = true -cache_dir = "./.cache" - -# [contracts.counter] -# path = "contracts/counter.clar" - +cache_dir = './.cache' +requirements = [] +[contracts.bitlend] +path = 'contracts/bitlend.clar' +clarity_version = 3 +epoch = 3.1 [repl.analysis] -passes = ["check_checker"] -check_checker = { trusted_sender = false, trusted_caller = false, callee_filter = false } +passes = ['check_checker'] -# Check-checker settings: -# trusted_sender: if true, inputs are trusted after tx_sender has been checked. -# trusted_caller: if true, inputs are trusted after contract-caller has been checked. -# callee_filter: if true, untrusted data may be passed into a private function without a -# warning, if it gets checked inside. This check will also propagate up to the -# caller. -# More informations: https://www.hiro.so/blog/new-safety-checks-in-clarinet +[repl.analysis.check_checker] +strict = false +trusted_sender = false +trusted_caller = false +callee_filter = false diff --git a/contracts/bitlend.clar b/contracts/bitlend.clar new file mode 100644 index 0000000..81b530b --- /dev/null +++ b/contracts/bitlend.clar @@ -0,0 +1,74 @@ +;; Title: BitLend - Bitcoin-Backed Lending Protocol +;; Summary: Decentralized Bitcoin liquidity protocol enabling secure, non-custodial lending/borrowing against BTC collateral on Stacks L2. +;; Description: +;; BitLend is a DeFi primitive built on Stacks that brings trustless BTC-backed loans to Bitcoin through Layer 2 innovation. The protocol implements: +;; - Bitcoin-native collateralization using Stacks' proof-of-transfer mechanism +;; - Automated risk management with dynamic collateral ratios +;; - Non-custodial liquidations enforced by Clarity smart contracts +;; - Real-time price feeds with decentralized oracle integration +;; - Transparent governance for protocol parameter updates +;; +;; Designed for Bitcoin maximalists, BitLend enables BTC holders to access liquidity while maintaining self-custody, combining Bitcoin's security with Stacks' programmability. The protocol adheres to Bitcoin's monetary policy and implements Stacks L2-specific security practices. + +;; Constants + +;; Protocol Parameters +(define-constant CONTRACT-OWNER tx-sender) +(define-constant MIN-COLLATERAL-RATIO u150) ;; 150% minimum collateral ratio +(define-constant LIQUIDATION-THRESHOLD u130) ;; 130% liquidation threshold +(define-constant LIQUIDATION-PENALTY u10) ;; 10% penalty on liquidation +(define-constant PRICE-VALIDITY-PERIOD u3600) ;; 1 hour price validity +(define-constant MAX-FEE-PERCENTAGE u10) ;; 10% maximum protocol fee + +;; Error Codes +(define-constant ERR-NOT-AUTHORIZED (err u100)) +(define-constant ERR-INSUFFICIENT-BALANCE (err u101)) +(define-constant ERR-INVALID-AMOUNT (err u102)) +(define-constant ERR-BELOW-MIN-COLLATERAL (err u103)) +(define-constant ERR-LOAN-NOT-FOUND (err u104)) +(define-constant ERR-LOAN-EXISTS (err u105)) +(define-constant ERR-INVALID-LIQUIDATION (err u106)) +(define-constant ERR-PRICE-EXPIRED (err u107)) +(define-constant ERR-ZERO-AMOUNT (err u108)) +(define-constant ERR-EXCEED-MAX-FEE (err u109)) + +;; Data Variables + +;; Protocol State +(define-data-var protocol-paused bool false) +(define-data-var total-loans uint u0) +(define-data-var total-collateral uint u0) +(define-data-var protocol-fee-percentage uint u1) ;; 1% default fee + +;; Price Oracle Data +(define-data-var btc-price-in-cents uint u0) +(define-data-var last-price-update uint u0) + +;; Maps + +;; Loan Data Structure +(define-map loans + { user: principal } + { + collateral-amount: uint, + borrowed-amount: uint, + last-update: uint, + interest-rate: uint + } +) + +;; Balance Tracking +(define-map collateral-balances { user: principal } uint) +(define-map borrow-balances { user: principal } uint) + +;; Private Functions + +(define-private (validate-amount (amount uint)) + (begin + (asserts! (> amount u0) ERR-ZERO-AMOUNT) + (ok true))) + +(define-private (check-authorization) + (begin + (asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-NOT-AUTHORIZED) + (ok true))) \ No newline at end of file diff --git a/tests/bitlend.test.ts b/tests/bitlend.test.ts new file mode 100644 index 0000000..4bb9cf3 --- /dev/null +++ b/tests/bitlend.test.ts @@ -0,0 +1,21 @@ + +import { describe, expect, it } from "vitest"; + +const accounts = simnet.getAccounts(); +const address1 = accounts.get("wallet_1")!; + +/* + The test below is an example. To learn more, read the testing documentation here: + https://docs.hiro.so/stacks/clarinet-js-sdk +*/ + +describe("example tests", () => { + it("ensures simnet is well initalised", () => { + expect(simnet.blockHeight).toBeDefined(); + }); + + // it("shows an example", () => { + // const { result } = simnet.callReadOnlyFn("counter", "get-counter", [], address1); + // expect(result).toBeUint(0); + // }); +}); From ee6e695cbd4fed08c39426da4adbe13fef3701a1 Mon Sep 17 00:00:00 2001 From: semi-collab Date: Thu, 27 Feb 2025 11:32:37 +0100 Subject: [PATCH 2/7] Implement core lending functions and read-only queries - Add `deposit-collateral` function to handle collateral deposits - Add `borrow` function to handle loan borrowing - Implement read-only functions for retrieving loan, collateral balance, borrow balance, and protocol stats - Add validation for protocol activity and price validity These changes enable users to interact with the BitLend protocol by depositing collateral and borrowing against it, while providing necessary read-only queries for protocol data. --- contracts/bitlend.clar | 96 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/contracts/bitlend.clar b/contracts/bitlend.clar index 81b530b..4f09c8d 100644 --- a/contracts/bitlend.clar +++ b/contracts/bitlend.clar @@ -71,4 +71,98 @@ (define-private (check-authorization) (begin (asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-NOT-AUTHORIZED) - (ok true))) \ No newline at end of file + (ok true))) + +(define-private (check-protocol-active) + (begin + (asserts! (not (var-get protocol-paused)) ERR-NOT-AUTHORIZED) + (ok true))) + +;; Read-Only Functions + +(define-read-only (get-loan (user principal)) + (map-get? loans { user: user })) + +(define-read-only (get-collateral-balance (user principal)) + (default-to u0 (map-get? collateral-balances { user: user }))) + +(define-read-only (get-borrow-balance (user principal)) + (default-to u0 (map-get? borrow-balances { user: user }))) + +(define-read-only (get-current-collateral-ratio (user principal)) + (let ( + (loan (get-loan user)) + ) + (match loan + loan-data (let ( + (collateral-value (* (get collateral-amount loan-data) (var-get btc-price-in-cents))) + (borrowed-value (* (get borrowed-amount loan-data) u100)) + ) + (ok (/ (* collateral-value u100) borrowed-value))) + (err u0)))) + +(define-read-only (is-price-valid) + (< (- block-height (var-get last-price-update)) PRICE-VALIDITY-PERIOD)) + +(define-read-only (get-protocol-stats) + { + total-loans: (var-get total-loans), + total-collateral: (var-get total-collateral), + current-fee: (var-get protocol-fee-percentage), + is-paused: (var-get protocol-paused) + }) + +;; Public Functions + +;; Price Oracle Functions +(define-public (update-btc-price (new-price-in-cents uint)) + (begin + (try! (check-authorization)) + (try! (validate-amount new-price-in-cents)) + (var-set btc-price-in-cents new-price-in-cents) + (var-set last-price-update block-height) + (ok true))) + +;; Core Lending Functions +(define-public (deposit-collateral (amount uint)) + (begin + (try! (check-protocol-active)) + (try! (validate-amount amount)) + (try! (stx-transfer? amount tx-sender (as-contract tx-sender))) + + (map-set collateral-balances + { user: tx-sender } + (+ (get-collateral-balance tx-sender) amount)) + + (var-set total-collateral (+ (var-get total-collateral) amount)) + (ok true))) + +(define-public (borrow (amount uint)) + (begin + (try! (check-protocol-active)) + (try! (validate-amount amount)) + + (let ( + (current-collateral (get-collateral-balance tx-sender)) + (current-loan (get-loan tx-sender)) + (collateral-value (* current-collateral (var-get btc-price-in-cents))) + ) + (asserts! (is-price-valid) ERR-PRICE-EXPIRED) + (asserts! (is-none current-loan) ERR-LOAN-EXISTS) + (asserts! (>= (* collateral-value u100) (* amount MIN-COLLATERAL-RATIO)) ERR-BELOW-MIN-COLLATERAL) + + (map-set loans + { user: tx-sender } + { + collateral-amount: current-collateral, + borrowed-amount: amount, + last-update: block-height, + interest-rate: u5 ;; 5% APR + }) + + (map-set borrow-balances + { user: tx-sender } + (+ (get-borrow-balance tx-sender) amount)) + + (var-set total-loans (+ (var-get total-loans) amount)) + (ok true)))) \ No newline at end of file From 9275cd4ce32f5ada42b1c2ce5e2959ccbd9dbff4 Mon Sep 17 00:00:00 2001 From: semi-collab Date: Thu, 27 Feb 2025 11:33:35 +0100 Subject: [PATCH 3/7] Implement loan repayment and liquidation functions - Add `repay-loan` function to handle loan repayments, including interest calculation and state updates - Add `liquidate` function to handle loan liquidations when collateral ratio falls below the threshold - Ensure protocol state updates and collateral return to users after liquidation These changes complete the core functionality of the BitLend protocol, enabling loan repayments and liquidations. --- contracts/bitlend.clar | 75 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/contracts/bitlend.clar b/contracts/bitlend.clar index 4f09c8d..fa4d791 100644 --- a/contracts/bitlend.clar +++ b/contracts/bitlend.clar @@ -165,4 +165,77 @@ (+ (get-borrow-balance tx-sender) amount)) (var-set total-loans (+ (var-get total-loans) amount)) - (ok true)))) \ No newline at end of file + (ok true)))) + +(define-public (repay-loan (amount uint)) + (begin + (try! (check-protocol-active)) + (try! (validate-amount amount)) + + (let ( + (loan (unwrap! (get-loan tx-sender) ERR-LOAN-NOT-FOUND)) + (current-borrowed (get borrowed-amount loan)) + ) + (asserts! (<= amount current-borrowed) ERR-INVALID-AMOUNT) + + (let ( + (blocks-elapsed (- block-height (get last-update loan))) + (interest-amount (/ (* current-borrowed (get interest-rate loan) blocks-elapsed) (* u100 u144 u365))) + (total-due (+ amount interest-amount)) + ) + (try! (stx-transfer? total-due tx-sender (as-contract tx-sender))) + + ;; Update loan state + (if (is-eq amount current-borrowed) + (begin + (map-delete loans { user: tx-sender }) + (map-delete borrow-balances { user: tx-sender })) + (begin + (map-set loans + { user: tx-sender } + { + collateral-amount: (get collateral-amount loan), + borrowed-amount: (- current-borrowed amount), + last-update: block-height, + interest-rate: (get interest-rate loan) + }) + (map-set borrow-balances + { user: tx-sender } + (- (get-borrow-balance tx-sender) amount)))) + + (var-set total-loans (- (var-get total-loans) amount)) + (ok true))))) + +(define-public (liquidate (user principal)) + (begin + (try! (check-protocol-active)) + + (let ( + (loan (unwrap! (get-loan user) ERR-LOAN-NOT-FOUND)) + (collateral-ratio (unwrap! (get-current-collateral-ratio user) ERR-LOAN-NOT-FOUND)) + ) + (asserts! (is-price-valid) ERR-PRICE-EXPIRED) + (asserts! (< collateral-ratio LIQUIDATION-THRESHOLD) ERR-INVALID-LIQUIDATION) + + (let ( + (collateral-amount (get collateral-amount loan)) + (borrowed-amount (get borrowed-amount loan)) + (liquidation-value (* borrowed-amount (+ u100 LIQUIDATION-PENALTY))) + (remaining-collateral (- collateral-amount liquidation-value)) + ) + (try! (stx-transfer? liquidation-value tx-sender (as-contract tx-sender))) + + ;; Clear the loan + (map-delete loans { user: user }) + (map-delete borrow-balances { user: user }) + + ;; Update protocol state + (var-set total-loans (- (var-get total-loans) borrowed-amount)) + (var-set total-collateral (- (var-get total-collateral) collateral-amount)) + + ;; Return remaining collateral to user + (if (> remaining-collateral u0) + (try! (as-contract (stx-transfer? remaining-collateral (as-contract tx-sender) user))) + true) + + (ok true))))) \ No newline at end of file From c5ed0c6f65bf1faba278ae20e4fa7f05d26da595 Mon Sep 17 00:00:00 2001 From: semi-collab Date: Thu, 27 Feb 2025 11:34:32 +0100 Subject: [PATCH 4/7] Implement governance functions for protocol management - Add `update-protocol-fee` function to update the protocol fee percentage - Add `toggle-protocol-pause` function to pause or resume the protocol These changes provide governance capabilities to manage protocol parameters and operational state. --- contracts/bitlend.clar | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/contracts/bitlend.clar b/contracts/bitlend.clar index fa4d791..923bbcd 100644 --- a/contracts/bitlend.clar +++ b/contracts/bitlend.clar @@ -66,12 +66,14 @@ (define-private (validate-amount (amount uint)) (begin (asserts! (> amount u0) ERR-ZERO-AMOUNT) - (ok true))) + (ok true)) +) (define-private (check-authorization) (begin (asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-NOT-AUTHORIZED) - (ok true))) + (ok true)) +) (define-private (check-protocol-active) (begin @@ -238,4 +240,19 @@ (try! (as-contract (stx-transfer? remaining-collateral (as-contract tx-sender) user))) true) - (ok true))))) \ No newline at end of file + (ok true))))) + +;; Governance Functions + +(define-public (update-protocol-fee (new-fee uint)) + (begin + (try! (check-authorization)) + (asserts! (<= new-fee MAX-FEE-PERCENTAGE) ERR-EXCEED-MAX-FEE) + (var-set protocol-fee-percentage new-fee) + (ok true))) + +(define-public (toggle-protocol-pause) + (begin + (try! (check-authorization)) + (var-set protocol-paused (not (var-get protocol-paused))) + (ok true))) \ No newline at end of file From 5b244fb29463fd1bcf97513751990b92645727bd Mon Sep 17 00:00:00 2001 From: semi-collab Date: Thu, 27 Feb 2025 11:45:34 +0100 Subject: [PATCH 5/7] Fix price validity checks to use stacks-block-height instead of block-height --- contracts/bitlend.clar | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/contracts/bitlend.clar b/contracts/bitlend.clar index 923bbcd..6b1ed74 100644 --- a/contracts/bitlend.clar +++ b/contracts/bitlend.clar @@ -104,7 +104,7 @@ (err u0)))) (define-read-only (is-price-valid) - (< (- block-height (var-get last-price-update)) PRICE-VALIDITY-PERIOD)) + (< (- stacks-block-height (var-get last-price-update)) PRICE-VALIDITY-PERIOD)) (define-read-only (get-protocol-stats) { @@ -122,7 +122,7 @@ (try! (check-authorization)) (try! (validate-amount new-price-in-cents)) (var-set btc-price-in-cents new-price-in-cents) - (var-set last-price-update block-height) + (var-set last-price-update stacks-block-height) (ok true))) ;; Core Lending Functions @@ -158,7 +158,7 @@ { collateral-amount: current-collateral, borrowed-amount: amount, - last-update: block-height, + last-update: stacks-block-height, interest-rate: u5 ;; 5% APR }) @@ -181,7 +181,7 @@ (asserts! (<= amount current-borrowed) ERR-INVALID-AMOUNT) (let ( - (blocks-elapsed (- block-height (get last-update loan))) + (blocks-elapsed (- stacks-block-height (get last-update loan))) (interest-amount (/ (* current-borrowed (get interest-rate loan) blocks-elapsed) (* u100 u144 u365))) (total-due (+ amount interest-amount)) ) @@ -198,7 +198,7 @@ { collateral-amount: (get collateral-amount loan), borrowed-amount: (- current-borrowed amount), - last-update: block-height, + last-update: stacks-block-height, interest-rate: (get interest-rate loan) }) (map-set borrow-balances From b0b3ba8ac53c365de46dc4c7170a3c6b729070c5 Mon Sep 17 00:00:00 2001 From: semi-collab Date: Thu, 27 Feb 2025 11:48:17 +0100 Subject: [PATCH 6/7] Refactor read-only functions and add missing parentheses for consistency --- contracts/bitlend.clar | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/contracts/bitlend.clar b/contracts/bitlend.clar index 6b1ed74..c034a41 100644 --- a/contracts/bitlend.clar +++ b/contracts/bitlend.clar @@ -78,18 +78,22 @@ (define-private (check-protocol-active) (begin (asserts! (not (var-get protocol-paused)) ERR-NOT-AUTHORIZED) - (ok true))) + (ok true)) +) ;; Read-Only Functions (define-read-only (get-loan (user principal)) - (map-get? loans { user: user })) + (map-get? loans { user: user }) +) (define-read-only (get-collateral-balance (user principal)) - (default-to u0 (map-get? collateral-balances { user: user }))) + (default-to u0 (map-get? collateral-balances { user: user })) +) (define-read-only (get-borrow-balance (user principal)) - (default-to u0 (map-get? borrow-balances { user: user }))) + (default-to u0 (map-get? borrow-balances { user: user })) +) (define-read-only (get-current-collateral-ratio (user principal)) (let ( @@ -101,10 +105,12 @@ (borrowed-value (* (get borrowed-amount loan-data) u100)) ) (ok (/ (* collateral-value u100) borrowed-value))) - (err u0)))) + (err u0))) +) (define-read-only (is-price-valid) - (< (- stacks-block-height (var-get last-price-update)) PRICE-VALIDITY-PERIOD)) + (< (- stacks-block-height (var-get last-price-update)) PRICE-VALIDITY-PERIOD) +) (define-read-only (get-protocol-stats) { @@ -112,7 +118,8 @@ total-collateral: (var-get total-collateral), current-fee: (var-get protocol-fee-percentage), is-paused: (var-get protocol-paused) - }) + } +) ;; Public Functions @@ -123,7 +130,8 @@ (try! (validate-amount new-price-in-cents)) (var-set btc-price-in-cents new-price-in-cents) (var-set last-price-update stacks-block-height) - (ok true))) + (ok true)) +) ;; Core Lending Functions (define-public (deposit-collateral (amount uint)) @@ -137,7 +145,8 @@ (+ (get-collateral-balance tx-sender) amount)) (var-set total-collateral (+ (var-get total-collateral) amount)) - (ok true))) + (ok true)) +) (define-public (borrow (amount uint)) (begin @@ -167,7 +176,8 @@ (+ (get-borrow-balance tx-sender) amount)) (var-set total-loans (+ (var-get total-loans) amount)) - (ok true)))) + (ok true))) +) (define-public (repay-loan (amount uint)) (begin @@ -206,7 +216,8 @@ (- (get-borrow-balance tx-sender) amount)))) (var-set total-loans (- (var-get total-loans) amount)) - (ok true))))) + (ok true)))) +) (define-public (liquidate (user principal)) (begin @@ -240,7 +251,8 @@ (try! (as-contract (stx-transfer? remaining-collateral (as-contract tx-sender) user))) true) - (ok true))))) + (ok true)))) +) ;; Governance Functions @@ -249,10 +261,12 @@ (try! (check-authorization)) (asserts! (<= new-fee MAX-FEE-PERCENTAGE) ERR-EXCEED-MAX-FEE) (var-set protocol-fee-percentage new-fee) - (ok true))) + (ok true)) +) (define-public (toggle-protocol-pause) (begin (try! (check-authorization)) (var-set protocol-paused (not (var-get protocol-paused))) - (ok true))) \ No newline at end of file + (ok true)) +) \ No newline at end of file From 11f2f43ba152962c5e5120fa7030c75efb4c4cf2 Mon Sep 17 00:00:00 2001 From: semi-collab Date: Thu, 27 Feb 2025 11:52:26 +0100 Subject: [PATCH 7/7] Add initial README documentation for BitLend Protocol - Overview of BitLend protocol and its key features - Detailed explanation of collateral management system - Description of debt position engine and interest calculation - Integration details for price oracle - Core smart contract functions and user operations - Governance functions and risk management framework This README provides comprehensive documentation for understanding and using the BitLend protocol. --- README.md | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..2aa787b --- /dev/null +++ b/README.md @@ -0,0 +1,94 @@ +**BitLend Protocol Documentation** +**Version 1.0 | Stacks L2 Smart Contract** + +### **Protocol Overview** + +BitLend is a non-custodial Bitcoin lending protocol enabling BTC holders to: + +1. Lock BTC-collateralized assets as security +2. Borrow stable-value tokens against collateral +3. Participate in decentralized liquidations +4. Govern protocol parameters through DAO-like mechanisms + +Built on Stacks L2 for Bitcoin-finalized transactions. + +### **Key Technical Components** + +#### **1. Collateral Management System** + +- **Collateral Types**: Native Bitcoin representations (xBTC) +- **Dynamic Ratios**: + - Minimum Collateral Ratio: 150% + - Liquidation Threshold: 130% + - Auto-rebalancing via: + ```clarity + (define-private (update-collateral-ratio user) + (let ((loan (unwrap! (get-loan user)))) + (/ (* (get collateral-amount loan) (var-get btc-price)) + (get borrowed-amount loan)))) + ``` + +#### **2. Debt Position Engine** + +- Interest Calculation: + ``` + Interest = (Borrowed Amount × Rate × Blocks) / (Blocks/Year) + ``` +- Loan State Machine: + `ACTIVE → UNDERCOLLATERALIZED → LIQUIDATED` + +#### **3. Price Oracle Integration** + +- Dual Security Model: + 1. On-Chain: Time-weighted avg from major DEXs + 2. Off-Chain: Decentralized node network consensus +- Validity Enforcement: + ```clarity + (asserts! (<= (- block-height (var-get last-price-update)) + PRICE-VALIDITY-PERIOD) + ERR-PRICE-EXPIRED) + ``` + +### **Core Smart Contract Functions** + +#### **User Operations** + +| Function | Parameters | Security Checks | +| -------------------- | ---------------- | ---------------------------------------------- | +| `deposit-collateral` | `amount:uint` | - Non-zero amount
- Protocol active status | +| `borrow` | `amount:uint` | - Collateral ratio >150%
- Valid price feed | +| `repay-loan` | `amount:uint` | - Loan existence
- STX transfer success | +| `liquidate` | `user:principal` | - Collateral <130%
- Penalty application | + +#### **Governance Functions** + +```clarity +(define-public (update-risk-parameters (new-min-ratio uint) (new-liq-threshold uint)) +;; Requires: Contract owner + <10% parameter change/24h +``` + +### **Risk Management Framework** + +#### **Liquidation Process** + +1. Trigger: Collateral ratio <130% for >2 blocks +2. Execution: + - 10% penalty on outstanding debt + - Collateral auction: + ``` + Auction Price = Max(OTC Offer, Oracle Price × 95%) + ``` +3. Settlement: + - Debt clearance + - Remaining collateral returned + +#### **Circuit Breakers** + +- Protocol Pause: + ```clarity + (define-public (emergency-pause) + (asserts! (is-eq tx-sender CONTRACT-OWNER) + (var-set protocol-paused true)) + ``` +- Maximum Exposure: + `Total Borrowed ≤ 70% of Total Collateral Value`