1

lib.rs code

pub fn bid(ctx: Context<Bid>, price: u64) -> Result<()> {
    let auction = &mut ctx.accounts.auction;

    //
    // Check bidder's balance against auction's price.
    //
    if price < auction.price {
        return Err(error!(MarketError::InsufficientMoney));
    }

    if (Clock::get()?.unix_timestamp as u128) > auction.end_time {
        return Err(error!(MarketError::AuctionEnded));
    }

    //if refund_receiver exist return the money
    if auction.refund_receiver != Pubkey::default() {
        let seeds = &[
            b"auction",
            ctx.accounts.mint_key.key.as_ref(),
            &[auction.bump]
        ];
        let signer = &[&seeds[..]];
        let cpi_program = ctx.accounts.token_program.to_account_info();
        let cpi_accounts = Transfer {
            from: ctx.accounts.auction_token_account.to_account_info(),
            to: ctx.accounts.refund_receiver.to_account_info(),
            authority: auction.to_account_info()
        };
        let cpi_context = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer);

        let result = transfer(cpi_context, auction.price);
        if let Err(_) = result {
            return Err(error!(MarketError::SolTransferFailed));
        }
    }

    // Transfer bid price to auction token account

    let cpi_accounts = Transfer {
        from: ctx.accounts.bidder_token_account.to_account_info(),
        to: ctx.accounts.auction_token_account.to_account_info(),
        authority: ctx.accounts.bidder.to_account_info()
    };
    let cpi_program = ctx.accounts.token_program.to_account_info();
    let cpi_context = CpiContext::new(cpi_program, cpi_accounts);
    let result = transfer(cpi_context, price);
    if let Err(_) = result {
        return Err(error!(MarketError::SolTransferFailed));
    }

    //update the auction info

    let auction = &mut ctx.accounts.auction;
    auction.refund_receiver = *ctx.accounts.bidder_token_account.to_account_info().key;
    auction.price = price;  

    Ok(())

  
}

#[derive(Accounts)]
pub struct Bid<'info> {
    #[account(
        mut,
        seeds = [
            b"auction",
            mint_key.key.as_ref(),
        ],
        bump,
        has_one = creator,
        close = creator
    )]
    pub auction: Account<'info, Auction>,

    #[account(
        mut, 
        associated_token::mint = auction.mint_key,
        associated_token::authority = auction,
    )]
    pub auction_token_account: Account<'info, TokenAccount>,

    /// CHECK: This account's address is only used.
    pub mint_key: AccountInfo<'info>,

    /// CHECK: This account's address is only used.
    #[account(mut)]
    pub creator: AccountInfo<'info>,

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

    #[account(
        mut,
        constraint=bidder_token_account.owner == bidder.key(),
        constraint=bidder_token_account.mint == mint_key.key(),
    )]
    pub bidder_token_account: Account<'info, TokenAccount>,

    /// CHECK: This account's address is only used.
    #[account(mut, "refund_receiver.key == &Pubkey::default() || refund_receiver.key == &auction.refund_receiver")]
    pub refund_receiver: AccountInfo<'info>,

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

This is how I am calling in my test:

await program.methods
    .bid(new BN(price))
    .accounts({
      auction: auctionAccount,
      auctionTokenAccount: auctionTokenAccount,
      mintKey: mintKey,
      creator: ownerKey,
      bidder: bidder.key.publicKey,
      bidderTokenAccount: bidderTokenAccount,
      refundReceiver: auction.refundReceiver,
    })
    .rpc();

what am i doing wrong here??

failed: custom program error: 0x7d0'

2 Answers 2

1

I am not sure what the error is but this error says that you are violating a constraint here:

/// CHECK: This account's address is only used.
    #[account(mut, "refund_receiver.key == &Pubkey::default() || refund_receiver.key == &auction.refund_receiver")]
    pub refund_receiver: AccountInfo<'info>,

afaik the standard way to specify constraints is as you did here:

    #[account(
        mut,
        constraint=bidder_token_account.owner == bidder.key(),
        constraint=bidder_token_account.mint == mint_key.key(),
    )]
    pub bidder_token_account: Account<'info, TokenAccount>,

That syntax is a bit new to me.

basically try checking the places where you're providing the Pubkey of refund_receiver and auction.refund_receiver. Or add them to your problem so we can help!

19
  • I have added it... In bid function I use auction.refund_receiver twice once in the if statement to compare it whether it is assigned a value..
    – Lobo1897
    Commented Aug 5, 2022 at 7:51
  • Then at the end to assign it the publickey of bidder_token_account
    – Lobo1897
    Commented Aug 5, 2022 at 7:52
  • Okay I defined constraints in the standard way but still gives same error. Can u plz check the "bid" Function provided at the top.
    – Lobo1897
    Commented Aug 5, 2022 at 8:16
  • 1
    To transfer refund amount. I need the account right. I can get the refund_receiver from auction but can I transfer refund amount to it without passing a "AccountInfo" Context..?
    – Lobo1897
    Commented Aug 5, 2022 at 11:41
  • 1
    Sol transfer will do... I can transfer sol to "auction" account using system_instruction::transfer.. Right?
    – Lobo1897
    Commented Aug 5, 2022 at 14:19
0

You may be missing the signers property when calling the instruction:

await program.methods
    .bid(new BN(price))
    .accounts({
      auction: auctionAccount,
      auctionTokenAccount: auctionTokenAccount,
      mintKey: mintKey,
      creator: ownerKey,
      bidder: bidder.key.publicKey,
      bidderTokenAccount: bidderTokenAccount,
      refundReceiver: auction.refundReceiver,
    })
    .signers([bidder.key])
    .rpc();
1
  • It still gives the same error
    – Lobo1897
    Commented Aug 5, 2022 at 7:48

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.