BSV
$52.97
Vol 33.62m
-8.19%
BTC
$96029
Vol 43997.27m
-3.07%
BCH
$449.06
Vol 393.02m
-4.57%
LTC
$99.56
Vol 917.24m
-4.93%
DOGE
$0.31
Vol 5989.48m
-10.04%
Getting your Trinity Audio player ready...

This post was first published on Medium.

We are thrilled to introduce the implementation of loan smart contracts that allow lending and borrowing directly on chain. Loan contracts are essential financial tools that allow individuals or entities to borrow money with agreed-upon terms and conditions. Leveraging the strengths of smart contract technology has the potential to revolutionize how loans are managed, enhancing security, efficiency, and transparency. Our implementation focuses on collateralized loans with a deadline.

Bitcoins, financial and loan concept illustration

How collateralized loans with a deadline work

In our example, a borrower borrows tokens from a lender using bitcoin as collaterals.

  • Collateral: a borrower provides an asset (in our case, bitcoin) as collateral. This is locked in the smart contract for the duration of the loan. The value of the collateral typically exceeds the loan amount (overcollateralized), providing security for the lender.
  • Loan Disbursement: once the collateral is locked, the loan amount in BSV-20 tokens is disbursed to the borrower. The contract specifies the terms, including the loan amount, interest rate, and repayment deadline.
  • Repayment Deadline: the borrower is required to repay the loan along with any agreed-upon interest by a specific deadline. This includes both the principal and interest amount.
  • Default and Liquidation: if the borrower fails to repay by the deadline, the smart contract allows the lender to withdraw the collateral that was provided by the borrower.
  • Repayment and Collateral Release: if the borrower repays the loan in full by the deadline, the smart contract releases the collateral back to the borrower.

Single borrower

The following loan smart contract examples lifecycle consisted of the following steps:

  1. Initialization: the contract is initialized with the loan terms (amount, interest rate, collateral, deadline) and the identities of the lender and borrower.
  2. Borrowing: the borrower triggers the borrow method, receiving the loan amount upon successfully locking in the collateral.
  3. Repayment or Foreclosure:
  • If the borrower repays the loan on time, the repay method is triggered, returning the collateral and paying the lender.
  • If the borrower fails to repay, the lender can invoke foreclose to claim the collateral.

bsv20LoanMultiple.ts

Contract instance BSV 20
Transaction diagram depicting a call of the borrow method

As before, an oracle is queried to validate the authenticity of tokens.

Multiple borrowers

The loan contract above only allows a single borrower. We can extend it to support multiple borrowers. This smart contract is able to manage several loans simultaneously.

In the upgraded contract, we’ve refined our approach to accommodate multiple borrowers within a single smart contract. The contract now utilizes a Borrower data structure, where each borrower’s details, such as public key, loan amount, and deadline, are stored.

The process of loan management has been streamlined to support dynamic borrower interactions. Borrowers can request loans, and these requests are recorded in individual slots within the contract, along with the necessary collateral calculated based on the loan amount. Lenders have the ability to review and approve these requests, setting the loan terms, and initiating the transfer of funds.

Furthermore, the contract facilitates the handling of repayments and the option for lenders to foreclose on loans in case of defaults.

Let’s define the technical workflow of this updated smart contract:

Requesting a loan

A borrower requests a loan, providing their public key and the desired loan amount. The contract calculates the necessary collateral and updates the borrower’s slot.

Request loan method

Approving the loan

The lender reviews the loan request and, if acceptable, approves it. The loan amount is transferred to the borrower, and the repayment deadline is set. To facilitate the enforcement of a relative time-lock, we extract the block height from the block header of the latest contract instance.

Minor fixes for bsv-20 contracts.

Repaying or foreclosing:

The borrower either repays the loan by the deadline or the lender initiates foreclosure to claim the collateral in case of default.

boilerplate src contracts

The contract also implements methods to cancel requests and deny approvals.

The full code for both the single-borrower and multiple-borrower contracts can be explored further on GitHub..

Watch: What really happens when you make a Bitcoin payment

Recommended for you

Who wants to be an entrepreneur?
Embodying the big five personality traits could be beneficial for aspiring entrepreneurs, but Block Dojo shows that there is more...
December 20, 2024
UNISOT, PSU China team up for supply chain business intelligence
UNISOT revealed a new partnership with business intelligence and research firm PSU China, which will combine its data with UNISOT's...
December 20, 2024
Advertisement
Advertisement
Advertisement