Intro & Writing a smart contract: Part (1/4)
Wasim
Posted on November 28, 2021
What project are we building?
We are building a simple banking application that lets people deposit and withdraw ethers using a crypto wallet.
The objective of this project is to cover the basic concepts of developing a full stack decentralized application on the blockchain, which includes
- Writing a smart contract.
- Deploying the smart contract to a test network.
- Creating a frontend react application.
- Finally, hosting the application on heroku.
Why this project & blog?
A while ago, I wanted to build a similar project, that's when I noticed a lack of resources or a clear guide for the process. It took some digging and trial & errors to figure it out. So I though through this blog series I could help out someone understand these concepts a little easily.
This project is hosted here.
Link to Github Repo of the project here. This will serve as a reference for you, if at some point you are not able to follow the tutorial.
Writing Smart Contract
What Is A Smart Contract?
A "smart contract" is simply a program that runs on the Ethereum blockchain. It's a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain.
Smart contracts are popularly writing in a language called Solidity. You can learn more about the same here.
Smart contracts can be written and tested on Remix which is a open source tool that helps you write Solidity contracts straight from the browser.
Bank.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
contract Bank {
// Mapping of user address to their balance
mapping(address => uint256) private balance;
// Deposit function (payable)
function deposit() public payable {
require(msg.value > 0);
if (balance[msg.sender] > 0) {
balance[msg.sender] = balance[msg.sender] + msg.value;
}else{
balance[msg.sender] = msg.value;
}
}
// Withdraw function (payable)
function withdraw(uint256 amount) public payable {
address payable client = msg.sender;
require(balance[msg.sender] >= amount);
client.transfer(amount);
balance[msg.sender] = balance[msg.sender] - amount;
}
// Check balance function (view)
function getBalance() public view returns(uint256){
return (balance[msg.sender]);
}
}
Let's understand what all these data structures and functions are for and what do they do.
mapping(address => uint256) private balance;
Mapping balance
is used to keep track of the balance of different users by their address.
function deposit() public payable {
require(msg.value > 0);
if (balance[msg.sender] > 0) {
balance[msg.sender] = balance[msg.sender] + msg.value;
}else{
balance[msg.sender] = msg.value;
}
}
deposit
function as the name suggests, is a function used to deposit amount. It checks first if the value (the amount to be deposited) is greater than 0. If affirmative, it updates the users balance.
function withdraw(uint256 amount) public payable {
address payable client = msg.sender;
require(balance[msg.sender] >= amount);
client.transfer(amount);
balance[msg.sender] = balance[msg.sender] - amount;
}
withdraw
function is for users to withdraw their money, it checks if the user has sufficient funds, if yes the amount is transferred to the user's wallet.
function getBalance() public view returns(uint256){
return (balance[msg.sender]);
}
getBalance
function is used to get the account balance of the caller.
This smart contract can be compiled, tested and deployed on Remix IDE. Here is the documentation for reference.
In the next part, we'll see how to create a local project and deploy our smart contract to a test network. Click here.
Posted on November 28, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.