2

I want to initialize a PDA with a big number for seed, but findProgramAddress returns an error: Uncaught (in promise) TypeError: (intermediate value).toBuffer is not a function

import { BN } from "@project-serum/anchor";

async function getPDA(id) {
    return await web3.PublicKey.findProgramAddress(
        [Buffer.from("seed1"), new BN(id).toBuffer()],
        PROGRAM_ID
    );
}

How do I findProgramAddress with a big number as seed?

Thanks.

1 Answer 1

3

Well, finally I solved it using:

Js:

async function getPDA(id) {
    return await web3.PublicKey.findProgramAddress(
        [Buffer.from("seed1"), new BN(id).toArrayLike(Buffer, "be", 8)],
        PROGRAM_ID
    );
}

Rust:

...
    #[account(init, seeds = [b"seed1".as_ref(), data_pda.number.to_be_bytes().as_ref(), payer=payer, space = 50], bump)]
    new_pda: Account<'info, SomeStruct>,
    #[account(mut, seeds = [b"master_account".as_ref()], bump)]
    data_pda: Account<'info, Data>,
...

#[account]
#[derive(Default)]
pub struct Data{
    pub number: u64,
}

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.