Basic Banking Smart Contract Creation

Creating a banking smart contract with basic features

solidity

Aniket Savji

@ankasaw99
1549 0 2

Banking Smart Contract


This smart contract will have all basic functionalities like ,

  • Account Creation
  • Deposite Amount
  • Withdraw amount
  • Transfer Amount
  • Send Amount to wallet

First need to add solidity compiler version

// SPDX-License-Identifier: MIT
pragma solidity  >=0.4.22 <0.7.0;

Then creating Banking contract ,

contract banking{
...
}

now let's create variables or objects

  mapping(address=>uint) public userAccount;
  mapping(address=>bool) public userExists;

Here userAccount will contain amount for each registered acccount and userExists for account restrictions 

 

Banking Functions

Now lets Create functions for each mentioned operations ,

1. createAcc() functions :

  function createAcc() public payable returns(string memory){
      require(userExists[msg.sender]==false,'Account Already Created');
      if(msg.value==0){
          userAccount[msg.sender]=0;
          userExists[msg.sender]=true;
          return 'account created';
      }
      require(userExists[msg.sender]==false,'account already created');
      userAccount[msg.sender] = msg.value;
      userExists[msg.sender] = true;
      return 'account created';
  }

Here we create user account using boolean method by making userExists mapping true after using createAcc() function 

 

2. deposit() function :

  function deposit() public payable returns(string memory){
      require(userExists[msg.sender]==true, 'Account is not created');
      require(msg.value>0, 'Value for deposit is Zero');
      userAccount[msg.sender]=userAccount[msg.sender]+msg.value;
      return 'Deposited Succesfully';
  }

With the help of userExists mapping we are only allowing registered users to deposit into our Smart Contract Bank .

 

3. withdraw(uint amount) function :

  function withdraw(uint amount) public payable returns(string memory){
      require(userAccount[msg.sender]>amount, 'insufficeint balance in Bank account');
      require(userExists[msg.sender]==true, 'Account is not created');
      require(amount>0, 'Enter non-zero value for withdrawal');
      userAccount[msg.sender]=userAccount[msg.sender]-amount;
      msg.sender.transfer(amount);
      return 'withdrawal Succesful';
  }

 

4. TransferAmount() function :

  function TransferAmount(address payable userAddress, uint amount) public returns(string memory){
      require(userAccount[msg.sender]>amount, 'insufficeint balance in Bank account');
      require(userExists[msg.sender]==true, 'Account is not created');
      require(userExists[userAddress]==true, 'to Transfer account does not exists in bank accounts ');
      require(amount>0, 'Enter non-zero value for sending');
      userAccount[msg.sender]=userAccount[msg.sender]-amount;
      userAccount[userAddress]=userAccount[userAddress]+amount;
      return 'transfer succesfully';
  }

Here TransferAmount function transfers amount from one account to other account in the bank only and both users must have created account on the bank to use this function

5. sendAmount() function :

  function sendEther(address payable toAddress , uint256 amount) public payable returns(string memory){
      require(amount>0, 'Enter non-zero value for withdrawal');
      require(userExists[msg.sender]==true, 'Account is not created');
      require(userAccount[msg.sender]>amount, 'insufficeint balance in Bank account');
      userAccount[msg.sender]=userAccount[msg.sender]-amount;
      toAddress.transfer(amount);
      return 'transfer success';
  }

Here sender's amount will be transfered from account in the bank to othe receiver's wallet 

So this is our simple banking smart contract and here is the full code ,

// SPDX-License-Identifier: MIT
pragma solidity  >=0.4.22 <0.7.0;

contract banking{
  mapping(address=>uint) public userAccount;
  mapping(address=>bool) public userExists;

  function createAcc() public payable returns(string memory){
      require(userExists[msg.sender]==false,'Account Already Created');
      if(msg.value==0){
          userAccount[msg.sender]=0;
          userExists[msg.sender]=true;
          return 'account created';
      }
      require(userExists[msg.sender]==false,'account already created');
      userAccount[msg.sender] = msg.value;
      userExists[msg.sender] = true;
      return 'account created';
  }
  
  function deposit() public payable returns(string memory){
      require(userExists[msg.sender]==true, 'Account is not created');
      require(msg.value>0, 'Value for deposit is Zero');
      userAccount[msg.sender]=userAccount[msg.sender]+msg.value;
      return 'Deposited Succesfully';
  }
  
  function withdraw(uint amount) public payable returns(string memory){
      require(userAccount[msg.sender]>amount, 'insufficeint balance in Bank account');
      require(userExists[msg.sender]==true, 'Account is not created');
      require(amount>0, 'Enter non-zero value for withdrawal');
      userAccount[msg.sender]=userAccount[msg.sender]-amount;
      msg.sender.transfer(amount);
      return 'withdrawal Succesful';
  }
 
  function TransferAmount(address payable userAddress, uint amount) public returns(string memory){
      require(userAccount[msg.sender]>amount, 'insufficeint balance in Bank account');
      require(userExists[msg.sender]==true, 'Account is not created');
      require(userExists[userAddress]==true, 'to Transfer account does not exists in bank accounts ');
      require(amount>0, 'Enter non-zero value for sending');
      userAccount[msg.sender]=userAccount[msg.sender]-amount;
      userAccount[userAddress]=userAccount[userAddress]+amount;
      return 'transfer succesfully';
  }
  
  function sendAmount(address payable toAddress , uint256 amount) public payable returns(string memory){
      require(amount>0, 'Enter non-zero value for withdrawal');
      require(userExists[msg.sender]==true, 'Account is not created');
      require(userAccount[msg.sender]>amount, 'insufficeint balance in Bank account');
      userAccount[msg.sender]=userAccount[msg.sender]-amount;
      toAddress.transfer(amount);
      return 'transfer success';
  }
  
  function userAccountBalance() public view returns(uint){
      return userAccount[msg.sender];
  }
  
  function accountExist() public view returns(bool){
      return userExists[msg.sender];
  }
  
}

 

 

 


More SmartBooks like this