From 0f438360accd6a0d2601dc12ce04e65d77e65bb2 Mon Sep 17 00:00:00 2001 From: HansBhatia <78050086+HansBhatia@users.noreply.github.com> Date: Fri, 23 Jun 2023 15:33:29 -0400 Subject: [PATCH] feat(if-else): add if-else access control (#44) * feat(if-else): add if-else access control * fix(if_else): CI issue * fix: add pr review changes * fix: pr review comments * docs: add documentation to contract * docs(src): add readme file * docs(src): add contract desc * fix: terminology & add to SUMMARY --------- Co-authored-by: msaug --- listings/ch00-introduction/if_else/.gitignore | 1 + listings/ch00-introduction/if_else/Scarb.toml | 8 ++++ .../if_else/src/access_log.cairo | 37 +++++++++++++++++++ .../ch00-introduction/if_else/src/lib.cairo | 1 + src/SUMMARY.md | 1 + src/ch00-09-if_statements.md | 11 ++++++ 6 files changed, 59 insertions(+) create mode 100644 listings/ch00-introduction/if_else/.gitignore create mode 100644 listings/ch00-introduction/if_else/Scarb.toml create mode 100644 listings/ch00-introduction/if_else/src/access_log.cairo create mode 100644 listings/ch00-introduction/if_else/src/lib.cairo create mode 100644 src/ch00-09-if_statements.md diff --git a/listings/ch00-introduction/if_else/.gitignore b/listings/ch00-introduction/if_else/.gitignore new file mode 100644 index 00000000..eb5a316c --- /dev/null +++ b/listings/ch00-introduction/if_else/.gitignore @@ -0,0 +1 @@ +target diff --git a/listings/ch00-introduction/if_else/Scarb.toml b/listings/ch00-introduction/if_else/Scarb.toml new file mode 100644 index 00000000..f0ebf1bd --- /dev/null +++ b/listings/ch00-introduction/if_else/Scarb.toml @@ -0,0 +1,8 @@ +[package] +name = "if_else" +version = "0.1.0" + +[dependencies] +starknet = "1.1.0" + +[[target.starknet-contract]] \ No newline at end of file diff --git a/listings/ch00-introduction/if_else/src/access_log.cairo b/listings/ch00-introduction/if_else/src/access_log.cairo new file mode 100644 index 00000000..562a64b9 --- /dev/null +++ b/listings/ch00-introduction/if_else/src/access_log.cairo @@ -0,0 +1,37 @@ +#[contract] +mod SimpleAccessLog { + // Import the Starknet contract API. + use starknet::{get_caller_address, ContractAddress}; + + struct Storage { + // Add the owner to the contract storage. + _owner: ContractAddress, + } + + #[event] + // Add a welcome event to the contract. + fn WelcomeEvent(name: felt252) {} + + #[constructor] + fn constructor(address: ContractAddress) { + // Set the owner to be the address that deployed the contract. + _owner::write(address); + } + + // Add a function that checks if the caller is the owner. + fn is_owner() -> bool { + return get_caller_address() == _owner::read(); + } + + #[external] + fn log_access() { + // Add a conditional event that welcomes the owner or the user. + if (is_owner()) { + // We know since is_owner() == true, the owner called the function. Call the welcome event with 'Welcome Admin!'. + WelcomeEvent('Welcome Admin!'); + } else { + // We know since is_owner() == false, a normal user(not owner) called the function. Call the welcome event with 'Welcome User!'. + WelcomeEvent('Welcome User!'); + } + } +} diff --git a/listings/ch00-introduction/if_else/src/lib.cairo b/listings/ch00-introduction/if_else/src/lib.cairo new file mode 100644 index 00000000..527913e1 --- /dev/null +++ b/listings/ch00-introduction/if_else/src/lib.cairo @@ -0,0 +1 @@ +mod access_log; diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 5969884c..4a363092 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -10,6 +10,7 @@ Summary - [Mappings](./ch00-06-mappings.md) - [Constructors](./ch00-07-constructor.md) - [Function Attributes](./ch00-08-function_attributes.md) + - [If statements](./ch00-09-if_statements.md) - [Applications](./ch01-00-applications.md) - [Upgradeable Contract](./ch01-01-upgradeable_contract.md) diff --git a/src/ch00-09-if_statements.md b/src/ch00-09-if_statements.md new file mode 100644 index 00000000..f34ac2af --- /dev/null +++ b/src/ch00-09-if_statements.md @@ -0,0 +1,11 @@ +# If Statements + +Using an if expression allows you to execute certain code depending on conditions. Within the if statement provide a condition and then specify what you want to execute within the block of code. You can specify an else expression which is reached if the If condition is not met. + +Here's a simple example of a contract using if and else statements for role based access log: + +The contract will log a welcome message to the console depending on who called the function. If the owner called the function, the welcome message will be 'Welcome Admin!'. If a normal user called the function, the welcome message will be 'Welcome User!'. + +```rust +{{#include ../listings/ch00-introduction/if_else/src/access_log.cairo}} +```