Skip to content

Commit

Permalink
chore: scarb fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Darlington02 committed Oct 9, 2024
1 parent 5ee3567 commit 6442a83
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/community.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod community;
pub mod communitynft;
pub mod communitynft;
108 changes: 69 additions & 39 deletions src/community/community.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ pub mod CommunityComponent {
use karst::base::constants::types::{
CommunityDetails, GateKeepType, CommunityType, CommunityMember, CommunityGateKeepDetails
};
use karst::base::constants::errors::Errors::{ALREADY_MEMBER, NOT_COMMUNITY_OWNER, NOT_MEMBER, BANNED_MEMBER, UNAUTHORIZED, ONLY_PREMIUM_COMMUNITIES};
use karst::base::constants::errors::Errors::{
ALREADY_MEMBER, NOT_COMMUNITY_OWNER, NOT_MEMBER, BANNED_MEMBER, UNAUTHORIZED,
ONLY_PREMIUM_COMMUNITIES
};


// *************************************************************************
Expand All @@ -28,10 +31,18 @@ pub mod CommunityComponent {
community_owner: Map<u256, ContractAddress>, // map<owner_address, community_id>
communities: Map<u256, CommunityDetails>, // map <community_id, community_details>
member_community_id: Map<ContractAddress, u256>, // map <member address, community id>
community_member: Map<(u256, ContractAddress), CommunityMember>, // map<(community_id, member address), Member_details>
community_mod: Map<(u256, ContractAddress), bool>, // map <(community id, mod_address), bool>
community_gate_keep: Map<u256, CommunityGateKeepDetails>, // map <community, CommunityGateKeepDetails>
gate_keep_permissioned_addresses: Map<(u256, ContractAddress), bool>, // map <(u256, ContractAddress), bool>,
community_member: Map<
(u256, ContractAddress), CommunityMember
>, // map<(community_id, member address), Member_details>
community_mod: Map<
(u256, ContractAddress), bool
>, // map <(community id, mod_address), bool>
community_gate_keep: Map<
u256, CommunityGateKeepDetails
>, // map <community, CommunityGateKeepDetails>
gate_keep_permissioned_addresses: Map<
(u256, ContractAddress), bool
>, // map <(u256, ContractAddress), bool>,
community_nft_classhash: ClassHash,
community_counter: u256,
}
Expand Down Expand Up @@ -136,14 +147,18 @@ pub mod CommunityComponent {
> of ICommunity<ComponentState<TContractState>> {
// TODO: Enforce gatekeeping
/// @notice creates a new community
fn create_comminuty(ref self: ComponentState<TContractState>, community_type: CommunityType) -> u256 {
fn create_comminuty(
ref self: ComponentState<TContractState>, community_type: CommunityType
) -> u256 {
let community_owner = get_caller_address();
let community_counter = self.community_counter.read();
let community_nft_classhash = self.community_nft_classhash.read();
let community_id = community_counter + 1;

let community_nft_address = self
._deploy_community_nft(community_id, community_nft_classhash, community_id.try_into().unwrap()); // use community_id as salt since its unique
._deploy_community_nft(
community_id, community_nft_classhash, community_id.try_into().unwrap()
); // use community_id as salt since its unique

// write to storage
let community_details = CommunityDetails {
Expand All @@ -169,7 +184,7 @@ pub mod CommunityComponent {
self.community_counter.write(community_counter + 1);

// upgrade if community type is not free
if(community_type != CommunityType::Free) {
if (community_type != CommunityType::Free) {
self._upgrade_community(community_id, community_type);
}

Expand Down Expand Up @@ -201,7 +216,8 @@ pub mod CommunityComponent {
assert(is_banned != true, BANNED_MEMBER);

// mint a community token to new joiner
let minted_token_id = self._mint_community_nft(profile, community.community_nft_address);
let minted_token_id = self
._mint_community_nft(profile, community.community_nft_address);

let community_member = CommunityMember {
profile_address: profile,
Expand All @@ -220,15 +236,16 @@ pub mod CommunityComponent {
self.communities.write(community_id, updated_community);

// emit event
self.emit(
JoinedCommunity {
community_id: community_id,
transaction_executor: get_caller_address(),
token_id: minted_token_id,
profile: profile,
block_timestamp: get_block_timestamp(),
}
);
self
.emit(
JoinedCommunity {
community_id: community_id,
transaction_executor: get_caller_address(),
token_id: minted_token_id,
profile: profile,
block_timestamp: get_block_timestamp(),
}
);
}

/// @notice removes a member from a community
Expand Down Expand Up @@ -257,23 +274,24 @@ pub mod CommunityComponent {
community_total_members: community_total_members, ..community
};
self.communities.write(community_id, updated_community);

// burn user's community token
self
._burn_community_nft(
community.community_nft_address, community_member_details.community_token_id
);

// emit event
self.emit(
LeftCommunity {
community_id: community_id,
transaction_executor: get_caller_address(),
token_id: community_member_details.community_token_id,
profile: profile,
block_timestamp: get_block_timestamp(),
}
);
self
.emit(
LeftCommunity {
community_id: community_id,
transaction_executor: get_caller_address(),
token_id: community_member_details.community_token_id,
profile: profile,
block_timestamp: get_block_timestamp(),
}
);
}

/// @notice set community metadata uri
Expand Down Expand Up @@ -420,8 +438,11 @@ pub mod CommunityComponent {

// check that only premium communities can do NFTGating or PaidGating
let community_details = self.communities.read(community_id);
if(gate_keep_type == GateKeepType::NFTGating || gate_keep_type == GateKeepType::PaidGating){
assert(community_details.community_premium_status == true, ONLY_PREMIUM_COMMUNITIES);
if (gate_keep_type == GateKeepType::NFTGating
|| gate_keep_type == GateKeepType::PaidGating) {
assert(
community_details.community_premium_status == true, ONLY_PREMIUM_COMMUNITIES
);
}

let mut community_gate_keep_details = CommunityGateKeepDetails {
Expand All @@ -432,7 +453,7 @@ pub mod CommunityComponent {
};

// permissioned gatekeeping
if(gate_keep_type == GateKeepType::PermissionedGating) {
if (gate_keep_type == GateKeepType::PermissionedGating) {
self._permissioned_gatekeeping(community_id, permissioned_addresses);
}

Expand Down Expand Up @@ -536,7 +557,7 @@ pub mod CommunityComponent {
) -> (bool, CommunityGateKeepDetails) {
let gatekeep_details = self.community_gate_keep.read(community_id);

if(gatekeep_details.gate_keep_type == GateKeepType::None){
if (gatekeep_details.gate_keep_type == GateKeepType::None) {
return (false, gatekeep_details);
}

Expand All @@ -553,16 +574,22 @@ pub mod CommunityComponent {
> of PrivateTrait<TContractState> {
/// @notice initalizes community component
/// @param community_nft_classhash classhash of community NFT
fn _initializer(ref self: ComponentState<TContractState>, community_nft_classhash: felt252) {
fn _initializer(
ref self: ComponentState<TContractState>, community_nft_classhash: felt252
) {
self.community_counter.write(0);
self.community_nft_classhash.write(community_nft_classhash.try_into().unwrap());
}

// TODO: JOLT UPGRADE SUBSCRIPTION
/// @notice internal function to upgrade community
/// @param community_id id of community to be upgraded
/// @param upgrade_type
fn _upgrade_community(ref self: ComponentState<TContractState>, community_id: u256, upgrade_type: CommunityType) {
/// @param upgrade_type
fn _upgrade_community(
ref self: ComponentState<TContractState>,
community_id: u256,
upgrade_type: CommunityType
) {
let community = self.communities.read(community_id);

// update storage
Expand All @@ -585,9 +612,12 @@ pub mod CommunityComponent {
}

/// @notice internal function for permissioned gatekeeping
/// @param community_id id of community to be gatekeeped
fn _permissioned_gatekeeping(ref self: ComponentState<TContractState>, community_id: u256,
permissioned_addresses: Array<ContractAddress>) {
/// @param community_id id of community to be gatekeeped
fn _permissioned_gatekeeping(
ref self: ComponentState<TContractState>,
community_id: u256,
permissioned_addresses: Array<ContractAddress>
) {
// for permissioned gating update array of addresses
let length = permissioned_addresses.len();
let mut index: u32 = 0;
Expand All @@ -602,7 +632,7 @@ pub mod CommunityComponent {

/// @notice internal function to deploy a community nft
/// @param community_id id of community
/// @param salt for randomization
/// @param salt for randomization
fn _deploy_community_nft(
ref self: ComponentState<TContractState>,
community_id: u256,
Expand Down
8 changes: 4 additions & 4 deletions src/interfaces/ICommunity.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use starknet::{ContractAddress};
use karst::base::constants::types::{GateKeepType, CommunityGateKeepDetails, CommunityType, CommunityDetails};
use karst::base::constants::types::{
GateKeepType, CommunityGateKeepDetails, CommunityType, CommunityDetails
};

// *************************************************************************
// INTERFACE of ICommunity
Expand Down Expand Up @@ -34,9 +36,7 @@ pub trait ICommunity<TState> {
// *************************************************************************
fn get_community(self: @TState, community_id: u256) -> CommunityDetails;
fn get_community_metadata_uri(self: @TState, community_id: u256) -> ByteArray;
fn is_community_member(
self: @TState, profile: ContractAddress, community_id: u256
) -> bool;
fn is_community_member(self: @TState, profile: ContractAddress, community_id: u256) -> bool;
fn get_total_members(self: @TState, community_id: u256) -> u256;
fn is_community_mod(self: @TState, profile: ContractAddress, community_id: u256) -> bool;
fn get_ban_status(self: @TState, profile: ContractAddress, community_id: u256) -> bool;
Expand Down
25 changes: 16 additions & 9 deletions tests/test_community.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ fn test_community_creation() {
start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap());
let community_id = communityDispatcher.create_comminuty(salt);
assert(community_id == 1, 'invalid community creation');
// TEST TODO: use assert to check for every single item within CommunityDetails structs to ensure they were instantiated with the correct values
// TEST TODO: check that community nft was deployed correctly and you received an address
// TEST TODO: use assert to check for every single item within CommunityGateKeepDetails structs to ensure they were instantiated with the correct values
// TEST TODO: use assert to check for every single item within CommunityDetails structs to
// ensure they were instantiated with the correct values TEST TODO: check that community nft was
// deployed correctly and you received an address TEST TODO: use assert to check for every
// single item within CommunityGateKeepDetails structs to ensure they were instantiated with the
// correct values
stop_cheat_caller_address(community_contract_address);
}

// TEST TODO: create a new test fn called `test_community_upgrade_on_creation` where you pass in a premium package type and checks upgrade was successful
// TEST TODO: create a new test fn called `test_community_upgrade_on_creation` where you pass in a
// premium package type and checks upgrade was successful

#[test]
fn test_create_community_emits_events() {
Expand Down Expand Up @@ -298,7 +301,8 @@ fn should_panic_if_caller_adding_mod_is_not_owner() {
communityDispatcher.add_community_mods(community_id, USER_SIX.try_into().unwrap());
}

// TEST TODO: write an extra test called `should_panic_if_mod_is_not_member` to check that a mod must first be a community member
// TEST TODO: write an extra test called `should_panic_if_mod_is_not_member` to check that a mod
// must first be a community member

#[test]
fn test_remove_community_mod() {
Expand Down Expand Up @@ -503,7 +507,8 @@ fn test_should_panic_if_caller_to_set_ban_status_is_not_owner_or_mod() {
communityDispatcher.set_ban_status(community_id, USER_TWO.try_into().unwrap(), true);
}

// TEST TODO: create a test fn called `test_can_only_set_ban_status_for_members` to check that you can only ban existing members
// TEST TODO: create a test fn called `test_can_only_set_ban_status_for_members` to check that you
// can only ban existing members

#[test]
fn test_community_upgrade() {
Expand All @@ -515,7 +520,7 @@ fn test_community_upgrade() {
communityDispatcher.upgrade_community(community_id, CommunityType::Standard);
let community = communityDispatcher.get_community(community_id);
assert(community.community_type == CommunityType::Standard, 'Community Upgrade failed');
// TEST TODO: check that upgraded communities have a premium status
// TEST TODO: check that upgraded communities have a premium status
stop_cheat_caller_address(community_contract_address);
}

Expand Down Expand Up @@ -618,8 +623,10 @@ fn test_paid_gatekeeping() {
}

// TEST TODO: add test fn `test_nft_gatekeeping` for NFTGating
// TEST TODO: add test fn `test_only_premium_communities_can_be_paid_gated` to test that only premium communities can enforce PaidGating
// TEST TODO: add test fn `test_only_premium_communities_can_be_nft_gated` to test that only premium communities can enforce NFTGating
// TEST TODO: add test fn `test_only_premium_communities_can_be_paid_gated` to test that only
// premium communities can enforce PaidGating TEST TODO: add test fn
// `test_only_premium_communities_can_be_nft_gated` to test that only premium communities can
// enforce NFTGating

#[test]
#[should_panic(expected: ('Karst: Not Community owner',))]
Expand Down

0 comments on commit 6442a83

Please sign in to comment.