Skip to content

Commit

Permalink
Feat events (#46)
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

* feat(events): add events

* chore: add to summary

* chore: add type

* fix: pr review changes

---------

Co-authored-by: msaug <msaug@protonmail.com>
  • Loading branch information
HansBhatia and enitrat authored Jun 27, 2023
1 parent ddf4da0 commit cb89213
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions listings/ch00-introduction/events/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
8 changes: 8 additions & 0 deletions listings/ch00-introduction/events/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "counter"
version = "0.1.0"

[dependencies]
starknet = "1.1.0"

[[target.starknet-contract]]
23 changes: 23 additions & 0 deletions listings/ch00-introduction/events/src/counter.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#[contract]
mod SimpleCounter {
struct Storage {
// Counter value
_counter: u256,
}

#[event]
// Increment event - emitted when the counter is incremented.
fn Increment(counterVal: u256) {}

#[constructor]
fn constructor() {}

#[external]
fn increment() {
let mut counter: u256 = _counter::read();
counter += 1;
_counter::write(counter);
// Emit event
Increment(counter);
}
}
1 change: 1 addition & 0 deletions listings/ch00-introduction/events/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod counter;
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Summary
- [Constructors](./ch00-07-constructor.md)
- [Function Attributes](./ch00-08-function_attributes.md)
- [If statements](./ch00-09-if_statements.md)
- [Events](./ch00-10-events.md)

- [Applications](./ch01-00-applications.md)
- [Upgradeable Contract](./ch01-01-upgradeable_contract.md)
Expand Down
9 changes: 9 additions & 0 deletions src/ch00-10-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Events

An event is defined as function with the #[event] attribute. The parameters of the function correspond to data that will be emitted. An event is to be logged for easy and fast access to querying the data at a later time.

Here's a simple example of a contract using events that emit an event each time a counter is incremented by the "increment" function:

```rust
{{#include ../listings/ch00-introduction/events/src/counter.cairo}}
```

0 comments on commit cb89213

Please sign in to comment.