Skip to content

Commit

Permalink
handles an edge case when we search for active auctions and some of t…
Browse files Browse the repository at this point in the history
…he auctions are not active in storage
gangov committed Sep 11, 2024
1 parent dcf9220 commit 699d8c2
Showing 3 changed files with 67 additions and 9 deletions.
4 changes: 2 additions & 2 deletions contracts/auctions/src/contract.rs
Original file line number Diff line number Diff line change
@@ -367,8 +367,8 @@ impl MarketplaceContract {
#[allow(dead_code)]
pub fn get_active_auctions(
env: Env,
start_index: Option<u32>,
limit: Option<u32>,
start_index: Option<u64>,
limit: Option<u64>,
) -> Result<Vec<Auction>, ContractError> {
let all_auctions = get_auctions(&env, start_index, limit)?;

22 changes: 16 additions & 6 deletions contracts/auctions/src/storage.rs
Original file line number Diff line number Diff line change
@@ -9,8 +9,8 @@ pub const LIFETIME_THRESHOLD: u32 = BUMP_AMOUNT - DAY_IN_LEDGERS;

// consts for Pagination
// since we start counting from 1, default would be 1 as well
pub const DEFAULT_INDEX: u32 = 1;
pub const DEFAULT_LIMIT: u32 = 10;
pub const DEFAULT_INDEX: u64 = 1;
pub const DEFAULT_LIMIT: u64 = 10;

#[contracttype]
#[derive(Clone)]
@@ -77,16 +77,26 @@ pub fn generate_auction_id(env: &Env) -> Result<u64, ContractError> {

pub fn get_auctions(
env: &Env,
start_index: Option<u32>,
limit: Option<u32>,
start_index: Option<u64>,
limit: Option<u64>,
) -> Result<Vec<Auction>, ContractError> {
let start_index = start_index.unwrap_or(DEFAULT_INDEX);
let limit = limit.unwrap_or(DEFAULT_LIMIT);

// this is a safeguard only for the case when `DEFAULT_LIMIT` is higher than the actually
// saved auctions and we use `None` and `None` for `start_index` and `limit`.
// I.e. we have just 3 auctions and we want to query them
let maybe_highest_index: u64 = env
.storage()
.instance()
.get(&DataKey::AuctionId)
.expect("no previous value");

let limit = limit.unwrap_or(DEFAULT_LIMIT).min(maybe_highest_index);

let mut auctions = vec![&env];

for id in start_index..=limit {
match get_auction_by_id(env, id as u64) {
match get_auction_by_id(env, id) {
Ok(auction) => auctions.push_back(auction),
Err(ContractError::AuctionNotFound) => continue,
Err(e) => return Err(e),
50 changes: 49 additions & 1 deletion contracts/auctions/src/test/finalize_auction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use soroban_sdk::{
testutils::{Address as _, Ledger},
Address, Env,
vec, Address, Env,
};

use crate::{
@@ -244,3 +244,51 @@ fn fail_to_finalyze_auction_when_not_correct_state() {
Err(Ok(ContractError::AuctionNotActive))
);
}

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

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

let token_client = token::Client::new(&env, &Address::generate(&env));
let (mp_client, nft_collection_client) = generate_marketplace_and_collection_client(
&env,
&seller,
&token_client.address,
None,
None,
);

nft_collection_client.mint_batch(&seller, &seller, &vec![&env, 1, 2, 3], &vec![&env, 1, 1, 1]);

let first_item = ItemInfo {
collection_addr: nft_collection_client.address.clone(),
item_id: 1u64,
minimum_price: Some(10),
buy_now_price: Some(50),
};

let second_item = ItemInfo {
collection_addr: nft_collection_client.address.clone(),
item_id: 2u64,
minimum_price: Some(10),
buy_now_price: Some(50),
};

let third_item = ItemInfo {
collection_addr: nft_collection_client.address.clone(),
item_id: 3u64,
minimum_price: Some(10),
buy_now_price: Some(50),
};
mp_client.create_auction(&first_item, &seller, &WEEKLY);
mp_client.create_auction(&second_item, &seller, &WEEKLY);
mp_client.create_auction(&third_item, &seller, &WEEKLY);

assert_eq!(mp_client.get_active_auctions(&None, &None).len(), 3);

mp_client.pause(&1);
assert_eq!(mp_client.get_active_auctions(&None, &None).len(), 2);
}

0 comments on commit 699d8c2

Please sign in to comment.