Skip to content

Commit

Permalink
Add Channel Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PavitraAgarwal21 committed Oct 3, 2024
1 parent a7d88f3 commit 3e40955
Show file tree
Hide file tree
Showing 9 changed files with 373 additions and 85 deletions.
41 changes: 20 additions & 21 deletions src/base/constants/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -158,34 +158,33 @@ pub struct QuoteParams {
}



// /**
// * @notice A struct containing the parameters which define channel
// * @notice A struct containing the parameters which define channel
// *
// * @param channel_id unquie id to each channel
// * @param channel_owner primary owner of channel , who create channel
// * @param channel_metadata_uri contain channel metadata
// * @param channel_nft_address contain channel token gated nfts
// * @param channel_total_members total number of member in the channel
// * @param channel_censorship cencorship of channel
// * @param channel_premium_status
// * @param channel_id unquie id to each channel
// * @param channel_owner primary owner of channel , who create channel
// * @param channel_metadata_uri contain channel metadata
// * @param channel_nft_address contain channel token gated nfts
// * @param channel_total_members total number of member in the channel
// * @param channel_censorship cencorship of channel
// * @param channel_premium_status
// */
#[derive(Drop, Serde, Clone, starknet::Store)]
pub struct channelParams {
channel_id: u256,
channel_owner: ContractAddress,
channel_metadata_uri: ByteArray,
channel_nft_address: ContractAddress,
channel_total_members: u256,
channel_censorship: bool,
pub channel_id: u256,
pub channel_owner: ContractAddress,
pub channel_metadata_uri: ByteArray,
pub channel_nft_address: ContractAddress,
pub channel_total_members: u256,
pub channel_censorship: bool,
}


