-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "claim" | ||
version = "0.0.0" | ||
|
||
[addresses] | ||
claim = "_" | ||
|
||
[dependencies] | ||
AptosStdlib = { local = "../../framework/aptos-stdlib" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/// Module to allow efficiently check and claim spots in a limited claim list. | ||
/// Developers can include this module in their application and just need to: | ||
/// 1. Change the package name to their own package name. | ||
/// 2. Add friend declarations for modules that need to depend on the claim_list module. | ||
/// 3. Call is_claimed and claim functions in their own code. | ||
module claim::claim_list { | ||
use aptos_std::smart_table::{Self, SmartTable}; | ||
|
||
// Add friend declarations for modules that need to depend on claim_list. | ||
|
||
struct ClaimList has key { | ||
// Key can also be replaced with address if the claim_list is tracked by user addresses. | ||
codes_claimed: SmartTable<u64, bool>, | ||
} | ||
|
||
fun init_module(claim_list_signer: &signer) { | ||
move_to<ClaimList>(claim_list_signer, ClaimList { | ||
codes_claimed: smart_table::new(), | ||
}); | ||
} | ||
|
||
public fun is_claimed(invite_code: u64): bool acquires ClaimList { | ||
let codes_claimed = &borrow_global<ClaimList>(@claim).codes_claimed; | ||
smart_table::contains(codes_claimed, invite_code) | ||
} | ||
|
||
public(friend) fun claim(invite_code: u64) acquires ClaimList { | ||
let codes_claimed = &mut borrow_global_mut<ClaimList>(@claim).codes_claimed; | ||
smart_table::add(codes_claimed, invite_code, true); | ||
} | ||
|
||
#[test_only] | ||
friend claim::test_claim; | ||
|
||
#[test_only] | ||
public fun init_for_test(claim: &signer) { | ||
init_module(claim); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
aptos-move/move-examples/claim/sources/tests/test_claim.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#[test_only] | ||
module claim::test_claim { | ||
use claim::claim_list; | ||
|
||
#[test(claim = @0xcafe)] | ||
fun test_claim(claim: &signer) { | ||
claim_list::init_for_test(claim); | ||
assert!(!claim_list::is_claimed(1), 0); | ||
claim_list::claim(1); | ||
assert!(claim_list::is_claimed(1), 1); | ||
} | ||
} |