1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use anchor_lang::prelude::*;

use crate::{errors::*, sell::config::*};

pub fn assert_auction_active(listing_config: &Account<ListingConfig>) -> Result<()> {
    let clock = Clock::get()?;
    let current_timestamp = clock.unix_timestamp;

    if current_timestamp < listing_config.start_time {
        return err!(AuctioneerError::AuctionNotStarted);
    } else if current_timestamp > listing_config.end_time {
        return err!(AuctioneerError::AuctionEnded);
    }

    Ok(())
}

pub fn assert_auction_over(listing_config: &Account<ListingConfig>) -> Result<()> {
    let clock = Clock::get()?;
    let current_timestamp = clock.unix_timestamp;

    if current_timestamp < listing_config.end_time {
        return err!(AuctioneerError::AuctionActive);
    }

    Ok(())
}

pub fn assert_higher_bid(
    listing_config: &Account<ListingConfig>,
    new_bid_price: u64,
) -> Result<()> {
    if new_bid_price <= listing_config.highest_bid.amount {
        return err!(AuctioneerError::BidTooLow);
    } else if (listing_config.highest_bid.amount > 0)
        && (new_bid_price < (listing_config.highest_bid.amount + listing_config.min_bid_increment))
    {
        return err!(AuctioneerError::BelowBidIncrement);
    }

    Ok(())
}

pub fn assert_exceeds_reserve_price(
    listing_config: &Account<ListingConfig>,
    new_bid_price: u64,
) -> Result<()> {
    if new_bid_price < listing_config.reserve_price {
        return err!(AuctioneerError::BelowReservePrice);
    }

    Ok(())
}

pub fn assert_highest_bidder(
    listing_config: &Account<ListingConfig>,
    buyer_trade_state: Pubkey,
) -> Result<()> {
    if buyer_trade_state != listing_config.highest_bid.buyer_trade_state {
        return err!(AuctioneerError::NotHighestBidder);
    }

    Ok(())
}

pub fn process_time_extension(listing_config: &mut Account<ListingConfig>) -> Result<()> {
    let clock = Clock::get()?;
    let current_timestamp = clock.unix_timestamp;

    if current_timestamp >= (listing_config.end_time - i64::from(listing_config.time_ext_period)) {
        listing_config.end_time += i64::from(listing_config.time_ext_delta);
    }

    Ok(())
}