Skip to content

Add event support via #[event] and #[indexed] attributes #7158

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tritao
Copy link
Contributor

@tritao tritao commented May 6, 2025

This PR adds two new attributes: #[event] and #[indexed].

The #[event] attribute marks a struct or enum as an event that can be emitted by a contract.

The #[indexed] attribute can be applied to fields within structs that are attributed with #[event]. This is particularly useful for event structs, allowing for efficient filtering and searching of emitted events based on the values of these fields.

When using this attribute, the indexed fields must be applied sequentially to the initial set of fields in a struct.

This attribute can only be applied to fields whose type is an exact size ABI type. The exact size ABI types include:

  • bool
  • u8, u16, u32, u64, u256
  • numeric
  • b256
  • str[N]
  • Tuples containing only exact size types
  • Structs containing only exact size types
  • Arrays of exact size types with a literal length
  • Type aliases to exact size types

Additionally it causes the event types to be included in the JSON ABI representation for the contract and emits an offset for indexed fields.

#[event]
struct MyEventStruct {
    #[indexed]
    id: u64,
    sender: Identity,
}

#[event]
enum MyEventEnum {
    A: (),
    B: (),
}

Add `#[abi_name(name = "foo")` attribute to rename enum and struct ABI
items.

Here is example of how it can be used:

```sway
contract;

struct MyStruct {}

enum MyEnum {
  A: ()
}

abi MyAbi {
    fn my_struct() -> MyStruct;
    fn my_enum() -> MyEnum;
}

impl MyAbi for Contract {
  fn my_struct() -> MyStruct { MyStruct{} }
  fn my_enum() -> MyEnum { MyEnum::A }
}
```

Closes FuelLabs#5955.
@tritao tritao added enhancement New feature or request compiler General compiler. Should eventually become more specific as the issue is triaged compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen labels May 6, 2025
@tritao tritao self-assigned this May 6, 2025
This adds two new attributes: `#[event]` and `#[indexed]`.

The `#[event]` attribute marks a struct or enum as an event that can be
emitted by a contract.

The `#[indexed]` attribute can be applied to fields within structs that
are attributed with `#[event]`. This is particularly useful for event
structs, allowing for efficient filtering and searching of emitted
events based on the values of these fields.

When using this attribute, the indexed fields must be applied
sequentially to the initial set of fields in a struct.

This attribute can only be applied to fields whose type is an exact size
ABI type. The exact size ABI types include:

- `bool`
- `u8`, `u16`, `u32`, `u64`, `u256`
- `numeric`
- `b256`
- `str[N]`
- Tuples containing only exact size types
- Structs containing only exact size types
- Arrays of exact size types with a literal length
- Type aliases to exact size types

Additionally it causes the event types to be included in the JSON ABI
representation for the contract.

```sway
#[event]
struct MyEventStruct {
    #[indexed]
    id: u64,
    sender: Identity,
}

#[event]
enum MyEventEnum {
    A: (),
    B: (),
}
```

Additionally this updates the JSON ABI to emit an `offset` for indexed
fields.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler General compiler. Should eventually become more specific as the issue is triaged enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant