Open In App

Decentralized Bank Smart Contract in Solidity

Last Updated : 27 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Solidity is an Object-oriented Programming Language for implementing Smart Contracts. It’s a High-Level Statically Typed Language like C. The solidity file has an extension .sol. The article focuses on discussing the decentralized bank smart contract in Solidity.

In the below example, the aim is to deploy a Smart Contract for Decentralized Bank System by using Solidity. In this contract, the Owner can add money (in the form of ETH, etc.) and he has certain functionalities like checking their Balance And Transferring to another Account. After performing the transaction owner can see the Balance.

Approach

  1. The first step is to deploy the smart contract using the Remix IDE. After writing the code compile the code. When it is successfully compiled then deploy it. 
  2. After deploying the contract a deployed Contract is obtained and then add the Amount as an owner. 
  3. If Owner has to transfer then add the receiver’s Address after that click on transfer. 

Implementation

Step 1: Open Remix IDE.

Step 2: Click on File Explorers and Create a new WORKSPACE (by Clicking on the + icon).

Create new workspace

 

Step 3: Click on File Explorers and select Solidity in the environment and create a new file Decetralized_Bank.sol by clicking on New File section.

Create Solidity file

 

Step 4: Add the solidity code in the Decentralized_Bank.sol file.

Solidity




// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
 
contract DBank
{
    address Owner;
 
    // We are creating the Mapping for the Adding & Transfer Amount in Account
    mapping(address=>uint)Balance;
 
    // constructor for the address of the Owner
    constructor()
    {
        Owner = msg.sender;
    }
 
    // function for adding the Ethereum in Account
    function addBalance(uint amount) public returns(uint)
    {
        // first we have to check the is it Owners Account or Not
        require(msg.sender == Owner, "Yes it is Owner Account !!");
        Balance[msg.sender] = Balance[msg.sender] + amount;
 
        return Balance[msg.sender];
    }
 
    // function for Get the Balance from an Account
    function getBalance() public view returns(uint)
    {
        return Balance[msg.sender];
    }
 
    // to transfer the Amount from Owner to Recipient
    function Transfer(address recipient, uint amount) public
    {
        // check the Self account is or not
        require(msg.sender != recipient, "Can't Transfer !! Self Account.");
 
        // check the owner has balance is available or not
        require(Balance[msg.sender] >= amount, "No, We Can't Transfer. Insufficient Balance !!");
 
        _transfer(msg.sender, recipient, amount);
    }
 
    function _transfer(address From, address To, uint Amount) private
    {
        Balance[From] = Balance[From] - Amount;
        Balance[To] = Balance[To] + Amount;
    }
 
}


Step 5: Build a smart contract that contains all the details of the Bank with the help of Remix IDE by clicking on the file name. also check the compiler version at the top.

 

Step 6: After successful compilation, to deploy the contract, select the Environment Remix VM (London) before clicking on the Deploy button. And the Contract Should be selected as Decentralized_Bank.sol

Compilation

 

Step 7: If the contract is successfully deployed then the deployed contract is obtained. Open the deployed contract and add the student details and transact it.

Output:

Output

 

1. Add Balance and Check Balance

Add the Balance in DBank Press on Transact. it will show in the all transaction tab (bottom size). after adding get the balance as the amount that are we adding

Add Balance

 

2. Transfer Balance and Check Balance

If the owner has to transfer money to another account then he required the address of the account and a sufficient amount of balance so that the transaction should be successful,

Transfer Balance

 

3. Transaction information

Transaction Information

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads