Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Add notes on events
Browse files Browse the repository at this point in the history
  • Loading branch information
urani-engineering-helper authored Mar 27, 2024
1 parent 4088a20 commit 585e739
Showing 1 changed file with 151 additions and 0 deletions.
151 changes: 151 additions & 0 deletions chapters/05_events.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,155 @@

<br>

* Events in Archor provide a mechanism for notifying and communicating between different components of a Solana dapp.

* Events are structured pieces of data holding information about a specific occurrence in a program.

* Since there is no native support for events in Solana, Anchor events depend on logging to emit events.
- Programs log `base64` encoded event data and clients parse the logs of the transaction to interpret the events.




<br>

---

### Defining Events

<br>


* Events are defined using `#[event]` attribute macro, which allows the specification of fields that an event should contain:

<br>

```rust
#[event]
pub struct TransferEvent {
from: Pubkey,
to: Pubkey,
amount: u64,
}
```

<br>

---

### Emitting Events

<br>

* To emit an event, you can use the `emit!` macro:

<br>

```rust
emit!(TransferEvent {
from: *ctx.accounts.from.key,
to: *ctx.accounts.to.key,
amount,
});
```

<br>

---

### Subscribing to Events

<br>

* Anyone can subscribe to events emitted by your program, through the [@coral-xyz/archor](@coral-xyz/anchor) library:

<br>

```rust
const subscriptionId = program.addEventListener("TransferEvent", (event) => {
// Handle event...
});
```

<br>

* The event listener should be removed once it's no longer needed:

<br>

```rust
program.removeEventListener(subscriptionId);
```

<br>

---

### CPI Events

<br>

* Solana nodes truncate logs larger than 10KB, which makes regular events emitted via `emit!` unreliable.

* Unlike logs, RPC providers store instruction data without truncation.
- CPI events make use of this by executing a self-invoke with the event data to store the event(s) in the instruction.

* To use CPI vents, you can enable `event-cpi`:

<br>

```
anchor-lang = { version = "0.29.0", features = ["event-cpi"] }
```

<br>

* Then, add `#[event_cpi]` to accounts struct:

<br>

```rust
#[event_cpi]
#[derive(Accounts)]
pub struct TransferContext {}
```

<br>

* And the instruction handler:

<br>

```rust

#![allow(unused)]
fn main() {
#[program]
pub mod my_program {
use super::*;

pub fn transfer(ctx: Context<TransferContext>, amount: u64) -> Result<()> {
// Perform transfer logic

// Emit the TransferEvent
emit_cpi!(TransferEvent {
from: *ctx.accounts.from.key,
to: *ctx.accounts.to.key,
amount,
});

Ok(())
}
}
}
```

<br>

---

### Resources

<br>

* [Events on The Anchor Book](https://book.anchor-lang.com/anchor_in_depth/events.html)

0 comments on commit 585e739

Please sign in to comment.