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

[1.0.rc-1] Crowdfund End Sale #324

Merged
merged 3 commits into from
Mar 14, 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
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,16 @@ fn execute_end_sale(ctx: ExecuteContext, limit: Option<u32>) -> Result<Response,
ensure!(state.is_some(), ContractError::NoOngoingSale {});
let state = state.unwrap();
let number_of_tokens_available = NUMBER_OF_TOKENS_AVAILABLE.load(deps.storage)?;
// In case the minimum sold tokens threshold is met, it has to be the owner who calls the function
let contract = ADOContract::default();
let has_minimum_sold = state.min_tokens_sold <= state.amount_sold;
let is_owner = contract.is_contract_owner(deps.storage, info.sender.as_str())?;

ensure!(
// If all tokens have been sold the sale can be ended too.
state.expiration.is_expired(&env.block) || number_of_tokens_available.is_zero(),
state.expiration.is_expired(&env.block)
|| number_of_tokens_available.is_zero()
|| (has_minimum_sold && is_owner),
ContractError::SaleNotEnded {}
);
if state.amount_sold < state.min_tokens_sold {
Expand Down
105 changes: 105 additions & 0 deletions contracts/non-fungible-tokens/andromeda-crowdfund/src/testing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,111 @@ fn test_end_sale_all_tokens_sold() {
);
}

#[test]
fn test_end_sale_some_tokens_sold_threshold_met() {
let mut deps = mock_dependencies_custom(&[]);
init(deps.as_mut(), None);

STATE
.save(
deps.as_mut().storage,
&State {
// Sale has not expired yet.
expiration: Expiration::AtHeight(mock_env().block.height + 1),
price: coin(100, "uusd"),
min_tokens_sold: Uint128::from(1u128),
max_amount_per_wallet: 5,
amount_sold: Uint128::from(2u128),
amount_to_send: Uint128::from(100u128),
amount_transferred: Uint128::zero(),
recipient: Recipient::from_string("recipient"),
},
)
.unwrap();

PURCHASES
.save(
deps.as_mut().storage,
"A",
&vec![Purchase {
token_id: MOCK_TOKENS_FOR_SALE[0].to_owned(),
purchaser: "A".to_string(),
tax_amount: Uint128::zero(),
msgs: vec![],
}],
)
.unwrap();

NUMBER_OF_TOKENS_AVAILABLE
.save(deps.as_mut().storage, &Uint128::one())
.unwrap();

let msg = ExecuteMsg::EndSale { limit: None };
// Only the owner can end the sale if only the minimum token threshold is met.
// Anyone can end the sale if it's expired or the remaining number of tokens available is zero.
let info = mock_info("anyone", &[]);
let err = execute(deps.as_mut(), mock_env(), info, msg.clone()).unwrap_err();
assert_eq!(err, ContractError::SaleNotEnded {});

let info = mock_info("owner", &[]);
let res = execute(deps.as_mut(), mock_env(), info, msg).unwrap();

assert_eq!(
Response::new()
.add_attribute("action", "transfer_tokens_and_send_funds")
// Burn tokens that were not purchased
.add_message(get_transfer_message(MOCK_TOKENS_FOR_SALE[0], "A")),
res
);
}

#[test]
fn test_end_sale_some_tokens_sold_threshold_not_met() {
let mut deps = mock_dependencies_custom(&[]);
init(deps.as_mut(), None);

STATE
.save(
deps.as_mut().storage,
&State {
// Sale has not expired yet.
expiration: Expiration::AtHeight(mock_env().block.height + 1),
price: coin(100, "uusd"),
min_tokens_sold: Uint128::from(2u128),
max_amount_per_wallet: 5,
amount_sold: Uint128::from(0u128),
amount_to_send: Uint128::from(100u128),
amount_transferred: Uint128::zero(),
recipient: Recipient::from_string("recipient"),
},
)
.unwrap();

PURCHASES
.save(
deps.as_mut().storage,
"A",
&vec![Purchase {
token_id: MOCK_TOKENS_FOR_SALE[0].to_owned(),
purchaser: "A".to_string(),
tax_amount: Uint128::zero(),
msgs: vec![],
}],
)
.unwrap();

NUMBER_OF_TOKENS_AVAILABLE
.save(deps.as_mut().storage, &Uint128::new(2))
.unwrap();

let msg = ExecuteMsg::EndSale { limit: None };

let info = mock_info("owner", &[]);
// Minimum sold is 2, actual sold is 0
let err = execute(deps.as_mut(), mock_env(), info, msg).unwrap_err();
assert_eq!(err, ContractError::SaleNotEnded {});
}

#[test]
fn test_end_sale_limit_zero() {
let mut deps = mock_dependencies_custom(&[]);
Expand Down
Loading