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

lang: Refactor discriminator generation #3182

Conversation

acheroncrypto
Copy link
Collaborator

Problem

Discriminator generation code is repeated in multiple places:

let mut discriminator = [0u8; 8];
discriminator.copy_from_slice(
&anchor_syn::hash::hash(discriminator_preimage.as_bytes()).to_bytes()[..8],
);
let discriminator: proc_macro2::TokenStream =
format!("{discriminator:?}").parse().unwrap();
quote! { &#discriminator }

let discriminator = args.discriminator.unwrap_or_else(|| {
let discriminator_preimage = format!("event:{event_name}").into_bytes();
let discriminator = anchor_syn::hash::hash(&discriminator_preimage);
let discriminator: proc_macro2::TokenStream =
format!("{:?}", &discriminator.0[..8]).parse().unwrap();
quote! { &#discriminator }
});

Summary of changes

Refactor the discriminator generation code to reuse functionality.

The above examples become:

gen_discriminator(namespace, name)

Note: This PR is part of a greater effort explained in #3097.

Copy link

vercel bot commented Aug 20, 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 merged commit 546945b into coral-xyz:master Aug 20, 2024
49 of 50 checks passed
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.

1 participant