Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement BTC Lending Protocol Smart Contract #1

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

savi-auto
Copy link
Owner

Overview

This PR implements a comprehensive decentralized lending protocol on the Stacks blockchain. The protocol enables users to deposit STX as collateral, borrow against it, and participate in liquidations, with built-in governance functionality and price oracle integration.

Features Added

Core Protocol Infrastructure

  • Implemented metadata and essential protocol parameters
  • Added comprehensive error handling system
  • Set up protocol state tracking and management
  • Integrated price oracle functionality
  • Established balance tracking system

Lending Functions

  • Collateral deposit mechanism with validation
  • Borrowing system with collateral ratio checks
  • Loan repayment with interest calculation
  • Liquidation mechanism for under-collateralized positions

Administrative Features

  • Protocol pause/unpause functionality
  • Fee management system
  • Price oracle update mechanism

Read-Only Functions

  • Loan information retrieval
  • Balance checking capabilities
  • Protocol statistics monitoring
  • Collateral ratio calculations

Technical Implementation

Protocol Parameters

MIN-COLLATERAL-RATIO: u150    // 150% minimum collateral ratio
LIQUIDATION-THRESHOLD: u130   // 130% liquidation threshold
LIQUIDATION-PENALTY: u10      // 10% liquidation penalty
PRICE-VALIDITY-PERIOD: u3600  // 1 hour price validity
MAX-FEE-PERCENTAGE: u10       // 10% maximum protocol fee

Test Coverage

  • Contract deployment and initialization
  • Collateral deposit and withdrawal flows
  • Borrowing and repayment scenarios
  • Liquidation mechanics
  • Administrative functions
  • Edge cases and error conditions

Clarinet Console Output

Contract identifier: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.btc-lending
Public functions verified:
- borrow
- deposit-collateral
- get-borrow-balance
- get-collateral-balance
- get-current-collateral-ratio
- get-loan
- get-protocol-stats
- is-price-valid
- liquidate
- repay-loan
- toggle-protocol-pause
- update-btc-price
- update-protocol-fee

Security Considerations

  1. Implemented strict access controls for administrative functions
  2. Added validation checks for all numerical operations
  3. Included price oracle validity checks
  4. Built-in pause mechanism for emergency situations
  5. Enforced minimum collateral ratios
  6. Added liquidation penalty to discourage risky positions

Testing Instructions

  1. Deploy the contract:
clarinet deploy
  1. Test core functions in console:
clarinet console

Deployment Plan

  1. Deploy to testnet for initial testing
  2. Security audit
  3. Mainnet deployment with controlled parameter settings
  4. Gradual parameter optimization based on market conditions

Documentation

  • Added comprehensive README with implementation details
  • Included inline code documentation
  • Created usage examples for core functions
  • Documented all error codes and their meanings

Checklist

  • Contract implementation complete
  • Error handling implemented
  • Documentation added
  • Tests written and passing
  • Security considerations addressed
  • Gas optimization performed
  • Contract verified in Clarinet console

Future Improvements

  1. Variable interest rate model
  2. Multi-collateral support
  3. Flash loan functionality
  4. Governance token integration
  5. Additional oracle price sources
  6. Automated liquidation bot support

Please review and provide feedback on the implementation, particularly regarding:

  • Security considerations
  • Parameter settings
  • Error handling
  • Gas optimization

Tag: @protocol-team @security-team

- Added title, summary, and description metadata to the smart contract.
- Defined protocol parameters including CONTRACT-OWNER, MIN-COLLATERAL-RATIO, LIQUIDATION-THRESHOLD, LIQUIDATION-PENALTY, PRICE-VALIDITY-PERIOD, and MAX-FEE-PERCENTAGE.
- Defined error codes for various failure scenarios including authorization, balance, amount validation, collateral requirements, loan existence, liquidation, price validity, zero amount, and fee limits.
- Defined data variables to track the protocol state including `protocol-paused`, `total-loans`, `total-collateral`, and `protocol-fee-percentage`.
- Defined data variables `btc-price-in-cents` and `last-price-update` to store the BTC price and the timestamp of the last price update.
- Defined maps `collateral-balances` and `borrow-balances` to track the collateral and borrowed balances for each user.
- Added `validate-amount` to ensure the amount is greater than zero.
- Added `check-authorization` to verify if the transaction sender is the contract owner.
- Added `check-protocol-active` to ensure the protocol is not paused.
- Added `get-loan` to retrieve loan details for a user.
- Added `get-collateral-balance` to get the collateral balance of a user.
- Added `get-borrow-balance` to retrieve the borrow balance of a user.
- Added `get-current-collateral-ratio` to calculate the current collateral ratio for a user.
- Added `is-price-valid` to check if the BTC price is still valid based on the last update.
- Added `get-protocol-stats` to retrieve the current protocol statistics including total loans, total collateral, current fee, and paused state.
- Added `update-btc-price` to allow the contract owner to update the BTC price and the timestamp of the last price update.
- Added `deposit-collateral` to allow users to deposit collateral into the protocol. The function checks if the protocol is active, validates the amount, transfers the collateral, updates the user's collateral balance, and updates the total collateral in the protocol.
- Added `borrow` to allow users to borrow against their collateral. The function checks if the protocol is active, validates the amount, ensures the price is valid, checks if the user has an existing loan, verifies the collateral ratio, updates the loan and borrow balances, and increments the total loans in the protocol.
- Added `repay-loan` to allow users to repay their loans. The function checks if the protocol is active, validates the amount, calculates the interest due, transfers the total due amount, updates or deletes the loan and borrow balances, and decrements the total loans in the protocol.
- Added `liquidate` to allow the protocol to liquidate under-collateralized loans. The function checks if the protocol is active, verifies the loan and collateral ratio, calculates the liquidation value, transfers the liquidation amount, clears the loan, updates the protocol state, and returns any remaining collateral to the user.
- Added `update-protocol-fee` to allow the contract owner to update the protocol fee percentage. The function checks authorization, validates the new fee against the maximum allowed fee, and updates the protocol fee percentage.
- Added `toggle-protocol-pause` to allow the contract owner to pause or unpause the protocol. The function checks authorization and toggles the `protocol-paused` state.
- Provided an overview of the BTC Lending Protocol, including key features and technical specifications.
- Detailed the core functions, including user functions, read-only functions, and administrative functions.
- Included usage examples for depositing collateral, borrowing, repaying loans, and liquidating positions.
- Highlighted security considerations and contributing guidelines.
- Added licensing information.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant