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.
depositfunction for users to deposit STX into the contractborrowfunction for users to borrow STX based on their depositsrepayfunction for users to repay their STX loansget-amount-owedfunction to get the total amount owed by the userclaim-yieldfunction for users to claim their yield based on the pool reserve and their deposits
To add a new contract, use Clarinet.
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.