2

I want to get all the program accounts with a filter. The variable country is a number in my code and I don't know how to convert it to bytes for the filtering to work.

const country = 0;

const sellerAccountFilters = {
  filters: [
        {
          dataSize: 198,
        },
        {
          memcmp: {
            offset: 8,
            // How to convert country from number to bytes ?
            bytes: country,
          },
        },
      ],
    };

const sellerAccount = await connection.getProgramAccounts(programID,filters);

Thanks in advance,

1 Answer 1

3

The memcmp bytes are expected to be a base-58 representation of the data to be compared. For numerical data comparisons, you can convert the number of base-58 with:

import bs58 from 'bs58'

const country = 0
const sellerAccountFilters = {
  filters: [
    {
      dataSize: 198,
    },
    {
      memcmp: {
        offset: 8,
        bytes: bs58.encode(Buffer.from([country])),
      },
    },
  ],
};

With country being 0, this should correctly encode the buffer as '1' in base-58.

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.