forked from glitchpilot/sui_TN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreedCommunity.move
56 lines (52 loc) · 1.72 KB
/
ThreedCommunity.move
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// ThreedCommunity.move - Smart contract for managing communities with token-gated access
import 0x1::Prelude;
import 0x2::Coin;
// Define the Community struct to represent a community
resource struct Community {
id: u64;
name: vector<u8>;
owner: address;
token_type: vector<u8>;
token_amount_required: u64;
members: vector<address>;
}
public fun create_community(
name: vector<u8>,
token_type: vector<u8>,
token_amount_required: u64,
): u64 {
let id = 1; // For simplicity, use a static ID
let owner = get_txn_sender();
let new_community = Community {
id: id,
name: name,
owner: owner,
token_type: token_type,
token_amount_required: token_amount_required,
members: empty<address>(),
};
move_to(new_community, &mut Ctx.self_communities);
id
}
public fun join_community(community_id: u64): bool {
let community: &mut Community;
let sender = get_txn_sender();
if borrow_global<Community>(community_id) {
community = copy(g);
if sender != community.owner {
let token_balance = Coin::balance(sender, community.token_type);
if token_balance >= community.token_amount_required {
move_from(sender, &mut Coin.self, community.token_type, community.token_amount_required);
push_back(&mut community.members, sender);
move_to(community, &mut Ctx.self_communities);
true
} else {
false // Insufficient tokens to join the community
}
} else {
false // Community owner cannot join their own community
}
} else {
false // Community with the given ID does not exist
}
}