Skip to content

Commit

Permalink
Add claim_list
Browse files Browse the repository at this point in the history
  • Loading branch information
movekevin committed Dec 22, 2023
1 parent 7b72dca commit a78d7a9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
9 changes: 9 additions & 0 deletions aptos-move/move-examples/claim/Move.toml
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" }
39 changes: 39 additions & 0 deletions aptos-move/move-examples/claim/sources/claim.move
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 aptos-move/move-examples/claim/sources/tests/test_claim.move
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);
}
}

0 comments on commit a78d7a9

Please sign in to comment.