I encountered an oddproblem with the seeds array when searching for a PDA via macro #account(init, seeds ...)].

Since each of the seeds array elements can be up to 32 bytes, I want to use the first 32 bytes of the text as a seeds element. I wrote a macro that converts a String to an array of bytes with the first n bytes.

macro_rules! to_trimmed_bytes {
    ($text:expr, $range:expr) => {{
        let bytes_slice: &[u8] = $text.as_bytes();
        let mut bytes_array: [u8;$range] = [0;$range];
        for i in 0..bytes_slice.len() {
            bytes_array[i] = bytes_slice[i];

The accounts struct with the seeds looks like this:

#[instruction(text: String)] 
pub struct CreatePost<'info> {

    pub authority: Signer<'info>,

    #[account(init, seeds = [b"post", authority.key().as_ref(), to_trimmed_bytes!(text,32)], bump, payer = authority, space = 800)] 
    pub post: Account<'info, Post>,

    pub system_program: Program<'info, System>,

Now, when I try to run this, I am getting the following compilation error message:

^^^^^^^^^^^^ expected an array with a fixed size of 4 elements, found one with 32 elements

After a little research, I discovered that these 4 elements are defined by the first seeds element b"post". It always allows the size of this first element, e.g. when the first element of seeds is of 7 characters (e.g. b"newpost") the error looks like this:

^^^^^^^^^^^^ expected an array with a fixed size of 7 elements, found one with 32 elements

Why is this first seeds entry a limiting factor to other elements of the seeds array? Is there a way around this?


Your Answer

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

Browse other questions tagged or ask your own question.