A DeFi loan utilizes smart contracts to enforce compliance with loan conditions, such as the application of interest rates and the automated distribution of yields on deposited assets.
NOTE: Normally, DeFi loans permit the borrowing of liquidated assets by using cryptocurrency as collateral. However, to simplify and effectively teach the use-case in a straightforward way, this particular DeFi loan example showcases both deposits and lending using the same STX asset.
The stx-defi.clar contract includes the following public functions.
deposit
function for users to deposit STX into the contractborrow
function for users to borrow STX based on their depositsrepay
function for users to repay their STX loansget-amount-owed
function to get the total amount owed by the userclaim-yield
function for users to claim their yield based on the pool reserve and their deposits
NOTE: To use this example with Clarinet inside Hiro Platform, you can open the terminal session inside VS code by navigating to File -> View -> Terminal.
You can manually test your contract in the Clarinet console. After entering the console with clarinet console
you can make contract function calls to verify the protocol functionality:
(contract-call? .stx-defi deposit u1000)
(contract-call? .stx-defi borrow u7)
(contract-call? .stx-defi get-amount-owed)
Alternatively, you can run the unit tests included in the tests/
folder. From the terminal session, navigate to /clarity-example/stx-defi
folder, and run npm test
.