diff --git a/src/community/community.cairo b/src/community/community.cairo index ac2a7bd..21cf5bf 100644 --- a/src/community/community.cairo +++ b/src/community/community.cairo @@ -15,11 +15,7 @@ pub mod CommunityComponent { }; use karst::interfaces::ICommunity::ICommunity; use karst::interfaces::ICommunityNft::{ICommunityNftDispatcher, ICommunityNftDispatcherTrait}; - // ICommunityNft - // use karst::interfaces::ICommunity::{ - // ICommunityDispatcher, ICommunityDispatcherTrait, ICommunityLibraryDispatcher - // }; use karst::base::constants::types::{ CommunityDetails, GateKeepType, CommunityType, CommunityMember, CommunityGateKeepDetails @@ -57,7 +53,7 @@ pub mod CommunityComponent { (u256, ContractAddress), bool >, // >, hub_address: ContractAddress, - community_nft_classhash: felt252 + community_nft_classhash: ClassHash } // ************************************************************************* @@ -126,7 +122,6 @@ pub mod CommunityComponent { #[derive(Drop, starknet::Event)] pub struct DeployedCommunityNFT { pub community_id: u256, - pub profile_address: ContractAddress, pub community_nft: ContractAddress, pub block_timestamp: u64, } @@ -145,23 +140,26 @@ pub mod CommunityComponent { ) { self.community_counter.write(0); self.hub_address.write(hub_address); - self.community_nft_classhash.write(community_nft_classhash); + self.community_nft_classhash.write(community_nft_classhash.try_into().unwrap()); } - fn create_comminuty(ref self: ComponentState) -> u256 { + fn create_comminuty(ref self: ComponentState, salt: felt252) -> u256 { let community_owner = get_caller_address(); let community_counter = self.community_counter.read(); + let community_id = community_counter + 1; + let karst_hub = self.hub_address.read(); + let community_nft_classhash = self.community_nft_classhash.read(); // deploy a new NFT and save the address in community_nft_address - // let community_nft_address = self + // let community_nft_address = self // ._get_or_deploy_community_nft( - // karst_hub, community_owner, community_id, collect_nft_impl_class_hash, salt + // karst_hub, community_id, community_nft_classhash, salt // ); let community_details = CommunityDetails { community_id: community_id, community_owner: community_owner, community_metadata_uri: "Community URI", - community_nft_address: community_owner, + community_nft_address: community_owner, // community_nft_address, -- COMING BACK community_premium_status: false, community_total_members: 0, community_type: CommunityType::Free, @@ -191,12 +189,12 @@ pub mod CommunityComponent { // community_token_id // a token is minted from the comunity token contract address - + // let mint_token_id = self._mint_community_nft(community.community_nft_address); let community_member = CommunityMember { profile_address: profile, community_id: community_id, total_publications: 0, - community_token_id: 1, // COMING BACK TO THIS + community_token_id: 45, //mint_token_id, ban_status: false }; @@ -221,6 +219,7 @@ pub mod CommunityComponent { assert(community_member == true, NOT_MEMBER); + let community_member_details = self.community_member.read((community_id, profile)); // remove the member_community_id self.community_membership_status.write((community_id, profile), false); @@ -244,6 +243,10 @@ pub mod CommunityComponent { // this function will also burn the nft on leaving // call the burn function from the community nft contract + // self + // ._burn_community_nft( + // community.community_nft_address, community_member_details.community_token_id + // ); } fn set_community_metadata_uri( ref self: ComponentState, community_id: u256, metadata_uri: ByteArray @@ -344,7 +347,7 @@ pub mod CommunityComponent { assert(community.community_owner == community_owner_address, NOT_COMMUNITY_OWNER); let community_member = self.community_membership_status.read((community_id, profile)); - println!("Before inside leave_community is member: {}", community_member); + assert(community_member == true, NOT_MEMBER); let community_member = self.community_member.read((community_id, profile)); @@ -500,21 +503,16 @@ pub mod CommunityComponent { (true, gate_keep.gate_keep_type) } } - // ************************************************************************* - // PRIVATE FUNCTIONS - // ************************************************************************* - // #[generate_trait] #[generate_trait] - impl CommunityPrivateImpl< - TContractState, +HasComponent, +Drop, - > of CommunityPrivateTrait { + pub impl Private< + TContractState, +HasComponent + > of PrivateTrait { fn _get_or_deploy_community_nft( ref self: ComponentState, karst_hub: ContractAddress, - community_owner_address: ContractAddress, community_id: u256, - community_nft_impl_class_hash: felt252, + community_nft_impl_class_hash: ClassHash, salt: felt252 ) -> ContractAddress { let mut community = self.communities.read(community_id); @@ -522,13 +520,7 @@ pub mod CommunityComponent { if community_nft.is_zero() { // Deploy a new Collect NFT contract let deployed_collect_nft_address = self - ._deploy_community_nft( - karst_hub, - community_owner_address, - community_id, - community_nft_impl_class_hash, - salt - ); + .nft_test(karst_hub, community_id, community_nft_impl_class_hash, salt); // Update the community with the deployed Collect NFT address let updated_community = CommunityDetails { @@ -538,32 +530,39 @@ pub mod CommunityComponent { // Write the updated community with the new Community NFT address self.communities.write(community_id, updated_community); } + + let community = self.communities.read(community_id); community.community_nft_address } - + fn nft_test( + ref self: ComponentState, + karst_hub: ContractAddress, + community_id: u256, + community_nft_impl_class_hash: ClassHash, + salt: felt252 + ) -> ContractAddress { + 'Demo'.try_into().unwrap() + } fn _deploy_community_nft( ref self: ComponentState, karst_hub: ContractAddress, - profile_address: ContractAddress, community_id: u256, - community_nft_impl_class_hash: felt252, + community_nft_impl_class_hash: ClassHash, salt: felt252 ) -> ContractAddress { let mut constructor_calldata: Array = array![ - karst_hub.into(), - profile_address.into(), - community_id.low.into(), - community_id.high.into() + karst_hub.into(), community_id.low.into(), community_id.high.into() ]; - let class_hash: ClassHash = community_nft_impl_class_hash.try_into().unwrap(); - let result = deploy_syscall(class_hash, salt, constructor_calldata.span(), true); - let (account_address, _) = result.unwrap_syscall(); + + let (account_address, _) = deploy_syscall( + community_nft_impl_class_hash, salt, constructor_calldata.span(), true + ) + .unwrap_syscall(); self .emit( DeployedCommunityNFT { community_id: community_id, - profile_address: profile_address, community_nft: account_address, block_timestamp: get_block_timestamp() } @@ -578,7 +577,6 @@ pub mod CommunityComponent { .mint_nft(caller); token_id } - fn _burn_community_nft( ref self: ComponentState, community_nft_address: ContractAddress, diff --git a/src/communitynft/communitynft.cairo b/src/communitynft/communitynft.cairo index 8685fe9..aff27e2 100644 --- a/src/communitynft/communitynft.cairo +++ b/src/communitynft/communitynft.cairo @@ -33,7 +33,6 @@ pub mod CommunityNft { last_minted_id: u256, mint_timestamp: Map, user_token_id: Map, - profile_address: ContractAddress, community_id: u256 } @@ -49,12 +48,11 @@ pub mod CommunityNft { #[constructor] fn constructor( ref self: ContractState, - karst_hub: ContractAddress, - profile_address: ContractAddress, + karst_hub: ContractAddress, // profile_address: ContractAddress, community_id: u256 ) { self.karst_hub.write(karst_hub); - self.profile_address.write(profile_address); + self.community_id.write(community_id); } @@ -105,10 +103,10 @@ pub mod CommunityNft { /// @notice returns the community name fn name(self: @ContractState) -> ByteArray { let mut collection_name = ArrayTrait::::new(); - let profile_address_felt252: felt252 = self.profile_address.read().into(); + // let profile_address_felt252: felt252 = self.profile_address.read().into(); let community_id_felt252: felt252 = self.community_id.read().try_into().unwrap(); collection_name.append('Karst Community | Profile #'); - collection_name.append(profile_address_felt252); + // collection_name.append(profile_address_felt252); collection_name.append('- Community #'); collection_name.append(community_id_felt252); let collection_name_byte = convert_into_byteArray(ref collection_name); diff --git a/src/interfaces/ICommunity.cairo b/src/interfaces/ICommunity.cairo index 8061798..80070d6 100644 --- a/src/interfaces/ICommunity.cairo +++ b/src/interfaces/ICommunity.cairo @@ -13,7 +13,7 @@ pub trait ICommunity { fn initializer( ref self: TState, hub_address: ContractAddress, community_nft_classhash: felt252 ); - fn create_comminuty(ref self: TState) -> u256; + fn create_comminuty(ref self: TState, salt: felt252) -> u256; fn join_community(ref self: TState, profile: ContractAddress, community_id: u256); fn leave_community(ref self: TState, profile: ContractAddress, community_id: u256); fn set_community_metadata_uri(ref self: TState, community_id: u256, metadata_uri: ByteArray); diff --git a/tests/test_community.cairo b/tests/test_community.cairo index db02a56..7ac8b68 100644 --- a/tests/test_community.cairo +++ b/tests/test_community.cairo @@ -78,7 +78,7 @@ fn __setup__() -> (ContractAddress, ContractAddress, ContractAddress, ContractAd ]; let (community_contract_address, _) = community_contract .deploy(@community_constructor_calldata) - .unwrap_syscall(); + .unwrap(); return ( community_contract_address, @@ -96,9 +96,9 @@ fn test_creation_community() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = '5t74rhufhu5'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); assert(community_id == 1, 'invalid community creation'); stop_cheat_caller_address(community_contract_address); } @@ -109,10 +109,11 @@ fn test_creation_community_emit_events() { let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; + let salt: felt252 = 'djkngkylu349586'; // spy on emitted events let mut spy = spy_events(); start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); assert(community_id == 1, 'invalid community creation'); // check events are emitted spy @@ -141,10 +142,10 @@ fn test_join_community() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'ngkylu349586'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); //create the community - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); // join the community communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); let (is_member, community) = communityDispatcher @@ -160,10 +161,10 @@ fn test_should_panic_join_one_community_twice() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'djkn49586'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); @@ -175,10 +176,10 @@ fn test_leave_community() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'djkn4t76349586'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); //create the community - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); // join the community communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); @@ -208,10 +209,10 @@ fn test_should_panic_not_member() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'djkn092346'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); //create the community - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); // join the community communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); @@ -228,13 +229,19 @@ fn test_set_community_metadata_uri() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'dlosheyr586'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); - let metadata_uri = "ipfs://helloworld"; - communityDispatcher.set_community_metadata_uri(community_id, metadata_uri); + let community_id = communityDispatcher.create_comminuty(salt); + // let metadata_uri: ByteArray = "ipfs://helloworld"; + + communityDispatcher + .set_community_metadata_uri( + community_id, "ipfs://QmSkDCsS32eLpcymxtn1cEn7Rc5hfefLBgfvZyjaYXr4gQ/" + ); let result_meta_uri = communityDispatcher.get_community_metadata_uri(community_id); - assert(result_meta_uri == "ipfs://helloworld", 'invalid uri'); + assert( + result_meta_uri == "ipfs://QmSkDCsS32eLpcymxtn1cEn7Rc5hfefLBgfvZyjaYXr4gQ/", 'invalid uri' + ); stop_cheat_caller_address(community_contract_address); } @@ -244,9 +251,9 @@ fn test_should_panic_set_community_metadata_uri() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'o0ijh9586'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); stop_cheat_caller_address(community_contract_address); @@ -261,9 +268,9 @@ fn test_add_community_mod() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'lkkhjfegky'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); // add a community mod communityDispatcher.add_community_mods(community_id, USER_SIX.try_into().unwrap()); @@ -282,8 +289,9 @@ fn test_add_community_mod_emit_event() { // spy on emitted events let mut spy = spy_events(); + let salt: felt252 = 'ryehggjh586'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); // add a community mod communityDispatcher.add_community_mods(community_id, USER_SIX.try_into().unwrap()); @@ -312,9 +320,9 @@ fn test_should_panic_add_community_mod() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'dfghopeuryljk'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); stop_cheat_caller_address(community_contract_address); @@ -330,9 +338,9 @@ fn test_remove_community_mod() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'djsdfghk9586'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); // add a community mod communityDispatcher.add_community_mods(community_id, USER_SIX.try_into().unwrap()); communityDispatcher.add_community_mods(community_id, USER_FOUR.try_into().unwrap()); @@ -353,11 +361,11 @@ fn test_remove_community_mod_emit_event() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'dddfhjk86'; // spy on emitted events let mut spy = spy_events(); start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); // add a community mod communityDispatcher.add_community_mods(community_id, USER_SIX.try_into().unwrap()); communityDispatcher.add_community_mods(community_id, USER_FOUR.try_into().unwrap()); @@ -390,9 +398,9 @@ fn test_should_panic_remove_community_mod() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'djkngkylu349586'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); stop_cheat_caller_address(community_contract_address); @@ -416,10 +424,10 @@ fn test_set_ban_status_owner() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'djkngkylu349586'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); //create the community - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); // join the community communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); communityDispatcher.join_community(USER_TWO.try_into().unwrap(), community_id); @@ -440,10 +448,10 @@ fn test_set_ban_status_by_mod() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'sdery586'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); //create the community - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); // join the community communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); communityDispatcher.join_community(USER_TWO.try_into().unwrap(), community_id); @@ -468,10 +476,10 @@ fn test_set_ban_status_emit_event() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = '495ksjdhfgjrkf86'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); //create the community - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); // join the community communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); communityDispatcher.join_community(USER_TWO.try_into().unwrap(), community_id); @@ -511,10 +519,10 @@ fn test_should_panic_set_ban_status() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'djkrtyhjejfg6'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); //create the community - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); // join the community communityDispatcher.join_community(USER_ONE.try_into().unwrap(), community_id); communityDispatcher.join_community(USER_TWO.try_into().unwrap(), community_id); @@ -532,9 +540,9 @@ fn test_should_panic_set_ban_status() { fn test_community_upgrade() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'sfhkmpkippe86'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); communityDispatcher.upgrade_community(community_id, CommunityType::Standard); let community = communityDispatcher.get_community(community_id); assert(community.community_type == CommunityType::Standard, 'Community Upgrade failed'); @@ -546,9 +554,9 @@ fn test_community_upgrade() { fn test_should_panic_community_upgrade_by_wrong_owner() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'djkdgjlorityi86'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); stop_cheat_caller_address(community_contract_address); @@ -562,12 +570,12 @@ fn test_should_panic_community_upgrade_by_wrong_owner() { fn test_community_upgrade_emits_event() { let (community_contract_address, _, _, _) = __setup__(); let communityDispatcher = ICommunityDispatcher { contract_address: community_contract_address }; - + let salt: felt252 = 'djcxbvnk586'; // spy on emitted events let mut spy = spy_events(); start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); communityDispatcher.upgrade_community(community_id, CommunityType::Standard); // check events are emitted @@ -598,9 +606,9 @@ fn test_community_gatekeep_permission() { permission_addresses.append(USER_SIX.try_into().unwrap()); permission_addresses.append(USER_FIVE.try_into().unwrap()); permission_addresses.append(USER_THREE.try_into().unwrap()); - + let salt: felt252 = 'djzcvnyoy6'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); communityDispatcher .gatekeep( community_id, @@ -625,9 +633,9 @@ fn test_community_gatekeep_paid() { permission_addresses.append(USER_SIX.try_into().unwrap()); permission_addresses.append(USER_FIVE.try_into().unwrap()); permission_addresses.append(USER_THREE.try_into().unwrap()); - + let salt: felt252 = 'djkngzxvbnlk'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); communityDispatcher .gatekeep( community_id, GateKeepType::Paid, NFT_ONE.try_into().unwrap(), permission_addresses, 450 @@ -649,9 +657,9 @@ fn test_should_panic_community_gatekeep() { permission_addresses.append(USER_SIX.try_into().unwrap()); permission_addresses.append(USER_FIVE.try_into().unwrap()); permission_addresses.append(USER_THREE.try_into().unwrap()); - + let salt: felt252 = 'djksfkityu9586'; start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); stop_cheat_caller_address(community_contract_address); @@ -672,12 +680,12 @@ fn test_community_gatekeep_emits_event() { permission_addresses.append(USER_SIX.try_into().unwrap()); permission_addresses.append(USER_FIVE.try_into().unwrap()); permission_addresses.append(USER_THREE.try_into().unwrap()); - + let salt: felt252 = 'djadfyh09023'; // spy on emitted events let mut spy = spy_events(); start_cheat_caller_address(community_contract_address, USER_ONE.try_into().unwrap()); - let community_id = communityDispatcher.create_comminuty(); + let community_id = communityDispatcher.create_comminuty(salt); communityDispatcher .gatekeep( community_id,