How create a USD Bank on Ethereum, BSC and Polygon blockchain?
Tags: blockchain, smartcontract, ethereum, bank
Published: 2021-12-20 03:00:27


![How create a USD Bank on ETH, BSC, Polygon](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1cqx9ixokti6yvtbxb85.png)

**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);

}
```
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

    ......
}
```
 
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;
    }

......
```

```
......

    // 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;
    }

......
```

```
.......

 // 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;
    }

......
```

```
......

    // 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;

} 

......
```

```
......

    // 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);
                
            }
            
        }
        
    }
......


}
```

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