Skip to content

Commit

Permalink
feat(if-else): add if-else access control (#44)
Browse files Browse the repository at this point in the history
* 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
HansBhatia and enitrat authored Jun 23, 2023
1 parent f0fadc9 commit 0f43836
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions listings/ch00-introduction/if_else/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
8 changes: 8 additions & 0 deletions listings/ch00-introduction/if_else/Scarb.toml
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]]
37 changes: 37 additions & 0 deletions listings/ch00-introduction/if_else/src/access_log.cairo
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!');
}
}
}
1 change: 1 addition & 0 deletions listings/ch00-introduction/if_else/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod access_log;
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions src/ch00-09-if_statements.md
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}}
```

0 comments on commit 0f43836

Please sign in to comment.