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

Events #89

Merged
merged 2 commits into from
Mar 25, 2021
Merged

Events #89

merged 2 commits into from
Mar 25, 2021

Conversation

armaniferrante
Copy link
Member

@armaniferrante armaniferrante commented Feb 17, 2021

Summary

Adds an API for emitting and subscribing to events.

Example

  • Defining an event in a Rust program
#[event]
pub struct MyEvent {
  data: u64,
  #[index]
  label: String,
}
  • Emitting an event from a Rust program:
emit!(MyEvent {
    data: 5,
    label: "hello".to_string(),
});
  • Subscribing to an event from JavaScript:
listener = program.addEventListener("MyEvent", (event, slot) => {
  // Do something with the event.
});
  • Subscribing to an event from a Rust client
program.on(|ctx: &EventContext, event: MyEvent| {
  // Do something with the event.
});

Note that the #[index] attribute is used as a marker to add to the IDL. No indexing is actually done by solana validators and it's up to a client consuming the events to perform any indexing.

Open questions

I'm not sure how event ordering w.r.t. slot number is handled by the Solana logsSubscribe API. Ideally, this API would provide exactly once semantics, but it may be possible that events show up out of order or appear more than once. In this case, either the anchor client, or user of the client, will need to add additional logic to ensure applications behave as expected.

@armaniferrante
Copy link
Member Author

This depends on the changes in solana-labs/solana#16045.

@@ -14,14 +14,14 @@
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noImplicitAny": true,
"noImplicitAny": false,
Copy link
Member Author

@armaniferrante armaniferrante Mar 24, 2021

Choose a reason for hiding this comment

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

This seems to be required due to @solana/web3.js 1.0.0, though I have not yet confirmed with a minimal example.

@armaniferrante armaniferrante marked this pull request as ready for review March 25, 2021 03:19
@armaniferrante armaniferrante merged commit 3cbc227 into master Mar 25, 2021
@armaniferrante armaniferrante deleted the armani/events branch March 25, 2021 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant