From 4474cef9af1f9961cbdf91837979f2ee6a62b2b1 Mon Sep 17 00:00:00 2001 From: Connor Barr Date: Mon, 11 Nov 2024 13:41:43 +0000 Subject: [PATCH 1/2] fix: amount sold incorrect for tiers with limits (#603) (cherry picked from commit 60d6ca65367658a8817b87d57d3c635792f06780) # Conflicts: # contracts/non-fungible-tokens/andromeda-crowdfund/src/mock.rs # contracts/non-fungible-tokens/andromeda-crowdfund/src/state.rs --- .../andromeda-crowdfund/src/mock.rs | 25 ++++++++++++++++- .../andromeda-crowdfund/src/state.rs | 11 ++++++++ tests-integration/tests/crowdfund_app.rs | 28 ++++++++++++++++++- 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/contracts/non-fungible-tokens/andromeda-crowdfund/src/mock.rs b/contracts/non-fungible-tokens/andromeda-crowdfund/src/mock.rs index 45a514d00..23cb45303 100644 --- a/contracts/non-fungible-tokens/andromeda-crowdfund/src/mock.rs +++ b/contracts/non-fungible-tokens/andromeda-crowdfund/src/mock.rs @@ -5,7 +5,7 @@ use andromeda_non_fungible_tokens::crowdfund::{ CampaignConfig, CampaignSummaryResponse, Cw20HookMsg, ExecuteMsg, InstantiateMsg, PresaleTierOrder, QueryMsg, SimpleTierOrder, Tier, TierMetaData, TiersResponse, }; -use andromeda_std::common::expiration::Expiry; +use andromeda_std::common::{expiration::Expiry, OrderBy}; use andromeda_testing::{ mock::MockApp, mock_ado, @@ -99,12 +99,23 @@ impl MockCrowdfund { self.query(app, msg) } +<<<<<<< HEAD pub fn query_tiers(&self, app: &mut MockApp) -> TiersResponse { let msg = QueryMsg::Tiers { start_after: None, limit: None, order_by: None, }; +======= + pub fn query_tiers( + &self, + app: &mut MockApp, + start_after: Option, + limit: Option, + order_by: Option, + ) -> TiersResponse { + let msg = mock_query_tiers_msg(start_after, limit, order_by); +>>>>>>> 60d6ca65 (fix: amount sold incorrect for tiers with limits (#603)) self.query(app, msg) } } @@ -177,3 +188,15 @@ pub fn mock_claim_msg() -> ExecuteMsg { pub fn mock_purchase_cw20_msg(orders: Vec) -> Cw20HookMsg { Cw20HookMsg::PurchaseTiers { orders } } + +pub fn mock_query_tiers_msg( + start_after: Option, + limit: Option, + order_by: Option, +) -> QueryMsg { + QueryMsg::Tiers { + start_after, + limit, + order_by, + } +} diff --git a/contracts/non-fungible-tokens/andromeda-crowdfund/src/state.rs b/contracts/non-fungible-tokens/andromeda-crowdfund/src/state.rs index 8b92d9e98..4bc2a0870 100644 --- a/contracts/non-fungible-tokens/andromeda-crowdfund/src/state.rs +++ b/contracts/non-fungible-tokens/andromeda-crowdfund/src/state.rs @@ -175,7 +175,11 @@ pub(crate) fn get_tiers( .take(limit) .map(|v| { let (level, tier) = v?; +<<<<<<< HEAD let sold_amount = TIER_SALES.load(storage, level).unwrap_or_default(); +======= + let sold_amount = TIER_SALES.may_load(storage, level)?.unwrap_or_default(); +>>>>>>> 60d6ca65 (fix: amount sold incorrect for tiers with limits (#603)) Ok(TierResponseItem { tier, sold_amount }) }) .collect() @@ -215,11 +219,18 @@ pub(crate) fn set_tier_orders( .unwrap_or_default(); sold_amount = sold_amount.checked_add(new_order.amount)?; if let Some(limit) = tier.limit { +<<<<<<< HEAD ensure!(limit > sold_amount, ContractError::PurchaseLimitReached {}); } update_tier(storage, &tier)?; set_tier_sales(storage, new_order.level.into(), sold_amount)?; +======= + ensure!(limit >= sold_amount, ContractError::PurchaseLimitReached {}); + } + update_tier(storage, &tier)?; + set_tier_sales(storage, new_order.level.into(), sold_amount)?; +>>>>>>> 60d6ca65 (fix: amount sold incorrect for tiers with limits (#603)) let mut order = TIER_ORDERS .load(storage, (new_order.orderer.clone(), new_order.level.into())) .unwrap_or_default(); diff --git a/tests-integration/tests/crowdfund_app.rs b/tests-integration/tests/crowdfund_app.rs index 69c924194..08525721c 100644 --- a/tests-integration/tests/crowdfund_app.rs +++ b/tests-integration/tests/crowdfund_app.rs @@ -493,12 +493,38 @@ fn test_crowdfund_app_native_with_ado_recipient( .query_balance(buyer_one.clone(), "uandr") .unwrap() .amount; + + let tiers_pre_sale = crowdfund.query_tiers(&mut router, None, None, None); let _ = crowdfund.execute_purchase( buyer_one.clone(), &mut router, - orders, + orders.clone(), vec![coin(5000, "uandr")], ); + let tiers_post_sale = crowdfund.query_tiers(&mut router, None, None, None); + + // Verify each tier's amount_sold increased by the correct amount + for order in orders { + let pre_tier = tiers_pre_sale + .tiers + .iter() + .find(|t| t.tier.level == order.level) + .unwrap(); + let post_tier = tiers_post_sale + .tiers + .iter() + .find(|t| t.tier.level == order.level) + .unwrap(); + + assert_eq!( + post_tier.sold_amount, + pre_tier.sold_amount + order.amount, + "Tier {} amount_sold should increase by {}", + order.level, + order.amount + ); + } + let buyer_one_balance = router .wrap() .query_balance(buyer_one.clone(), "uandr") From f86d174cdd5209616d745223713f6080252bed74 Mon Sep 17 00:00:00 2001 From: Connor Barr Date: Tue, 19 Nov 2024 13:10:18 +0000 Subject: [PATCH 2/2] fix: conflicts --- .../andromeda-crowdfund/src/mock.rs | 9 --------- .../andromeda-crowdfund/src/state.rs | 11 ----------- tests-integration/tests/crowdfund_app.rs | 4 ++-- 3 files changed, 2 insertions(+), 22 deletions(-) diff --git a/contracts/non-fungible-tokens/andromeda-crowdfund/src/mock.rs b/contracts/non-fungible-tokens/andromeda-crowdfund/src/mock.rs index 23cb45303..532d77f79 100644 --- a/contracts/non-fungible-tokens/andromeda-crowdfund/src/mock.rs +++ b/contracts/non-fungible-tokens/andromeda-crowdfund/src/mock.rs @@ -99,14 +99,6 @@ impl MockCrowdfund { self.query(app, msg) } -<<<<<<< HEAD - pub fn query_tiers(&self, app: &mut MockApp) -> TiersResponse { - let msg = QueryMsg::Tiers { - start_after: None, - limit: None, - order_by: None, - }; -======= pub fn query_tiers( &self, app: &mut MockApp, @@ -115,7 +107,6 @@ impl MockCrowdfund { order_by: Option, ) -> TiersResponse { let msg = mock_query_tiers_msg(start_after, limit, order_by); ->>>>>>> 60d6ca65 (fix: amount sold incorrect for tiers with limits (#603)) self.query(app, msg) } } diff --git a/contracts/non-fungible-tokens/andromeda-crowdfund/src/state.rs b/contracts/non-fungible-tokens/andromeda-crowdfund/src/state.rs index 4bc2a0870..ca880ee78 100644 --- a/contracts/non-fungible-tokens/andromeda-crowdfund/src/state.rs +++ b/contracts/non-fungible-tokens/andromeda-crowdfund/src/state.rs @@ -175,11 +175,7 @@ pub(crate) fn get_tiers( .take(limit) .map(|v| { let (level, tier) = v?; -<<<<<<< HEAD - let sold_amount = TIER_SALES.load(storage, level).unwrap_or_default(); -======= let sold_amount = TIER_SALES.may_load(storage, level)?.unwrap_or_default(); ->>>>>>> 60d6ca65 (fix: amount sold incorrect for tiers with limits (#603)) Ok(TierResponseItem { tier, sold_amount }) }) .collect() @@ -219,18 +215,11 @@ pub(crate) fn set_tier_orders( .unwrap_or_default(); sold_amount = sold_amount.checked_add(new_order.amount)?; if let Some(limit) = tier.limit { -<<<<<<< HEAD ensure!(limit > sold_amount, ContractError::PurchaseLimitReached {}); } update_tier(storage, &tier)?; set_tier_sales(storage, new_order.level.into(), sold_amount)?; -======= - ensure!(limit >= sold_amount, ContractError::PurchaseLimitReached {}); - } - update_tier(storage, &tier)?; - set_tier_sales(storage, new_order.level.into(), sold_amount)?; ->>>>>>> 60d6ca65 (fix: amount sold incorrect for tiers with limits (#603)) let mut order = TIER_ORDERS .load(storage, (new_order.orderer.clone(), new_order.level.into())) .unwrap_or_default(); diff --git a/tests-integration/tests/crowdfund_app.rs b/tests-integration/tests/crowdfund_app.rs index 08525721c..8edc3854c 100644 --- a/tests-integration/tests/crowdfund_app.rs +++ b/tests-integration/tests/crowdfund_app.rs @@ -272,7 +272,7 @@ fn test_successful_crowdfund_app_native(setup: TestCase) { assert_eq!(summary.current_capital, 0); assert_eq!(summary.current_stage, CampaignStage::ONGOING.to_string()); - let tiers = crowdfund.query_tiers(&mut router).tiers; + let tiers = crowdfund.query_tiers(&mut router, None, None, None).tiers; assert_eq!(tiers.len(), 2); // Purchase tiers @@ -679,7 +679,7 @@ fn test_successful_crowdfund_app_cw20(#[with(false)] setup: TestCase) { assert_eq!(summary.current_capital, 0); assert_eq!(summary.current_stage, CampaignStage::ONGOING.to_string()); - let tiers = crowdfund.query_tiers(&mut router).tiers; + let tiers = crowdfund.query_tiers(&mut router, None, None, None).tiers; assert_eq!(tiers.len(), 2); // Purchase tiers