You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update event docs for pallet-revive and multi ABI support (#482)
* Refactor event docs for `pallet-revive` and multi ABI support
* Update "all" ABI docs
* typo
Co-authored-by: Michael Müller <mich@elmueller.net>
* typo
Co-authored-by: Michael Müller <mich@elmueller.net>
---------
Co-authored-by: Michael Müller <mich@elmueller.net>
Copy file name to clipboardExpand all lines: docs/basics/events.md
+72-38Lines changed: 72 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -82,9 +82,12 @@ pub struct Transferred {
82
82
amount:u128,
83
83
}
84
84
```
85
-
> Note that generics are [not currently supported](https://github.com/use-ink/ink/issues/2044)
86
-
> , so the concrete types of `Environment`
87
-
> specific types such as `AccountId` must match up with the types used in the contract.
85
+
86
+
:::note
87
+
Generics are [not currently supported](https://github.com/use-ink/ink/issues/2044),
88
+
so the concrete types of `Environment` specific types such as `AccountId`
89
+
must match up with the types used in the contract.
90
+
:::
88
91
89
92
This definition can exist within a contract definition module (inline events), in a different
90
93
module in the same crate or even in a different crate to be shared by multiple contracts.
@@ -98,37 +101,48 @@ using the new `event` attribute macro directly will behave exactly the same.
98
101
99
102
### Topics
100
103
101
-
When an event is emitted, 0 or more topics can be associated with it. The event is then indexed
102
-
together with other events with the same topic value.
104
+
When an event is emitted, up to 4 topics (including the signature topic, if any) can be associated with it.
105
+
The event is then indexed together with other events with the same topic value.
103
106
104
107
An event's fields can be annotated with `#[ink(topic)]` (see example), which will result in a
105
108
topic derived from the value of that field being emitted together with the event.
106
109
107
-
Topics are by default a 32 byte array (`[u8; 32]`), although this is configurable on the
108
-
Polkadot SDK runtime level. If the SCALE encoded bytes of a field value are `<= 32`, then the
109
-
encoded bytes are used directly as the topic value.
110
+
Topics are a 32 byte array (`[u8; 32]`), and the topic value is encoded as follows:
110
111
111
-
For example, in the common case of indexing a field of type `AccountId`, where the default
112
-
`AccountId` type is 32 bytes in length, the topic value will be the encoded account id itself. This
113
-
makes it easy to filter for all events which have a topic of a specific `AccountId`.
112
+
- If the SCALE encoded bytes of a field value are `<= 32`,
113
+
then the encoded bytes are used directly as the topic value.
114
+
If necessary, the topic value is padded on the right side with zero-bytes such that its length is 32 bytes.
115
+
- For example, in the common case of indexing a field of type `AccountId`, where the default
116
+
`AccountId` type is 32 bytes in length, the topic value will be the encoded account id itself.
117
+
This makes it easy to filter for all events which have a topic of a specific `AccountId`.
118
+
- If the size of the SCALE encoded bytes of the field value exceeds 32,
119
+
then the encoded bytes are hashed using the `Blake2x256` hash function.
114
120
115
-
If however the size of the encoded bytes of the value of a field exceeds 32, then the encoded
116
-
bytes will be hashed using the `Blake2x256` hasher.
121
+
:::note
122
+
The topic encoding specification above only applies to native/ink! ABI encoded events.
117
123
118
-
> Topics are a native concept in the Polkadot SDK, and can be queried via [`EventTopics`](https://docs.rs/frame-system/latest/frame_system/pallet/storage_types/struct.EventTopics.html)
124
+
For Solidity ABI encoded events, topics (and event data) are encoded according to the
125
+
[Solidity ABI specification for events][sol-abi-events] and [indexed event parameters][sol-abi-topics].
126
+
:::
119
127
120
-
How to choose which fields to make topics? A good rule of thumb is to ask yourself if somebody
121
-
might want to search for this topic. For this reason the `amount` in the example `Transferred` event
122
-
above was not made indexable ‒ there will most probably be a lot of different events with differing
Topics are a native concept in the Polkadot SDK, and can be queried via [`EventTopics`](https://docs.rs/frame-system/latest/frame_system/pallet/storage_types/struct.EventTopics.html)
133
+
:::
126
134
127
-
By default all events have a signature topic. This allows indexing of all events of the same
128
-
type, emitted by different contracts. The `#[ink::event]` macro generates a signature topic at
129
-
compile time by hashing the name of the event concatenated with the *names of the types* of the all
130
-
the field
131
-
names:
135
+
### How to choose which fields to make topics?
136
+
A good rule of thumb is to ask yourself if somebody might want to search for this topic.
137
+
For this reason the `amount` in the example `Transferred` event above was not made indexable ‒
138
+
there will most probably be a lot of different events with differing amounts each.
139
+
140
+
### Signature Topic
141
+
142
+
By default all events have a signature topic.
143
+
This allows indexing of all events of the same type, emitted by different contracts.
144
+
The `#[ink::event]` macro generates a signature topic at compile time by
145
+
hashing the name of the event concatenated with the *names of the types* of all the fields:
132
146
```
133
147
blake2b("Event(field1_type,field2_type)")`
134
148
```
@@ -137,18 +151,25 @@ So for our `Transferred` example it will be:
0 commit comments