DeFi Lending

Contract Summary

A DeFi lending protocol that allows users to deposit STX, borrow against their deposits, and earn yield from interest payments. This contract demonstrates core DeFi mechanics including collateralized loans, interest accrual, and yield distribution.

What this contract does:

  • Accepts STX deposits from users and tracks individual balances

  • Allows users to borrow up to 50% of their deposited amount

  • Calculates and accrues interest on loans based on block height

  • Charges 10% interest rate on borrowed amounts

  • Enables borrowers to repay loans with accrued interest

  • Distributes yield to depositors based on their share of the pool

  • Maintains a pool reserve funded by interest payments

  • Prevents over-borrowing beyond collateral limits

  • Tracks loan details including principal and last interaction block

What developers can learn:

  • Building collateralized lending protocols with loan-to-value ratios

  • Time-based interest calculation using block heights

  • Managing multiple user states with separate maps (deposits vs loans)

  • Interest accrual patterns in DeFi applications

  • Yield distribution mechanics based on pool shares

  • Using as-contract for contract-initiated transfers

  • Preventing common DeFi errors (over-borrowing, over-repayment)

  • Tracking global state (total deposits, pool reserve) with data variables

  • Read-only functions for user balance and debt queries

  • Error handling for financial operations with descriptive constants

  • Basic DeFi math: percentage calculations, pro-rata distributions

Was this helpful?