Skip to content

Commit

Permalink
unit test for external function
Browse files Browse the repository at this point in the history
  • Loading branch information
mubarak23 committed Oct 6, 2024
1 parent 37fe188 commit bd57e22
Show file tree
Hide file tree
Showing 5 changed files with 367 additions and 23 deletions.
45 changes: 24 additions & 21 deletions src/community/community.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[starknet::component]
mod CommunityComponent {
pub mod CommunityComponent {
// *************************************************************************
// IMPORT
// *************************************************************************
Expand Down Expand Up @@ -35,7 +35,7 @@ mod CommunityComponent {
// STORAGE
// *************************************************************************
#[storage]
struct Storage {
pub struct Storage {
community_counter: u256,
community_owner: Map<u256, ContractAddress>, // map<owner_address, community_id>
communities: Map<u256, CommunityDetails>, // map <community_id, community_details>
Expand All @@ -56,14 +56,16 @@ mod CommunityComponent {
gate_keep_permissioned_addresses: Map<
(u256, ContractAddress), bool
>, // <u256, Array<ContractAddress>>,
hub_address: ContractAddress,
community_nft_classhash: felt252
}

// *************************************************************************
// EVENT
// *************************************************************************
#[event]
#[derive(Drop, starknet::Event)]
enum Event {
pub enum Event {
CommunityCreated: CommunityCreated,
CommunityModAdded: CommunityModAdded,
CommunityBanStatusUpdated: CommunityBanStatusUpdated,
Expand Down Expand Up @@ -132,22 +134,28 @@ mod CommunityComponent {
// *************************************************************************
// EXTERNAL FUNCTIONS
// *************************************************************************
#[embeddable_as(Community)]
#[embeddable_as(KarstCommunity)]
impl CommunityImpl<
TContractState, +HasComponent<TContractState>
> of ICommunity<ComponentState<TContractState>> {
fn initializer(ref self: ComponentState<TContractState>,) {
fn initializer(
ref self: ComponentState<TContractState>,
hub_address: ContractAddress,
community_nft_classhash: felt252
) {
self.community_counter.write(0);
self.hub_address.write(hub_address);
self.community_nft_classhash.write(community_nft_classhash);
}
fn create_comminuty(ref self: ComponentState<TContractState>,) -> u256 {
fn create_comminuty(ref self: ComponentState<TContractState>) -> u256 {
let community_owner = get_caller_address();
let community_counter = self.community_counter.read();
let community_id = community_counter + 1;

// deploy a new NFT and save the address in community_nft_address
// let community_nft_address = self
// ._get_or_deploy_community_nft(
// karst_hub, profile_address, pub_id, collect_nft_impl_class_hash, salt
// karst_hub, community_owner, community_id, collect_nft_impl_class_hash, salt
// );
let community_details = CommunityDetails {
community_id: community_id,
Expand Down Expand Up @@ -183,10 +191,7 @@ mod CommunityComponent {
let community_member = self
.community_membership_status
.read((community_id, member_address));
assert(community_member == true, ALREADY_MEMBER);

// let member_community_id = self.member_community_id.read(member_address);
// assert(member_community_id != community_id, "Already a member");
assert(community_member != true, ALREADY_MEMBER);

// community_token_id
// a token is minted from the comunity token contract address
Expand Down Expand Up @@ -218,7 +223,8 @@ mod CommunityComponent {
let community_member = self
.community_membership_status
.read((community_id, member_address));
assert(community_member != true, NOT_MEMBER);
println!("Before inside leave_community is member: {}", community_member);
assert(community_member == true, NOT_MEMBER);

// remove the member_community_id
self.community_membership_status.write((community_id, member_address), false);
Expand Down Expand Up @@ -451,11 +457,7 @@ mod CommunityComponent {
let is_community_member_id = self
.community_membership_status
.read((community_id, profile));
if (is_community_member_id) {
(true, community_id)
} else {
(false, community_id)
}
(is_community_member_id, community_id)
}
fn get_total_members(self: @ComponentState<TContractState>, community_id: u256) -> u256 {
let community = self.communities.read(community_id);
Expand Down Expand Up @@ -499,14 +501,15 @@ mod CommunityComponent {
// PRIVATE FUNCTIONS
// *************************************************************************

// #[generate_trait]
#[generate_trait]
impl InternalImpl<
impl CommunityPrivateImpl<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>,
> of InternalTrait<TContractState> {
> of CommunityPrivateTrait<TContractState> {
fn _get_or_deploy_community_nft(
ref self: ComponentState<TContractState>,
karst_hub: ContractAddress,
profile_address: ContractAddress,
community_owner_address: ContractAddress,
community_id: u256,
community_nft_impl_class_hash: felt252,
salt: felt252
Expand All @@ -518,7 +521,7 @@ mod CommunityComponent {
let deployed_collect_nft_address = self
._deploy_community_nft(
karst_hub,
profile_address,
community_owner_address,
community_id,
community_nft_impl_class_hash,
salt
Expand Down
6 changes: 4 additions & 2 deletions src/interfaces/ICommunity.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use starknet::ContractAddress;
use starknet::{ContractAddress};
use karst::base::constants::types::{GateKeepType, CommunityType, CommunityDetails};

// *************************************************************************
Expand All @@ -10,7 +10,9 @@ pub trait ICommunity<TState> {
// *************************************************************************
// EXTERNALS
// *************************************************************************
fn initializer(ref self: TState,);
fn initializer(
ref self: TState, hub_address: ContractAddress, community_nft_classhash: felt252
);
fn create_comminuty(ref self: TState) -> u256;
fn join_community(ref self: TState, community_id: u256);
fn leave_community(ref self: TState, community_id: u256);
Expand Down
1 change: 1 addition & 0 deletions src/presets.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod profile;
pub mod publication;
pub mod community;
31 changes: 31 additions & 0 deletions src/presets/community.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[starknet::contract]
pub mod KarstCommunity {
use starknet::ContractAddress;
use karst::community::community::CommunityComponent;
use karst::interfaces::ICommunity::ICommunity;

component!(path: CommunityComponent, storage: community, event: CommunityEvent);

#[abi(embed_v0)]
impl communityImpl = CommunityComponent::KarstCommunity<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
community: CommunityComponent::Storage
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
CommunityEvent: CommunityComponent::Event
}

#[constructor]
fn constructor(
ref self: ContractState, hub_address: ContractAddress, community_nft_classhash: felt252
) {
self.community.initializer(hub_address, community_nft_classhash);
}
}
Loading

0 comments on commit bd57e22

Please sign in to comment.