5

First of all can someone explain to me what a discriminator is, in the context of Solana instructions, in simple terms?

Second, how can I get the discriminator of an instruction in an Anchor Solana IDL?

both of these Solana programs https://solscan.io/account/1349iiGjWC7ZTbu6otFmJwztms122jEEnShKgpVnNewy#anchorProgramIDL and https://solscan.io/account/7h7uYbDBZkiTvWLmMsWNAEs2VVNqfV56uSfP9hbjJMP8#anchorProgramIDL

have the same instruction name "completeWithdraw".

I have seen posts where they said to take the 8 first bytes of the "global:completeWithdraw", but wouldn't that mean they would have the same discriminator?

I have found in non-anchor IDLs, the discriminator is defined as part of the instruction object. https://github.com/pyth-network/pyth-client/blob/idl/program/idl.json

1
  • If your question is answered, please mark this as answered. Thank you.
    – vicyyn
    Commented Dec 27, 2022 at 13:02

1 Answer 1

8

First of all can someone explain to me what a discriminator is, in the context of Solana instructions, in simple terms?

What is Anchor (8 bytes) discriminator

Second, how can I get the discriminator of an instruction in an Anchor Solana IDL?

pub fn sighash(namespace: &str, name: &str) -> [u8; 8] {
    let preimage = format!("{}:{}", namespace, name);

    let mut sighash = [0u8; 8];
    sighash.copy_from_slice(
        &anchor_client::anchor_lang::solana_program::hash::hash(preimage.as_bytes()).to_bytes()
            [..8],
    );
    sighash
}

println!("{:?}", sighash("global", "initialize"));

You can use this function to get the discriminator for your instructions. You only need to pass the instruction names.

1
  • 1
    absolute life saver. tysm
    – Pixeled
    Commented Jan 17, 2023 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.