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 events #46

Merged
merged 13 commits into from
Jun 27, 2023
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 @@ -11,6 +11,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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@enitrat when does the new syntactic update come out?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With 0.12


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}}
```