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

fix: amount sold incorrect for tiers with limits (backport #603) #615

Merged
merged 2 commits into from
Nov 19, 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
28 changes: 21 additions & 7 deletions contracts/non-fungible-tokens/andromeda-crowdfund/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -99,12 +99,14 @@ impl MockCrowdfund {
self.query(app, msg)
}

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<u64>,
limit: Option<u32>,
order_by: Option<OrderBy>,
) -> TiersResponse {
let msg = mock_query_tiers_msg(start_after, limit, order_by);
self.query(app, msg)
}
}
Expand Down Expand Up @@ -177,3 +179,15 @@ pub fn mock_claim_msg() -> ExecuteMsg {
pub fn mock_purchase_cw20_msg(orders: Vec<SimpleTierOrder>) -> Cw20HookMsg {
Cw20HookMsg::PurchaseTiers { orders }
}

pub fn mock_query_tiers_msg(
start_after: Option<u64>,
limit: Option<u32>,
order_by: Option<OrderBy>,
) -> QueryMsg {
QueryMsg::Tiers {
start_after,
limit,
order_by,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub(crate) fn get_tiers(
.take(limit)
.map(|v| {
let (level, tier) = v?;
let sold_amount = TIER_SALES.load(storage, level).unwrap_or_default();
let sold_amount = TIER_SALES.may_load(storage, level)?.unwrap_or_default();
Ok(TierResponseItem { tier, sold_amount })
})
.collect()
Expand Down
32 changes: 29 additions & 3 deletions tests-integration/tests/crowdfund_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -653,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
Expand Down
Loading