DeFi Bank (Decentralised Finance) -Blockchain, Ethereum, Smart Contracts
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)
- 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.
- 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 ?
- 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.
- 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
- 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).
Ethereum, Solidity, Blockchain, Truffle framework, web3.js, Ganache, React.js, Metamask
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.
- Blockchain Setup: In-memory blockchain, Ganache
- Smart Contracts (Solidity): DeFi Bank & ERC-20 Token
- Truffle Framework
Deposit / Withdraw:
- Users deposit Ether (ETH, the native asset of Ethereum blockchain) to the (DeFi) bank.
- 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).
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
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.
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:
We emit events for Deposits and Withdrawal transactions.
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 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 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.
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.
Dapp is successfully deployed to Binance Smart Chain (BSC) test net.
Smart contract deployment details:
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.
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):
Originally published at https://www.linkedin.com.