Developer Way #5. Learn Leo together.
Step five. The practical lesson. We created two programs using ZKP.
Hi everyone, I continue to learn the Leo programming language. Today we are going to have a practical lesson. Now we are going to create 2 programs. If you haven’t read the previous 4 articles, I suggest you do it to understand how the Leo programming language works.
Bank.
To do this, we open Aleo Studio and create a new project. The first project is a bank. The concept is this. We invest money in the bank for 10 years. The interest rate is 5% a year. We use the program to calculate how much money we will get. We will need 2 variables: ‘capital’ and ‘rate’. The variable ‘rate’ will be public. They will be of type u32. The output will also contain a variable of type u32.
The variable ‘capital’ stores the amount of investment.
The variable “rate” stores the interest rate.
Let’s create a variable with the value “amount”. We will return it as a result of the program. At the beginning it will have the value of the variable capital. This variable is also of type u32.
Now we will create a loop that will add 5% per year 10 times. To do this we enter the following text.
If we want it to be less than 10 years, we can change that number, for example 5 years. Now we have to calculate the income for one year. To do this, we can do the following.
Thus, we add the annual income to the variable. Using the “for” loop, we do this as many times as necessary. Now, after executing the loop, we return the variable amount.
We need to add the inputs. To do this, we open the inputs folder. We need to enter data for two variables: rate and capital.
Let’s save the program and run it.
Everything works, we got the right result.
Simple token.
Let’s create a new project. Let’s call it simple_token. In this project we will make a transaction from one wallet to another. First we will create a new record. Let’s call it “token”. The record will contain these data: owner, gates and amount.
Now we need to create a Transition Function. It gets these inputs: a record with information about the token, the address where the token will arrive and the number of tokens. The output from the function is 2 “token” records.
Now we need to create a variable that will store information about how many tokens are left in this wallet. To do this, we subtract the transfer amount from the number of tokens in the wallet.
All we need to do is to create two variables and return them. The first variable counts the remainder. We create a variable called ‘remaining’, which is of type “token”. It has the following information: the owner and gates we get from the input data. The number of tokens is the difference.
And the second variable, it shows the recipient and the number of tokens that have arrived at that address.
Now we need to return these two variables.
Let’s type in the inputs. To do this, go to the inputs folder. Here we specify the data. We write the name of the function and specify the data that entered into it. For example, here the sender’s address is 100 tokens. The transfer amount is 30 tokens.
Addresses are a very important point with which I had problems. The address must be added to the input data, as well as to the program.json file. In order to generate an address we go to aleo.tools. Here we click the “generate” button.
We need an address and a private key. In the input we enter the address. In the program.json file we enter the address and the private key.
For the second address, we press the generate button again. As a result, we get the following input data.
Let’s save the file and run our program.
Everything is working correctly. There are 70 tokens left at the sender’s address, and 30 tokens were sent.
That’s all for today. If you like this article, subscribe to me to get a lot of useful and interesting content.
Resources.
My video on this topic: https://youtu.be/wGKI2NT35kY