leo/examples/basic_bank
Collin Chin d83c4be330
Implement Mapping::get_or_use (#2413)
* get_or_init -> get_or_use

* use mapping get_or_use in examples

* fix codegen for Mapping::get_or_use

* fix tests for Mapping::get_or_use

* fix warnings, update snarvm rev

* bump circle rust img v1.70

* fix codegen

* update snarkvm rev and regen tests

* Update compiler/parser/src/parser/expression.rs

Co-authored-by: d0cd <pranavsaig@gmail.com>
Signed-off-by: Collin Chin <16715212+collinc97@users.noreply.github.com>

* Delete program.json

Signed-off-by: Collin Chin <16715212+collinc97@users.noreply.github.com>

* Delete program.json

Signed-off-by: Collin Chin <16715212+collinc97@users.noreply.github.com>

---------

Signed-off-by: Collin Chin <16715212+collinc97@users.noreply.github.com>
Co-authored-by: d0cd <pranavsaig@gmail.com>
2023-06-13 12:09:33 -07:00
..
inputs [Update] snarkvm 0.11.0 (#2377) 2023-05-10 21:32:37 -07:00
src Implement Mapping::get_or_use (#2413) 2023-06-13 12:09:33 -07:00
.gitignore unify leo examples with workshop, run all examples in circleci 2022-09-21 15:59:33 -07:00
program.json unify leo examples with workshop, run all examples in circleci 2022-09-21 15:59:33 -07:00
README.md Rename console.assert* to assert*. (#2205) 2022-12-06 21:56:48 -08:00
run.sh [Update] snarkvm 0.11.0 (#2377) 2023-05-10 21:32:37 -07:00

workshop/basic_bank

A simple-interest yielding bank account in Leo.

Summary

This program implements a bank that issues tokens to users and allows users to deposit tokens to accrue simple interest on their deposits.

User Flow

  1. The bank issues users tokens via the issue function.
  2. A user deposits tokens via the deposit function.
  3. Upon a user's request to withdraw, the bank calculates the appropriate amount of compound interest and pays the user the principal and interest via the withdraw function.

Note that the program can be easily extended to include addition features such as a transfer function, which would allow users to transfer tokens to other users.

Bugs

You may have already guessed that this program has a few bugs. We list some of them below:

  • withdraw can only be invoked by the bank. A malicious bank could lock users' tokens by not invoking withdraw.
  • withdraw fails if the sum of the interest and principal is greater than the user's balance.
  • User's can increase their principal by depositing tokens multiple times, including immediately before withdrawl.
  • Integer division rounds down; if the calculated interest is too small, then it will be rounded down to zero.

Can you find any others?

Language Features and Concepts

  • record declarations
  • assert_eq
  • core functions, e.g. BHP256::hash
  • record ownership
  • loops and bounded iteration
  • mappings
  • finalize

Running the Program

Leo provides users with a command line interface for compiling and running Leo programs. Users may either specify input values via the command line or provide an input file in inputs/.

Configuring Accounts

The program.json file contains a private key and address. This is the account that will be used to sign transactions and is checked for record ownership. When executing programs as different parties, be sure to set the private_key and address fields in program.json to the appropriate values.

See ./run.sh for an example of how to run the program as different parties.

The Aleo SDK provides a command line interface for generating new accounts. To generate a new account, run

leo account new

Providing inputs via the command line.

  1. Run
leo run <function_name> <input_1> <input_2> ...

See ./run.sh for an example.

Using an input file.

  1. Modify inputs/auction.in with the desired inputs.
  2. Run
leo run <function_name>

For example,

leo run issue
leo run deposit
leo run withdraw