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'