blog bg

June 03, 2025

Creating a Simple DeFi Lending Smart Contract in Solidity

Share what you learn in this blog to prepare for your interview, create your forever-free profile now, and explore how to monetize your valuable knowledge.

 

Decentralised Finance (DeFi) is changing conventional financial systems. Banks and intermediaries are no longer needed to lend and borrow money. Blockchain technology lets everyone lend and borrow assets trustlessly and decentralized. This blog shows how to design a basic Solidity DeFi lending and borrowing smart contract. This tutorial will teach you how to design a basic DeFi protocol in Solidity, whether you are a beginner or an experienced developer. 

 

Understanding DeFi Lending 

Without a central authority, DeFi financing involves borrowing and lending digital assets. Lenders gain interest by lending assets to a pool, while borrowers pay interest for borrowing. Ethereum blockchain smart contracts secure these transactions without middlemen. 

Decentralized lending platforms like Aave and Compound are well-known in DeFi. Why not create your own loan protocol instead of using these major platforms? This post shows how to achieve it using a basic smart contract. 

 

Prerequisites 

To prepare for this project, there are a few requirements before we start coding. Solidity and Ethereum should be familiar to you. Before proceeding, learn about smart contracts and Ethereum. 

For this project, I will use Remix IDE, an excellent online Solidity contract development, testing, and deployment tool. To interact with the contract via Ethereum wallet, you require MetaMask. ERC20 tokens, Ethereum's most widespread token standard, will represent our lending and borrowing assets. Setting up these tools is easy, and Remix lets us concentrate on coding.

 

Smart Contract Structure: Code Walkthrough

Now, let's examine the code that creates this. I will explain the main features and how they work.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface IERC20 {
    function transfer(address recipient, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
}

contract DeFiLending {
    IERC20 public token; 
    mapping(address => uint256) public deposits;
    mapping(address => uint256) public loans;
    uint256 public interestRate = 5; // Simple interest rate

    constructor(address _token) {
        token = IERC20(_token);
    }

    function deposit(uint256 amount) public {
        require(amount > 0, "Deposit amount must be greater than zero");
        deposits[msg.sender] += amount;
        token.transferFrom(msg.sender, address(this), amount);
    }

    function borrow(uint256 amount) public {
        require(deposits[msg.sender] >= amount, "Not enough collateral");
        loans[msg.sender] += amount;
        token.transfer(msg.sender, amount);
    }

    function repay(uint256 amount) public {
        require(loans[msg.sender] >= amount, "Repay amount exceeds loan");
        loans[msg.sender] -= amount;
        uint256 interest = (amount * interestRate) / 100;
        token.transferFrom(msg.sender, address(this), amount + interest);
    }
}

This is what each function does: 

  1. deposit(): This lets users put tokens into the contract, which means they are lending them. It needs the user to call the function with a defined amount, which is passed to the contract. 
  2. borrow(): Users can borrow tokens by putting up collateral from their accounts. This method verifies that the user's security is enough before giving the loan. 
  3. repay(): This is used for making loan payments. The user repays the borrowed amount along with interest, which lowers the loan total. 

This code creates a simple form of a DeFi loan contract. 

 

Testing the Smart Contract

Our smart contract is ready for Remix IDE deployment and testing. Remix lets us engage with the contract, simulate deposits, loans, and repayments, and see how it acts in real life. 

Start by deploying the contract on Remix Ethereum VM. To replicate a lending process, use MetaMask to transfer tokens, deposit them into the contract, then borrow or refund using the stated methods. Testing on Remix's test network verifies contract functionality without financial risk. 

 

Conclusion 

We constructed a basic but functioning Solidity DeFi loan and borrowing smart contract in this post. Deposit, borrow, and payback functions form a decentralized financial structure. To improve this contract, include flexible interest rates, control of assets, or quick loans. 

This project showed how DeFi smart contracts work, creating a foundation for more advanced decentralized apps. Keep testing your code, who knows? You could invent the next great DeFi protocol.

123 views

Please Login to create a Question