Merge pull request #2079 from AleoHQ/example/broken_bank

Add `broken_bank` example.
This commit is contained in:
Collin Chin 2022-09-20 10:39:08 -07:00 committed by GitHub
commit 19e9744158
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 205 additions and 0 deletions

View File

@ -4,6 +4,17 @@
$LEO run main
)
# Build and run the broken_bank Leo program.
(
cd ./project/examples/broken_bank || exit
$LEO run issue
$LEO run deposit
$LEO run withdraw
chmod +x ./run.sh
./run.sh
)
# Build and run the bubblesort Leo program.
(
cd ./project/examples/bubblesort || exit

2
examples/broken_bank/.gitignore vendored Normal file
View File

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

View File

@ -0,0 +1,8 @@
# bank.aleo
## Build Guide
To compile this Aleo program, run:
```bash
aleo build
```

View File

@ -0,0 +1,20 @@
// The program input for broken_bank/src/main.leo
[issue]
owner: address = aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a;
amount: u64 = 1234u64;
[deposit]
token: Token = Token {
owner: aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a,
gates: 0u64,
amount: 1234u64,
_nonce: 0group,
};
amount: u64 = 321u64;
[withdraw]
recipient: address = aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a;
amount: u64 = 321u64;
rate: u64 = 1500u64;
periods: u64 = 10u64;

View File

@ -0,0 +1,10 @@
{
"program": "bank.aleo",
"version": "0.0.0",
"description": "",
"development": {
"private_key": "APrivateKey1zkpHtqVWT6fSHgUMNxsuVf7eaR6id2cj7TieKY1Z8CP5rCD",
"address": "aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a"
},
"license": "MIT"
}

79
examples/broken_bank/run.sh Executable file
View File

@ -0,0 +1,79 @@
#!/bin/bash
# First check that Leo is installed.
if ! command -v leo &> /dev/null
then
echo "leo is not installed."
exit
fi
# The private key and address of the bank.
# Swap these into program.json, when running transactions as the first bidder.
# "private_key": "APrivateKey1zkpHtqVWT6fSHgUMNxsuVf7eaR6id2cj7TieKY1Z8CP5rCD",
# "address": "aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a"
# The private key and address of the user.
# 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 program.json.
echo "{
\"program\": \"bank.aleo\",
\"version\": \"0.0.0\",
\"description\": \"\",
\"development\": {
\"private_key\": \"APrivateKey1zkpHtqVWT6fSHgUMNxsuVf7eaR6id2cj7TieKY1Z8CP5rCD\",
\"address\": \"aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a\"
},
\"license\": \"MIT\"
}" > program.json
# Have the bank issue 100 tokens to the user.
echo "
The bank is issuing 100 tokens to the user."
leo run issue aleo1zeklp6dd8e764spe74xez6f8w27dlua3w7hl4z2uln03re52egpsv46ngg 100u64;
# Swap in the private key and address of the user to program.json.
echo "{
\"program\": \"bank.aleo\",
\"version\": \"0.0.0\",
\"description\": \"\",
\"development\": {
\"private_key\": \"APrivateKey1zkp75cpr5NNQpVWc5mfsD9Uf2wg6XvHknf82iwB636q3rtc\",
\"address\": \"aleo1zeklp6dd8e764spe74xez6f8w27dlua3w7hl4z2uln03re52egpsv46ngg\"
},
\"license\": \"MIT\"
}" > program.json
# Have the user deposit 50 tokens into the bank.
echo "
The user is depositing 50 tokens into the bank."
leo run deposit "{
owner: aleo1zeklp6dd8e764spe74xez6f8w27dlua3w7hl4z2uln03re52egpsv46ngg.private,
gates: 0u64.private,
amount: 100u64.private,
_nonce: 4668394794828730542675887906815309351994017139223602571716627453741502624516group.public
}" 50u64
# Swap in the private key and address of the bank to program.json.
echo "{
\"program\": \"bank.aleo\",
\"version\": \"0.0.0\",
\"description\": \"\",
\"development\": {
\"private_key\": \"APrivateKey1zkpHtqVWT6fSHgUMNxsuVf7eaR6id2cj7TieKY1Z8CP5rCD\",
\"address\": \"aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a\"
},
\"license\": \"MIT\"
}" > program.json
# Have the bank withdraw all of the user's tokens with compound interest over 15 periods at 12.34%.
echo "
The bank is withdrawing the user's tokens after 75 periods."
leo run withdraw aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a 50u64 1234u64 15u64;

View File

@ -0,0 +1,75 @@
record Token {
owner: address,
gates: u64,
amount: u64,
}
mapping balances: field => u64;
@program
function issue(owner: address, amount: u64) -> Token {
console.assert_eq(self.caller, aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a);
return Token {
owner: owner,
gates: 0u64,
amount: amount,
};
}
@program
function deposit(token: Token, amount: u64) -> Token {
let difference: u64 = token.amount - amount;
let remaining: Token = Token {
owner: token.owner,
gates: token.gates,
amount: difference,
};
let hash: field = BHP256::hash(token.owner);
finalize(hash, amount);
return remaining;
}
finalize deposit(hash: field, amount: u64) {
increment(balances, hash, amount);
}
@program
function withdraw(recipient: address, amount: u64, rate: u64, periods: u64) -> Token {
console.assert_eq(self.caller, aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a);
let hash: field = BHP256::hash(recipient);
let total: u64 = calculate_interest(amount, rate, periods);
let token: Token = Token {
owner: recipient,
gates: 0u64,
amount: total,
};
finalize(hash, amount);
return token;
}
finalize withdraw(hash: field, amount: u64) {
decrement(balances, hash, amount);
}
function calculate_interest(principal: u64, rate: u64, periods: u64) -> u64 {
let amount: u64 = principal;
for i:u64 in 0u64..100u64 {
if i < periods {
amount += (amount * rate) / 10000u64;
}
}
return amount;
}