Skip to content

Commit

Permalink
workshop basic_bank add more expressive script
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Chen committed Oct 10, 2023
1 parent 531d291 commit 806efe0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 27 deletions.
18 changes: 9 additions & 9 deletions basic_bank/build/main.aleo
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ record Token:


mapping balances:
key left as field.public;
value right as u64.public;
key as field.public;
value as u64.public;

function issue:
input r0 as address.private;
Expand All @@ -22,9 +22,9 @@ function deposit:
input r1 as u64.private;
sub r0.amount r1 into r2;
cast r0.owner r2 into r3 as Token.record;
hash.bhp256 r0.owner into r4 as field; output r3 as Token.record;

finalize r4 r1;
hash.bhp256 r0.owner into r4 as field;
async deposit r4 r1 into r5; output r3 as Token.record;
output r5 as basic_bank.aleo/deposit.future;

finalize deposit:
input r0 as field.public;
Expand Down Expand Up @@ -547,11 +547,11 @@ function withdraw:
input r2 as u64.private;
input r3 as u64.private;
assert.eq self.caller aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a;
hash.bhp256 r0 into r4 as field; call calculate_interest r1 r2 r3 into r5;
hash.bhp256 r0 into r4 as field;
call calculate_interest r1 r2 r3 into r5;
cast r0 r5 into r6 as Token.record;
output r6 as Token.record;

finalize r4 r1;
async withdraw r4 r1 into r7; output r6 as Token.record;
output r7 as basic_bank.aleo/withdraw.future;

finalize withdraw:
input r0 as field.public;
Expand Down
89 changes: 71 additions & 18 deletions basic_bank/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ then
exit
fi

echo "
We will be playing the role of these two parties:
# The private key and address of the bank.
# Swap these into program.json, when running transactions as the first bidder.
# "private_key": "APrivateKey1zkpHtqVWT6fSHgUMNxsuVf7eaR6id2cj7TieKY1Z8CP5rCD",
Expand All @@ -15,18 +18,32 @@ fi
# Swap these into program.json, when running transactions as the second bidder.
# "private_key": "APrivateKey1zkp75cpr5NNQpVWc5mfsD9Uf2wg6XvHknf82iwB636q3rtc"
# "address": "aleo1zeklp6dd8e764spe74xez6f8w27dlua3w7hl4z2uln03re52egpsv46ngg"
"

# Swap in the private key and address of the bank to .env.
echo "
Let's make some bank transactions. We'll take the role of the bank and issue 100 tokens to the user. We swap the private key into .env and run the issue transition function. The inputs are simply the recipient of the issuance and the amount.
echo '
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkpHtqVWT6fSHgUMNxsuVf7eaR6id2cj7TieKY1Z8CP5rCD
' > .env
leo run issue aleo1zeklp6dd8e764spe74xez6f8w27dlua3w7hl4z2uln03re52egpsv46ngg 100u64 || exit
"

# swaps in the private key of the bank to .env
echo "
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkpHtqVWT6fSHgUMNxsuVf7eaR6id2cj7TieKY1Z8CP5rCD
" > .env

# Have the bank issue 100 tokens to the user.
# runs the issue transition with inputs (user address, amount)
leo run issue aleo1zeklp6dd8e764spe74xez6f8w27dlua3w7hl4z2uln03re52egpsv46ngg 100u64 || exit

echo "
###############################################################################
######## ########
######## STEP 1: Initialize 100 tokens for aleo1zeklp...v46ngg ########
######## Initialize 100 tokens for aleo1zeklp...v46ngg ########
######## ########
######## ----------------------------------------- ########
######## | ACTION | AMOUNT | ########
Expand Down Expand Up @@ -63,19 +80,43 @@ echo "
######## ########
###############################################################################
"
leo run issue aleo1zeklp6dd8e764spe74xez6f8w27dlua3w7hl4z2uln03re52egpsv46ngg 100u64 || exit

