Lesson 4.2: Creating a Decentralized Finance (DeFi) Application

Welcome back, DeFi enthusiasts!🚀✨

smileIn this lesson, we’re going to explore the exciting world of Decentralized Finance (DeFi). We’ll dive into key DeFi concepts like lending, borrowing, and yield farming, and we’ll write smart contracts to handle lending and borrowing. Ready to build your own DeFi platform? Let’s get started!

Understanding DeFi Concepts

1. Lending: Lending in DeFi allows users to lend their crypto assets to others in exchange for interest. It’s like a digital version of peer-to-peer lending without intermediaries.

2. Borrowing: Borrowing in DeFi lets users take out loans by providing collateral. This allows them to access liquidity without selling their assets.

3. Yield Farming: Yield farming involves earning rewards by staking or lending crypto assets. It’s a way to maximize returns on your investments.

Writing Smart Contracts to Handle Lending and Borrowing

Let’s write smart contracts to create a simple lending platform where users can lend and borrow tokens.

1. Define the Lending Platform Contract:

solidity

pragma solidity ^0.8.0;

import “@openzeppelin/contracts/token/ERC20/IERC20.sol”;

import “@openzeppelin/contracts/access/Ownable.sol”;

import “@openzeppelin/contracts/utils/math/SafeMath.sol”;

contract LendingPlatform is Ownable {

using SafeMath for uint256;

IERC20 public token;

uint256 public totalLent;

uint256 public totalBorrowed;

struct Loan {

uint256 amount;

uint256 interestRate;

uint256 startTime;

bool isActive;

}

mapping(address => uint256) public deposits;

mapping(address => Loan) public loans;

event Deposited(address indexed user, uint256 amount);

event Withdrawn(address indexed user, uint256 amount);

event Borrowed(address indexed user, uint256 amount, uint256 interestRate);

event Repaid(address indexed user, uint256 amount);

constructor(IERC20 _token) {

token = _token;

}

function deposit(uint256 _amount) public {

require(_amount > 0, “Amount must be greater than 0”);

token.transferFrom(msg.sender, address(this), _amount);

deposits[msg.sender] = deposits[msg.sender].add(_amount);

totalLent = totalLent.add(_amount);

emit Deposited(msg.sender, _amount);

}

function withdraw(uint256 _amount) public {

require(_amount <= deposits[msg.sender], “Insufficient balance”);

deposits[msg.sender] = deposits[msg.sender].sub(_amount);

token.transfer(msg.sender, _amount);

totalLent = totalLent.sub(_amount);

emit Withdrawn(msg.sender, _amount);

}

function borrow(uint256 _amount, uint256 _interestRate) public onlyOwner {

require(_amount > 0, “Amount must be greater than 0”);

require(loans[msg.sender].isActive == false, “Existing loan must be repaid first”);

loans[msg.sender] = Loan({

amount: _amount,

interestRate: _interestRate,

startTime: block.timestamp,

isActive: true

});

token.transfer(msg.sender, _amount);

totalBorrowed = totalBorrowed.add(_amount);

emit Borrowed(msg.sender, _amount, _interestRate);

}

function repay() public {

Loan storage loan = loans[msg.sender];

require(loan.isActive == true, “No active loan found”);

uint256 interest = loan.amount.mul(loan.interestRate).mul(block.timestamp.sub(loan.startTime)).div(365 days).div(100);

uint256 totalRepayment = loan.amount.add(interest);

token.transferFrom(msg.sender, address(this), totalRepayment);

loan.isActive = false;

totalBorrowed = totalBorrowed.sub(loan.amount);

 

emit Repaid(msg.sender, totalRepayment);

}

}

Explanation:

  • ERC20 Token: The platform uses an ERC20 token for lending and borrowing.
  • Deposits and Loans: Mappings to track user deposits and loans.
  • Deposit Function: Allows users to deposit tokens to the platform.
  • Withdraw Function: Allows users to withdraw their deposited tokens.
  • Borrow Function: Allows users to borrow tokens from the platform by the owner setting the interest rate.
  • Repay Function: Allows users to repay their loans with interest.

 

Real-World Example: Developing a Simple Lending Platform

Let’s put it all together and develop a simple lending platform where users can lend and borrow tokens.

  1. Deploy Your ERC20 Token: Deploy an ERC20 token contract that will be used for lending and borrowing on the platform.
  2. Deploy Your Lending Platform Contract: Deploy the LendingPlatform contract with the token address.
  3. Interact with the Platform: Use the functions to deposit tokens, borrow tokens with an interest rate set by the owner, and repay loans with interest. This simulates a decentralized lending and borrowing platform.