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

Commit

Permalink
Add events sesh (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
urani-engineering-helper authored Mar 27, 2024
1 parent defdfdd commit 355fefa
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 6 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<br>

### Our development resources aim to onboard engineers to Solana development. From geeks to geeks.
### Our development resources, aiming to onboard engineers to Solana development. From geeks to geeks.

<br>

Expand All @@ -13,10 +13,11 @@
* **[1. Solana Blockchain Overview](chapters/01_intro.md)**
* **[2. Setup a Dev Environment](chapters/02_dev_env.md)**
* **[3. Mastering Anchor and CPI](chapters/03_anchor.md)**
* **[4. Program Derived Addresses](chapters/04_program_derived_addresses.md)**
* **[5. Mastering Solana Frontend](chapters/05_frontend.md)**
* **[6. Mastering Transfer Hooks](chapters/06_transfer_hooks.md)**
* **[7. Additional Resources](chapters/07_additional_resources.md)**
* **[4. Program Derived Addresses](chapters/04_pda.md)**
* **[5. Events and Communication](chapters/05_events.md)**
* **[6. Mastering Solana Frontend](chapters/06_frontend.md)**
* **[7. Mastering Transfer Hooks](chapters/07_transfer_hooks.md)**
* **[8. Additional Resources](chapters/08_additional_resources.md)**



Expand Down
2 changes: 1 addition & 1 deletion chapters/02_dev_env.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* In each environment, you will be using one of three networks:
* **Mainnet**: the production network where all the action happens. Transactions cost real money.
* **Testnet**: used for stress testing recent releases. Focused on network performance, stability, and validator behavior.
* **Devnet**: the primary network for development (these tokens have no financial value, and you can get them from [these faucets](https://github.com/urani-labs/solana-dev-onboarding-rs/blob/main/chapters/07_additional_resources.md#faucets).
* **Devnet**: the primary network for development (these tokens have no financial value).

<br>

Expand Down
File renamed without changes.
160 changes: 160 additions & 0 deletions chapters/05_events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# 🛹 Events and Communication

<br>

### tl; dr

<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)
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 355fefa

Please sign in to comment.