From d45cee16aabfb480d4729248ddb6dd8d02e309fe Mon Sep 17 00:00:00 2001 From: Hans Bhatia Date: Thu, 22 Jun 2023 00:00:09 -0400 Subject: [PATCH 1/8] feat(if-else): add if-else access control --- listings/ch00-introduction/if_else/.gitignore | 1 + listings/ch00-introduction/if_else/Scarb.toml | 8 +++++++ .../if_else/src/access_control.cairo | 21 +++++++++++++++++++ .../ch00-introduction/if_else/src/lib.cairo | 1 + 4 files changed, 31 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_control.cairo create mode 100644 listings/ch00-introduction/if_else/src/lib.cairo 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_control.cairo b/listings/ch00-introduction/if_else/src/access_control.cairo new file mode 100644 index 00000000..0526c621 --- /dev/null +++ b/listings/ch00-introduction/if_else/src/access_control.cairo @@ -0,0 +1,21 @@ +#[contract] +mod SimpleAccessControl { + use starknet::get_caller_address; + + struct Storage { + owner: ContractAddress, + } + + #[constructor] + fn constructor(_name: felt252, _address: ContractAddress) { + names::write(_address, _name); + } + + #[external] + fn onlyOwner() -> bool { + if (get_caller_address() == Storage.owner) { + return true; + } + return false; + } +} 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..a29fcd6c --- /dev/null +++ b/listings/ch00-introduction/if_else/src/lib.cairo @@ -0,0 +1 @@ +mod access_control; From 4ca2eac30485bff75df730689550f1bd250c2041 Mon Sep 17 00:00:00 2001 From: Hans Bhatia Date: Thu, 22 Jun 2023 18:07:02 -0400 Subject: [PATCH 2/8] fix(if_else): CI issue --- .../ch00-introduction/if_else/src/access_control.cairo | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/listings/ch00-introduction/if_else/src/access_control.cairo b/listings/ch00-introduction/if_else/src/access_control.cairo index 0526c621..ed263024 100644 --- a/listings/ch00-introduction/if_else/src/access_control.cairo +++ b/listings/ch00-introduction/if_else/src/access_control.cairo @@ -1,19 +1,21 @@ +use starknet::ContractAddress; + #[contract] mod SimpleAccessControl { use starknet::get_caller_address; struct Storage { - owner: ContractAddress, + owner: starknet::ContractAddress, } #[constructor] - fn constructor(_name: felt252, _address: ContractAddress) { - names::write(_address, _name); + fn constructor(_address: starknet::ContractAddress) { + owner::write(_address); } #[external] fn onlyOwner() -> bool { - if (get_caller_address() == Storage.owner) { + if (get_caller_address() == owner::read()) { return true; } return false; From 0782ff787e4924dfc9a2c2d678bb0c1370137ecd Mon Sep 17 00:00:00 2001 From: Hans Bhatia Date: Fri, 23 Jun 2023 13:42:35 -0400 Subject: [PATCH 3/8] fix: add pr review changes --- .../if_else/src/access_control.cairo | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/listings/ch00-introduction/if_else/src/access_control.cairo b/listings/ch00-introduction/if_else/src/access_control.cairo index ed263024..767a7579 100644 --- a/listings/ch00-introduction/if_else/src/access_control.cairo +++ b/listings/ch00-introduction/if_else/src/access_control.cairo @@ -1,23 +1,30 @@ -use starknet::ContractAddress; - #[contract] mod SimpleAccessControl { - use starknet::get_caller_address; + use starknet::{get_caller_address,ContractAddress}; struct Storage { - owner: starknet::ContractAddress, + owner: ContractAddress, } + #[event] + fn welcomeEvent(name: felt252) {} + #[constructor] - fn constructor(_address: starknet::ContractAddress) { + fn constructor(_address: ContractAddress) { owner::write(_address); } + #[internal] + fn only_owner() -> bool { + return get_caller_address() == owner::read(); + } + #[external] - fn onlyOwner() -> bool { - if (get_caller_address() == owner::read()) { - return true; + fn log_access() { + if(only_owner()) { + welcomeEvent('Welcome Admin!'); + } else { + welcomeEvent('Welcome User!'); } - return false; } } From 5f8ebc3406bcdffe5c6a9c950422d770e2786311 Mon Sep 17 00:00:00 2001 From: Hans Bhatia Date: Fri, 23 Jun 2023 14:39:52 -0400 Subject: [PATCH 4/8] fix: pr review comments --- .../if_else/src/access_control.cairo | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/listings/ch00-introduction/if_else/src/access_control.cairo b/listings/ch00-introduction/if_else/src/access_control.cairo index 767a7579..2c0944bf 100644 --- a/listings/ch00-introduction/if_else/src/access_control.cairo +++ b/listings/ch00-introduction/if_else/src/access_control.cairo @@ -3,28 +3,27 @@ mod SimpleAccessControl { use starknet::{get_caller_address,ContractAddress}; struct Storage { - owner: ContractAddress, + _owner: ContractAddress, } #[event] - fn welcomeEvent(name: felt252) {} + fn WelcomeEvent(name: felt252) {} #[constructor] - fn constructor(_address: ContractAddress) { - owner::write(_address); + fn constructor(address: ContractAddress) { + _owner::write(address); } - #[internal] fn only_owner() -> bool { - return get_caller_address() == owner::read(); + return get_caller_address() == _owner::read(); } #[external] fn log_access() { if(only_owner()) { - welcomeEvent('Welcome Admin!'); + WelcomeEvent('Welcome Admin!'); } else { - welcomeEvent('Welcome User!'); + WelcomeEvent('Welcome User!'); } } } From 81b840fb49bdaad8c5692cfdfe75e6ff5966808a Mon Sep 17 00:00:00 2001 From: Hans Bhatia Date: Fri, 23 Jun 2023 15:07:12 -0400 Subject: [PATCH 5/8] docs: add documentation to contract --- .../ch00-introduction/if_else/src/access_control.cairo | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/listings/ch00-introduction/if_else/src/access_control.cairo b/listings/ch00-introduction/if_else/src/access_control.cairo index 2c0944bf..70d91bc5 100644 --- a/listings/ch00-introduction/if_else/src/access_control.cairo +++ b/listings/ch00-introduction/if_else/src/access_control.cairo @@ -1,28 +1,36 @@ #[contract] mod SimpleAccessControl { + // 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 only_owner() -> bool { return get_caller_address() == _owner::read(); } #[external] fn log_access() { + // Add a conditional event that welcomes the owner or the user. if(only_owner()) { + // We know since only_owner() == true, the owner called the function. Call the welcome event with 'Welcome Admin!'. WelcomeEvent('Welcome Admin!'); } else { + // We know since only_owner() == false, a normal user(not owner) called the function. Call the welcome event with 'Welcome User!'. WelcomeEvent('Welcome User!'); } } From 8e824f398b14599e7e1aa21bdb2daa3edf65efd0 Mon Sep 17 00:00:00 2001 From: Hans Bhatia Date: Fri, 23 Jun 2023 15:07:37 -0400 Subject: [PATCH 6/8] docs(src): add readme file --- src/ch00-09-if_else.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/ch00-09-if_else.md diff --git a/src/ch00-09-if_else.md b/src/ch00-09-if_else.md new file mode 100644 index 00000000..00319626 --- /dev/null +++ b/src/ch00-09-if_else.md @@ -0,0 +1,9 @@ +# Control Flow (if_else) + +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 control: + +```rust +{{#include ../listings/ch00-introduction/if_else/src/access_control.cairo}} +``` From d172d3fadcc8a024739d4c7d7045d01ba09cc969 Mon Sep 17 00:00:00 2001 From: Hans Bhatia Date: Fri, 23 Jun 2023 15:09:50 -0400 Subject: [PATCH 7/8] docs(src): add contract desc --- src/ch00-09-if_else.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ch00-09-if_else.md b/src/ch00-09-if_else.md index 00319626..e2970246 100644 --- a/src/ch00-09-if_else.md +++ b/src/ch00-09-if_else.md @@ -4,6 +4,8 @@ Using an if expression allows you to execute certain code depending on condition Here's a simple example of a contract using if and else statements for role based access control: +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_control.cairo}} ``` From a5cd1b9ed11ceedbb9c8363a4b47aa0054c8df17 Mon Sep 17 00:00:00 2001 From: msaug Date: Fri, 23 Jun 2023 21:31:59 +0200 Subject: [PATCH 8/8] fix: terminology & add to SUMMARY --- .../src/{access_control.cairo => access_log.cairo} | 14 +++++++------- listings/ch00-introduction/if_else/src/lib.cairo | 2 +- src/SUMMARY.md | 1 + ...ch00-09-if_else.md => ch00-09-if_statements.md} | 6 +++--- 4 files changed, 12 insertions(+), 11 deletions(-) rename listings/ch00-introduction/if_else/src/{access_control.cairo => access_log.cairo} (63%) rename src/{ch00-09-if_else.md => ch00-09-if_statements.md} (82%) diff --git a/listings/ch00-introduction/if_else/src/access_control.cairo b/listings/ch00-introduction/if_else/src/access_log.cairo similarity index 63% rename from listings/ch00-introduction/if_else/src/access_control.cairo rename to listings/ch00-introduction/if_else/src/access_log.cairo index 70d91bc5..562a64b9 100644 --- a/listings/ch00-introduction/if_else/src/access_control.cairo +++ b/listings/ch00-introduction/if_else/src/access_log.cairo @@ -1,11 +1,11 @@ #[contract] -mod SimpleAccessControl { +mod SimpleAccessLog { // Import the Starknet contract API. - use starknet::{get_caller_address,ContractAddress}; + use starknet::{get_caller_address, ContractAddress}; struct Storage { // Add the owner to the contract storage. - _owner: ContractAddress, + _owner: ContractAddress, } #[event] @@ -19,18 +19,18 @@ mod SimpleAccessControl { } // Add a function that checks if the caller is the owner. - fn only_owner() -> bool { + 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(only_owner()) { - // We know since only_owner() == true, the owner called the function. Call the welcome event with 'Welcome Admin!'. + 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 only_owner() == false, a normal user(not owner) called the function. Call the welcome event with 'Welcome User!'. + // 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 index a29fcd6c..527913e1 100644 --- a/listings/ch00-introduction/if_else/src/lib.cairo +++ b/listings/ch00-introduction/if_else/src/lib.cairo @@ -1 +1 @@ -mod access_control; +mod access_log; diff --git a/src/SUMMARY.md b/src/SUMMARY.md index b7d637e2..e136f30c 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_else.md b/src/ch00-09-if_statements.md similarity index 82% rename from src/ch00-09-if_else.md rename to src/ch00-09-if_statements.md index e2970246..f34ac2af 100644 --- a/src/ch00-09-if_else.md +++ b/src/ch00-09-if_statements.md @@ -1,11 +1,11 @@ -# Control Flow (if_else) +# 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 control: +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_control.cairo}} +{{#include ../listings/ch00-introduction/if_else/src/access_log.cairo}} ```