Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auction: moves the amount of items to be sold within the item_info struct #22

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions contracts/auctions/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ impl MarketplaceContract {
// placeholder
&item_info.buy_now_price.unwrap_or(1),
&item_info.minimum_price.unwrap_or(1),
&item_info.amount,
];

validate_input_params(&env, &input_values[..])?;

let auction_token = get_auction_token(&env)?;
Expand All @@ -70,7 +72,7 @@ impl MarketplaceContract {
);

// we need at least one item to start an auction
if item_balance < 1 {
if item_balance < item_info.amount {
log!(
&env,
"Auction: Create Auction: Not enough balance of the item to sell"
Expand Down Expand Up @@ -213,7 +215,7 @@ impl MarketplaceContract {
&auction.seller,
&highest_bid.bidder,
&auction.item_info.item_id,
&1,
&auction.item_info.amount,
);

auction.status = AuctionStatus::Ended;
Expand Down
6 changes: 4 additions & 2 deletions contracts/auctions/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct ItemInfo {
pub item_id: u64,
pub minimum_price: Option<u64>,
pub buy_now_price: Option<u64>,
pub amount: u64,
}

#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -177,7 +178,8 @@ pub fn validate_input_params(env: &Env, values_to_check: &[&u64]) -> Result<(),
if i < &&1 {
log!(
&env,
"Auction: Validate input: parameters cannot be less than 1"
"Auction: Validate input: parameter is less than 1: ",
**i
);
panic_with_error!(&env, ContractError::InvalidInputs);
}
Expand Down Expand Up @@ -307,7 +309,7 @@ mod test {
use super::validate_input_params;

#[test]
#[should_panic(expected = "Auction: Validate input: parameters cannot be less than 1")]
#[should_panic(expected = "Auction: Validate input: parameter is less than 1: ")]
fn validate_input_params_should_fail_with_invalid_input() {
let env = Env::default();
let _ = validate_input_params(&env, &[&1, &2, &3, &0]);
Expand Down
18 changes: 17 additions & 1 deletion contracts/auctions/src/test/bids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn should_place_a_bid() {
item_id: 1u64,
minimum_price: Some(10),
buy_now_price: Some(50),
amount: 1,
};

mp_client.create_auction(&item_info, &seller, &WEEKLY);
Expand Down Expand Up @@ -123,6 +124,7 @@ fn fail_to_place_bid_when_auction_inactive() {
item_id: 1u64,
minimum_price: Some(10),
buy_now_price: Some(50),
amount: 1,
};

mp_client.create_auction(&item_info, &seller, &WEEKLY);
Expand Down Expand Up @@ -162,6 +164,7 @@ fn seller_tries_to_place_a_bid_should_fail() {
item_id: 1,
minimum_price: None,
buy_now_price: None,
amount: 1,
};

mp_client.create_auction(&item_info, &seller, &WEEKLY);
Expand Down Expand Up @@ -203,6 +206,7 @@ fn buy_now_should_fail_when_auction_not_active() {
item_id: 1,
minimum_price: None,
buy_now_price: Some(50),
amount: 1,
};

mp_client.create_auction(&item_info, &seller, &DAY);
Expand Down Expand Up @@ -244,6 +248,7 @@ fn buy_now_should_fail_when_no_buy_now_price_has_been_set() {
item_id: 1,
minimum_price: None,
buy_now_price: None,
amount: 1,
};

mp_client.create_auction(&item_info, &seller, &DAY);
Expand Down Expand Up @@ -280,7 +285,7 @@ fn buy_now() {
None,
);

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

collections_client.set_approval_for_transfer(&mp_client.address, &1u64, &true);

Expand All @@ -289,6 +294,7 @@ fn buy_now() {
item_id: 1,
minimum_price: Some(10),
buy_now_price: Some(50),
amount: 5,
};

mp_client.create_auction(&item_info, &seller, &WEEKLY);
Expand Down Expand Up @@ -381,6 +387,7 @@ fn pause_changes_status_and_second_attempt_fails_to_pause() {
item_id: 1,
minimum_price: None,
buy_now_price: None,
amount: 1,
};

mp_client.create_auction(&item_info, &seller, &WEEKLY);
Expand Down Expand Up @@ -432,6 +439,7 @@ fn pause_after_enddate_should_fail() {
item_id: 1,
minimum_price: None,
buy_now_price: None,
amount: 1,
};

mp_client.create_auction(&item_info, &seller, &WEEKLY);
Expand Down Expand Up @@ -472,6 +480,7 @@ fn unpause_changes_status_and_second_attempt_fails_to_unpause() {
item_id: 1,
minimum_price: None,
buy_now_price: Some(10),
amount: 1,
};

mp_client.create_auction(&item_info, &seller, &WEEKLY);
Expand Down Expand Up @@ -565,6 +574,7 @@ fn multiple_auction_by_multiple_sellers() {
item_id: 1,
minimum_price: Some(100),
buy_now_price: Some(500),
amount: 1,
};

collection_a_client.mint(&seller_a, &seller_a, &2, &1);
Expand All @@ -576,6 +586,7 @@ fn multiple_auction_by_multiple_sellers() {
item_id: 2,
minimum_price: Some(500),
buy_now_price: Some(900),
amount: 1,
};

mp_client.create_auction(&second_item_info_seller_a, &seller_a, &WEEKLY);
Expand All @@ -585,6 +596,7 @@ fn multiple_auction_by_multiple_sellers() {
item_id: 1,
minimum_price: Some(50),
buy_now_price: None,
amount: 1,
};

mp_client.create_auction(&item_info_seller_b, &seller_b, &WEEKLY);
Expand All @@ -594,6 +606,7 @@ fn multiple_auction_by_multiple_sellers() {
item_id: 1,
minimum_price: None,
buy_now_price: None,
amount: 1,
};

mp_client.create_auction(&item_info_seller_c, &seller_c, &DAY);
Expand Down Expand Up @@ -871,6 +884,7 @@ fn buy_now_should_fail_when_status_is_different_from_active() {
item_id: 1,
minimum_price: None,
buy_now_price: Some(10),
amount: 1,
};

mp_client.create_auction(&item_info, &seller, &WEEKLY);
Expand Down Expand Up @@ -917,6 +931,7 @@ fn buy_now_should_work_when_no_previous_bid() {
item_id: 1,
minimum_price: Some(10),
buy_now_price: Some(50),
amount: 1,
};

mp_client.create_auction(&item_info, &seller, &WEEKLY);
Expand Down Expand Up @@ -963,6 +978,7 @@ fn buy_now_should_refund_previous_buyer() {
item_id: 1,
minimum_price: Some(10),
buy_now_price: Some(50),
amount: 1,
};

mp_client.create_auction(&item_info, &seller, &WEEKLY);
Expand Down
14 changes: 13 additions & 1 deletion contracts/auctions/src/test/finalize_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fn finalize_auction() {
item_id: 1,
minimum_price: None,
buy_now_price: None,
amount: 1,
};

collections_client.set_approval_for_transfer(&mp_client.address, &1, &true);
Expand Down Expand Up @@ -135,6 +136,7 @@ fn fail_to_finalyze_auction_when_endtime_not_reached() {
item_id: 1u64,
minimum_price: Some(10),
buy_now_price: Some(50),
amount: 1,
};

mp_client.create_auction(&item_info, &seller, &WEEKLY);
Expand Down Expand Up @@ -178,6 +180,7 @@ fn finalize_auction_when_minimal_price_not_reached_should_refund_last_bidder() {
item_id: 1u64,
minimum_price: Some(10),
buy_now_price: Some(50),
amount: 1,
};

mp_client.create_auction(&item_info, &seller, &WEEKLY);
Expand Down Expand Up @@ -231,6 +234,7 @@ fn fail_to_finalyze_auction_when_not_correct_state() {
item_id: 1u64,
minimum_price: Some(10),
buy_now_price: Some(50),
amount: 1,
};

mp_client.create_auction(&item_info, &seller, &WEEKLY);
Expand Down Expand Up @@ -261,27 +265,35 @@ fn get_active_auctions_should_list_correct_number_of_active_auctions() {
None,
);

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

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

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

let third_item = ItemInfo {
collection_addr: nft_collection_client.address.clone(),
item_id: 3u64,
minimum_price: Some(10),
buy_now_price: Some(50),
amount: 15,
};
mp_client.create_auction(&first_item, &seller, &WEEKLY);
mp_client.create_auction(&second_item, &seller, &WEEKLY);
Expand Down
38 changes: 35 additions & 3 deletions contracts/auctions/src/test/initialization.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extern crate std;
use soroban_sdk::{testutils::Address as _, token, Address, Env};
use soroban_sdk::{testutils::Address as _, Address, Env};

use crate::{
collection,
Expand Down Expand Up @@ -35,7 +35,7 @@ fn mp_should_create_auction() {
env.budget().reset_unlimited();
let seller = Address::generate(&env);

let token_client = token::Client::new(&env, &Address::generate(&env));
let token_client = deploy_token_contract(&env, &Address::generate(&env));
let (mp_client, nft_collection_client) = generate_marketplace_and_collection_client(
&env,
&seller,
Expand All @@ -49,6 +49,7 @@ fn mp_should_create_auction() {
item_id: 1u64,
minimum_price: Some(10),
buy_now_price: Some(50),
amount: 1,
};

// check if we have minted two
Expand Down Expand Up @@ -100,7 +101,7 @@ fn mp_should_fail_to_create_auction_where_not_enought_balance_of_the_item() {
env.budget().reset_unlimited();
let seller = Address::generate(&env);

let token_client = token::Client::new(&env, &Address::generate(&env));
let token_client = deploy_token_contract(&env, &Address::generate(&env));
// we don't want to use the collection from the setup method, as this will automatically
// mint an item for the auction.
let (mp_client, _) = generate_marketplace_and_collection_client(
Expand All @@ -125,6 +126,7 @@ fn mp_should_fail_to_create_auction_where_not_enought_balance_of_the_item() {
item_id: 1u64,
minimum_price: Some(10),
buy_now_price: Some(50),
amount: 1,
};

assert_eq!(
Expand Down Expand Up @@ -238,3 +240,33 @@ fn get_auction_by_seller_should_return_an_err_when_id_not_found() {
Err(Ok(ContractError::AuctionNotFound))
)
}

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

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

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

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

assert_eq!(
mp_client.try_create_auction(&item_info, &seller, &WEEKLY),
Err(Ok(ContractError::InvalidInputs))
);
}
1 change: 1 addition & 0 deletions contracts/auctions/src/test/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub fn create_multiple_auctions(
item_id: idx as u64,
minimum_price: None,
buy_now_price: None,
amount: 1,
};
mp_client.create_auction(&item_info, seller, &WEEKLY);
}
Expand Down
Loading