Peer-to-Peer Lending DApp

 πŸ’‘ What is a Peer-to-Peer Lending DApp?


It’s a decentralized application that enables:


Borrowers to request loans


Lenders to fund those loan requests


Smart contracts to hold funds, enforce repayment terms, and release repayments


🧱 Tech Stack Overview

Component Technology

Smart Contracts Solidity

Blockchain Ethereum / Polygon (testnet)

Frontend React.js

Web3 Ethers.js or Web3.js

Storage (optional) IPFS for loan agreements/files

Wallet MetaMask

Dev Tools Hardhat or Truffle

🧠 Key Features

πŸ“Œ For Borrowers:


Submit a loan request (amount, duration, interest)


View funding status


Repay loan


πŸ“Œ For Lenders:


View available loan requests


Fund all or part of a loan


Receive repayment + interest


πŸ” Smart Contract: Basic Example (Solidity)

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;


contract P2PLending {

    struct Loan {

        address borrower;

        address lender;

        uint amount;

        uint interest;

        uint dueDate;

        bool funded;

        bool repaid;

    }


    Loan[] public loans;


    event LoanRequested(uint loanId, address borrower, uint amount);

    event LoanFunded(uint loanId, address lender);

    event LoanRepaid(uint loanId);


    function requestLoan(uint _amount, uint _interest, uint _durationDays) external {

        loans.push(Loan({

            borrower: msg.sender,

            lender: address(0),

            amount: _amount,

            interest: _interest,

            dueDate: block.timestamp + (_durationDays * 1 days),

            funded: false,

            repaid: false

        }));

        emit LoanRequested(loans.length - 1, msg.sender, _amount);

    }


    function fundLoan(uint _loanId) external payable {

        Loan storage loan = loans[_loanId];

        require(!loan.funded, "Already funded");

        require(msg.value == loan.amount, "Incorrect amount");


        loan.lender = msg.sender;

        loan.funded = true;

        payable(loan.borrower).transfer(loan.amount);


        emit LoanFunded(_loanId, msg.sender);

    }


    function repayLoan(uint _loanId) external payable {

        Loan storage loan = loans[_loanId];

        require(msg.sender == loan.borrower, "Only borrower can repay");

        require(loan.funded && !loan.repaid, "Invalid loan status");


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

        require(msg.value >= totalRepayment, "Insufficient repayment");


        loan.repaid = true;

        payable(loan.lender).transfer(msg.value);


        emit LoanRepaid(_loanId);

    }


    function getLoansCount() public view returns (uint) {

        return loans.length;

    }


    function getLoan(uint _loanId) public view returns (Loan memory) {

        return loans[_loanId];

    }

}


πŸ–₯️ Frontend Functionality (React + Ethers.js)

✅ Connect Wallet

const provider = new ethers.providers.Web3Provider(window.ethereum);

const signer = provider.getSigner();


✅ Submit a Loan Request

await contract.requestLoan(ethers.utils.parseEther("1"), 10, 30); // 1 ETH, 10% interest, 30 days


✅ Fund a Loan

await contract.fundLoan(0, { value: ethers.utils.parseEther("1") });


✅ Repay a Loan

await contract.repayLoan(0, { value: ethers.utils.parseEther("1.1") }); // amount + interest


πŸ§ͺ Sample Hardhat Test

it("Should allow a loan to be requested, funded, and repaid", async () => {

  const [borrower, lender] = await ethers.getSigners();

  const Lending = await ethers.getContractFactory("P2PLending");

  const lending = await Lending.deploy();


  await lending.connect(borrower).requestLoan(ethers.utils.parseEther("1"), 10, 30);

  await lending.connect(lender).fundLoan(0, { value: ethers.utils.parseEther("1") });


  await lending.connect(borrower).repayLoan(0, { value: ethers.utils.parseEther("1.1") });


  const loan = await lending.getLoan(0);

  expect(loan.repaid).to.equal(true);

});


πŸ“ Project Folder Structure

p2p-lending-dapp/

├── contracts/

│   └── P2PLending.sol

├── frontend/

│   ├── src/

│   │   └── App.js

│   │   └── components/

│   │       └── LoanList.js

├── test/

│   └── p2p-test.js

├── scripts/

│   └── deploy.js

├── hardhat.config.js

└── README.md


🌐 Optional Enhancements


NFT receipts for funded loans


Credit scoring (based on repayment history)


Partial funding from multiple lenders


Loan expiration logic


Frontend dashboard with filters and statuses


πŸš€ Deployment Steps


Deploy contract to a testnet (Polygon Mumbai, Sepolia)


Host frontend with Netlify, Vercel, or GitHub Pages


Integrate MetaMask for wallet connection


Include contract address and ABI for interaction


✅ Demo Flow Summary


Borrower connects wallet and submits a loan request


Lender browses open loans and funds one


Funds go to the borrower via smart contract


Borrower repays loan before due date


Smart contract sends repayment + interest to the lender

Learn Blockchain Course in Hyderabad

Read More

Decentralized Music Streaming Demo

Blockchain-Based Resume Verification App

Simple DAO Voting DApp

Crypto Portfolio Tracker Using Web3


Comments

Popular posts from this blog

Entry-Level Cybersecurity Jobs You Can Apply For Today

Understanding Snowflake Editions: Standard, Enterprise, Business Critical

Installing Tosca: Step-by-Step Guide for Beginners