I have used the emit macro in Anchor to emit events from the smart contract as follows,
use anchor_lang::prelude::*;
// handler function inside #[program]
pub fn initialize(_ctx: Context<Initialize>) -> Result<()> {
emit!(MyEvent {
data: 5,
label: [1,2,3,4,5],
});
Ok(())
}
#[event]
pub struct MyEvent {
pub data: u64,
pub label: [u8; 5],
}
Now I want to subscribe to these events from my TS frontend. I want the ability to subscribe to new events as well as the ability to query past events. Is this possible on Solana and if so, how can I do this?
The final answer I found was using getSignaturesForAddress
on your program, followed by getTransaction
for each one returned, and then looking through the logMessages
for MyEvent
.
For further reference,
https://docs.solana.com/developing/clients/jsonrpc-api#getsignaturestatuses
https://docs.solana.com/developing/clients/jsonrpc-api#gettransaction