1

Here everything seems fine but still got this error please guide me here :-

Place bid program. :-

    pub fn place_bid(ctx: Context<PlaceBid>, bid_amount: u64) -> Result<()> {
            msg!("Bidding started");
        
            let auction = &mut ctx.accounts.auction;
            let current_time = Clock::get()?.unix_timestamp;
        
            require!(current_time >= auction.start_time, AuctionError::AuctionNotStarted);
            require!(current_time < auction.end_time, AuctionError::AuctionEnded);
            require!(bid_amount > auction.highest_bid, AuctionError::BidTooLow);
        
            auction.highest_bid = bid_amount;
            auction.highest_bidder = ctx.accounts.bidder.key();
        
            system_program::transfer(
                CpiContext::new(
                    ctx.accounts.system_program.to_account_info(),
                    system_program::Transfer {
                        from: ctx.accounts.bidder.to_account_info(),
                        to: ctx.accounts.escrow_account.to_account_info(),
                    },
                ),
                bid_amount,
            )?;
        
            msg!("Bid has been placed");
        
            Ok(())
        }
    #[derive(Accounts)]
pub struct PlaceBid<'info> {
    #[account(mut)]
    pub auction: Account<'info, Auction>,

    #[account(mut)]
    pub bidder: Signer<'info>,

    #[account(
        mut, 
        associated_token::mint = mint,
        associated_token::authority = bidder,
    )]
    pub bidder_token_account: Account<'info, TokenAccount>,

    #[account (
        mut,
        seeds = [b"escrow", auction.key().as_ref()],
        bump,
    )]
    pub escrow_account: Account<'info, TokenAccount>,

    pub mint: Account<'info, Mint>,

    pub token_program: Program<'info, Token>,
    pub system_program: Program<'info, System>,
    pub associated_token_program: Program<'info, AssociatedToken>,
    pub rent: Sysvar<'info, Rent>,
}

This is the test case :-

it('Place bid', async () => {
        const bidderTokenAccount = getAssociatedTokenAddressSync(
            mintKeyPair.publicKey,
            bidderKeypair.publicKey
        );

        const escrowAccount = getAssociatedTokenAddressSync(
            auctionKeyPair.publicKey,
            auctionKeyPair.publicKey,
            true
        );

        const createEscrowAccountIx = createAssociatedTokenAccountInstruction(
            payer.publicKey,
            escrowAccount,
            auctionKeyPair.publicKey,
            mintKeyPair.publicKey,
            TOKEN_PROGRAM_ID,
            SystemProgram.programId
        );

        const bidAmount = new anchor.BN(1500000000);

        const transactionSignature = await program.methods
            .placeBid(bidAmount)
            .accounts({
                auction: auctionKeyPair.publicKey,
                bidder: bidderKeypair.publicKey,
                bidderTokenAccount: bidderTokenAccount,
                escrowAccount: escrowAccount,
                mint: mintKeyPair.publicKey,
                tokenProgram: TOKEN_PROGRAM_ID,
                systemProgram: SystemProgram.programId,
            })
            .preInstructions([createEscrowAccountIx])
            .signers([bidderKeypair])
            .rpc({ skipPreflight: true });

        console.log('Bid placed');
        console.log('Transaction signature', transactionSignature);
    });

1 Answer 1

3

Your auction account is a program derived address (PDA), defined by your Auction struct. In the client side, you must derive that PDA and then pass it, it can't be just a public key as you are doing it right now.

In your program as well, you must define the seeds that the Auction PDA needs along with some other attributes, which you are not doing right now.

More about PDAs: https://solanacookbook.com/core-concepts/pdas.html#generating-pdas

Detailed Guide on using them: https://www.quicknode.com/guides/solana-development/anchor/how-to-use-program-derived-addresses

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.