Define and emit an event
Add#[event] to a struct and call emit! after the state change succeeds.
lib.rs
emit! writes the event bytes with Solana’s sol_log_data syscall. Transaction
logs show the result as Program data: <base64>. The encoded value starts with
the event discriminator followed by its Borsh-serialized fields.
Keep events focused. Large strings and vectors consume compute, increase
transaction metadata, and are more likely to be affected by log truncation.
Choose between log and CPI events
Anchor supports two emission paths:| Path | Storage location | Tradeoff |
|---|---|---|
emit! | Program data logs | Simpler and cheaper, but logs may be truncated |
emit_cpi! | Inner instruction data from a CPI | More durable metadata, with extra accounts and compute |
event-cpi feature:
Cargo.toml
#[event_cpi] and emit
from a handler whose context variable is named ctx:
lib.rs
#[event_cpi] adds the event authority and current program accounts required by
the signed self-CPI. CPI events cannot be consumed through Anchor’s log event
subscription; fetch the transaction and decode the matching inner instruction
instead.
Use the ledger as the sourceRPC providers may truncate program logs. If missing an event would affect
balances, permissions, or another correctness-critical workflow, derive the
result from program accounts or transaction instruction data. Use events to
make indexing efficient, not as the only record of state.
Subscribe from TypeScript
The current Anchor TypeScript client is@anchor-lang/core. A Program
instance created from the program IDL can decode matching log events and deliver
them over the RPC log subscription:
"paymentRecorded"), as the TypeScript client
normalises IDL names. A PascalCase name never matches and fails silently. The
decoded event.name is camelCase too. The callback receives decoded event data,
the slot, and the transaction signature. Subscriptions only cover logs delivered
while the connection is active. Persist processed signatures and backfill missed
transactions after a disconnect if the consumer needs complete history.
Decode fetched transaction logs
For backfills and one-off inspection, fetch a transaction and pass eachProgram data: payload to the event coder:
maxSupportedTransactionVersion: 1 lets the RPC return legacy, version 0, and
version 1 transactions. With a lower value, fetching a version 1 transaction
fails with error -32015 (transaction version not supported).
A transaction may invoke several programs. For a production parser, track the
Program <address> invoke and success log boundaries or use Anchor’s event
parser so a different program’s Program data: entry is not decoded as yours.
Failed transactions can retain logs and inner instructions from work completed
before the error, so always reject transaction metadata with a non-null err
before indexing either event form. Anchor’s log subscription does this check
before delivering an event callback.
For an emit_cpi! event, locate the self-CPI in meta.innerInstructions,
confirm that its program ID is your program, and base58-decode its instruction
data. Before stripping any bytes, verify Anchor’s
EVENT_IX_TAG_LE:
Version event schemas
By default, Anchor derives an 8-byte event discriminator from the event name. Anchor also supports an explicit discriminator through#[event(discriminator = ...)]. Discriminators must remain unique within the
program and stable for every client that decodes the event.
Changing an emitted struct’s fields changes its Borsh layout. For a breaking
schema change, define a new event such as PaymentRecordedV2 rather than
silently changing PaymentRecorded. Keep emitting the old event during a
migration window when existing indexers still depend on it.
For evolvable data, prefer fixed-width fields and identifiers that let an
indexer fetch richer account data separately. Publish the updated IDL before or
with the program upgrade so consumers can decode the new event immediately.
Test events
Test the decoded contract, not the base64 string. Register the listener before sending the instruction, assert the decoded fields, and transaction signature, and always remove the listener:emit_cpi!, test decoding from innerInstructions because log listeners do not
receive those events directly.