The Wayback Machine - https://web.archive.org/web/20241127033237/https://www.geeksforgeeks.org/decentralized-bank-smart-contract-in-solidity/
Open In App

Decentralized Bank Smart Contract in Solidity

Last Updated : 27 Feb, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

 



Similar Reads

How to use GANACHE Truffle Suite to Deploy a Smart Contract in Solidity (Blockchain)?
There are various processes involved in deploying a smart contract using Ganache and Truffle Suite: 1. Install Ganache first. Ganache is a personal blockchain for Ethereum development. You must first download and install it. It is available for download from https://www.trufflesuite.com/ganache, the official website. 2. Install Truffle Suite: The n
4 min read
Steps to Execute Solidity Smart Contract using Remix IDE
Remix IDE is generally used to compile and run Solidity smart contracts. Below are the steps for the compilation, execution, and debugging of the smart contract. Step 1: Open Remix IDE on any of your browsers, select on New File and click on Solidity to choose the environment. Step 2: Write the Smart contract in the code section, and click the Comp
1 min read
Creating a Smart Contract that Returns Address and Balance of Owner using Solidity
Problem: Create a smart contract named MyContract having a state variable as owner. Create a constructor to fetch the address of the owner from msg and hold it into the state variable owner. Also, create a function getBalance() to show the current balance of the owner. Solution: Every smart contract is owned by an address called as owner. A smart c
2 min read
What is Smart Contract in Solidity?
Solidity's code is encapsulated in contracts which means a contract in Solidity is a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain. A contract is a fundamental block of building an application on Ethereum. Now let's look at the code example for making smart contract : Example #
4 min read
How to use MetaMask to Deploy a Smart contract in Solidity (Blockchain)?
Smart contracts are self-executing contracts. They were first proposed by Nick Szabo in the 90s. They are set of rules and protocols which two parties agree upon and have to follow. One of the main features is that they are immutable once deployed on the blockchain. It is widely used in the Ethereum blockchain. The main language used for writing sm
3 min read
Solidity - Deploy a Smart Contract for Marks Management System
Solidity is a high-level language. The structure of smart contracts in solidity is very similar to the structure of classes in object-oriented languages. The solidity file has an extension .sol. What are Smart Contracts?Solidity’s code is encapsulated in contracts which means a contract in Solidity is a collection of code (its functions) and data (
3 min read
Make HTTP Request Using Your Solidity Smart Contract
Smart contracts are self-executing digital programs that are designed to automate the enforcement of the terms of a contract. Solidity is a programming language used to write these smart contracts on the Ethereum blockchain. One of the challenges with smart contracts is their inability to interact with the outside world, including making HTTP reque
10 min read
Unlock the Power of Solidity: Exploring the Essential Keywords for Smart Contract Development
Solidity is a smart contract programming language built solely to develop smart contracts that can be deployed on blockchains. C++ and JavaScript inspired it, an Object Oriented and High-Level language (HLL), that helps developers define the behaviour and rules for a Decentralized Application (Dapp). Smart contract facilitates autonomous, secure, a
13 min read
Solidity - Abstract Contract
Abstract contracts are contracts that have at least one function without its implementation or in the case when you don't provide arguments for all of the base contract constructors. Also in the case when we don't intend to create a contract directly we can consider the contract to be abstract. An instance of an abstract cannot be created. Abstract
3 min read
Interaction Between Smart Contracts with Solidity
Interaction between two smart contracts in Solidity means invoking functions of one contract from another contract so that we can use the content of one contract in another. For example, if you want to use contractA methods into contractB, so simply import the contractA inside contract B and create an instance of contractA then call methods using t
5 min read
Building a Simple Fixed Deposit Application Using Solidity Smart Contracts
Blockchain technology has revolutionized the way we handle financial transactions, and the world of decentralized finance (DeFi) is growing at an unprecedented rate. One of the most intriguing aspects of this financial evolution is the use of smart contracts, which are self-executing agreements with the power to automate and secure various financia
13 min read
What is Escrow Smart Contract?
Escrow is the third party that holds the asset(asset can be money, bond, or stocks) in the presence of two parties. Escrow will release the fund when certain conditions are met. For Example: "A" is a seller and wants to sell his car, and "B" is a buyer who wants to buy "A"'s car so they will contact Escrow "C"(an arbiter) which holds the asset unti
2 min read
What is Wallet Smart Contract?
Wallets are just like your bank account, through which we can receive money, send money, and can check the balance. Important terms:uint- unsigned integer.address- It is a unique set of the string provided for the transaction.msg.sender- Address of the current user.msg.value- Cost put by the current user.public- Visibility of the function.view- Thi
4 min read
Smart Contract to Store Employee Details
Solidity is a high-level language. The structure of smart contracts in solidity is very similar to the structure of classes in object-oriented languages. The solidity file has an extension .sol. What are Smart Contracts? Solidity’s code is encapsulated in contracts which means a contract in Solidity is a collection of code (its functions) and data
3 min read
Deploying Smart Contract on Test/Main Network Using Truffle
A Smart Contract is a computer program that directly and automatically controls the transfer of digital assets between the parties under certain conditions. A smart contract works in the same way as a traditional contract while also automatically enforcing the contract. Smart contracts are programs that execute exactly as they are set up(coded, pro
5 min read
Interacting With Ethereum Smart Contract Using Web3js
Ethereum is a cryptocurrency platform in the market just like Bitcoin. It is an open-source & decentralized blockchain featuring working on smart contracts. It has its own cryptocurrency known as ether. The smart contracts in Ethereum are written in solidity. TestRPC The Ethereum TestRPC is like a manual emulator for blockchain. It provides blo
4 min read
How to Simply Deploy a Smart Contract on Ethereum?
Smart contracts are blocks of code that reside on the blockchain. It is like an Ethereum account but there is a critical difference between an external account and a smart contract. Unlike a smart contract, an external account can connect to multiple Ethereum networks (Goerli testnet, mainnet, etc.) whereas a smart contract is only specific to one
7 min read
Setting Up Smart Contract Development Environment
A development environment is an environment in which all the resources and tools are available which are used to develop a program or software product. Here, an attempt to create a development environment that is a collection of the processes and tools that are used to develop smart contracts. There are mainly two choices available to set up the en
5 min read
Deploy a Smart Contract on Ethereum with Python, Truffle and web3py
This article discusses deploying a Smart Contract on Ethereum with Python, Truffle, and web3py. Prerequisite: In order to write a simple smart contract, deploy it to a personal Ethereum blockchain, and call the contract using Python3, install the below software on a PC: 1. Python3 v3.5.3 or later versions: Install python following the instructions
6 min read
How to Test a Smart Contract for Ethereum?
Public Blockchains like Ethereum are immutable, it is difficult to update the code of a smart contract once it has been deployed. To guarantee security, smart contracts must be tested before they are deployed to Mainnet. There are several strategies for testing contracts, but a test suite comprised of various tools and approaches is suitable for de
15+ min read
How to Test a Smart Contract with Remix?
The Remix IDE is an open-source development environment for building, testing, and deploying smart contracts on the Ethereum blockchain. One of the most notable features of Remix IDE is its integration with the Solidity programming language. Solidity is a contract-oriented language that is specifically designed for building smart contracts on Ether
7 min read
How to Calling Smart Contract Functions with Web3.js?
Have you wondered how smart contracts interact with the front end? So, the answer is the web3.js library. First, briefly discuss some terms related to web3.js and learn about them. Web3.js: Web3. js is a collection of libraries that allow users to interact between smart contracts and the front end. It is used to develop websites that can interact w
4 min read
Steps to Create, Test and Deploy Ethereum Smart Contract
Smart contracts are self-execution programs stored on a blockchain that are automatically executed when predefined conditions are met. They allow the participants on the blockchain to transact with each other without a trusted central authority. After creating a smart contract, the next step is to deploy the smart contract onto a blockchain for exe
7 min read
What is a NFT Smart Contract?
NFT Smart contracts play an important role in the blockchain space across many use cases. In particular, NFT smart contracts are becoming increasingly important as the metaverse and Web3 attract more interest. NFT smart contracts are specialized blockchain contracts that facilitate the creation, ownership, transfer, and management of NFTs. This art
8 min read
Integrating your Smart Contract with Frontend
In this article, we are going to learn how to connect a smart contract to the front end. To keep the process less complex and focus on the integration process, the smart contract in itself is kept simple. You can easily introduce multiple complex functions once you understand the basics. For this tutorial, we are simply putting a campaign on the lo
14 min read
Life Cycle of Smart Contract
Smart contracts are self-executing agreements with the terms directly written into code. Understanding their life cycle is essential for anyone involved in blockchain development. This life cycle includes key phases such as development, testing, deployment, interaction, and maintenance. Each phase plays a crucial role in ensuring the contract funct
7 min read
Decentralized Voting System using Blockchain
Blockchain is a technology that is rapidly gaining momentum in era of industry 4.0. With high security and transparency provisions, it is being widely used in supply chain management systems, healthcare, payments, business, IoT, voting systems, etc. Why do we need it? Current voting systems like ballot box voting or electronic voting suffer from va
4 min read
What is Decentralized Transactions?
Nowadays, the most common term used is a virtual currency, the currency that is only available virtually like bitcoins, ether. So, all these virtual currencies are mined by the miners and created a new block which then added to the existing blocks thus creating the blockchain. So, it's the rough idea of what blockchain is. Before discussing more ov
6 min read
What is Decentralized Voting Application (DApps)?
The Project Name is Decentralized Voting Application (DApps) which is built on Solidity Language. This Project showcases a lot of Solidity’s features. It implements a voting contract. Of course, the main problem of electronic voting is how to prevent to assign the duplicate Vote. Some Important Concepts are: 1. Contract: A contract is just like a c
4 min read
What are Decentralized Apps (dApps) in Blockchain?
Decentralized apps are digital applications or programs that are based on Blockchain and fundamentally different from normal applications. Unlike normal applications that run on centralized servers that belong to the company which owns them, dApps run on a decentralized peer-to-peer (P2P) network that is based on Blockchain. What are Decentralized
7 min read
three90RightbarBannerImg