From d31b707c4b5bd8707248af78129df6b6184f38ab Mon Sep 17 00:00:00 2001
From: Collin Chin <16715212+collinc97@users.noreply.github.com>
Date: Mon, 24 Jul 2023 10:33:41 -0700
Subject: [PATCH] Update leo examples (#2501)
* improve leo example run scripts
* add leo build warning message, reduce number of calls to snarkvm package open
* cargo clippy
* Update examples/token/run.sh
Co-authored-by: Eric McCarthy <7607035+bendyarm@users.noreply.github.com>
Signed-off-by: Collin Chin <16715212+collinc97@users.noreply.github.com>
* Update examples/token/run.sh
Co-authored-by: Eric McCarthy <7607035+bendyarm@users.noreply.github.com>
Signed-off-by: Collin Chin <16715212+collinc97@users.noreply.github.com>
* Update examples/token/run.sh
Co-authored-by: Eric McCarthy <7607035+bendyarm@users.noreply.github.com>
Signed-off-by: Collin Chin <16715212+collinc97@users.noreply.github.com>
* Update examples/token/run.sh
Co-authored-by: Eric McCarthy <7607035+bendyarm@users.noreply.github.com>
Signed-off-by: Collin Chin <16715212+collinc97@users.noreply.github.com>
* Update examples/token/run.sh
Co-authored-by: Eric McCarthy <7607035+bendyarm@users.noreply.github.com>
Signed-off-by: Collin Chin <16715212+collinc97@users.noreply.github.com>
* fix comments
* make auctioneer the starting private key in auction example
---------
Signed-off-by: Collin Chin <16715212+collinc97@users.noreply.github.com>
Co-authored-by: Eric McCarthy <7607035+bendyarm@users.noreply.github.com>
---
examples/auction/.env | 2 +-
examples/auction/README.md | 3 +-
examples/auction/run.sh | 8 ++
examples/basic_bank/README.md | 3 +-
examples/basic_bank/run.sh | 9 ++
examples/battleship/README.md | 4 +-
examples/battleship/run.sh | 7 +
examples/tictactoe/README.md | 3 +-
examples/tictactoe/run.sh | 7 +
examples/token/README.md | 3 +-
examples/token/run.sh | 239 ++++++++++++++++++++++++++++++++++
examples/vote/README.md | 3 +-
examples/vote/run.sh | 8 ++
leo/cli/cli.rs | 18 ++-
leo/cli/commands/build.rs | 13 +-
leo/cli/commands/clean.rs | 4 +-
16 files changed, 315 insertions(+), 19 deletions(-)
create mode 100755 examples/token/run.sh
diff --git a/examples/auction/.env b/examples/auction/.env
index ab44af07b5..8222b1f559 100644
--- a/examples/auction/.env
+++ b/examples/auction/.env
@@ -1,4 +1,4 @@
NETWORK=testnet3
-PRIVATE_KEY=APrivateKey1zkpG9Af9z5Ha4ejVyMCqVFXRKknSm8L1ELEwcc4htk9YhVK
+PRIVATE_KEY=APrivateKey1zkp5wvamYgK3WCAdpBQxZqQX8XnuN2u11Y6QprZTriVwZVc
diff --git a/examples/auction/README.md b/examples/auction/README.md
index 1daa6aff9f..16001a6196 100644
--- a/examples/auction/README.md
+++ b/examples/auction/README.md
@@ -1,5 +1,6 @@
-
+
+[//]: # ()
A first-price sealed-bid auction in Leo.
diff --git a/examples/auction/run.sh b/examples/auction/run.sh
index b1e2dd3145..560c345515 100755
--- a/examples/auction/run.sh
+++ b/examples/auction/run.sh
@@ -1,3 +1,11 @@
+#!/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 first bidder.
# Swap these into program.json, when running transactions as the first bidder.
# "private_key": "APrivateKey1zkpG9Af9z5Ha4ejVyMCqVFXRKknSm8L1ELEwcc4htk9YhVK"
diff --git a/examples/basic_bank/README.md b/examples/basic_bank/README.md
index a5cc843230..d84cf19285 100644
--- a/examples/basic_bank/README.md
+++ b/examples/basic_bank/README.md
@@ -1,5 +1,6 @@
-
+
+[//]: # ()
A simple-interest yielding bank account in Leo.
diff --git a/examples/basic_bank/run.sh b/examples/basic_bank/run.sh
index 4cbcf58220..e4894a008e 100755
--- a/examples/basic_bank/run.sh
+++ b/examples/basic_bank/run.sh
@@ -1,3 +1,11 @@
+#!/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",
@@ -199,3 +207,4 @@ echo "
###############################################################################
"
leo run withdraw aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a 50u64 1234u64 15u64 || exit
+
diff --git a/examples/battleship/README.md b/examples/battleship/README.md
index 604e695b38..e93fd9918e 100644
--- a/examples/battleship/README.md
+++ b/examples/battleship/README.md
@@ -1,4 +1,6 @@
-# Leo Battleship ๐ดโโ ๏ธ
+
+
+[//]: # ()
- [Summary](#summary)
- [Build](#how-to-build)
diff --git a/examples/battleship/run.sh b/examples/battleship/run.sh
index 1ecbd0fe93..8c8ef6b1c5 100755
--- a/examples/battleship/run.sh
+++ b/examples/battleship/run.sh
@@ -1,3 +1,10 @@
+#!/bin/bash
+# First check that Leo is installed.
+if ! command -v leo &> /dev/null
+then
+ echo "leo is not installed."
+ exit
+fi
# Follow along in the README.md for a detailed explanation of each step.
# 1: Initializing Player 1
diff --git a/examples/tictactoe/README.md b/examples/tictactoe/README.md
index b28412db66..47877d52b6 100644
--- a/examples/tictactoe/README.md
+++ b/examples/tictactoe/README.md
@@ -1,5 +1,6 @@
-
+
+[//]: # ()
A standard game of Tic-Tac-Toe in Leo.
diff --git a/examples/tictactoe/run.sh b/examples/tictactoe/run.sh
index ac6fca6fcd..0c57c04e4e 100755
--- a/examples/tictactoe/run.sh
+++ b/examples/tictactoe/run.sh
@@ -1,3 +1,10 @@
+#!/bin/bash
+# First check that Leo is installed.
+if ! command -v leo &> /dev/null
+then
+ echo "leo is not installed."
+ exit
+fi
# Create a new game.
echo "
###############################################################################
diff --git a/examples/token/README.md b/examples/token/README.md
index cd91e70678..a7cab97a63 100644
--- a/examples/token/README.md
+++ b/examples/token/README.md
@@ -1,5 +1,6 @@
-
+
+[//]: # ()
A transparent & shielded custom token in Leo.
diff --git a/examples/token/run.sh b/examples/token/run.sh
new file mode 100755
index 0000000000..45fdc91936
--- /dev/null
+++ b/examples/token/run.sh
@@ -0,0 +1,239 @@
+#!/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 Alice.
+# Swap these into program.json, when running transactions as the first bidder.
+# "private_key": "APrivateKey1zkp1w8PTxrRgGfAtfKUSq43iQyVbdQHfhGbiNPEg2LVSEXR",
+# "address": "aleo13ssze66adjjkt795z9u5wpq8h6kn0y2657726h4h3e3wfnez4vqsm3008q"
+
+# The private key and address of Bob.
+# Swap these into program.json, when running transactions as the second bidder.
+# "private_key": "APrivateKey1zkpFo72g7N9iFt3JzzeG8CqsS5doAiXyFvNCgk2oHvjRCzF"
+# "address": "aleo17vy26rpdhqx4598y5gp7nvaa9rk7tnvl6ufhvvf4calsrrqdaqyshdsf5z"
+
+# Swap in the private key of Alice.
+echo "
+NETWORK=testnet3
+PRIVATE_KEY=APrivateKey1zkp1w8PTxrRgGfAtfKUSq43iQyVbdQHfhGbiNPEg2LVSEXR
+" > .env
+
+# Publicly mint 100 tokens for Alice.
+echo "
+###############################################################################
+######## ########
+######## STEP 1: Publicly mint 100 tokens for Alice ########
+######## ########
+######## ----------------------------------------- ########
+######## | PUBLIC BALANCES | ########
+######## ----------------------------------------- ########
+######## ----------------------------------------- ########
+######## | Alice | 100 | ########
+######## ----------------------------------------- ########
+######## | Bob | 0 | ########
+######## ----------------------------------------- ########
+######## ########
+######## ----------------------------------------- ########
+######## | PRIVATE BALANCES | ########
+######## ----------------------------------------- ########
+######## ----------------------------------------- ########
+######## | Alice | 0 | ########
+######## ----------------------------------------- ########
+######## | Bob | 0 | ########
+######## ----------------------------------------- ########
+######## ########
+###############################################################################
+"
+leo run mint_public aleo13ssze66adjjkt795z9u5wpq8h6kn0y2657726h4h3e3wfnez4vqsm3008q 100u64
+
+# Swap in the private key of Bob.
+echo "
+NETWORK=testnet3
+PRIVATE_KEY=APrivateKey1zkpFo72g7N9iFt3JzzeG8CqsS5doAiXyFvNCgk2oHvjRCzF
+" > .env
+
+# Privately mint 100 tokens for Bob.
+echo "
+###############################################################################
+######## ########
+######## STEP 2: Privately mint 100 tokens for Bob ########
+######## ########
+######## ----------------------------------------- ########
+######## | PUBLIC BALANCES | ########
+######## ----------------------------------------- ########
+######## ----------------------------------------- ########
+######## | Alice | 100 | ########
+######## ----------------------------------------- ########
+######## | Bob | 0 | ########
+######## ----------------------------------------- ########
+######## ########
+######## ----------------------------------------- ########
+######## | PRIVATE BALANCES | ########
+######## ----------------------------------------- ########
+######## ----------------------------------------- ########
+######## | Alice | 0 | ########
+######## ----------------------------------------- ########
+######## | Bob | 100 | ########
+######## ----------------------------------------- ########
+######## ########
+###############################################################################
+"
+leo run mint_private aleo17vy26rpdhqx4598y5gp7nvaa9rk7tnvl6ufhvvf4calsrrqdaqyshdsf5z 100u64
+
+# Swap in the private key of Alice.
+echo "
+NETWORK=testnet3
+PRIVATE_KEY=APrivateKey1zkp1w8PTxrRgGfAtfKUSq43iQyVbdQHfhGbiNPEg2LVSEXR
+" > .env
+
+# Publicly transfer 10 tokens from Alice to Bob.
+echo "
+###############################################################################
+######## ########
+######## STEP 3: Publicly transfer 10 tokens from Alice to Bob ########
+######## ########
+######## ----------------------------------------- ########
+######## | PUBLIC BALANCES | ########
+######## ----------------------------------------- ########
+######## ----------------------------------------- ########
+######## | Alice | 90 | ########
+######## ----------------------------------------- ########
+######## | Bob | 10 | ########
+######## ----------------------------------------- ########
+######## ########
+######## ----------------------------------------- ########
+######## | PRIVATE BALANCES | ########
+######## ----------------------------------------- ########
+######## ----------------------------------------- ########
+######## | Alice | 0 | ########
+######## ----------------------------------------- ########
+######## | Bob | 100 | ########
+######## ----------------------------------------- ########
+######## ########
+###############################################################################
+"
+leo run transfer_public aleo17vy26rpdhqx4598y5gp7nvaa9rk7tnvl6ufhvvf4calsrrqdaqyshdsf5z 10u64
+
+# Swap in the private key of Bob.
+echo "
+NETWORK=testnet3
+PRIVATE_KEY=APrivateKey1zkpFo72g7N9iFt3JzzeG8CqsS5doAiXyFvNCgk2oHvjRCzF
+" > .env
+
+# Privately transfer 20 tokens from Bob to Alice.
+echo "
+###############################################################################
+######## ########
+######## STEP 4: Privately transfer 20 tokens from Bob to Alice ########
+######## ########
+######## ----------------------------------------- ########
+######## | PUBLIC BALANCES | ########
+######## ----------------------------------------- ########
+######## ----------------------------------------- ########
+######## | Alice | 90 | ########
+######## ----------------------------------------- ########
+######## | Bob | 10 | ########
+######## ----------------------------------------- ########
+######## ########
+######## ----------------------------------------- ########
+######## | PRIVATE BALANCES | ########
+######## ----------------------------------------- ########
+######## ----------------------------------------- ########
+######## | Alice | 20 | ########
+######## ----------------------------------------- ########
+######## | Bob | 80 | ########
+######## ----------------------------------------- ########
+######## ########
+###############################################################################
+"
+leo run transfer_private "{
+ owner: aleo17vy26rpdhqx4598y5gp7nvaa9rk7tnvl6ufhvvf4calsrrqdaqyshdsf5z.private,
+ amount: 100u64.private,
+ _nonce: 6586771265379155927089644749305420610382723873232320906747954786091923851913group.public
+ }" aleo13ssze66adjjkt795z9u5wpq8h6kn0y2657726h4h3e3wfnez4vqsm3008q 20u64
+
+# Swap in the private key of Alice.
+echo "
+NETWORK=testnet3
+PRIVATE_KEY=APrivateKey1zkp1w8PTxrRgGfAtfKUSq43iQyVbdQHfhGbiNPEg2LVSEXR
+" > .env
+
+# Convert 30 public tokens from Alice into 30 private tokens for Bob.
+echo "
+###############################################################################
+######## ########
+######## STEP 5: Convert 30 public tokens from Alice into 30 ########
+######## private tokens for Bob. ########
+######## ########
+######## ----------------------------------------- ########
+######## | PUBLIC BALANCES | ########
+######## ----------------------------------------- ########
+######## ----------------------------------------- ########
+######## | Alice | 60 | ########
+######## ----------------------------------------- ########
+######## | Bob | 10 | ########
+######## ----------------------------------------- ########
+######## ########
+######## ----------------------------------------- ########
+######## | PRIVATE BALANCES | ########
+######## ----------------------------------------- ########
+######## ----------------------------------------- ########
+######## | Alice | 20 | ########
+######## ----------------------------------------- ########
+######## | Bob | 110 | ########
+######## ----------------------------------------- ########
+######## ########
+###############################################################################
+"
+leo run transfer_public_to_private aleo17vy26rpdhqx4598y5gp7nvaa9rk7tnvl6ufhvvf4calsrrqdaqyshdsf5z 30u64
+
+# Swap in the private key of Bob.
+echo "
+NETWORK=testnet3
+PRIVATE_KEY=APrivateKey1zkpFo72g7N9iFt3JzzeG8CqsS5doAiXyFvNCgk2oHvjRCzF
+" > .env
+
+# Convert 40 private tokens from Bob into 40 public tokens for Alice.
+echo "
+###############################################################################
+######## ########
+######## STEP 6: Convert 40 private tokens from Bob into 40 ########
+######## public tokens for Alice. ########
+######## ########
+######## ----------------------------------------- ########
+######## | PUBLIC BALANCES | ########
+######## ----------------------------------------- ########
+######## ----------------------------------------- ########
+######## | Alice | 100 | ########
+######## ----------------------------------------- ########
+######## | Bob | 10 | ########
+######## ----------------------------------------- ########
+######## ########
+######## ----------------------------------------- ########
+######## | PRIVATE BALANCES | ########
+######## ----------------------------------------- ########
+######## ----------------------------------------- ########
+######## | Alice | 20 | ########
+######## ----------------------------------------- ########
+######## | Bob | 70 | ########
+######## ----------------------------------------- ########
+######## ########
+###############################################################################
+"
+leo run transfer_private_to_public "{
+ owner: aleo17vy26rpdhqx4598y5gp7nvaa9rk7tnvl6ufhvvf4calsrrqdaqyshdsf5z.private,
+ amount: 80u64.private,
+ _nonce: 1852830456042139988098466781381363679605019151318121788109768539956661608520group.public
+ }" aleo13ssze66adjjkt795z9u5wpq8h6kn0y2657726h4h3e3wfnez4vqsm3008q 40u64
+
+
+# Swap in the private key of Alice.
+# This is done to ensure that program.json is the same after every execution of ./run.sh.
+echo "
+NETWORK=testnet3
+PRIVATE_KEY=APrivateKey1zkp1w8PTxrRgGfAtfKUSq43iQyVbdQHfhGbiNPEg2LVSEXR
+" > .env
diff --git a/examples/vote/README.md b/examples/vote/README.md
index 1b470c54cd..18e3c6398f 100644
--- a/examples/vote/README.md
+++ b/examples/vote/README.md
@@ -1,5 +1,6 @@
-
+
+[//]: # ()
## Summary
diff --git a/examples/vote/run.sh b/examples/vote/run.sh
index 1e203161dc..2b8bec9398 100755
--- a/examples/vote/run.sh
+++ b/examples/vote/run.sh
@@ -1,3 +1,11 @@
+#!/bin/bash
+# First check that Leo is installed.
+if ! command -v leo &> /dev/null
+then
+ echo "leo is not installed."
+ exit
+fi
+
echo "
###############################################################################
######## ########
diff --git a/leo/cli/cli.rs b/leo/cli/cli.rs
index 943b5a4436..4a9a609eb6 100644
--- a/leo/cli/cli.rs
+++ b/leo/cli/cli.rs
@@ -18,6 +18,7 @@ use crate::cli::{commands::*, context::*, helpers::*};
use leo_errors::Result;
use clap::Parser;
+use colored::Colorize;
use std::{path::PathBuf, process::exit};
/// CLI Arguments entry point - includes global parameters and subcommands
@@ -114,7 +115,22 @@ pub fn run_with_args(cli: CLI) -> Result<()> {
match cli.command {
Commands::New { command } => command.try_execute(context),
- Commands::Build { command } => command.try_execute(context),
+ Commands::Build { command } => {
+ // Enter tracing span
+ let span = command.log_span();
+ let span = span.enter();
+
+ // Leo build is deprecated in version 1.9.0
+ tracing::info!(
+ "โ ๏ธ Attention - This command is deprecated. Use the {} command.\n",
+ "'run'".to_string().bold()
+ );
+
+ // Drop tracing span
+ drop(span);
+
+ command.try_execute(context)
+ }
Commands::Clean { command } => command.try_execute(context),
Commands::Run { command } => command.try_execute(context),
Commands::Execute { command } => command.try_execute(context),
diff --git a/leo/cli/commands/build.rs b/leo/cli/commands/build.rs
index 314cb99651..9eb7c18c6f 100644
--- a/leo/cli/commands/build.rs
+++ b/leo/cli/commands/build.rs
@@ -165,7 +165,9 @@ impl Command for Build {
None
};
- // Leo build is deprecated in version 1.9.0
+ // `Package::open` checks that the build directory and that `main.aleo` and all imported files are well-formed.
+ Package::::open(&build_directory).map_err(CliError::failed_to_execute_build)?;
+
// // Unset the Leo panic hook.
// let _ = std::panic::take_hook();
//
@@ -237,13 +239,6 @@ fn compile_leo_file(
.write_all(instructions.as_bytes())
.map_err(CliError::failed_to_load_instructions)?;
- // `Package::open` checks that the build directory and that `main.aleo` is well-formed.
- match Package::::open(build) {
- // Log the build as successful.
- Ok(_) => tracing::info!("Compiled '{}' into Aleo instructions", file_name),
- // Log the error.
- Err(e) => tracing::info!("{}", CliError::failed_to_execute_build(e)),
- };
-
+ tracing::info!("โ
Compiled '{}' into Aleo instructions", file_name);
Ok(symbol_table.structs)
}
diff --git a/leo/cli/commands/clean.rs b/leo/cli/commands/clean.rs
index e0dd97c708..014599e922 100644
--- a/leo/cli/commands/clean.rs
+++ b/leo/cli/commands/clean.rs
@@ -37,11 +37,11 @@ impl Command for Clean {
// Removes the outputs/ directory.
let outputs_path = OutputsDirectory::remove(&path)?;
- tracing::info!("cleaned the outputs directory {}", outputs_path.dimmed());
+ tracing::info!("๐งน Cleaned the outputs directory {}", outputs_path.dimmed());
// Removes the build/ directory.
let build_path = BuildDirectory::remove(&path)?;
- tracing::info!("cleaned the build directory {}", build_path.dimmed());
+ tracing::info!("๐งน Cleaned the build directory {}", build_path.dimmed());
Ok(())
}