DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Beaudry Jean Sauvenel
Beaudry Jean Sauvenel

Posted on

How create a USD Bank on Ethereum, BSC and Polygon blockchain?

How create a USD Bank on ETH, BSC, Polygon

The digital currency revolution is now spreading to the banking industry, as cryptocurrency begins to reshape the way people borrow and save.

Decentralized finance, or DeFi, describes an alternative financial ecosystem where consumers transfer, exchange, borrow, and lend cryptocurrency, independent of the traditional financial institutions and regulatory structures that have been built around banking services and the central bank.

In this article, I will show you how to start your own bank without the approval of your country's central bank. I have always been fascinated by how to change the banking system in my country (Haiti) where almost all banking services are reserved and concentrated to a group of people and the majority of the population does not have a bank account and does not even access to basic credit.

Let's started:

We're going to use the ERC20 standard from Ethereum.

ERC20?
Ethereum request for comment

An ERC20 token is a standard used for creating and issuing smart contracts on the Ethereum (BSC, Polygon, Avalanche, etc..) blockchain. Smart contracts can then be used to create smart property or tokenized assets that people can invest in. ERC stands for "Ethereum request for comment," and the ERC20 standard was implemented in 2015.

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

interface IERC20 {

    function totalSupply() external view returns (uint);

    function balanceOf(address account) external view returns (uint);

    function transfer(address recipient, uint amount) external returns (bool);

    function allowance(address owner, address spender) external view returns (uint);

    function approve(address spender, uint amount) external returns (bool);

    function transferFrom(
        address sender,
        address recipient,
        uint amount
    ) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint value);

    event Approval(address indexed owner, address indexed spender, uint value);

}
Enter fullscreen mode Exit fullscreen mode

The first part content the minimum you need as parent functions to create your bank smart contract.

Understand these functions above: https://ethereum.org/en/developers/tutorials/understand-the-erc-20-token-smart-contract/

Now it's time to code our smart contract:


contract DefiBank {

    // call it DefiBank
    string public name = "DefiBank";

    //Owner of the bank
    address public owner;

    // create 2 state variables
    address public usdc;      // The token you will accept
    address public bankToken; // The token that represents your bank that will be used to pay interest

    // create 1 array to add all your clients
    address[] public stakers;

    // create a 3 maps 
    mapping(address => uint) public stakingBalance; //Clients balance
    mapping(address => bool) public hasStaked; // Find out if this customer has created an account
    mapping(address => bool) public isStaking; // Find out if this customer is using their account

    ......
}
Enter fullscreen mode Exit fullscreen mode

New we will create the functions to interact with these variables.

......

    // In constructor pass in the address for USDC token,  set your custom bank token and the owner will be who will deploy the contract
    constructor() public {
        usdc = INPUT-USDC-TOKEN-ADDRESS-RELATE-TO-THE-BLOACKCHAIN;
        bankToken = INPUT-YOUR-TOKEN-ADDRESS-HERE;
        owner = msg.sender;
    }

......
Enter fullscreen mode Exit fullscreen mode
......

    // Change the ownership 
     function changeOwner(address newOwner) public {
    // require the permission of the current owner
        require(owner == msg.sender, β€œY”our are not the current owner);
        owner = newOwner;
    }

......
Enter fullscreen mode Exit fullscreen mode
.......

 // allow user to deposit usdc tokens in your contract

    function deposit(uint _amount) public {

        // Transfer usdc tokens to contract
        IERC20(usdc).transferFrom(msg.sender, address(this), _amount);

        // Update the account balance in map
        stakingBalance[msg.sender] = stakingBalance[msg.sender] + _amount;

        // Add user to stakers array if they haven't staked already
        if(!hasStaked[msg.sender]) {
            stakers.push(msg.sender);
        }

        // Update staking status to track
        isStaking[msg.sender] = true;
        hasStaked[msg.sender] = true;
    }

......
Enter fullscreen mode Exit fullscreen mode
......

    // allow user to withdraw total balance and withdraw USDC from the contract

     function withdrawTokens() public {

        // get the users staking balance in usdc
        uint balance = stakingBalance[msg.sender];

        // require the amount staked needs to be greater then 0
        require(balance > 0, "staking balance can not be 0");

        // transfer usdc tokens out of this contract to the msg.sender (client)
        IERC20(usdc).transfer(msg.sender, balance);

        // reset staking balance map to 0
        stakingBalance[msg.sender] = 0;

        // update the staking status
        isStaking[msg.sender] = false;

} 

......
Enter fullscreen mode Exit fullscreen mode
......

    // Send bank tokens as a reward for staking. You can change the way you need to give interest if you want

    function sendInterestToken() public {
    // require the permission of the current owner
        require(owner == msg.sender, "Your are not the current owner");

        for (uint i=0; i<stakers.length; i++) {
            address recipient = stakers[i];
            uint balance = stakingBalance[recipient];

    // if there is a balance transfer the same amount of bank tokens to the account that is staking as interest

            if(balance >0 ) {
                IERC20(bankToken).transfer(recipient, balance);

            }

        }

    }
......


}
Enter fullscreen mode Exit fullscreen mode

That's all you need to disrupt your country's banking system.

Remember to create the bank token separately before deploying the smart contract.

Thanks, hope this minimal smart contract will help a lot of you.

Complete code: https://gist.github.com/jsbeaudry/d61de7c738cc31018e210f3a96ba1508

Top comments (0)

Why You Need to Study Javascript Fundamentals

The harsh reality for JS Developers: If you don't study the fundamentals, you'll be just another β€œCoder”. Top learnings on how to get to the mid/senior level faster as a JavaScript developer by Dragos Nedelcu.