Breaking down the Aleo example: Interest

Kaylej
3 min readMar 20, 2023

--

Let’s analyze the construction of a program that provides a toolkit for calculating interest for a fixed or limited number of iterations.

First, let’s take the simplest version:

The code
inputs

In this program, we will calculate what the return on a 25% deposit will be. We have “capital”, which is money, and “rate”, which is interest. Next, we will take a calendar year as the time frame.

Since this is a simple version of program execution, let’s hardcode the number of time periods. In line 5, we see that we are doing two iterations, i.e., we are counting two years of profit. When calculating, be sure to note that since we are working with numbers of type integer (Leo does not support float values), the resulting number is rounded down when divided.

Here we get:

A more complicated example:

assert here performs a check (and if it returns false, the program does not execute). This check is optional here, but the developers decided to keep it so as not to overload the server.

The difference in this example is that we can change the number of operations:

Let’s try to improve the program ourselves. Sometimes rounding down can be a problem when we are working with small amounts. So I propose to increase the amount by a factor of 1000 before the cycle, and to divide it back at the end. Then let’s check the execution of the old and new program with data: capital=10; rate=19; iterations=4.

The old program. The answer is printed on the right: 17.
New program. The answer on the right is: 20.

Let’s check with a calculator:

20.053%

Original version: https://medium.com/@kaylej/cf118cc31f53

--

--