# Swap in the private key and address of the user to .env.
echo "
Now, let's have the user deposit 50 of their tokens with the bank. We'll take the role of the user and call the deposit function, having the user use the output record that was issued to them by the bank. The inputs are the output record from the issue transition and the amount the user wishes to deposit.
echo '
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkp75cpr5NNQpVWc5mfsD9Uf2wg6XvHknf82iwB636q3rtc
' > .env
leo run deposit '{
owner: aleo1zeklp6dd8e764spe74xez6f8w27dlua3w7hl4z2uln03re52egpsv46ngg.private,
amount: 100u64.private,
_nonce: 4668394794828730542675887906815309351994017139223602571716627453741502624516group.public
}' 50u64 || exit
"

# swaps in the private key of the user to .env
echo "
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkp75cpr5NNQpVWc5mfsD9Uf2wg6XvHknf82iwB636q3rtc
" > .env

# Have the user deposit 50 tokens into the bank.
# runs the deposit transition with inputs (private record with credits, amount)
leo run deposit "{
owner: aleo1zeklp6dd8e764spe74xez6f8w27dlua3w7hl4z2uln03re52egpsv46ngg.private,
amount: 100u64.private,
_nonce: 4668394794828730542675887906815309351994017139223602571716627453741502624516group.public
}" 50u64 || exit

echo "
You'll see that the output contains a new private record belonging to the user with 50 credits, and a public mapping with the bank that also shows 50 credits.
"

echo "
###############################################################################
######## ########
######## STEP 2: aleo1zeklp...v46ngg deposits 50 tokens ########
######## aleo1zeklp...v46ngg deposits 50 tokens ########
######## ########
######## ----------------------------------------- ########
######## | ACTION | AMOUNT | ########
Expand Down Expand Up @@ -112,16 +153,13 @@ echo "
######## ########
###############################################################################
"
leo run deposit "{
owner: aleo1zeklp6dd8e764spe74xez6f8w27dlua3w7hl4z2uln03re52egpsv46ngg.private,
amount: 100u64.private,
_nonce: 4668394794828730542675887906815309351994017139223602571716627453741502624516group.public
}" 50u64 || exit

echo "
With the 50 token deposit, let's say 15 periods of time pass with compounding interest at a rate of 12.34% on the principal amount.
###############################################################################
######## ########
######## STEP 3: Wait 15 periods ########
######## Wait 15 periods ########
######## ########
######## ----------------------------------------- ########
######## | ACTION | AMOUNT | ########
Expand Down Expand Up @@ -159,17 +197,34 @@ echo "
###############################################################################
"

# Swap in the private key and address of the bank to .env.
echo "
Now, let's have the bank withdraw all tokens after 15 periods. Let's switch to the bank role, and call the withdraw transition function. The inputs are the recipient's address, amount, rate, and periods.
echo '
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkpHtqVWT6fSHgUMNxsuVf7eaR6id2cj7TieKY1Z8CP5rCD
' > .env
leo run withdraw aleo1zeklp6dd8e764spe74xez6f8w27dlua3w7hl4z2uln03re52egpsv46ngg 50u64 1234u64 15u64 || exit
"
# swaps in the private key of the bank to .env
echo "
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkpHtqVWT6fSHgUMNxsuVf7eaR6id2cj7TieKY1Z8CP5rCD
" > .env

# Have the bank withdraw all of the user's tokens with compound interest over 15 periods at 12.34%.
# runs the withdraw transition with inputs (recipient, amount, rate, periods)
leo run withdraw aleo1zeklp6dd8e764spe74xez6f8w27dlua3w7hl4z2uln03re52egpsv46ngg 50u64 1234u64 15u64 || exit

echo "
You'll see here the withdrawal function creates a new private record for the user containing all 266 withdrawn tokens, and then sets the public balance of the bank back to 0.
"

echo "
###############################################################################
######## ########
######## STEP 4: Withdraw tokens of aleo1zeklp...v46ngg w/ interest ########
######## Withdraw tokens of aleo1zeklp...v46ngg w/ interest ########
######## ########
######## ----------------------------------------- ########
######## | ACTION | AMOUNT | ########
Expand Down Expand Up @@ -206,5 +261,3 @@ echo "
######## ########
###############################################################################
"
leo run withdraw aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a 50u64 1234u64 15u64 || exit

0 comments on commit 806efe0

Please sign in to comment.