Automating Loan Disbursements

Let’s walk through Automating Loan Disbursements using a smart contract. In this example, a smart contract is used to handle a loan disbursement once certain conditions are met, such as the verification of the borrower’s credit score, collateral, or the signing of necessary agreements.

Problem Setup:

  • A borrower requests a loan from a bank.
  • The bank (or lender) creates a smart contract, defining the loan terms (amount, interest, collateral, repayment schedule).
  • Once the borrower fulfills the predefined conditions (e.g., collateral is deposited, or credit score is verified), the smart contract disburses the loan.
  • The contract also automatically enforces the repayment schedule, including penalties if the borrower fails to pay on time.

We’ll implement this logic using Solidity.

Solidity Smart Contract Code for Loan Disbursement

solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract LoanDisbursement {

    address public lender;             // The lender (e.g., bank)
    address payable public borrower;   // The borrower
    uint public loanAmount;            // Loan amount in Wei (smallest Ether unit)
    uint public interestRate;          // Interest rate as a percentage
    uint public loanDuration;          // Duration of loan in seconds
    uint public collateralAmount;      // Required collateral in Wei
    uint public loanStartTime;         // Time the loan was disbursed
    bool public loanActive;            // Status of the loan (disbursed or not)

    // Events for logging important actions
    event LoanRequested(address borrower, uint loanAmount, uint collateralAmount);
    event LoanDisbursed(address borrower, uint loanAmount);
    event LoanRepaid(address borrower, uint repaymentAmount);
    event CollateralReturned(address borrower, uint collateralAmount);
    event CollateralSeized(address lender, uint collateralAmount);

    // Constructor sets the lender's address
    constructor() {
        lender = msg.sender;  // The lender is the deployer of the contract
    }

    // Struct to store loan details
    struct Loan {
        uint amount;
        uint interest;
        uint dueDate;
        uint collateral;
        bool repaid;
    }

    Loan public loan;

    // Function to request a loan
    function requestLoan(uint _loanAmount, uint _collateralAmount, uint _loanDuration, uint _interestRate) external payable {
        require(msg.value == _collateralAmount, "Collateral must be deposited");
        require(loanActive == false, "Loan already active");

        borrower = payable(msg.sender);
        loanAmount = _loanAmount;
        collateralAmount = _collateralAmount;
        loanDuration = _loanDuration;
        interestRate = _interestRate;

        loan = Loan({
            amount: loanAmount,
            interest: interestRate,
            dueDate: block.timestamp + loanDuration,
            collateral: collateralAmount,
            repaid: false
        });

        emit LoanRequested(borrower, loanAmount, collateralAmount);
    }

    // Function for the lender to disburse the loan after checking conditions
    function disburseLoan() external payable {
        require(msg.sender == lender, "Only lender can disburse loan");
        require(loanActive == false, "Loan already disbursed");
        require(address(this).balance >= collateralAmount, "Collateral must be deposited");

        borrower.transfer(loanAmount);  // Disburse the loan to the borrower
        loanActive = true;
        loanStartTime = block.timestamp;

        emit LoanDisbursed(borrower, loanAmount);
    }

    // Function for borrower to repay the loan with interest
    function repayLoan() external payable {
        require(msg.sender == borrower, "Only the borrower can repay the loan");
        require(loanActive == true, "No active loan to repay");
        require(block.timestamp <= loan.dueDate, "Loan is overdue");

        uint repaymentAmount = loan.amount + (loan.amount * loan.interest / 100); // Loan + interest

        require(msg.value == repaymentAmount, "Repayment must include loan + interest");

        // Send the repayment amount to the lender
        payable(lender).transfer(repaymentAmount);

        // Mark the loan as repaid and return the collateral
        loan.repaid = true;
        loanActive = false;
        borrower.transfer(collateralAmount);  // Return collateral to borrower

        emit LoanRepaid(borrower, repaymentAmount);
        emit CollateralReturned(borrower, collateralAmount);
    }

    // Function for the lender to seize collateral if the loan is not repaid on time
    function seizeCollateral() external {
        require(msg.sender == lender, "Only lender can seize collateral");
        require(loanActive == true, "No active loan");
        require(block.timestamp > loan.dueDate, "Loan is not yet overdue");
        require(loan.repaid == false, "Loan already repaid");

        loanActive = false;

        // Lender seizes the collateral
        payable(lender).transfer(collateralAmount);

        emit CollateralSeized(lender, collateralAmount);
    }

    // Fallback function to receive Ether
    receive() external payable {}
}

Explanation of the Smart Contract:

  1. Variables:
    • lender: The address of the lender (who deploys the contract).
    • borrower: The address of the borrower who requests the loan.
    • loanAmount: The amount of the loan (in Ether, but represented in Wei).
    • collateralAmount: The amount of collateral that the borrower must deposit.
    • loanDuration: The time period for which the loan is active.
    • interestRate: The interest rate applied to the loan.
    • loanStartTime: The timestamp when the loan is disbursed.
    • loanActive: A boolean flag indicating if the loan is active.
  2. Loan Struct: This stores the details of the loan, including:
    • amount: The principal loan amount.
    • interest: The interest rate.
    • dueDate: The loan’s repayment deadline.
    • collateral: The collateral provided by the borrower.
    • repaid: Boolean indicating if the loan has been repaid.
  3. Functions:
    • requestLoan:
      • The borrower requests a loan by depositing the required collateral.
      • The contract ensures that the borrower deposits the exact amount of collateral specified in _collateralAmount.
    • disburseLoan:
      • The lender can disburse the loan once conditions are met (such as collateral being deposited).
      • This function transfers the loan amount to the borrower’s address.
    • repayLoan:
      • The borrower repays the loan, including interest.
      • The smart contract verifies that the repayment is made before the due date and checks if the repayment amount is correct.
      • If successful, the collateral is returned to the borrower.
    • seizeCollateral:
      • If the borrower fails to repay the loan by the due date, the lender can seize the collateral by calling this function.
      • The collateral is transferred to the lender as a form of penalty.
  4. Events:
    • The contract emits events such as LoanRequested, LoanDisbursed, LoanRepaid, CollateralReturned, and CollateralSeized to log important actions for transparency.

Example Scenario:

  1. Loan Request:
    • The borrower calls requestLoan and deposits the collateral (e.g., 5 ETH). The loan is not yet disbursed but the request is logged in the contract.
  2. Loan Disbursement:
    • The lender verifies that the borrower has deposited the collateral. The lender then calls disburseLoan, transferring the loan amount (e.g., 10 ETH) to the borrower.
  3. Repayment:
    • The borrower repays the loan within the loan duration. The smart contract calculates the repayment amount (loan + interest). Upon successful repayment, the borrower gets back the collateral, and the lender receives the repayment with interest.
  4. Collateral Seizure:
    • If the borrower fails to repay by the due date, the lender can call seizeCollateral, and the contract transfers the collateral to the lender.

Benefits of Automating Loan Disbursement with Smart Contracts:

  1. Trustless Environment:
    • Both the lender and borrower can trust the contract to enforce loan terms, removing the need for intermediaries.
  2. Transparency:
    • Loan details, repayments, and collateral handling are recorded transparently on the blockchain, preventing disputes.
  3. Efficiency:
    • The entire process, from loan disbursement to repayment and collateral management, is automated, reducing manual intervention and operational delays.
  4. Security:
    • Smart contracts ensure that conditions are strictly enforced (e.g., loan only disbursed if collateral is deposited), minimizing the risk for both parties.

This smart contract example showcases how blockchain can revolutionize traditional loan processes by automating key tasks, making them more transparent, efficient, and secure.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share via
Copy link