![Building a bank with Solidity for beginners [Ethereum Blockchain Development Tutorial]](https://miro.medium.com/v2/resize:fit:700/1*cB4mQl3mRAS3z7vVXMZ62w.jpeg)
Building a bank with Solidity for beginners [Ethereum Blockchain Development Tutorial]
Weâll be developing a Personal Bank Account using Ethereum â where you can deposit money and earn interest. Guess what, this wonât be a toy product that wonât work in the real world. It will be something you can start deploying in the real world directly.
Content
1. Introduction
2. Remix
3. First contract, get contract balance
4. Compile, deploy & contract address
5. Add money to our contract đ°
6. Payable value, msg.value, msg.send, params, wei, âŚ
7. Introducing block interest
9. Withdraw & transfer
1. Introduction
In this article, you will learn all about the basics of developing applications on Ethereum. We donât require you to have any background on Ethereum development. If youâre a developer in any programming language and youâve heard the terms like Ethereum, Blockchain, Crypto Currencies etc. then you are good to go.
In this article, weâll be developing a Personal Bank Account using Ethereum â where you can deposit money and earn interest. Guess what, this wonât be a toy product that wonât work in the real world. It will be something you can start deploying in the real world directly.
You will quickly see how Ethereum & Solidity are so much easier to develop applications that involve transacting real money â unlike any traditional programming language youâd have seen.
Solidity has some constructs and data structures built into it that make building financial applications simple and secure.
By the end of this article, youâll be able to deploy a bank that can start transacting real money.
Banks are some of the most sophisticated softwares to build because of how much security is needed. Using solidity, youâll be able to write a secure bank that is as secure as the most secure bank on the planet with less than 30 lines of code.
Ethereum is the underlying blockchain infrastructure, and Solidity is a programming language to write applications.
At the end of this article, youâll know how to build contracts that are almost as good as contracts written by projects like PoolTogether and Compound â which are multi-billion-dollar projects right now.
2. Remix
Remix IDE allows developing, deploying and administering smart contracts for Ethereum like blockchains
We will be writing all our code in a new IDE called Remix. It sucks, but itâs the best editor for Solidity available out of the box. It is a browser-based IDE, so you donât have to install any software to get started.
Remix is a code editor for Solidity. It also runs a toy blockchain that weâll be using to deploy our first contract. Most of the steps are automated in Remix.
3. First contract (get contract balance)
đ Open code in Remix
This is the first contract.
- First of all, mention license type. If you want to make source code open source, then write a commented line as the first line of solidity code. // SPDX-License-Identifier: MIT
- Notice the first line âpragmaâ . This is basically a way to tell remix which version of solidity to use. Most programming languages encourage this, but arenât required. But in solidity, it is required â because, the development of solidity is so fast that a new version is released almost every week and things keep breaking. To be sure, the solidity compiler version should be mentioned on the top of the file.
- The next thing youâd notice is the keyword contract . Programs on solidity are called contracts. A contract keyword is exactly similar to the class keyword you would have encountered on js/py/java.
- Lastly , the function that weâve written in this class aka contract is to get account balance. It returns a uint â a slightly different syntax here.
This is one place where Solidity shines. A class can accept and store money natively â without having to integrate payment gateways like stripe or razorpay.
Every user and every program on Ethereum has an account. An account is identified by an address. It is unique for each account and looks something like â0x123123âŚâ. This account can hold money. The little program weâve written will also have an account by default. Whatever money we send to this account, the program is allowed to do whatever it wants with those funds. It can transfer that money to any other account, it can burn the money or it can just sit on that cash and do nothing.
Using solidity, we can write the logic of how the program will use the money in the account.
Weâve not sent any money to our contract (aka programâs) account yet. But in the next part, youâll see how we can write the logic to receive money and use those funds to build a smart bank account.
4. Compile, Deploy, Contract Address
Unlike JS/Py, solidity code needs to be compiled before it can be deployed or run.
đ On the left bar, look for the compile button and hit âcompile 1.solâ (or what ever name your contract has in Remix).
â ď¸ You might see some warnings, but thatâs OK for now.
Once the compilation is successful, weâll deploy it.
đ Tap on the âdeploy & transactionâ button on the left sidebar.
đ Before we actually deploy this contract, we should look at a few concepts that are new to solidity and Ethereum.
On the top, youâll see that there are a few accounts for you to choose from. Remix has automatically created 20 accounts for you and preloaded it with 100ETH money. These accounts are identified by addresses, as we had seen earlier. Remix allows you to change accounts by choosing one from the dropdown.
I want you to notice that the account youâve selected has 100ETH in it. This is because it costs some money to deploy a contract. So you need to select an account that actually has some Ethers in it. However these 100ETH are toy Ethers, available to use only within the Remix interface & only for testing.
đ Then, hit deploy.
There are various other options on this screen, that weâll ignore for now.
Ethereum is a computer owned by everyone. Anyone can run code on that computer. We have to deploy code to be able to run on Ethereum. Anyone in the world can start calling the functions in the smart contracts that youâve deployed immediately. You can even charge people for the same. Remix has an inbuilt toy version of Ethereum. Which is where we will be deploying first.
Now that youâve deployed it, youâll be able to start calling the functions.
On the right youâll see a tick in the console box, meaning that it has been deployed and the balance of the selected account is now 99.99⌠This is because every deployment in Ethereum costs money. Every function call costs money.
Now that the program has been deployed, an account has been created for this program where it can hold money. We can also start calling the functions weâve written.

đ To interact with the contract you have just deployed, you can tap on the arrow next to the contract address on the left bar under deployed contracts and hit the button that corresponds to the function that weâve written âgetContractBalanceâ.
Remix creates this UI with buttons and input boxes automatically, based on the content of the contract.
Each time you deploy a contract, it deploys a new instance. You cannot upgrade an already deployed contract by default (for now).

When you hit the button and call the function, youâll see the return in the output on the console on the bottom right.
đ Make sure you tap on the expansion arrow next to âDebugâ to see the entire log. You have to look for âdecoded_outputâ in these logs.
It is zero right now because weâve not sent any money to our contract.
Let us now send some money in!
5. Add money to contract

What would we have to do if we have to add some balance to a user? Weâll create a function that takes parameters address of the user whoâs balance we want to update and a value of by how much.
Thatâs exactly what this function does here.
đ Lets compile & deploy this.
đ Once itâs deployed tap on add balance

đ Give an address: hereâs a sample address for you 0x89Ce0f71D7387a580c6C07032f74f393a65d77F4
đ Give a value: say 1,000,000 to the call
đ Hit transact
đ After doing that tap the button getContractBalance.
Youâll notice the output says 1M. But where did this balance come from? Did we just pull out a Million dollars from thin air? If money has come into this account, it must have gotten deducted from somewhere else, right?
To make sure this is a valid transaction, we need to add the following checks:
- Is the user calling this function allowed to update the account identified by the address in the parameter?
What if someone sends calls this function with 0 as amount and overwriting a victim of all their life savings? - Does the user who is calling this function âaddBalanceâ even have the amount of money they are looking add to the balance of the said account?
- If yes (for the above), has the money been debited from some account before it is credited to the account of this smart contract?
This is a lot of mess, right? Ethereum lets you bypass all of these checks. Letâs see how to write this code better.

In this code, weâve added a function called addBalance. First thing to note is the keyword payable. Typically, you only send parameters to a function call â on other programming languages.
With Solidity, you can send parameters AND money. To tell the compiler that this function is allowed to accept money â you add the modifier âpayableâ. If you have the payable keyword, you can start depositing money to this contract using this function.
How much money is being sent in this function call is denoted by the variable msg.value.
Who sent this message is denoted by msg.sender.
Msg is a special object that Ethereum attaches with every function call thatâs made to a smart contract. Ethereum takes care of the user authentication under the hood.
For now, Remix takes care of the authentication on your behalf â however, we will need to explicitly authenticate when calling function production.
It authenticates who is calling the function and stores the address of the user thatâs calling the function in msg.sender. If there is money being sent to the function call, Ethereum also guarantees that the sender actually holds those many ethers, and when they are sending ethers to this function, the money has been actually been deducted from their account.
When this function is called, we update the balance of that user, and the total amount being held by this contract.
We use mapping to store what is the balance of which user. A mapping is the same as the dictionary or map data structure you might know from other programming languages. While defining the mapping, we defined the key and value data types.
The key over here is an âaddressâ of the user who will be depositing money to this smart account and the value is an integer (uint) denoting how much money has been deposited into this contract by this user.
We will be storing this information so that when they decide to withdraw it, we know how much money theyâre entitled to withdraw. We will also update the total amount being stored in this contract. Next, letâs compile and run a payable function.
6. Payable value, msg.value, msg.sender & more
Compile this program just like we did before & deploy it. Youâll see that now there are two functions in the deployed contract. The payable function has a different color from the one that we had written earlier. To call this function, we need to add some money. For that, pick an account from the drop down and pick how much money you want to send to the contract from this address in the box called âvalueâ , choose ethers as the denomination.

The number you entered in the âvalueâ text box, is the amount of money you are going to send.
Youâll see that there are 2 functions in this deployed contract. The function âaddBalanceâ is of a different color â signifying it is payable. We can send money to this function.
đ To do that find the input box âvalueâ above the deploy button.
â¨ď¸ Type in a value and select Ethers from the dropdown. This is how much money will be sent to the function call.
đ Now tap on the addBalance button in the contract.
đ When you see the tick in the console, youâll also see that the balance has reduced from the account you chose. This is also when msg.value gets set.
Ethereum takes care of the logistics like deducting the amount from your account and populating the msg object so that you as a developer donât have to worry about it.
đ If you hit get contract balance now, youâll see that it has now become non zero.
Youâll also notice that the number is much larger than the number of ethers you sent. That is because all transactions happen in the smallest possible denomination of ethers called wei. 1 wei = 10^-18 eth.
Now that we have money, how do we generate interest?
7. introducing block, interest
When withdrawing, we not only want to give the money back, we also want to add some interest.
So we need to store when the deposit was made along with how much. Hence weâll introduce a new mapping called deposit timestamp.
Now timestamps are tricky in solidity. There is no such thing as current timestamp. That is because, the functions you call on Ethereum/solidity arenât executed immediately. They are batched into a few thousands. Once there are a few thousand functioncalls called transactions are registered, all of them are run together. This batch of function calls is called a block.
The block in blockchain. A block is nothing but a set of all transactions that were a part of this batch. So we only get the timestamp of when the entire batch was run. i.e. block.timestamp = when were all the transactions in this block executed. That is why it takes between 10â20 seconds for a transaction to be completed on the real blockchain. Since we are using a toy blockchain, it appears to execute immediately.
Now that we have the timestamp stored of when the transaction was made to deposit money, weâll add a function called getBalance.
Here, the user address can be a parameter because weâll let anyone look up any other personâs balance â but only the actual owner of the balance can withdraw it.
Let us in this function not only return the principal they deposited, but also a simple interest. Note that the calculation looks a little complex because solidity doesnât have support for decimals (float or double) yet.
Now that the interest has been calculated, letâs withdraw!
8. Withdraw & transfer

Here we will allow for a withdrawal.
We need to look up what is the balance of the user who is requesting a withdrawal. So we will use msg.sender now just to make sure the withdrawal is being made by the person who is actually requesting the same.
Weâve already written how much money this user has including the simple interest. So weâll just use the same function to get how much money to send to the withdrawer.
We need to send money to an account identified by an address. This address we know from msg.sender. But weâll need to convert it into a payable address before we can send money. This is just a check to make sure we donât send money to undeserving addresses by mistake. Then we initiate a transfer.
đ Letâs deploy!
This time after deploying, letâs add money to the account. Check the balance a few times. We see that the balance is increasing, by whatever small amount.
đ° Now letâs withdraw.
âď¸ But you wouldnât have received any amount back into your account. Why do you think that is? The transaction failed.
đ Letâs check balance in contract : 10ETH
đ Letâs check the balance of the user : 10.0000001ETH because of interest.
Weâve added a secret â+1â to our get Balance, so that there is always some interest that is credited to your deposited amount, no matter how much time has passed! Free money!
âď¸ Where is this interest coming from? Are we pulling it out of thin air?
The contract can only send as much money it has in its coffers. Weâre trying to send more than it has.
đ So it fails.
We need to add money to the contract account itself. So letâs add that function, letâs compile and deploy again.

This time we will make sure we deposit money, fill up the account and only then withdraw money.
𤊠Awesome this time it works. You can see that the balance has increased in the account.
â You have successfully written your first contract!