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.
You might be wondering, well I think this point will be valid ones:
Deploy a smart contract on TON blockchain.
Learn a little bit more about FunC.
Learn that silly ideas or projects can help to become better at something.
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:
Users can deposit money on it.
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.
Users can withdraw their money from the bank.
As a measurement, users will have a maximum deposit of 10 tons, which at the current price is 17.704 USD.
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?
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.
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.
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
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.
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 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. This can be done consulting the getters method for the Telemint smart contract, 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 to a node that we deploy. Deploying a node is expensive, I'm poor.
Feel free to donate to this wallet 😏
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:
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