Skip to content
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
23 changes: 12 additions & 11 deletions docs/basics/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ mod erc20 {
/// every time value is transferred.
#[ink(event)]
pub struct Transferred {
from: Option<AccountId>,
to: Option<AccountId>,
from: Option<Address>,
to: Option<Address>,
value: Balance,
}

Expand Down Expand Up @@ -71,21 +71,21 @@ contract indexers/explorers are now able to group all e.g. `Transferred` events.
This is how an event definition looks:

```rust
use ink::primitives::AccountId;


#[ink::event]
pub struct Transferred {
#[ink(topic)]
from: Option<AccountId>,
from: Option<Address>,
#[ink(topic)]
to: Option<AccountId>,
to: Option<Address>,
amount: u128,
}
```

:::note
Generics are [not currently supported](https://github.com/use-ink/ink/issues/2044),
so the concrete types of `Environment` specific types such as `AccountId`
so the concrete types of `Environment` specific types such as `Address`
must match up with the types used in the contract.
:::

Expand All @@ -112,9 +112,10 @@ Topics are a 32 byte array (`[u8; 32]`), and the topic value is encoded as follo
- If the SCALE encoded bytes of a field value are `<= 32`,
then the encoded bytes are used directly as the topic value.
If necessary, the topic value is padded on the right side with zero-bytes such that its length is 32 bytes.
- For example, in the common case of indexing a field of type `AccountId`, where the default
`AccountId` type is 32 bytes in length, the topic value will be the encoded account id itself.
This makes it easy to filter for all events which have a topic of a specific `AccountId`.
- For example, in the common case of indexing a field of type `Address`, where `Address`
is 20 bytes in length, the encoded bytes are padded with 12 zero-bytes on the right
to reach 32 bytes, which is then used as the topic value. This makes it easy to
filter for all events which have a topic of a specific address.
- If the size of the SCALE encoded bytes of the field value exceeds 32,
then the encoded bytes are hashed using the `Blake2x256` hash function.

Expand Down Expand Up @@ -148,7 +149,7 @@ blake2b("Event(field1_type,field2_type)")`
```
So for our `Transferred` example it will be:
```
blake2b("Transferred(Option<AccountId>,Option<AccountId>,u128)")`
blake2b("Transferred(Option<Address>,Option<Address>,u128)")`
```

:::note
Expand Down Expand Up @@ -234,7 +235,7 @@ In a message events are emitted via `self.env().emit_event()`:

```rust
#[ink(message)]
pub fn transfer(&mut self, to: AccountId, amount: Balance) -> Result {
pub fn transfer(&mut self, to: Address, amount: Balance) -> Result {
let from = self.env().caller();
// implementation hidden
self.env().emit_event(Transferred {
Expand Down