DeFi Bank (Decentralised Finance) -Blockchain, Ethereum, Smart Contracts

Introduction:

Decentralised Bank or dBank is a blockchain based bank built on DeFi which offers its customers a unique decentralised banking experience.

Customers can deposit digital currency (ETH) in the decentralised bank dBank and get interest upon withdrawal, all managed by robust smart contracts underneath!

DeFi Bank features:

  • Deposit (ETH)
  • Withdraw (ETH + Interest)
  • Lending (Wip)
  • Payoff loan (Wip)

Project Scope:

  • End to end Project setup using Truffle Framework.
  • Creating robust smart contracts using Solidity Programming.
  • Built unit tests to test smart contracts based on different cases.
  • Smart contracts deployment to blockchains like BSC, Ethereum.
  • Built DeFi Dapp UI for users to connect and interact with the smart contract.

Dependencies:

  • Node js (npm for backend packages)
  • Truffle Suite (Smart contract development, blockchain project setup)
  • Metamask extension (blockchain wallet for Web 3.0)

Quick question: What exactly is DeFi ?

Short Answer:

  • DeFi is an abbreviation of decentralised finance which generally refers to digital assets and financial smart contracts, protocols, and decentralised applications (DApps), which are built on Ethereum.
  • In simpler terms, its financial software built on blockchain that can be pieced together like Money Legos.

Smart contract:

  • Contains the main business logic of the dapp.
  • Immutable and executes on the (Ethereum) blockchain.
  • Reads and writes data (uses gas fee) on the blockchain.

DeFi bank Dapp requires following smart contracts:

1. DIT (Interest token — ERC-20)

2. DeFi bank core functionality

Features:

  • Dapp Built on robust smart contracts using Solidity programming
  • Users can deposit, withdraw Ethereum (ETH), borrow and payoff loans.
  • Test driven development (JS) for sanity checks and handle exceptions.
  • Async / await to handle JS promises.
  • App Frontend built on HTML, React JS, Web3 JS.
  • Leveraged React JS re-usable components in frontend.
  • Dapp deployed to binance smart chain and
  • Codebase available on GitHub (check Resources section).

Tools Utilised:

Ethereum, Solidity, Blockchain, Truffle framework, web3.js, Ganache, React.js, Metamask

Working:

Client-side web app talks to (Ethereum) Blockchain which then hooks up to the dBank and interest token smart contracts.

Users deposit (eth) into the dBank Dapp and withdraw from the dBank.

Project Development:

Backend:

  • Blockchain Setup: In-memory blockchain, Ganache
  • Smart Contracts (Solidity): DeFi Bank & ERC-20 Token
  • Truffle Framework

Frontend:

  • HTML, Css, JavaScript
  • Web3.JS
  • React.JS

Functionality:

Deposit / Withdraw:

  1. Users deposit Ether (ETH, the native asset of Ethereum blockchain) to the (DeFi) bank.
  2. Users withdraw the deposited ether and get an interest using an ERC-20 standard interest token created using smart contracts.

Interest token based on the ERC-20 open zeppelin contract standard. Interest token is minted by account with minter role in the smart contract hence checks are incorporated in smart contract to ensure the minting is performed only by minter role account (the decentralised bank in this case).

Keeping in view the decentralised nature of the project, the ownership (role) to mint tokens is transferred for to the Decentralised Bank (dBank) on smart contract deployment.

The dBank then mints tokens for user’s deposit which are given as interest at the time of withdrawal with the principal (ETH).

Note: Initial total supply of Interest tokens (DIT) is 0 (zero).

Withdraw:

When users withdraw, they get back:

a) The principal (ETH), and

b) Accumulated Interest through the ERC-20 interest token

I utilised block.timestamp property to calculate the elapsed time between deposit start and withdrawal (deposit end) and use that to calculate the interest earnings (tokens).

The ETH balance (principal) is sent back to the account using the Solidity provided msg.sender which tracks the account which actually initiated the transaction.

Since this Dapp supports only 1 deposit, we should update the blockchain data to reset the deposit balance for the account after the withdrawal transaction.

  • Hence, the smart contract is updated with code to reset the deposit (essentially the ETH deposit balance is reset to 0) &
  • The isDeposited Boolean is reset to False

Frontend Application

For the application to work as intended:

  • Dapp needs to connect to blockchain (where smart contracts are deployed). To achieve this, web3 JS library was used as the interface.
  • Browser needs to be connected to the blockchain (Metamask extension)

Essentially, DBank Dapp connects to the blockchain to read/write data using the underlying logic written on robust solidity smart contract.

REACT JS:

For the frontend, I made use of React JS extensively.

React JS is a library for building beautiful User Interfaces and work with reusable components within the application.

Utilised the following React Features:

  • React JS components for splitting into different parts, separation of concerns.
  • React JS bootstrap for Tabs UI for overall look and feel.

React JS state objects maintain application state data and act like tiny, un-persistent Databases.

dBank dapp uses react JS state object heavily to store data coming from the blockchain.

(For example, account balance, address, ether balance)

Loading Smart Contracts:

Web3 JS library is used to create JavaScript versions of the smart contracts using abi (application blockchain interface) imported from json objects and the deployed network address.

Events:

We emit events for Deposits and Withdrawal transactions.

Tests:

Tests built with Truffle test framework using Mocha Framework & Chai assertion library.

  • Basic tests for token & DeFi bank smart contracts deployment.
  • Minter role assertion test for DeFi bank smart contract.

Metamask:

Metamask is a browser extension, check the Metamask section below to learn more.

Essentially, Metamask connects to a blockchain node and then exposes this information to web3 JS library.

To use the Defi bank Dapp, browser needs to have Metamask installed. Hence, a window alert popup is added to inform users about this.

NetID:

NetID or NetworkID is an identifier which gives the decentralised application information about the connected network. This could be Ganache (localhost) , Rinkeby (Ethereum testnet) or mainnet etc. and can be seen in Metamask also.

Accounts:

Web3 JS allows us to fetch accounts data from the blockchain which essentially is the information about all of the accounts connected to the network.

In a localhost setup, Ganache provides us with 10 accounts to work with.

Deployment:

Dapp is successfully deployed to Binance Smart Chain (BSC) test net.

Smart contract deployment details:

Summary:

Through this article, we understood what is DeFi and also learnt in detail about a decentralised (DeFi) bank Dapp which lets users use banking products and services directly with the help of smart contracts code, blockchain and cryptography.

Resources:

DeFi bank Dapp deployed to GitHub pages: https://itznishant.github.io/dBank

DeFi bank Code base available on: https://github.com/itznishant/DeFi-Bank

Smart contract link (Binance Smart Chain testnet):

https://testnet.bscscan.com/address/0x634e390eD9F36449571f6f52Ee6A539C3b7490E5

Originally published at https://www.linkedin.com.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nishant Vemulakonda

Nishant Vemulakonda

Prolific Blockchain Smart Contract Developer, Experienced Data Scientist , forever Learner