11

https://book.anchor-lang.com/ has no article on these yet, but I see many programs using them.

What is the problem that they are solving? What are the common code patterns and best practices for implementing and consuming events?

2 Answers 2

11

What: log messages (anything you would do with msg!()). But they are not human readable as they are base64 encoded.

Why: msg!() with string formatting is relatively expensive for compute units

How: example

1
  • In your example how can you extract the tx signature from the program call program.rpc.initialize(); that is wrapped in the Promise? Jan 6, 2023 at 9:07
6

They allow encoding of data into the transaction through the logs. They are more efficient that formatting a string (very expensive) and allow easy decoding through the anchor SDK

They can be used for:

  • Notification
  • Program activity tracking
2
  • Thanks, that sort of answers the "what / why?" When you say "tracking," do you mean realtime tracking? Pubsub? How is this accomplished? Can you point me to any illustrative examples on implementation and consumption?
    – HelmetFace
    Aug 16, 2022 at 12:50
  • 1
    I think it's websockets under the hood. But you can use Anchor's Program.addEventListener to subscribe to them on the client: coral-xyz.github.io/anchor/ts/classes/…
    – Callum M
    Aug 16, 2022 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.