Skip to content

Commit

Permalink
wip finalize bid
Browse files Browse the repository at this point in the history
  • Loading branch information
gangov committed Aug 29, 2024
1 parent 7247960 commit 776b9e5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contracts/auctions/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl MarketplaceContract {
return Err(ContractError::NoBuyNowOption);
}

// we should probably pause the auction while the tx is happening?
// refund any previous highest bid
let token = token::Client::new(&env, &auction.currency);
token.transfer(
&buyer,
Expand Down
51 changes: 51 additions & 0 deletions contracts/auctions/src/test/bids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use crate::{
token,
};

const FOUR_HOURS: u64 = 14_400u64;

#[test]
fn should_place_a_bid() {
let env = Env::default();
Expand Down Expand Up @@ -264,3 +266,52 @@ fn seller_tries_to_place_a_bid_should_fail() {
Err(Ok(ContractError::InvalidBidder))
);
}

#[test]
fn finalize_auctoin() {
let env = Env::default();
env.mock_all_auths();
env.budget().reset_unlimited();

let admin = Address::generate(&env);
let seller = Address::generate(&env);

let bidder_a = Address::generate(&env);
let bidder_b = Address::generate(&env);
let bidder_c = Address::generate(&env);

let token_client = deploy_token_contract(&env, &admin);

token_client.mint(&bidder_a, &100);
token_client.mint(&bidder_b, &100);
token_client.mint(&bidder_c, &100);

let (mp_client, collections_client) =
generate_marketplace_and_collection_client(&env, &seller, None, None);

collections_client.mint(&seller, &seller, &1, &1);

let item_info = ItemInfo {
collection_addr: collections_client.address,
item_id: 1,
minimum_price: None,
buy_now_price: None,
};

mp_client.create_auction(&item_info, &seller, &WEEKLY, &token_client.address);

// 4 hours after the start of the auctions `bidder_a` places a bid
env.ledger().with_mut(|li| li.timestamp = FOUR_HOURS);
mp_client.place_bid(&1, &bidder_a, &5);
assert_eq!(token_client.balance(&bidder_a), 95);
assert_eq!(token_client.balance(&mp_client.address), 5);

// another 4 hours pass by and `bidder_b` places a higher bid
env.ledger().with_mut(|li| {
li.timestamp = FOUR_HOURS * 2;
});
mp_client.place_bid(&1, &bidder_b, &10);
assert_eq!(token_client.balance(&bidder_a), 100);
assert_eq!(token_client.balance(&bidder_b), 90);
assert_eq!(token_client.balance(&mp_client.address), 10);
}

0 comments on commit 776b9e5

Please sign in to comment.