Skip to content

Lending & borrowing #1

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

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

Lending & borrowing #1

wants to merge 3 commits into from

Conversation

walterthesmart
Copy link
Owner

Implement Basic Lending Platform Smart Contract

Overview

This pull request introduces a new Clarity smart contract that implements a basic lending platform on the Stacks blockchain. The contract allows users to deposit and withdraw sBTC, borrow against their deposits, and includes an interest calculation mechanism for loans.

Changes Introduced

  1. Implemented a new Clarity smart contract with the following main features:
    • Deposit functionality
    • Withdrawal functionality
    • Borrowing functionality
    • Basic interest calculation
  2. Defined data structures to track user deposits and loans
  3. Implemented error handling for common scenarios
  4. Added a pool reserve to collect interest payments

Detailed Description

Data Structures

  • deposits: A map that associates each user's principal with their deposited amount.
  • loans: A map that tracks each user's borrowed amount and the block height of their last interaction.
  • total-deposits: A variable to keep track of the total sBTC deposited into the platform.
  • pool-reserve: A variable to track the total interest collected.
  • loan-interest-rate: A variable representing the interest rate for loans (currently set at 10%).

Main Functions

  1. deposit: Allows users to deposit sBTC into the platform. It updates the user's deposit balance and the total deposits.

  2. withdraw: Enables users to withdraw their deposited sBTC. It checks if the user has sufficient balance and updates the relevant data structures.

  3. borrow: Allows users to borrow sBTC against their deposits. It calculates interest, updates the loan balance, and adjusts the user's deposit.

Error Handling

We've defined several error constants to handle different scenarios:

  • err-no-interest: For issues with interest calculation
  • err-overpay: To prevent withdrawing more than deposited
  • err-overborrow: To prevent borrowing more than the allowed amount

Security Considerations

  • The contract uses the as-contract function to ensure that token transfers are executed with the contract's authority.
  • Assertions are used to prevent overborrowing and overwithdrawing.

How This Relates to the Project

This smart contract forms the core of our decentralized lending platform. It provides the essential functionality for users to interact with the platform, including depositing collateral, borrowing assets, and the framework for interest calculations.

Next Steps

  1. Implement a repayment function for loans.
  2. Add a liquidation mechanism for undercollateralized loans.
  3. Implement more sophisticated interest rate models.
  4. Add events for important actions (deposits, withdrawals, borrows).
  5. Develop comprehensive test suites for all functions.
  6. Set up continuous integration for automated testing.
  7. Prepare deployment scripts for testnet and mainnet.

Testing

  • Basic functionality tests have been performed manually.
  • Formal test suites need to be developed (marked as a next step).

Dependencies

  • This contract assumes the existence of an asset contract for sBTC transfers. This will need to be properly integrated or mocked for testing.

Notes for Reviewers

  • Please pay special attention to the security aspects of the contract, particularly around the token transfer mechanics.
  • The interest calculation is currently very basic. We may want to discuss more sophisticated models for future iterations.
  • Consider if the current error handling is sufficient or if we need more granular error codes.
  • The contract currently lacks events for tracking important actions. Please advise if we should add these in this iteration or a future one.

This PR lays the groundwork for our lending platform. It implements the core functionality while leaving room for future enhancements and optimizations.

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