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 d926a48
Show file tree
Hide file tree
Showing 4 changed files with 71 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);
}
}
11 changes: 11 additions & 0 deletions aptos-move/move-examples/tests/move_unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ fn test_resource_account_common(pkg: &str) {
run_tests_for_pkg(pkg, named_address);
}

// TODO: Figure out how to automatically run all tests and do address assignments.
// The below is too messy.
#[test]
fn test_veiled_coin() {
let named_address = BTreeMap::from([(
Expand Down Expand Up @@ -246,6 +248,15 @@ fn test_nft_dao_test() {
run_tests_for_pkg("dao/nft_dao", named_address);
}

#[test]
fn test_claim() {
let named_address = BTreeMap::from([(
String::from("claim"),
AccountAddress::from_hex_literal("0xcafe").unwrap(),
)]);
run_tests_for_pkg("claim", named_address);
}

#[test]
fn test_swap() {
let named_address = BTreeMap::from([
Expand Down

0 comments on commit d926a48

Please sign in to comment.