Skip to content

Conversation

@acheroncrypto
Copy link
Collaborator

Problem

It's not easy to create an unknown event from bytes as described in #2885.

Summary of changes

  • Add utils module to the declare_program! macro.

  • Add autogenerated Event type inside the utils module:

    /// An enum that includes all events of the declared program as a tuple variant.
    ///
    /// See [`Self::try_from_bytes`] to create an instance from bytes.
    pub enum Event {
        SomeEvent(SomeEvent),
        OtherEvent(OtherEvent),
    }
    
    impl Event {
        /// Try to create an event based on the given bytes.
        ///
        /// This method returns an error if the discriminator of the given bytes don't match
        /// with any of the existing events, or if the deserialization fails.
        pub fn try_from_bytes(bytes: &[u8]) -> Result<Self> {
            // ...
        }
    }

Resolves #2885

@vercel
Copy link

vercel bot commented Apr 9, 2024

@acheroncrypto is attempting to deploy a commit to the coral-xyz Team on Vercel.

A member of the Team first needs to authorize it.

@acheroncrypto acheroncrypto force-pushed the add-event-utility-type branch from 93c0e7c to a221aa4 Compare April 9, 2024 15:11
@acheroncrypto acheroncrypto merged commit 0be5b00 into solana-foundation:master Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature: event bytes to enum of events (rust)

1 participant