From 19d1c79a8404252a97b860d9774b5d0985864f50 Mon Sep 17 00:00:00 2001 From: PavitraAgarwal21 Date: Thu, 5 Dec 2024 18:51:12 +0530 Subject: [PATCH] remove channel nft --- src/base/constants/types.cairo | 2 - src/channel.cairo | 1 - src/channel/channel.cairo | 102 ++----------------------- src/channel/channelNFT.cairo | 134 --------------------------------- src/hub/hub.cairo | 3 +- src/presets/channel.cairo | 6 +- src/presets/publication.cairo | 3 +- tests/test_channel.cairo | 81 +------------------- tests/test_hub.cairo | 3 - tests/test_publication.cairo | 2 - 10 files changed, 10 insertions(+), 327 deletions(-) delete mode 100644 src/channel/channelNFT.cairo diff --git a/src/base/constants/types.cairo b/src/base/constants/types.cairo index 757c4ed..eb1957a 100644 --- a/src/base/constants/types.cairo +++ b/src/base/constants/types.cairo @@ -272,7 +272,6 @@ pub struct ChannelDetails { pub community_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, } @@ -290,7 +289,6 @@ pub struct ChannelMember { pub profile: ContractAddress, pub channel_id: u256, pub total_publications: u256, - pub channel_token_id: u256, } // ************************************************************************* diff --git a/src/channel.cairo b/src/channel.cairo index 480cc64..ff02972 100644 --- a/src/channel.cairo +++ b/src/channel.cairo @@ -1,2 +1 @@ pub mod channel; -pub mod channelNFT; diff --git a/src/channel/channel.cairo b/src/channel/channel.cairo index e446913..2d1c32f 100644 --- a/src/channel/channel.cairo +++ b/src/channel/channel.cairo @@ -38,7 +38,7 @@ pub mod ChannelComponent { channel_counter: u256, channel_members: Map<(u256, ContractAddress), ChannelMember>, channel_moderators: Map<(u256, ContractAddress), bool>, - channel_nft_classhash: ClassHash, + // channel_nft_classhash: ClassHash, channel_ban_status: Map<(u256, ContractAddress), bool>, } @@ -54,7 +54,6 @@ pub mod ChannelComponent { ChannelModAdded: ChannelModAdded, ChannelModRemoved: ChannelModRemoved, ChannelBanStatusUpdated: ChannelBanStatusUpdated, - DeployedChannelNFT: DeployedChannelNFT } #[derive(Drop, starknet::Event)] @@ -62,7 +61,6 @@ pub mod ChannelComponent { pub channel_id: u256, pub community_id: u256, pub channel_owner: ContractAddress, - pub channel_nft_address: ContractAddress, pub block_timestamp: u64, } @@ -71,7 +69,6 @@ pub mod ChannelComponent { pub channel_id: u256, pub transaction_executor: ContractAddress, pub profile: ContractAddress, - pub token_id: u256, pub block_timestamp: u64, } @@ -80,7 +77,6 @@ pub mod ChannelComponent { pub channel_id: u256, pub transaction_executor: ContractAddress, pub profile: ContractAddress, - pub token_id: u256, pub block_timestamp: u64, } @@ -109,12 +105,6 @@ pub mod ChannelComponent { pub block_timestamp: u64, } - #[derive(Drop, starknet::Event)] - pub struct DeployedChannelNFT { - pub channel_id: u256, - pub channel_nft: ContractAddress, - pub block_timestamp: u64, - } // ************************************************************************* // EXTERNAL FUNCTIONS @@ -132,19 +122,12 @@ pub mod ChannelComponent { fn create_channel(ref self: ComponentState, community_id: u256) -> u256 { let channel_id = self.channel_counter.read() + 1; let channel_owner = get_caller_address(); - let channel_nft_classhash = self.channel_nft_classhash.read(); // check that community exists let community_instance = get_dep_component!(@self, Community); let _community_id = community_instance.get_community(community_id).community_id; assert(community_id == _community_id, COMMUNITY_DOES_NOT_EXIST); - let channel_nft_address = self - ._deploy_channel_nft( - channel_id, channel_nft_classhash, get_block_timestamp().try_into().unwrap() - ); - // use channel_id as salt since its unique - // check that owner is a member of the community let (membership_status, _) = community_instance .is_community_member(channel_owner, community_id); assert(membership_status, NOT_COMMUNITY_MEMBER); @@ -154,7 +137,6 @@ pub mod ChannelComponent { community_id: community_id, channel_owner: channel_owner, channel_metadata_uri: "", - channel_nft_address: channel_nft_address, channel_total_members: 0, channel_censorship: false, }; @@ -173,7 +155,6 @@ pub mod ChannelComponent { channel_id: new_channel.channel_id, community_id: community_id, channel_owner: new_channel.channel_owner, - channel_nft_address: new_channel.channel_nft_address, block_timestamp: get_block_timestamp(), } ); @@ -212,9 +193,6 @@ pub mod ChannelComponent { let total_members: u256 = channel.channel_total_members; assert(total_members > 1, CHANNEL_HAS_NO_MEMBER); - // burn user's community token - self._burn_channel_nft(channel.channel_nft_address, channel_member.channel_token_id); - // update storage self .channel_members @@ -224,7 +202,7 @@ pub mod ChannelComponent { profile: contract_address_const::<0>(), channel_id: 0, total_publications: 0, - channel_token_id: 0, + // channel_token_id: 0, } ); @@ -237,7 +215,6 @@ pub mod ChannelComponent { channel_id: channel_id, transaction_executor: get_caller_address(), profile: profile, - token_id: channel_member.channel_token_id, block_timestamp: get_block_timestamp(), } ) @@ -422,9 +399,8 @@ pub mod ChannelComponent { > of InternalTrait { /// @notice initalizes channel component /// @param channel_nft_classhash classhash of channel NFT - fn _initializer(ref self: ComponentState, channel_nft_classhash: felt252) { + fn _initializer(ref self: ComponentState) { self.channel_counter.write(0); - self.channel_nft_classhash.write(channel_nft_classhash.try_into().unwrap()); } /// @notice internal function to join a channel @@ -441,14 +417,8 @@ pub mod ChannelComponent { .is_community_member(profile, channel.community_id); assert(membership_status, NOT_COMMUNITY_MEMBER); - // mint a channel token to new joiner - let minted_token_id = self._mint_channel_nft(profile, channel.channel_nft_address); - let channel_member = ChannelMember { - profile: profile, - channel_id: channel_id, - total_publications: 0, - channel_token_id: minted_token_id, + profile: profile, channel_id: channel_id, total_publications: 0, }; // update storage @@ -463,7 +433,7 @@ pub mod ChannelComponent { channel_id: channel_id, transaction_executor: get_caller_address(), profile: profile, - token_id: minted_token_id, + // token_id: minted_token_id, block_timestamp: get_block_timestamp(), } ) @@ -554,17 +524,9 @@ pub mod ChannelComponent { while index < length { let profile = *profiles[index]; let ban_status = *ban_statuses[index]; - - // check profile is a channel member let (is_channel_member, _) = self.is_channel_member(profile, channel_id); 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); self.channel_ban_status.write((channel_id, profile), ban_status); - // emit event self .emit( ChannelBanStatusUpdated { @@ -578,59 +540,5 @@ pub mod ChannelComponent { index += 1; }; } - - /// @notice internal function to deploy a channel nft - /// @param channel_id id of channel - /// @param salt for randomization - fn _deploy_channel_nft( - ref self: ComponentState, - channel_id: u256, - channel_nft_impl_class_hash: ClassHash, - salt: felt252 - ) -> ContractAddress { - let mut constructor_calldata: Array = array![ - channel_id.low.into(), channel_id.high.into() - ]; - - let (account_address, _) = deploy_syscall( - channel_nft_impl_class_hash, salt, constructor_calldata.span(), true - ) - .unwrap_syscall(); - - self - .emit( - DeployedChannelNFT { - channel_id: channel_id, - channel_nft: account_address, - block_timestamp: get_block_timestamp() - } - ); - account_address - } - - /// @notice internal function to mint a channel nft - /// @param profile profile to be minted to - /// @param channel_nft_address address of channel nft - fn _mint_channel_nft( - ref self: ComponentState, - profile: ContractAddress, - channel_nft_address: ContractAddress - ) -> u256 { - let token_id = ICustomNFTDispatcher { contract_address: channel_nft_address } - .mint_nft(profile); - token_id - } - - /// @notice internal function to burn a channel nft - /// @param channel_nft_address address of channel nft - /// @param token_id to burn - fn _burn_channel_nft( - ref self: ComponentState, - channel_nft_address: ContractAddress, - token_id: u256 - ) { - ICustomNFTDispatcher { contract_address: channel_nft_address } - .burn_nft(get_caller_address(), token_id); - } } } diff --git a/src/channel/channelNFT.cairo b/src/channel/channelNFT.cairo deleted file mode 100644 index 37381d2..0000000 --- a/src/channel/channelNFT.cairo +++ /dev/null @@ -1,134 +0,0 @@ -#[starknet::contract] -pub mod ChannelNFT { - // ************************************************************************* - // IMPORTS - // ************************************************************************* - use starknet::{ContractAddress, get_block_timestamp}; - use core::num::traits::zero::Zero; - use openzeppelin::introspection::src5::SRC5Component; - use openzeppelin::token::erc721::{ERC721Component, ERC721HooksEmptyImpl}; - - use coloniz::interfaces::ICustomNFT::ICustomNFT; - - use coloniz::base::{ - constants::errors::Errors::{ALREADY_MINTED, NOT_TOKEN_OWNER, TOKEN_DOES_NOT_EXIST}, - utils::base64_extended::convert_into_byteArray, - token_uris::channel_token_uri::ChannelTokenUri::get_token_uri, - }; - use starknet::storage::{ - Map, StoragePointerWriteAccess, StoragePointerReadAccess, StorageMapReadAccess, - StorageMapWriteAccess - }; - - // ************************************************************************* - // COMPONENTS - // ************************************************************************* - component!(path: ERC721Component, storage: erc721, event: ERC721Event); - component!(path: SRC5Component, storage: src5, event: SRC5Event); - - // ERC721 Mixin - impl ERC721MixinImpl = ERC721Component::ERC721MixinImpl; - impl ERC721InternalImpl = ERC721Component::InternalImpl; - - // ************************************************************************* - // STORAGE - // ************************************************************************* - #[storage] - struct Storage { - #[substorage(v0)] - erc721: ERC721Component::Storage, - #[substorage(v0)] - src5: SRC5Component::Storage, - last_minted_id: u256, - mint_timestamp: Map, - user_token_id: Map, - channel_id: u256 - } - - // ************************************************************************* - // EVENTS - // ************************************************************************* - #[event] - #[derive(Drop, starknet::Event)] - enum Event { - #[flat] - ERC721Event: ERC721Component::Event, - #[flat] - SRC5Event: SRC5Component::Event - } - - // ************************************************************************* - // CONSTRUCTOR - // ************************************************************************* - #[constructor] - fn constructor(ref self: ContractState, channel_id: u256) { - self.channel_id.write(channel_id); - } - - #[abi(embed_v0)] - impl ChannelNFT of ICustomNFT { - // ************************************************************************* - // EXTERNAL - // ************************************************************************* - - /// @notice mints a channel NFT - /// @param address address of user trying to mint the channel NFT token - fn mint_nft(ref self: ContractState, user_address: ContractAddress) -> u256 { - let balance = self.erc721.balance_of(user_address); - assert(balance.is_zero(), ALREADY_MINTED); - - let mut token_id = self.last_minted_id.read() + 1; - self.erc721.mint(user_address, token_id); - let timestamp: u64 = get_block_timestamp(); - self.user_token_id.write(user_address, token_id); - - self.last_minted_id.write(token_id); - self.mint_timestamp.write(token_id, timestamp); - self.last_minted_id.read() - } - - /// @notice burns a channel NFT - /// @param address address of user trying to burn the channel NFT token - fn burn_nft(ref self: ContractState, user_address: ContractAddress, token_id: u256) { - let user_token_id = self.user_token_id.read(user_address); - assert(user_token_id == token_id, NOT_TOKEN_OWNER); - // check the token exists - assert(self.erc721.exists(token_id), TOKEN_DOES_NOT_EXIST); - self.erc721.burn(token_id); - self.user_token_id.write(user_address, 0); - } - - // ************************************************************************* - // GETTERS - // ************************************************************************* - /// @notice gets the token ID for a user address - /// @param user address of user to retrieve token ID for - fn get_user_token_id(self: @ContractState, user_address: ContractAddress) -> u256 { - self.user_token_id.read(user_address) - } - - // ************************************************************************* - // METADATA - // ************************************************************************* - /// @notice returns the channel name - fn name(self: @ContractState) -> ByteArray { - let mut collection_name = ArrayTrait::::new(); - let channel_id_felt252: felt252 = self.channel_id.read().try_into().unwrap(); - collection_name.append('coloniz Channel | #'); - collection_name.append(channel_id_felt252); - let collection_name_byte = convert_into_byteArray(ref collection_name); - collection_name_byte - } - - /// @notice returns the collection symbol - fn symbol(self: @ContractState) -> ByteArray { - return "coloniz:CHANNEL"; - } - - /// @notice returns the token_uri for a particular token_id - fn token_uri(self: @ContractState, token_id: u256) -> ByteArray { - let token_mint_timestamp = self.mint_timestamp.read(token_id); - get_token_uri(token_id, token_mint_timestamp) - } - } -} diff --git a/src/hub/hub.cairo b/src/hub/hub.cairo index 1b2ff17..da4e905 100644 --- a/src/hub/hub.cairo +++ b/src/hub/hub.cairo @@ -120,7 +120,6 @@ pub mod ColonizHub { handle_contract_address: ContractAddress, handle_registry_contract_address: ContractAddress, follow_nft_classhash: felt252, - channel_nft_classhash: felt252, community_nft_classhash: felt252, collect_nft_classhash: felt252, owner: ContractAddress @@ -132,7 +131,7 @@ pub mod ColonizHub { ); self.handle_contract_address.write(handle_contract_address); self.handle_registry_contract_address.write(handle_registry_contract_address); - self.channel._initializer(channel_nft_classhash); + self.channel._initializer(); self.publication._initializer(collect_nft_classhash); self.community._initializer(community_nft_classhash); self.jolt._initializer(owner); diff --git a/src/presets/channel.cairo b/src/presets/channel.cairo index 2f9786a..61de035 100644 --- a/src/presets/channel.cairo +++ b/src/presets/channel.cairo @@ -44,10 +44,8 @@ pub mod ColonizChannel { } #[constructor] - fn constructor( - ref self: ContractState, channel_nft_classhash: felt252, community_nft_classhash: felt252 - ) { - self.channel._initializer(channel_nft_classhash); + fn constructor(ref self: ContractState, community_nft_classhash: felt252) { + self.channel._initializer(); self.community._initializer(community_nft_classhash); } } diff --git a/src/presets/publication.cairo b/src/presets/publication.cairo index 5a97d0a..5c7f558 100644 --- a/src/presets/publication.cairo +++ b/src/presets/publication.cairo @@ -75,14 +75,13 @@ pub mod ColonizPublication { coloniznft_contract_address: ContractAddress, hub_address: ContractAddress, follow_nft_classhash: felt252, - channel_nft_classhash: felt252, community_nft_classhash: felt252, collect_nft_classhash: felt252, owner: ContractAddress ) { self.profile._initializer(coloniznft_contract_address, hub_address, follow_nft_classhash); self.publication._initializer(collect_nft_classhash); - self.channel._initializer(channel_nft_classhash); + self.channel._initializer(); self.community._initializer(community_nft_classhash); self.jolt._initializer(owner); } diff --git a/tests/test_channel.cairo b/tests/test_channel.cairo index d7e09cb..a7c1f95 100644 --- a/tests/test_channel.cairo +++ b/tests/test_channel.cairo @@ -27,12 +27,8 @@ const USER_FIVE: felt252 = 'RANDY'; fn __setup__() -> ContractAddress { let community_nft_class_hash = declare("CommunityNFT").unwrap().contract_class().class_hash; - let channel_nft_class_hash = declare("ChannelNFT").unwrap().contract_class().class_hash; - let channel_contract = declare("ColonizChannel").unwrap().contract_class(); - let mut channel_constructor_calldata = array![ - (*(channel_nft_class_hash)).into(), (*(community_nft_class_hash)).into() - ]; + let mut channel_constructor_calldata = array![(*(community_nft_class_hash)).into()]; let (channel_contract_address, _) = channel_contract .deploy(@channel_constructor_calldata) .unwrap_syscall(); @@ -85,7 +81,6 @@ fn test_create_channel_emits_events() { channel_id: channel_id, community_id: community_id, channel_owner: USER_ONE.try_into().unwrap(), - channel_nft_address: channel_details.channel_nft_address, block_timestamp: get_block_timestamp(), } ) @@ -132,7 +127,6 @@ fn test_should_panic_if_channel_creator_is_not_community_member() { stop_cheat_caller_address(channel_contract_address); start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); - // dispatcher.join_community(community_id); let channel_id = dispatcher.create_channel(community_id); assert(channel_id == 1, 'invalid channel creation'); stop_cheat_caller_address(channel_contract_address); @@ -156,12 +150,10 @@ fn test_profile_can_join_channel() { let channel_id = dispatcher.create_channel(community_id); stop_cheat_caller_address(channel_contract_address); - // join the community first start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); dispatcher.join_community(community_id); stop_cheat_caller_address(channel_contract_address); - // join channel start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); dispatcher.join_channel(channel_id); stop_cheat_caller_address(channel_contract_address); @@ -172,75 +164,8 @@ fn test_profile_can_join_channel() { assert(channel_member.profile == USER_TWO.try_into().unwrap(), 'Invalid Channel Member'); assert(channel_member.channel_id == channel_id, 'Invalid Channel Id'); assert(channel_member.total_publications == 0, 'Invalid Total Publication'); - assert(channel_member.channel_token_id != 0, 'Invalid nft mint token '); } -#[test] -fn test_channel_nft_minted_to_profile_on_joining() { - let channel_contract_address = __setup__(); - let dispatcher = IChannelComposableDispatcher { contract_address: channel_contract_address }; - - // create a community - start_cheat_caller_address(channel_contract_address, USER_ONE.try_into().unwrap()); - let community_id = dispatcher.create_community(); - stop_cheat_caller_address(channel_contract_address); - - // create a channel - start_cheat_caller_address(channel_contract_address, USER_ONE.try_into().unwrap()); - let channel_id = dispatcher.create_channel(community_id); - stop_cheat_caller_address(channel_contract_address); - - // join the community - start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); - dispatcher.join_community(community_id); - stop_cheat_caller_address(channel_contract_address); - - // join the channel - start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); - dispatcher.join_channel(channel_id); - stop_cheat_caller_address(channel_contract_address); - - // check that nft is minted to the profile - start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); - let (_, channel_member) = dispatcher - .is_channel_member(USER_TWO.try_into().unwrap(), channel_id); - assert(channel_member.channel_token_id != 0, 'Invalid nft mint token '); -} - -#[test] -fn test_channel_nft_is_burnt_on_leaving_channel() { - let channel_contract_address = __setup__(); - let dispatcher = IChannelComposableDispatcher { contract_address: channel_contract_address }; - - // create a community - start_cheat_caller_address(channel_contract_address, USER_ONE.try_into().unwrap()); - let community_id = dispatcher.create_community(); - stop_cheat_caller_address(channel_contract_address); - - // create a channel - start_cheat_caller_address(channel_contract_address, USER_ONE.try_into().unwrap()); - let channel_id = dispatcher.create_channel(community_id); - stop_cheat_caller_address(channel_contract_address); - - // join the community - start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); - dispatcher.join_community(community_id); - stop_cheat_caller_address(channel_contract_address); - - // join the channel - start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); - dispatcher.join_channel(channel_id); - stop_cheat_caller_address(channel_contract_address); - - // leave the channel - start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); - dispatcher.leave_channel(channel_id); - stop_cheat_caller_address(channel_contract_address); - - let (_, channel_member) = dispatcher - .is_channel_member(USER_TWO.try_into().unwrap(), channel_id); - assert(channel_member.channel_token_id == 0, 'nft is burned '); -} #[test] #[should_panic(expected: ('coloniz: already a Member',))] @@ -399,7 +324,6 @@ fn test_joining_channel_emits_event() { channel_id: channel_id, transaction_executor: USER_TWO.try_into().unwrap(), profile: channel_member.profile, - token_id: channel_member.channel_token_id, block_timestamp: get_block_timestamp(), } ) @@ -433,7 +357,6 @@ fn test_leave_channel() { stop_cheat_caller_address(channel_contract_address); let get_total_channel_members = dispatcher.get_total_channel_members(channel_id); assert(get_total_channel_members == 1, 'No reduction in total members'); - assert(channel_member.channel_token_id == 0, 'NFT is not burn '); assert(channel_member.total_publications == 0, 'Invalid Total Publication'); assert(channel_member.profile == contract_address_const::<0>(), 'Invalid Channel Member'); assert(channel_member.channel_id == 0, 'Invalid Channel Id'); @@ -491,7 +414,6 @@ fn test_leave_channel_emits_event() { start_cheat_caller_address(channel_contract_address, USER_TWO.try_into().unwrap()); let (_, channel_member) = dispatcher .is_channel_member(USER_TWO.try_into().unwrap(), channel_id); - let channel_token_id = channel_member.channel_token_id; dispatcher.leave_channel(channel_id); spy .assert_emitted( @@ -503,7 +425,6 @@ fn test_leave_channel_emits_event() { channel_id: channel_id, transaction_executor: USER_TWO.try_into().unwrap(), profile: USER_TWO.try_into().unwrap(), - token_id: channel_token_id, block_timestamp: get_block_timestamp(), } ) diff --git a/tests/test_hub.cairo b/tests/test_hub.cairo index fa9a72c..e2552d7 100644 --- a/tests/test_hub.cairo +++ b/tests/test_hub.cairo @@ -54,8 +54,6 @@ fn __setup__() -> (ContractAddress, ContractAddress, ContractAddress, ContractAd // declare follownft let follow_nft_classhash = declare("Follow").unwrap().contract_class(); - let channel_nft_classhash = declare("ChannelNFT").unwrap().contract_class(); - // declare community_nft let community_nft_classhash = declare("CommunityNFT").unwrap().contract_class(); @@ -69,7 +67,6 @@ fn __setup__() -> (ContractAddress, ContractAddress, ContractAddress, ContractAd handle_contract_address.into(), handle_registry_contract_address.into(), (*follow_nft_classhash.class_hash).into(), - (*channel_nft_classhash.class_hash).into(), (*community_nft_classhash.class_hash).into(), (*collect_nft_classhash.class_hash).into(), OWNER diff --git a/tests/test_publication.cairo b/tests/test_publication.cairo index df870f6..99f2938 100644 --- a/tests/test_publication.cairo +++ b/tests/test_publication.cairo @@ -49,7 +49,6 @@ fn __setup__() -> ( // declare follownft let follow_nft_classhash = declare("Follow").unwrap().contract_class(); // declare channel_nft - let channel_nft_classhash = declare("ChannelNFT").unwrap().contract_class(); // declare community_nft let community_nft_classhash = declare("CommunityNFT").unwrap().contract_class(); // declare collectnft @@ -61,7 +60,6 @@ fn __setup__() -> ( nft_contract_address.into(), HUB_ADDRESS, (*follow_nft_classhash.class_hash).into(), - (*channel_nft_classhash.class_hash).into(), (*community_nft_classhash.class_hash).into(), (*collect_nft_classhash.class_hash).into(), ADMIN