Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(if-else): add if-else access control #44

Merged
merged 8 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
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}}
```