3

I'm trying to fetch all of a certain account that matches defined u32 values. My account struct looks something like this:

#[account]
pub struct Round {
    pub bump: u8, // 1
    pub authority: Pubkey, // 32
    pub state: RoundState, // 1
    pub participants: u16, // 2
    pub id: u32, // 4,
}

What I have now is, but it doesn't seem to be fetching anything. I've tried using different iterations of UInt8Array, but can't quite figure it out. Is it possible to search like this?

const rounds = [
      { dataSize: ROUND_SIZE },
      {
        memcmp: {
          offset: 1 + 32 + 1 + 2,
          bytes: roundId,
        },
      },
    ];

Also, is it possible to search for multiple different parameters?

1 Answer 1

6

I think the problem is that you're not accounting for the discriminator, which is 8 bytes prepended to the account and therefore also the offset.

I think you want:

const rounds = [
      { dataSize: ROUND_SIZE },
      {
        memcmp: {
          offset: 8 + 1 + 32 + 1 + 2,
          bytes: roundId,
        },
      },
    ];

See eg. https://lorisleiva.com/create-a-solana-dapp-from-scratch/fetching-tweets-from-the-program#filtering-tweets-by-topic

3
  • Do you know how I would create a bytes string for a u32 number? It only seems to work when looking for one if I convert the base10 number to a base58 string
    – web3man
    Aug 4, 2022 at 7:14
  • I think the conversion you mention is what you need to do: "data to match, as base-58 encoded string and limited to less than 129 bytes" from coral-xyz.github.io/anchor/ts/modules/web3.html#MemcmpFilter
    – Callum M
    Aug 4, 2022 at 8:14
  • Yeah I see that in the docs, but I'm unsure how to properly turn the integer into a base58 string. Nothing I'm doing seems to be returning the right results
    – web3man
    Aug 4, 2022 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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