Loading...
In this smartbook i will show you how to create the bank contrat with the help of the solidity programming language.So let's startrd
For our contract we mainly need two mapping first of all.mapping is nothing but it is the table in which you assign a integer or bool value to particular address.
mapping (address => uint256) public account_amount;
mapping (address => bool) public account_existed;
In banking contract we mainly contain the different function such
In this part we mainly show you how create the three main function such as a how to create the account of the user,how to deposit the ether into contract and withdraw the ether from user account.
so how to create the user account with solidity programming language.
function createaccount() public returns(bool)
{
require(account_existed[msg.sender]==false,"user does not existed");
account_existed[msg.sender]=true;
return true;
}
In the above function we created the function to create the user account then we require one condition that create the user and but it cannot be previously have account in the contract and if user is not existed then create it account and return the bool value at the last.
Then we have to create the function through which you have to deposit the ether within your contract
function deposit(uint256 amount) public payable returns(bool)
{
require(account_existed[msg.sender]==true,'user does not existed');
msg.value>0;
msg.value==amount;
account_amount[msg.sender]=account_amount[msg.sender]+amount;
return true;
}
So what the above function mainly contain in which we require that the user is alredy existed otherwise it cannot deposit the money into that contract then that deposited value must be greater than the 0 and amount that user deposited it go into the uint integer amount. Then we increament the value of the amount with the sg.sender and finally we return the true value.
After this we have to create the function called as a withdraw the ether from the account
function withdrawether(uint256 amount) public payable
{
require(account_existed[msg.sender]==true,'user does not existed');
account_amount[msg.sender]<=amount;
account_amount[msg.sender]=amount- account_amount[msg.sender];
}
In the above function we mainly created the function to withdraw the ether from the accout with already existed for that we assign the one require condition first then we want that amount should be less than the amount which is present on that account then we decrement the account balance of that particular user.
In this part we mainly covers the three main function called as create account ,deposit amount and withdraw the particular amount from the acount.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract BANK{
mapping (address => uint256) public account_amount;
mapping (address => bool) public account_existed;
function createaccount() public returns(bool)
{
require(account_existed[msg.sender]==false,"user does not existed");
account_existed[msg.sender]=true;
return true;
}
function deposit(uint256 amount) public payable returns(bool)
{
require(account_existed[msg.sender]==true,'user does not existed');
msg.value>0;
msg.value==amount;
account_amount[msg.sender]=account_amount[msg.sender]+amount;
return true;
}
function withdrawether(uint256 amount) public payable
{
require(account_existed[msg.sender]==true,'user does not existed');
account_amount[msg.sender]<=amount;
account_amount[msg.sender]=amount- account_amount[msg.sender];
}
}