Skip to content

Commit

Permalink
Merge pull request #125 from PavitraAgarwal21/main
Browse files Browse the repository at this point in the history
add : test for channel component
  • Loading branch information
Darlington02 authored Oct 20, 2024
2 parents 3e1e5c7 + eb0a708 commit 59c7891
Show file tree
Hide file tree
Showing 7 changed files with 1,338 additions and 499 deletions.
1 change: 0 additions & 1 deletion src/base/constants/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ pub struct ChannelMember {
pub channel_id: u256,
pub total_publications: u256,
pub channel_token_id: u256,
pub ban_status: bool,
}

// *************************************************************************
Expand Down
88 changes: 46 additions & 42 deletions src/channel/channel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub mod ChannelComponent {
channel_members: Map<(u256, ContractAddress), ChannelMember>,
channel_moderators: Map<(u256, ContractAddress), bool>,
channel_nft_classhash: ClassHash,
channel_ban_status: Map<(u256, ContractAddress), bool>,
}

// *************************************************************************
Expand All @@ -58,53 +59,53 @@ pub mod ChannelComponent {

#[derive(Drop, starknet::Event)]
pub struct ChannelCreated {
channel_id: u256,
channel_owner: ContractAddress,
channel_nft_address: ContractAddress,
block_timestamp: u64,
pub channel_id: u256,
pub channel_owner: ContractAddress,
pub channel_nft_address: ContractAddress,
pub block_timestamp: u64,
}

#[derive(Drop, starknet::Event)]
pub struct JoinedChannel {
channel_id: u256,
transaction_executor: ContractAddress,
profile: ContractAddress,
token_id: u256,
block_timestamp: u64,
pub channel_id: u256,
pub transaction_executor: ContractAddress,
pub profile: ContractAddress,
pub token_id: u256,
pub block_timestamp: u64,
}

#[derive(Drop, starknet::Event)]
pub struct LeftChannel {
channel_id: u256,
transaction_executor: ContractAddress,
profile: ContractAddress,
token_id: u256,
block_timestamp: u64,
pub channel_id: u256,
pub transaction_executor: ContractAddress,
pub profile: ContractAddress,
pub token_id: u256,
pub block_timestamp: u64,
}

#[derive(Drop, starknet::Event)]
pub struct ChannelModAdded {
channel_id: u256,
transaction_executor: ContractAddress,
mod_address: ContractAddress,
block_timestamp: u64,
pub channel_id: u256,
pub transaction_executor: ContractAddress,
pub mod_address: ContractAddress,
pub block_timestamp: u64,
}

#[derive(Drop, starknet::Event)]
pub struct ChannelModRemoved {
channel_id: u256,
transaction_executor: ContractAddress,
mod_address: ContractAddress,
block_timestamp: u64,
pub channel_id: u256,
pub transaction_executor: ContractAddress,
pub mod_address: ContractAddress,
pub block_timestamp: u64,
}

#[derive(Drop, starknet::Event)]
pub struct ChannelBanStatusUpdated {
channel_id: u256,
transaction_executor: ContractAddress,
profile: ContractAddress,
ban_status: bool,
block_timestamp: u64,
pub channel_id: u256,
pub transaction_executor: ContractAddress,
pub profile: ContractAddress,
pub ban_status: bool,
pub block_timestamp: u64,
}

#[derive(Drop, starknet::Event)]
Expand Down Expand Up @@ -143,6 +144,11 @@ pub mod ChannelComponent {
channel_id, channel_nft_classhash, channel_id.try_into().unwrap()
); // use channel_id as salt since its unique

// check that caller is a member of the community
let (membership_status, _) = community_instance
.is_community_member(channel_owner, community_id);
assert(membership_status, NOT_COMMUNITY_MEMBER);

let new_channel = ChannelDetails {
channel_id: channel_id,
community_id: community_id,
Expand All @@ -153,11 +159,10 @@ pub mod ChannelComponent {
channel_censorship: false,
};

// include channel owner as first member
self._join_channel(channel_owner, channel_id);

// update storage
self.channels.write(channel_id, new_channel.clone());
// include channel owner as first member
self._join_channel(channel_owner, channel_id);
self.channel_counter.write(channel_id);

// emit event
Expand All @@ -182,8 +187,9 @@ pub mod ChannelComponent {
// check user is not already a channel member and wasn't previously banned
let (is_channel_member, _) = self.is_channel_member(profile, channel_id);
let is_banned = self.get_channel_ban_status(profile, channel_id);
assert(!is_channel_member, ALREADY_MEMBER);

assert(!is_banned, BANNED_FROM_CHANNEL);
assert(!is_channel_member, ALREADY_MEMBER);

// join channel
self._join_channel(profile, channel_id);
Expand Down Expand Up @@ -217,13 +223,11 @@ pub mod ChannelComponent {
channel_id: 0,
total_publications: 0,
channel_token_id: 0,
ban_status: false,
}
);

channel.channel_total_members -= 1;
self.channels.write(channel_id, channel);

// emit event
self
.emit(
Expand Down Expand Up @@ -362,7 +366,9 @@ pub mod ChannelComponent {
/// @notice gets the total number of members in a channel
/// @param channel_id the id of the channel
/// @return u256 the number of members in a channel
fn get_total_members(self: @ComponentState<TContractState>, channel_id: u256) -> u256 {
fn get_total_channel_members(
self: @ComponentState<TContractState>, channel_id: u256
) -> u256 {
self.channels.read(channel_id).channel_total_members
}

Expand Down Expand Up @@ -392,8 +398,7 @@ pub mod ChannelComponent {
fn get_channel_ban_status(
self: @ComponentState<TContractState>, profile: ContractAddress, channel_id: u256
) -> bool {
let channel_member: ChannelMember = self.channel_members.read((channel_id, profile));
channel_member.ban_status
self.channel_ban_status.read((channel_id, profile))
}
}

Expand Down Expand Up @@ -438,7 +443,6 @@ pub mod ChannelComponent {
channel_id: channel_id,
total_publications: 0,
channel_token_id: minted_token_id,
ban_status: false,
};

// update storage
Expand Down Expand Up @@ -547,13 +551,13 @@ pub mod ChannelComponent {

// check profile is a channel member
let (is_channel_member, _) = self.is_channel_member(profile, channel_id);
assert(is_channel_member == true, NOT_COMMUNITY_MEMBER);
assert(is_channel_member == true, NOT_CHANNEL_MEMBER);

// update storage
let channel_member = self.channel_members.read((channel_id, profile));
let updated_member = ChannelMember { ban_status: ban_status, ..channel_member };
self.channel_members.write((channel_id, profile), updated_member);

// let channel_member = self.channel_members.read((channel_id, profile));
// let updated_member = ChannelMember { ban_status: ban_status, ..channel_member };
// self.channel_members.write((channel_id, profile), updated_member);
self.channel_ban_status.write((channel_id, profile), ban_status);
// emit event
self
.emit(
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IChannel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub trait IChannel<TState> {
fn is_channel_member(
self: @TState, profile: ContractAddress, channel_id: u256
) -> (bool, ChannelMember);
fn get_total_members(self: @TState, channel_id: u256) -> u256;
fn get_total_channel_members(self: @TState, channel_id: u256) -> u256;
fn is_channel_mod(self: @TState, profile: ContractAddress, channel_id: u256) -> bool;
fn get_channel_censorship_status(self: @TState, channel_id: u256) -> bool;
fn get_channel_ban_status(self: @TState, profile: ContractAddress, channel_id: u256) -> bool;
Expand Down
1 change: 1 addition & 0 deletions src/mocks/interfaces.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod IComposable;
pub mod IJoltUpgrade;
pub mod IChannelComposable;
87 changes: 87 additions & 0 deletions src/mocks/interfaces/IChannelComposable.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use starknet::ContractAddress;
use karst::base::constants::types::{ChannelDetails, ChannelMember};
use karst::base::constants::types::{
GateKeepType, CommunityGateKeepDetails, CommunityType, CommunityDetails, CommunityMember
};
// *************************************************************************
// INTERFACE of KARST CHANNEL
// *************************************************************************
#[starknet::interface]
pub trait IChannelComposable<TState> {
// *************************************************************************
// INTERFACE of ICHANNEL
// *************************************************************************

// *************************************************************************
// EXTERNALS
// *************************************************************************
fn create_channel(ref self: TState, community_id: u256) -> u256;
fn join_channel(ref self: TState, channel_id: u256);
fn leave_channel(ref self: TState, channel_id: u256);
fn set_channel_metadata_uri(ref self: TState, channel_id: u256, metadata_uri: ByteArray);
fn add_channel_mods(ref self: TState, channel_id: u256, moderators: Array<ContractAddress>);
fn remove_channel_mods(ref self: TState, channel_id: u256, moderators: Array<ContractAddress>);
fn set_channel_censorship_status(ref self: TState, channel_id: u256, censorship_status: bool);
fn set_channel_ban_status(
ref self: TState,
channel_id: u256,
profiles: Array<ContractAddress>,
ban_statuses: Array<bool>
);
// *************************************************************************
// GETTERS
// *************************************************************************
fn get_channel(self: @TState, channel_id: u256) -> ChannelDetails;
fn get_channel_metadata_uri(self: @TState, channel_id: u256) -> ByteArray;
fn is_channel_member(
self: @TState, profile: ContractAddress, channel_id: u256
) -> (bool, ChannelMember);
fn get_total_channel_members(self: @TState, channel_id: u256) -> u256;
fn is_channel_mod(self: @TState, profile: ContractAddress, channel_id: u256) -> bool;
fn get_channel_censorship_status(self: @TState, channel_id: u256) -> bool;
fn get_channel_ban_status(self: @TState, profile: ContractAddress, channel_id: u256) -> bool;
// *************************************************************************
// INTERFACE of ICOMMUNITY
// *************************************************************************

// *************************************************************************
// EXTERNALS
// *************************************************************************
fn create_community(ref self: TState) -> u256;
fn join_community(ref self: TState, community_id: u256);
fn leave_community(ref self: TState, community_id: u256);
fn set_community_metadata_uri(ref self: TState, community_id: u256, metadata_uri: ByteArray);
fn add_community_mods(ref self: TState, community_id: u256, moderators: Array<ContractAddress>);
fn remove_community_mods(
ref self: TState, community_id: u256, moderators: Array<ContractAddress>
);
fn set_ban_status(
ref self: TState,
community_id: u256,
profiles: Array<ContractAddress>,
ban_statuses: Array<bool>
);
fn upgrade_community(ref self: TState, community_id: u256, upgrade_type: CommunityType);
fn gatekeep(
ref self: TState,
community_id: u256,
gate_keep_type: GateKeepType,
nft_contract_address: ContractAddress,
permissioned_addresses: Array<ContractAddress>,
entry_fee: u256
);

// *************************************************************************
// GETTERS
// *************************************************************************
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, CommunityMember);
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;
fn is_premium_community(self: @TState, community_id: u256) -> (bool, CommunityType);
fn is_gatekeeped(self: @TState, community_id: u256) -> (bool, CommunityGateKeepDetails);
}
13 changes: 13 additions & 0 deletions src/presets/channel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ pub mod KarstChannel {

#[abi(embed_v0)]
impl channelImpl = ChannelComponent::KarstChannel<ContractState>;
impl channelPrivateImpl = ChannelComponent::InternalImpl<ContractState>;

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

#[storage]
struct Storage {
Expand All @@ -37,4 +42,12 @@ pub mod KarstChannel {
#[flat]
OwnableEvent: OwnableComponent::Event
}

#[constructor]
fn constructor(
ref self: ContractState, channel_nft_classhash: felt252, community_nft_classhash: felt252
) {
self.channel._initializer(channel_nft_classhash);
self.community._initializer(community_nft_classhash);
}
}
Loading

0 comments on commit 59c7891

Please sign in to comment.