-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
1 parent
f0fadc9
commit 0f43836
Showing
6 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "if_else" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
starknet = "1.1.0" | ||
|
||
[[target.starknet-contract]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod access_log; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}} | ||
``` |