mirror of
https://github.com/AleoHQ/leo.git
synced 2024-11-11 04:49:15 +03:00
Dedup interest examples; update docs
This commit is contained in:
parent
056dbc0606
commit
94f026bd52
@ -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.
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
2
examples/interest2/.gitignore
vendored
2
examples/interest2/.gitignore
vendored
@ -1,2 +0,0 @@
|
||||
outputs/
|
||||
build/
|
@ -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
|
||||
```
|
@ -1,5 +0,0 @@
|
||||
// The program input for interest2/src/main.leo
|
||||
[main]
|
||||
capital: u32 = 80u32;
|
||||
rate: u32 = 5u32; // 5%
|
||||
periods: u8 = 10u8;
|
@ -1,10 +0,0 @@
|
||||
{
|
||||
"program": "interest2.aleo",
|
||||
"version": "0.0.0",
|
||||
"description": "",
|
||||
"development": {
|
||||
"private_key": "APrivateKey1zkp4i2UqV6HqAn8ZHY68qrWfwuapNZ86doD3xHYfcTGYNFf",
|
||||
"address": "aleo1t0t2y04rw8evnmvxzlz4xqt4ydgr9k0yk9qltfk8w0a7yt9lvv9qxnzs7w"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user