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

Adding documentation for events discriminator #613

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions docs/programs/anchor/idl.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,5 +459,58 @@ Note that different programs using identical account names will generate the
same discriminator. When deserializing account data, Anchor programs will also
check an account is owned by the expected program for a specified account type.

</Tab>
<Tab value="Events">

The event discriminator is used to identify the specific event type when
deserializing on-chain data on event emission.

```json filename="IDL" {4}
"events": [
{
"name": "NewEvent",
"discriminator": [113, 21, 185, 70, 164, 99, 232, 201]
}
]
```

The discriminator for an event is the first 8 bytes of the Sha256 hash of the
prefix `event` plus the event name.

For example:

```
sha256("event:NewEvent")
```

Hexadecimal output:

```
71 15 b9 46 a4 63 e8 c9 2a 3c 4d 83 87 16 cd 9b 66 28 cb e2 cb 7c 5d 70 59 f3 42 2b dc 35 03 53
```

The first 8 bytes are used as the discriminator for the account.

Hex to decimal gives us:

```
71 = 113
15 = 21
b9 = 185
46 = 70
a4 = 164
63 = 99
e8 = 232
c9 = 201
```

You can find the implementation of the discriminator generation in the Anchor
codebase
[here](https://github.com/coral-xyz/anchor/blob/v0.30.1/lang/attribute/event/src/lib.rs#L23-L27).

Note that different programs using identical event names will generate the same
discriminator. When deserializing event data, Anchor programs will also check an
event is owned by the expected program for a specified event type.

</Tab>
</Tabs>
Loading