I tried creating a PDA for my escrow program but I keep running into the error above. I ensured the seeds used in my testing code FindProgramAddress and the rust file are the same, but I still get this error. Does anyone know why? Should I be using uft8 encode for the seeds? My code is as such:
#[derive(Accounts)]
#[instruction(application_idx: u64, bump: u8)]
pub struct InitNewExchange<'info> {
//Derived PDAs (Must specify seed and bump for PDA)
#[account(
init, //Initializing a new account
payer = user_sending,
space = Exchange::LEN,
seeds = [b"exchange",
user_sending.key.as_ref(),],
bump,
)]
application_exchange: Account<'info, Exchange>,
#[account(
init, //Initializing a new account
payer = user_sending,
seeds = [b"wallet",
user_sending.key.as_ref(),],
bump,
token::mint = mint_of_token_being_sent,
token::authority = application_exchange,
)]
//Should not be signer or writable
escrow_wallet_exchange: Account<'info, TokenAccount>,
//User Accounts
#[account(mut)]
user_sending: Signer<'info>,
///CHECK: Not dangerous
#[account(mut)]
user_receiving: AccountInfo<'info>,
#[account(mut)]
mint_of_token_being_sent: Account<'info, Mint>,
//Token wallet that escrow wallet has alredy approved
#[account(
mut,
constraint = wallet_to_withdraw_from.owner == user_sending.key(),
constraint = wallet_to_withdraw_from.mint == mint_of_token_being_sent.key(),
)]
wallet_to_withdraw_from: Account<'info, TokenAccount>,
//App level accounts
system_program: Program<'info, System>,
token_program: Program<'info, Token>,
rent: Sysvar<'info, Rent>,
}
and my test code:
let pda: PDAParams;
//FUNCTIONS to prepare cross program invocation
const getPDAparams = async(
connection: anchor.web3.Connection,
sender?: anchor.web3.PublicKey,
receiver?: anchor.web3.PublicKey,
token?: anchor.web3.PublicKey): Promise<PDAParams> => {
//Get timestamp for transaction
const uid = new anchor.BN(parseInt((Date.now() / 1000).toString()));
const uidbuffer = uid.toBuffer("le", 8);
//findProgramAddress:
//Takes in seeds and program ID
//Return a public key of PDA and checks that the pubkey is on the ed25519 curve
let [exchangePubKey,] = await anchor.web3.PublicKey.findProgramAddress(
[
utf8.encode("exchange"),
sender!.toBuffer(),
],
program.programId,
);
let [walletPubKey,] = await anchor.web3.PublicKey.findProgramAddress(
[
utf8.encode("wallet"),
sender!.toBuffer(),
],
program.programId,
);
return {
escrowWalletKey: walletPubKey,
exchangeKey: exchangePubKey,
idx: uid,
}
}
it("Initialize a payment by sender1", async() => {
const amount = new anchor.BN(20000000);
//Initialize mint and fund the account
const tx1 = await program.methods
.initNewExchange(pda.idx, amount)
.accounts({
applicationExchange: pda.exchangeKey,
escrowWalletExchange: pda.escrowWalletKey,
userSending: sender.publicKey,
userReceiving: receiver.publicKey,
//Account data type of mint
mintOfTokenBeingSent: mintAddress,
//Wallet of sender for that particular token
walletToWithdrawFrom: senderWallet,
systemProgram: anchor.web3.SystemProgram.programId,
tokenProgram: spl.TOKEN_PROGRAM_ID,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
})
.signers([sender])
.rpc();
console.log("Initialized a new exchange instance! 20 tokens being paid");
Any help is greatly appreciated!