Smart contract bank in solidity

By akohad Oct20,2022

[ad_1]

  • Owner
  • Account create function
  • Deposit function
  • Withdraw function
  • Transfer function
  • Get balance
  • All balance
البلوكشين
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.16;
contract Bank{
address owner;
mapping(address=>uint) public account;
mapping(address=>bool) public userExist; //check if user exist
constructor(){
owner=msg.sender;
}
}
function createAccount()public returns(bool){
require(userExist[msg.sender]==false,"user existed");
userExist[msg.sender]=true;
return true;
}
function deposit(uint amountDeposit) public payable returns(bool){
amountDeposit>0;
account[msg.sender]=account[msg.sender]+amountDeposit;
return true;
}
function withdraw(uint amountWithdraw)public payable {
account[msg.sender]>=amountWithdraw;
account[msg.sender]=account[msg.sender]-amountWithdraw;
}
function transfer(address add,uint amountTransfer) public{
require(userExist[add]==true,"account not existed");
account[msg.sender]>=amountTransfer;
account[msg.sender]=account[msg.sender]-amountTransfer;account[add]=account[add]+amountTransfer;
}
function checkBalnce() public view returns(uint){
return account[msg.sender];
}
function getAllBalance() public view returns(uint){
return address(this).balance;
}
  • To check that the owner who run the function
  • This modifier use in getAllBalance function
modifier onlyOwner(){
require(msg.sender==owner,"you are not the owner");
_;
}
  • This modifier use in deposit -withdraw-transfer-checkbalance function
modifier isUserExisted(){
require(userExist[msg.sender]==true,"account not existed");
_;
}
  • use msg.value in deposit function
  • delete parameter in deposit function
function deposit() public payable returns(bool){
msg.value>0;
account[msg.sender]=account[msg.sender]+msg.value;
return true;
}

All code

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.16;
contract Bank{
address owner;
mapping(address=>uint) public account;
mapping(address=>bool) public userExist;
constructor(){
owner=msg.sender;
}
modifier onlyOwner(){
require(msg.sender==owner,"you are not the owner");
_;
}
modifier isUserExisted(){
require(userExist[msg.sender]==true,"account not existed");
_;
}
function createAccount()public returns(bool){
require(userExist[msg.sender]==false,"user existed");
userExist[msg.sender]=true;
return true;
}
function deposit() public payable isUserExisted returns(bool){
msg.value>0;
account[msg.sender]+=msg.value;
return true;
}
function withdraw(uint amountWithdraw)public payable isUserExisted {
account[msg.sender]>=amountWithdraw;
account[msg.sender]-=amountWithdraw;
}
function transfer(address add,uint amountTransfer) public isUserExisted{
require(userExist[add]==true,"account not existed");
account[msg.sender]>=amountTransfer;
account[msg.sender]-=amountTransfer;
account[add]+=amountTransfer;
}
function checkBalnce() public view isUserExisted returns(uint){
return account[msg.sender];
}
function getAllBalance() public view onlyOwner returns(uint){
return address(this).balance;
}
}

New to trading? Try crypto trading bots or copy trading

[ad_2]

Source link

By akohad

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *