From cb89213cfeda8bd2083df23781f55eb1a7429929 Mon Sep 17 00:00:00 2001 From: HansBhatia <78050086+HansBhatia@users.noreply.github.com> Date: Tue, 27 Jun 2023 11:54:31 -0400 Subject: [PATCH] Feat events (#46) * 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 --- listings/ch00-introduction/events/.gitignore | 1 + listings/ch00-introduction/events/Scarb.toml | 8 +++++++ .../events/src/counter.cairo | 23 +++++++++++++++++++ .../ch00-introduction/events/src/lib.cairo | 1 + src/SUMMARY.md | 1 + src/ch00-10-events.md | 9 ++++++++ 6 files changed, 43 insertions(+) create mode 100644 listings/ch00-introduction/events/.gitignore create mode 100644 listings/ch00-introduction/events/Scarb.toml create mode 100644 listings/ch00-introduction/events/src/counter.cairo create mode 100644 listings/ch00-introduction/events/src/lib.cairo create mode 100644 src/ch00-10-events.md diff --git a/listings/ch00-introduction/events/.gitignore b/listings/ch00-introduction/events/.gitignore new file mode 100644 index 00000000..eb5a316c --- /dev/null +++ b/listings/ch00-introduction/events/.gitignore @@ -0,0 +1 @@ +target diff --git a/listings/ch00-introduction/events/Scarb.toml b/listings/ch00-introduction/events/Scarb.toml new file mode 100644 index 00000000..63071abe --- /dev/null +++ b/listings/ch00-introduction/events/Scarb.toml @@ -0,0 +1,8 @@ +[package] +name = "counter" +version = "0.1.0" + +[dependencies] +starknet = "1.1.0" + +[[target.starknet-contract]] \ No newline at end of file diff --git a/listings/ch00-introduction/events/src/counter.cairo b/listings/ch00-introduction/events/src/counter.cairo new file mode 100644 index 00000000..3c23ad23 --- /dev/null +++ b/listings/ch00-introduction/events/src/counter.cairo @@ -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); + } +} diff --git a/listings/ch00-introduction/events/src/lib.cairo b/listings/ch00-introduction/events/src/lib.cairo new file mode 100644 index 00000000..f5271637 --- /dev/null +++ b/listings/ch00-introduction/events/src/lib.cairo @@ -0,0 +1 @@ +mod counter; diff --git a/src/SUMMARY.md b/src/SUMMARY.md index cdaba3fb..113a2061 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) diff --git a/src/ch00-10-events.md b/src/ch00-10-events.md new file mode 100644 index 00000000..417f36e1 --- /dev/null +++ b/src/ch00-10-events.md @@ -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}} +```