Skip to content

Commit

Permalink
chore:fix deploy function
Browse files Browse the repository at this point in the history
  • Loading branch information
mubarak23 committed Oct 8, 2024
1 parent d90b0f1 commit 0e90eae
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 132 deletions.
73 changes: 19 additions & 54 deletions src/community/community.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ pub mod CommunityComponent {
gate_keep_permissioned_addresses: Map<
(u256, ContractAddress), bool
>, // <u256, Array<ContractAddress>>,
hub_address: ContractAddress,
community_nft_classhash: ClassHash
}

Expand Down Expand Up @@ -133,33 +132,26 @@ pub mod CommunityComponent {
impl CommunityImpl<
TContractState, +HasComponent<TContractState>
> of ICommunity<ComponentState<TContractState>> {
fn initializer(
ref self: ComponentState<TContractState>,
hub_address: ContractAddress,
community_nft_classhash: felt252
) {
fn initializer(ref self: ComponentState<TContractState>, community_nft_classhash: felt252) {
self.community_counter.write(0);
self.hub_address.write(hub_address);
self.community_nft_classhash.write(community_nft_classhash.try_into().unwrap());
}

fn create_comminuty(ref self: ComponentState<TContractState>, 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
// ._get_or_deploy_community_nft(
// karst_hub, community_id, community_nft_classhash, salt
// );
let community_nft_address = self
._deploy_community_nft(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, -- COMING BACK
community_nft_address: community_nft_address,
community_premium_status: false,
community_total_members: 0,
community_type: CommunityType::Free,
Expand Down Expand Up @@ -189,12 +181,13 @@ 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 mint_token_id = self._mint_community_nft(profile, community.community_nft_address);

let community_member = CommunityMember {
profile_address: profile,
community_id: community_id,
total_publications: 0,
community_token_id: 45, //mint_token_id,
community_token_id: mint_token_id,
ban_status: false
};

Expand Down Expand Up @@ -241,12 +234,12 @@ pub mod CommunityComponent {
};
self.communities.write(community_id, updated_community);
// this function will also burn the nft on leaving
// call the burn function from the community nft contract
// call the burn function from the community nft contract

// self
// ._burn_community_nft(
// community.community_nft_address, community_member_details.community_token_id
// );
self
._burn_community_nft(
community.community_nft_address, community_member_details.community_token_id
);
}
fn set_community_metadata_uri(
ref self: ComponentState<TContractState>, community_id: u256, metadata_uri: ByteArray
Expand Down Expand Up @@ -508,46 +501,17 @@ pub mod CommunityComponent {
pub impl Private<
TContractState, +HasComponent<TContractState>
> of PrivateTrait<TContractState> {
fn _get_or_deploy_community_nft(
ref self: ComponentState<TContractState>,
karst_hub: ContractAddress,
community_id: u256,
community_nft_impl_class_hash: ClassHash,
salt: felt252
) -> ContractAddress {
let mut community = self.communities.read(community_id);
let community_nft = community.community_nft_address;
if community_nft.is_zero() {
// Deploy a new Collect NFT contract
let deployed_collect_nft_address = self
._deploy_community_nft(
karst_hub, community_id, community_nft_impl_class_hash, salt
);

// Update the community with the deployed Collect NFT address
let updated_community = CommunityDetails {
community_nft_address: deployed_collect_nft_address, ..community
};

// 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 _deploy_community_nft(
ref self: ComponentState<TContractState>,
karst_hub: ContractAddress,
community_id: u256,
community_nft_impl_class_hash: ClassHash,
salt: felt252
) -> ContractAddress {
let mut constructor_calldata: Array<felt252> = array![
karst_hub.into(), community_id.low.into(), community_id.high.into()
community_id.low.into(), community_id.high.into()
];

println!("inside _deploy_community_nft");
let (account_address, _) = deploy_syscall(
community_nft_impl_class_hash, salt, constructor_calldata.span(), true
)
Expand All @@ -564,11 +528,12 @@ pub mod CommunityComponent {
account_address
}
fn _mint_community_nft(
ref self: ComponentState<TContractState>, community_nft_address: ContractAddress
ref self: ComponentState<TContractState>,
profile: ContractAddress,
community_nft_address: ContractAddress
) -> u256 {
let caller: ContractAddress = get_caller_address();
let token_id = ICommunityNftDispatcher { contract_address: community_nft_address }
.mint_nft(caller);
.mint_nft(profile);
token_id
}
fn _burn_community_nft(
Expand Down
10 changes: 2 additions & 8 deletions src/communitynft/communitynft.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ pub mod CommunityNft {
erc721: ERC721Component::Storage,
#[substorage(v0)]
src5: SRC5Component::Storage,
karst_hub: ContractAddress,
last_minted_id: u256,
mint_timestamp: Map<u256, u64>,
user_token_id: Map<ContractAddress, u256>,
Expand All @@ -46,13 +45,7 @@ pub mod CommunityNft {
}

#[constructor]
fn constructor(
ref self: ContractState,
karst_hub: ContractAddress, // profile_address: ContractAddress,
community_id: u256
) {
self.karst_hub.write(karst_hub);

fn constructor(ref self: ContractState, community_id: u256) {
self.community_id.write(community_id);
}

Expand All @@ -72,6 +65,7 @@ pub mod CommunityNft {
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()
Expand Down
4 changes: 1 addition & 3 deletions src/interfaces/ICommunity.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ pub trait ICommunity<TState> {
// *************************************************************************
// EXTERNALS
// *************************************************************************
fn initializer(
ref self: TState, hub_address: ContractAddress, community_nft_classhash: felt252
);
fn initializer(ref self: TState, community_nft_classhash: felt252);
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);
Expand Down
6 changes: 2 additions & 4 deletions src/presets/community.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ pub mod KarstCommunity {
}

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

0 comments on commit 0e90eae

Please sign in to comment.