Simulating a bank with a smart contract on TON blockchain. Part I.
Tags: blockchain, ton, func, smartcontract
Published: 2023-06-02 12:35:07

# Simulating a bank with a smart contract on TON blockchain. Part I.


![bank https://gealber.com/bank-smart-contract-ton-part-ismart contract](https://res.cloudinary.com/djjd0wi5i/image/upload/v1685708031/Blog/user_contract_xkt3dg.jpg)


Let's start by pointing out that this is **not a serious smart contract**, something that I'm doing with the goal of learning more about smart contracts and about FunC. Which is the language that I'm learning now. Please don't take this as reliable software or anything like that, okay? 

In the first part I'll explain the idea behind the smart contract I want to write, and in the second part I'll go through the code in detail. Stay tuned for the second part.


## What can I learn from this?

You might be wondering, well I think this point will be valid ones:

1. Deploy a smart contract on TON blockchain.

2. Learn a little bit more about FunC.

3. Learn that silly ideas or projects can help to become better at something.


## Goal

The goal in this article is to be able to write a smart contract that simulates part of the functionalities of a bank. Specifically this ones:

1. Users can **deposit** money on it.

2. Users can **borrow** money from it, the user could borrow **1%** of the total assets he possesses. I know, is an extremely cheap bank. I had a better idea, but unfortunately cannot do it at the moment. Checkout the **Improvements** section.

3. Users can **withdraw** their money from the bank. 

5. As a measurement, users will have a **maximum deposit of 10 tons**, which at the current price is 17.704 USD.


## Valid warning

Given that people normally don't read the articles in full, I warn you again

**THIS IS JUST A TUTORIAL FOR EDUCATIONAL PURPOSE** **ANY MONEY YOU PUT ON THE SMART CONTRACT, WILL BE ON RISK OF LOSING IT. GOT IT?**


## Points to analyze


### Incentive to deposit

Take a look again at the points that I have to implement, for this smart contract. You can notice that users **only have one** real incentive on depositing money on this smart contract. 

Given that one of the main benefits of putting your money on a bank is to be able to handle transactions through the bank application and to purchase goods online. In this case a user doesn't need that, given that if you have a non-custodial wallet, you don't need some central authority to **handle** transfers for you.

Another possible incentive, would be if I could return you a percent of interest, for trusting me on keeping your money safe. Let's say that if you deposit 10 TON I could give back to you in a month a 0.15%, so your total balance at the end of the month would be 10.015 TONs. Given that we are **not making staking** or anything that allows me to assure you that I'm going to be able to fulfill that. Then this incentive is not possible.

The only real incentive, which at the same time is the weak point in this smart contract, is the point where we are going to allow users to **borrow** money from the bank(smart contract). I'm going to explain that in detail now.


### Clear weak point and incentive at the same time

The main weak point in this smart contract is the second goal. The one that allows users to **borrow money** from the smart contract. Is very easy for a user to **deposit** 1 TON and then **borrow** 10% and repeat this process several times with different addresses. Until the bank goes into bankruptcy. A better implementation would require the user to provide some collateral assets before asking for a loan.


### Computing money to lend

Let's suppose a user has in balance 10 TON, the maximum he can have:

**total assets evaluation: 10 TON**

**amount users could borrow: 10 % of their total assets, which in this case is 1.**

**total assets he can withdraw: 10 TON(evaluation of assets in deposit) + 1 = 11 TONS**


### Bank benefits for lending

First the lending will have a maximum of 30 days, after those 30 days if the assets lended hasn't been returned, the bank will ban the user.

The bank will charge an interest of 10% for a period of one month. Returning to the previous example, if a user **borrowed** 1 TONs, he should return **1 + 0.1 = 1.1  TONs** at the end of the month. In case the user decides to not return the amount borrowed, then will be banned for life.


## Improvement

To improve a little bit this contract would be better if the bank asked the user for some collateral assets before allowing a loan. For example the bank could ask the user to provide an [Anonymous Number](https://fragment.com/numbers) as collateral. The problem with this solution, that was my original idea, is that the smart contract should be able to check that this NFT that was transferred to it, does actually belong to the collection of [Anonymous Numbers](https://verifier.ton.org/EQAOQdwdw8kGftJCSFgOErM1mBjYPe4DBPq8-AhF6vr9si5N). This can be done consulting the getters method for the [Telemint smart contract](https://github.com/TelegramMessenger/telemint), but this is only possible off-chain. The only way to communicate from the smart contract to outside of the chain, would be using [Message Logs](https://docs.ton.org/develop/smart-contracts/messages#types-of-messages) to a node that we deploy. Deploying a node is expensive, I'm poor.

_Feel free to donate to this wallet 😏_


```
EQC9n6aFb2oxQPMTPrHOnZDFcvvC2YLYIgBUms2yAB_LcAtv
```



## Code

Now that we have our plan completely described, let's implement this smart contract. Given the extensiveness of the code, I'll share with you a github repository so you can check it by yourself. In my next article, I'll go in detail about the code in the smart contract. In the meanwhile you can check the address for this smart contract here: 



```
EQCJyC8kTRnKZIx4K1f4fepy417p3isZhCVaZvxjJ8fcVEDr
```

[Tonscan link](https://tonscan.org/address/EQCJyC8kTRnKZIx4K1f4fepy417p3isZhCVaZvxjJ8fcVEDr)


Should I put another warning that this a tutorial, and people shouldn't put their money on this smart contract?🤔

**Repository with the code**: [Gealber/blog_recipes/bank_contract](https://github.com/Gealber/blog_recipes/tree/main/bank_contract)