Dedup interest examples; update docs

This commit is contained in:
Pranav Gaddamadugu 2022-08-04 10:30:29 -07:00
parent 056dbc0606
commit 94f026bd52
8 changed files with 45 additions and 54 deletions

View File

@ -1,13 +1,22 @@
# interest.aleo
# Calculating Interest
This program calculates interest over a fixed number of periods.
This program provides utilities for calculating interest over a fixed or bounded number of periods.
We note that the Leo code is much more readable and writable than the
corresponding Aleo code, which can be seen in `build/main.aleo` after building.
## Building the Program
## Build Guide
To compile this Aleo program, run:
To compile this program, run:
```bash
aleo build
leo build
```
## Running the Program
To run this program, run:
```bash
leo run <function_name>
```
where `<function_name>` is one of the following:
* `fixed_period_interest`
* `bounded_period_interest`
Be sure to update `inputs/interest.in` with the desired inputs.

View File

@ -1,4 +1,9 @@
// The program input for interest/src/main.leo
[main]
[fixed_period_interest_accrued]
capital: u32 = 80u32;
rate: u32 = 5u32; // 5%
[bounded_period_interest_accrued]
capital: u32 = 80u32;
rate: u32 = 5u32; // 5%
periods: u8 = 10u8;

View File

@ -1,8 +1,26 @@
// The 'interest' main function.
function main(capital: u32, public rate: u32) -> u32 {
// This function calculates the interest accrued over ten periods for some `capital` and `rate`.
function fixed_period_interest(capital: u32, public rate: u32) -> u32 {
let amount: u32 = capital;
for i:u8 in 0u8..10u8 { // accrue for 10 periods
amount += amount * rate / 100u32; // round down
// Accrue for exactly 10 periods.
for i:u8 in 0u8..10u8 {
// Note that the added amount is rounded down.
amount += (amount * rate) / 100u32;
}
return amount;
}
// This function calculates the interest accrued over a variable number of periods (max 50) for some `capital` and `rate`.
function bounded_period_interest(capital: u32, public rate: u32, periods: u8) -> u32 {
console.assert(periods <= 50u8);
let amount: u32 = capital;
// Accrue for up to 50 periods.
for i:u8 in 0u8..50u8 {
if i < periods {
// Note that the added amount is rounded down.
amount += (amount * rate) / 100u32;
} // Skip the remaining periods.
}
return amount;
}

View File

@ -1,2 +0,0 @@
outputs/
build/

View File

@ -1,13 +0,0 @@
# interest2.aleo
This program calculates interest over a fixed number of periods.
We note that the Leo code is much more readable and writable than the
corresponding Aleo code, which can be seen in `build/main.aleo` after building.
## Build Guide
To compile this Aleo program, run:
```bash
aleo build
```

View File

@ -1,5 +0,0 @@
// The program input for interest2/src/main.leo
[main]
capital: u32 = 80u32;
rate: u32 = 5u32; // 5%
periods: u8 = 10u8;

View File

@ -1,10 +0,0 @@
{
"program": "interest2.aleo",
"version": "0.0.0",
"description": "",
"development": {
"private_key": "APrivateKey1zkp4i2UqV6HqAn8ZHY68qrWfwuapNZ86doD3xHYfcTGYNFf",
"address": "aleo1t0t2y04rw8evnmvxzlz4xqt4ydgr9k0yk9qltfk8w0a7yt9lvv9qxnzs7w"
},
"license": "MIT"
}

View File

@ -1,11 +0,0 @@
// The 'interest2' main function.
function main(capital: u32, public rate: u32, periods: u8) -> u32 {
console.assert(periods <= 50u8);
let amount: u32 = capital;
for i:u8 in 0u8..50u8 { // accrue for max 50 periods
if i < periods {
amount += amount * rate / 100u32; // round down
} // skip remaining periods
}
return amount;
}