From 699d8c27468847d88b1a38623faf4432378c0f49 Mon Sep 17 00:00:00 2001 From: Kaloyan Gangov <6922910+gangov@users.noreply.github.com> Date: Wed, 11 Sep 2024 16:43:32 +0300 Subject: [PATCH] handles an edge case when we search for active auctions and some of the auctions are not active in storage --- contracts/auctions/src/contract.rs | 4 +- contracts/auctions/src/storage.rs | 22 +++++--- .../auctions/src/test/finalize_auction.rs | 50 ++++++++++++++++++- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/contracts/auctions/src/contract.rs b/contracts/auctions/src/contract.rs index 3208a296..6f4eb273 100644 --- a/contracts/auctions/src/contract.rs +++ b/contracts/auctions/src/contract.rs @@ -367,8 +367,8 @@ impl MarketplaceContract { #[allow(dead_code)] pub fn get_active_auctions( env: Env, - start_index: Option, - limit: Option, + start_index: Option, + limit: Option, ) -> Result, ContractError> { let all_auctions = get_auctions(&env, start_index, limit)?; diff --git a/contracts/auctions/src/storage.rs b/contracts/auctions/src/storage.rs index e73f3f11..48b7ae2e 100644 --- a/contracts/auctions/src/storage.rs +++ b/contracts/auctions/src/storage.rs @@ -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 { pub fn get_auctions( env: &Env, - start_index: Option, - limit: Option, + start_index: Option, + limit: Option, ) -> Result, 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), diff --git a/contracts/auctions/src/test/finalize_auction.rs b/contracts/auctions/src/test/finalize_auction.rs index f2ab100d..45c6f0b9 100644 --- a/contracts/auctions/src/test/finalize_auction.rs +++ b/contracts/auctions/src/test/finalize_auction.rs @@ -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); +}