#[derive(Drop, Serde, Clone, starknet::Store)]
pub struct channelMember {
profile: ContractAddress,
channel_id: u256,
total_publications: u256,
channel_token_id: u256,
ban_status: bool,
}
pub profile: ContractAddress,
pub channel_id: u256,
pub total_publications: u256,
pub channel_token_id: u256,
pub ban_status: bool,
}
2 changes: 1 addition & 1 deletion src/channel.cairo
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod channel ;
pub mod channel;
117 changes: 72 additions & 45 deletions src/channel/channel.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
//TODO make the channel as the componet
//TODO make the channel as the componet
#[starknet::component]
mod ChannelComponent {
pub mod ChannelComponent {
use core::clone::Clone;
use core::starknet::{ContractAddress, get_caller_address, get_block_timestamp};
use core::starknet::{
ContractAddress, get_caller_address, get_block_timestamp,
storage::{
StoragePointerWriteAccess, StoragePointerReadAccess, Map, StorageMapReadAccess,
StorageMapWriteAccess
}
};
use karst::interfaces::IChannel::IChannel;
use karst::base::{
constants::errors::Errors::{
Expand All @@ -11,11 +17,11 @@ mod ChannelComponent {
constants::types::{channelParams, channelMember}
};
#[storage]
struct Storage {
channels: LegacyMap<u256, channelParams>,
pub struct Storage {
channels: Map<u256, channelParams>,
channel_counter: u256,
channel_members: LegacyMap<ContractAddress, channelMember>,
channel_moderators: LegacyMap<(u256, ContractAddress), bool>,
channel_members: Map<ContractAddress, channelMember>,
channel_moderators: Map<(u256, ContractAddress), bool>,
}

#[derive(Drop, starknet::Event)]
Expand All @@ -41,7 +47,7 @@ mod ChannelComponent {
token_id: u256,
block_timestamp: u64,
}
// this should also contain the IChannel & IChannelNFT implmentation , waht is the self what d.
// this should also contain the IChannel & IChannelNFT implmentation , waht is the self what d.
#[derive(Drop, starknet::Event)]
pub struct ChannelModAdded {
channel_id: u256,
Expand All @@ -68,7 +74,7 @@ mod ChannelComponent {

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
pub enum Event {
ChannelCreated: ChannelCreated,
JoinedChannel: JoinedChannel,
LeftChannel: LeftChannel,
Expand All @@ -78,28 +84,29 @@ mod ChannelComponent {
}


// mnaking the constructor to store the onwer , who can set the moderators
// mnaking the constructor to store the onwer , who can set the moderators

#[embeddable_as(KarstChannel)]
#[embeddable_as(KarstChannel)]
impl ChannelImpl<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>>
of IChannel<ComponentState<TContractState>> {
///@notice Create a new channel
TContractState, +HasComponent<TContractState>, +Drop<TContractState>
> of IChannel<ComponentState<TContractState>> {
///@notice Create a new channel
///@param channel_params: The parameters of the channel
fn create_channel(ref self: ComponentState<TContractState>, channel_params: channelParams) -> u256 {

let channel_id : u256 = self.channel_counter.read() ;
fn create_channel(
ref self: ComponentState<TContractState>, channel_params: channelParams
) -> u256 {
let channel_id: u256 = self.channel_counter.read();
let new_channel = channelParams {
channel_id: channel_id ,
channel_id: channel_id,
channel_owner: channel_params.channel_owner,
channel_metadata_uri: channel_params.channel_metadata_uri,
channel_nft_address: channel_params.channel_nft_address,
channel_total_members: 0,
channel_censorship: channel_params.channel_censorship,
};
// increment
self.channels.write(channel_id , new_channel.clone());
self.channel_counter.write(channel_id + 1 );
// increment
self.channels.write(channel_id, new_channel.clone());
self.channel_counter.write(channel_id + 1);
self
.emit(
ChannelCreated {
Expand All @@ -117,15 +124,15 @@ mod ChannelComponent {
/// @notice Join the channel
/// @param channel_id: The id of the channel
fn join_channel(ref self: ComponentState<TContractState>, channel_id: u256) {
// check that i prioor not baned
// check that i prioor not baned
let channel_member: channelMember = self.channel_members.read(get_caller_address());
assert(channel_member.ban_status, BANNED_FROM_CHANNEL);
assert(!channel_member.ban_status, BANNED_FROM_CHANNEL);

let mut new_channel: channelParams = self.channels.read(channel_id);
new_channel.channel_total_members += 1;
self.channels.write(channel_id, new_channel);

// calculate the total channel member add +1 is the new member id
// calculate the total channel member add +1 is the new member id

self
.channel_members
Expand Down Expand Up @@ -155,14 +162,33 @@ mod ChannelComponent {

/// @notice Leave the channel
/// @param channel_id: The id of the channel
/// @dev The user must be a member of the channel
/// @dev The user must be a member of the channel
fn leave_channel(ref self: ComponentState<TContractState>, channel_id: u256) {
let channel_member: channelParams = self.channels.read(channel_id);
assert(channel_member.channel_owner == get_caller_address(), NOT_CHANNEL_OWNER);

assert(
self.channel_members.read(get_caller_address()).channel_id == channel_id,
NOT_CHANNEL_MEMBER
);
assert(!self.channel_members.read(get_caller_address()).ban_status, BANNED_FROM_CHANNEL);
assert(self.channels.read(channel_id).channel_total_members > 1 , 'total_member > 1');

self.channel_members.write(get_caller_address(), channelMember {
profile: get_caller_address(),
// todo , what default channel id should set max but not optimize to store
channel_id: 10000000,
total_publications: 0,
channel_token_id: 0,
ban_status: false,
});



let mut new_channel: channelParams = self.channels.read(channel_id);
new_channel.channel_total_members -= 1;
self.channels.write(channel_id, new_channel);

//TODO Delete the mapping at the caller address
// self.channel_members.write(get_caller_address , None );
//TODO : burn the NFT
//TODO Delete the mapping at the caller address
//TODO : burn the NFT
self
.emit(
LeftChannel {
Expand Down Expand Up @@ -201,8 +227,8 @@ mod ChannelComponent {
fn add_channel_mods(
ref self: ComponentState<TContractState>, channel_id: u256, moderator: ContractAddress
) {
let channel_member: channelParams = self.channels.read(channel_id);
assert(channel_member.channel_owner == get_caller_address(), NOT_CHANNEL_OWNER);

assert(self.channels.read(channel_id).channel_owner == get_caller_address(), NOT_CHANNEL_OWNER);

self.channel_moderators.write((channel_id, moderator), true);
self
Expand All @@ -224,8 +250,8 @@ mod ChannelComponent {
fn remove_channel_mods(
ref self: ComponentState<TContractState>, channel_id: u256, moderator: ContractAddress
) {
let channel_member: channelParams = self.channels.read(channel_id);
assert(channel_member.channel_owner == get_caller_address(), NOT_CHANNEL_OWNER);
// let channel_member: channelParams = self.channels.read(channel_id);
assert(self.channels.read(channel_id).channel_owner == get_caller_address(), NOT_CHANNEL_OWNER);

self.channel_moderators.write((channel_id, moderator), false);

Expand All @@ -241,13 +267,13 @@ mod ChannelComponent {
}


/// @notice Set the censorship status of the channel
/// @notice Set the censorship status of the channel
/// @param channel_id: The id of the channel
fn set_channel_censorship_status(
ref self: ComponentState<TContractState>, channel_id: u256, censorship_status: bool
) {
let channel_member: channelParams = self.channels.read(channel_id);
assert(channel_member.channel_owner == get_caller_address(), NOT_CHANNEL_OWNER);
// let channel_member: channelParams = self.channels.read(channel_id);
assert(self.channels.read(channel_id).channel_owner == get_caller_address(), NOT_CHANNEL_OWNER);
let mut new_channel: channelParams = self.channels.read(channel_id);
new_channel.channel_censorship = censorship_status;
self.channels.write(channel_id, new_channel);
Expand All @@ -264,11 +290,12 @@ mod ChannelComponent {
profile: ContractAddress,
ban_status: bool
) {
// let channel_member: channelParams = self.channels.read(channel_id);
assert(
self.channel_moderators.read((channel_id, get_caller_address())),
self.channels.read(channel_id).channel_owner == get_caller_address()
|| self.channel_moderators.read((channel_id, get_caller_address())),
NOT_CHANNEL_MODERATOR
);

let mut new_channel_member: channelMember = self.channel_members.read(profile);
new_channel_member.ban_status = ban_status;
self.channel_members.write(profile, new_channel_member);
Expand Down Expand Up @@ -317,17 +344,17 @@ mod ChannelComponent {
}


///TODO :get the total number of mener of the channel
///TODO :get the total number of mener of the channel
fn get_total_members(self: @ComponentState<TContractState>, channel_id: u256) -> u256 {
let channel: channelParams = self.channels.read(channel_id);
channel.channel_total_members
}

///@notice check for moderator
/// @param channel id
/// @param profile addresss
///@notice check for moderator
/// @param channel id
/// @param profile addresss
fn is_channel_mod(
self: @ComponentState<TContractState>, channel_id: u256, profile: ContractAddress
self: @ComponentState<TContractState>, profile: ContractAddress, channel_id: u256
) -> bool {
self.channel_moderators.read((channel_id, profile))
}
Expand All @@ -339,7 +366,7 @@ mod ChannelComponent {
channel.channel_censorship
}

///TODO introduce new storage for ban status
///TODO introduce new storage for ban status
fn get_ban_status(self: @ComponentState<TContractState>, profile: ContractAddress) -> bool {
let channel_member: channelMember = self.channel_members.read(profile);
channel_member.ban_status
Expand Down
11 changes: 5 additions & 6 deletions src/hub/hub.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@ pub mod KarstHub {
// *************************************************************************
// COMPONENTS
// *************************************************************************
component!(path: ChannelComponent , storage : channel , event : ChannelEvent ) ;
component!(path: ChannelComponent, storage: channel, event: ChannelEvent);
component!(path: ProfileComponent, storage: profile, event: ProfileEvent);
component!(path: PublicationComponent, storage: publication, event: PublicationEvent);


impl ChannelImpl = ChannelComponent::KarstChannel<ContractState>;
// impl ChannelImpl = ChannelComponent::KarstChannel<ContractState>;
impl ProfileImpl = ProfileComponent::KarstProfile<ContractState>;
impl PublicationImpl = PublicationComponent::KarstPublication<ContractState>;


// *************************************************************************
// STORAGE
Expand All @@ -81,7 +80,7 @@ pub mod KarstHub {
enum Event {
ProfileEvent: ProfileComponent::Event,
PublicationEvent: PublicationComponent::Event,
ChannelEvent : ChannelComponent::Event
ChannelEvent: ChannelComponent::Event
}

// *************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ pub mod IHandle;
pub mod IHandleRegistry;
pub mod IHub;
pub mod ICollectNFT;
pub mod IChannel;
pub mod IChannel;
15 changes: 8 additions & 7 deletions src/interfaces/IChannel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub trait IChannel<TState> {
// *************************************************************************
// EXTERNALS
// *************************************************************************
fn create_channel(ref self: TState, channel_params: channelParams) -> u256 ;
fn create_channel(ref self: TState, channel_params: channelParams) -> 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);
Expand All @@ -21,19 +21,20 @@ pub trait IChannel<TState> {
// // *************************************************************************
fn get_channel(self: @TState, channel_id: u256) -> channelParams;
fn get_channel_metadata_uri(self: @TState, channel_id: u256) -> ByteArray;
// so is the profile is channel member or not we have to say that which channel id , i think it will be good
// so is the profile is channel member or not we have to say that which channel id , i think it
// will be good
fn is_channel_member(self: @TState, profile: ContractAddress, channel_id: u256) -> bool;
// what is the means by the paid channel member
// how we can calcualte the get_total_member
// what is the means by the paid channel member
// how we can calcualte the get_total_member

fn get_total_members(self: @TState, channel_id: u256) -> u256;

// we have to pass the channel id how is the ch
fn is_channel_mod(self: @TState, channel_id: u256, profile: ContractAddress) -> bool;
fn is_channel_mod(self: @TState, profile: ContractAddress, channel_id: u256) -> bool;

// this one is easy
// this one is easy
fn get_channel_censorship_status(self: @TState, channel_id: u256) -> bool;

fn get_ban_status(self: @TState, profile: ContractAddress) -> bool;
}

}
2 changes: 1 addition & 1 deletion src/presets.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod profile;
pub mod publication;
pub mod channel ;
pub mod channel;
3 changes: 0 additions & 3 deletions src/presets/channel.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

#[starknet::contract]
pub mod KarstChannel {
use karst::channel::channel::ChannelComponent;

component!(path: ChannelComponent, storage: channel, event: ChannelEvent);
#[abi(embed_v0)]
impl channelImpl = ChannelComponent::KarstChannel<ContractState>;
Expand All @@ -20,4 +18,3 @@ pub mod KarstChannel {
}
}

// creatin the best things in the file thankyous o much that
Loading

0 comments on commit 3e40955

Please sign in to comment.