Skip to content

Commit

Permalink
feat: add remove_whitelist_account and fix update_allowance (#106)
Browse files Browse the repository at this point in the history
* fix: improve remaining allowance and fix `update_allowance` with option

* feat: add `remove_whitelist_account`
  • Loading branch information
willemneal authored Aug 17, 2022
1 parent def68d9 commit 1b62467
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
20 changes: 20 additions & 0 deletions __test__/allowance.ava.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,24 @@ runner.test("presale allowance should only allow 2 then 2 in public", async (t,
t.assert(last_try.failed, "tx didn't fail");
tokens = await getTokens(tenk, alice);
t.assert(tokens.length == 4);
});


runner.test("presale allowance should only allow 1", async (t, { root, tenk, alice }) => {
await root.call(tenk, "add_whitelist_accounts", { accounts: [alice], max_allowance: 1 });
const cost = await totalCost(tenk, 1, alice.accountId);
await mint(tenk, alice, cost);
let last_try = await mint_raw(tenk, alice, cost);
t.assert(last_try.failed, "tx didn't fail");
const tokens = await getTokens(tenk, alice);
t.assert(tokens.length == 1);

await root.call(tenk, "start_sale", {})
t.log(await tenk.view("get_sale_info"));

t.is(await tenk.view("remaining_allowance",{account_id: alice}), 1);

t.assert(await root.call(tenk, "update_allowance", {}));

t.is(await tenk.view("remaining_allowance",{account_id: alice}), null);
});
15 changes: 13 additions & 2 deletions contracts/tenk/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ impl Contract {
true
}

/// This is the allowance during the public sale.
/// When an allowance isn't provided, it is unlimited.
/// e.g. submit with no `allowance` argument
/// @allow ["::admins", "::owner"]
pub fn update_allowance(&mut self, allowance: u16) -> bool {
pub fn update_allowance(&mut self, allowance: Option<u16>) -> bool {
self.assert_owner_or_admin();
self.sale.allowance = Some(allowance);
self.sale.allowance = allowance;
true
}

Expand Down Expand Up @@ -69,6 +72,14 @@ impl Contract {
true
}

/// Remove whitelisted account. If account is removed, the number of tokens left in returned.
/// @allow ["::admins", "::owner"]
pub fn remove_whitelist_account(&mut self, account_id: AccountId) -> Option<u16> {
self.assert_owner_or_admin();
self.whitelist.remove(&account_id).as_ref().map(Allowance::left)
}


/// Increases allowance for whitelist accounts
/// @allow ["::admins", "::owner"]
pub fn update_whitelist_accounts(
Expand Down
19 changes: 10 additions & 9 deletions contracts/tenk/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,17 @@ impl Contract {
}

/// How many tokens an account is still allowed to mint. None, means unlimited
pub fn remaining_allowance(&self, account_id: &AccountId, new_max: Option<u16>) -> Option<u16> {
pub fn remaining_allowance(&self, account_id: &AccountId) -> Option<u16> {
let allowance = if self.is_presale() {
0
} else if let Some(allowance) = self.sale.allowance {
allowance
} else {
return None;
};
self.whitelist
.get(account_id)
.map(|a| a.raise_max(new_max.unwrap_or(0)).left())
.map(|a| a.raise_max(allowance).left())
}

/// Max number of mints in one transaction. None, means unlimited
Expand All @@ -78,13 +85,7 @@ impl Contract {
/// Information about a current user. Whether they are VIP and how many tokens left in their allowance.
pub fn get_user_sale_info(&self, account_id: &AccountId) -> UserSaleInfo {
let sale_info = self.get_sale_info();
let remaining_allowance = if self.is_presale() {
self.remaining_allowance(account_id, None)
} else if let Some(allowance) = self.sale.allowance {
self.remaining_allowance(account_id, Some(allowance))
} else {
None
};
let remaining_allowance = self.remaining_allowance(account_id);
UserSaleInfo {
sale_info,
remaining_allowance,
Expand Down

0 comments on commit 1b62467

Please sign in to comment.