Skip to content

Commit

Permalink
chore: fix publication tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Darlington02 committed May 31, 2024
1 parent 6f09eca commit 3fd9c14
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 110 deletions.
1 change: 1 addition & 0 deletions src/base/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// *************************************************************************
pub mod Errors {
pub const NOT_PROFILE_OWNER: felt252 = 'NOT_PROFILE_OWNER';
pub const ALREADY_MINTED: felt252 = 'USER_ALREADY_MINTED';
pub const INITIALIZED: felt252 = 'ALREADY_INITIALIZED';
pub const HUB_RESTRICTED: felt252 = 'CALLER_IS_NOT_HUB';
pub const FOLLOWING: felt252 = 'USER_ALREADY_FOLLOWING';
Expand Down
4 changes: 2 additions & 2 deletions src/base/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub struct PostParams {
pub struct Profile {
pub_count: u256,
metadata_URI: ByteArray,
// profile_address: ContractAddress,
// profile_owner: ContractAddress
// profile_address: ContractAddress,
// profile_owner: ContractAddress
}


Expand Down
8 changes: 5 additions & 3 deletions src/interfaces/IProfile.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ pub trait IKarstProfile<TState> {
registry_hash: felt252,
implementation_hash: felt252,
salt: felt252,
recipient:ContractAddress
recipient: ContractAddress
) -> ContractAddress;
fn set_profile_metadata_uri(ref self: TState, profile_address:ContractAddress, metadata_uri: ByteArray);
fn increment_publication_count(ref self: TState, profile_address:ContractAddress) -> u256;
fn set_profile_metadata_uri(
ref self: TState, profile_address: ContractAddress, metadata_uri: ByteArray
);
fn increment_publication_count(ref self: TState, profile_address: ContractAddress) -> u256;
// *************************************************************************
// GETTERS
// *************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IPublication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub trait IKarstPublications<T> {
contentURI: ByteArray,
profile_address: ContractAddress,
profile_contract_address: ContractAddress,
user:ContractAddress
user: ContractAddress
) -> u256;
fn comment(
ref self: T,
Expand Down
20 changes: 13 additions & 7 deletions src/karstnft/karstnft.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ pub mod KarstNFT {
// IMPORTS
// *************************************************************************
use starknet::{ContractAddress, get_caller_address};
use core::num::traits::zero::Zero;
use karst::interfaces::IKarstNFT;
use karst::base::{hubrestricted::HubRestricted::hub_only, errors::Errors::ALREADY_MINTED};
use openzeppelin::{
account, access::ownable::OwnableComponent,
token::erc721::{
ERC721Component, erc721::ERC721Component::InternalTrait as ERC721InternalTrait
},
introspection::{src5::SRC5Component}
};
use karst::base::{hubrestricted::HubRestricted::hub_only};
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
component!(path: SRC5Component, storage: src5, event: SRC5Event);
component!(path: ERC721Component, storage: erc721, event: ERC721Event);
Expand All @@ -64,7 +65,6 @@ pub mod KarstNFT {
// allow to query name of nft collection
#[abi(embed_v0)]
impl ERC721MetadataImpl = ERC721Component::ERC721MetadataImpl<ContractState>;

// add an owner
#[abi(embed_v0)]
impl OwnableImpl = OwnableComponent::OwnableImpl<ContractState>;
Expand All @@ -82,11 +82,14 @@ pub mod KarstNFT {
src5: SRC5Component::Storage,
#[substorage(v0)]
ownable: OwnableComponent::Storage,
karst_hub: ContractAddress,
admin: ContractAddress,
token_id: u256,
user_token_id: LegacyMap<ContractAddress, u256>,
}

// *************************************************************************
// EVENTS
// *************************************************************************
#[event]
#[derive(Drop, starknet::Event)]
enum Event {
Expand All @@ -104,31 +107,34 @@ pub mod KarstNFT {
#[constructor]
fn constructor(
ref self: ContractState,
hub: ContractAddress,
admin: ContractAddress,
name: ByteArray,
symbol: ByteArray,
base_uri: ByteArray
base_uri: ByteArray,
) {
self.karst_hub.write(hub);
self.admin.write(admin);
self.erc721.initializer(name, symbol, base_uri);
}

#[abi(embed_v0)]
impl KarstImpl of IKarstNFT::IKarstNFT<ContractState> {
/// @notice mints kartsnft
fn mint_karstnft(ref self: ContractState, address: ContractAddress) {
hub_only(self.karst_hub.read());
let balance = self.erc721.balance_of(address);
assert(balance.is_zero(), ALREADY_MINTED);
let mut current_token_id = self.token_id.read();
self.erc721._mint(address, current_token_id);
self.user_token_id.write(address, current_token_id);
current_token_id += 1;
self.token_id.write(current_token_id);
}

/// @notice returns karstnft token_id
/// @param user the address of user to query its token_id
fn get_user_token_id(self: @ContractState, user: ContractAddress) -> u256 {
self.user_token_id.read(user)
}

/// @notice returns current token_id
fn get_current_token_id(self: @ContractState) -> u256 {
self.token_id.read()
Expand Down
39 changes: 20 additions & 19 deletions src/profile/profile.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ mod KarstProfile {
};
use karst::interfaces::IERC721::{IERC721Dispatcher, IERC721DispatcherTrait};
use karst::interfaces::IProfile::IKarstProfile;
use karst::base::errors::Errors::{NOT_PROFILE_OWNER};
use karst::base::types::Profile;
use karst::base::{hubrestricted::HubRestricted::hub_only};

Expand All @@ -22,7 +21,7 @@ mod KarstProfile {
// *************************************************************************
#[storage]
struct Storage {
profile: LegacyMap<ContractAddress, Profile>, //maps profile_address => Profile
profile: LegacyMap<ContractAddress, Profile>,
karst_hub: ContractAddress,
}

Expand All @@ -38,10 +37,10 @@ mod KarstProfile {
#[derive(Drop, starknet::Event)]
struct CreateProfile {
#[key]
user: ContractAddress, // address of user creating a profile
user: ContractAddress,
#[key]
profile_address: ContractAddress, // address of created profile
token_id: u256, // profile nft token ID
profile_address: ContractAddress,
token_id: u256,
}

// *************************************************************************
Expand All @@ -67,9 +66,8 @@ mod KarstProfile {
registry_hash: felt252,
implementation_hash: felt252,
salt: felt252,
recipient:ContractAddress
recipient: ContractAddress
) -> ContractAddress {
hub_only(self.karst_hub.read());
let owns_karstnft = IERC721Dispatcher { contract_address: karstnft_contract_address }
.balance_of(recipient);
if owns_karstnft == 0 {
Expand All @@ -83,29 +81,29 @@ mod KarstProfile {
class_hash: registry_hash.try_into().unwrap()
}
.create_account(implementation_hash, karstnft_contract_address, token_id, salt);
let new_profile = Profile {
pub_count: 0,
metadata_URI: "",
};
let new_profile = Profile { pub_count: 0, metadata_URI: "", };
self.profile.write(profile_address, new_profile);
self.emit(CreateProfile { user: profile_address, token_id, profile_address });
profile_address
}

/// @notice set profile metadata_uri (`banner_image, description, profile_image` to be uploaded to arweave or ipfs)
/// @params metadata_uri the profile CID
fn set_profile_metadata_uri(ref self: ContractState, profile_address:ContractAddress, metadata_uri: ByteArray) {
hub_only(self.karst_hub.read());
fn set_profile_metadata_uri(
ref self: ContractState, profile_address: ContractAddress, metadata_uri: ByteArray
) {
let mut profile = self.profile.read(profile_address);
profile.metadata_URI = metadata_uri;
self.profile.write(profile_address, profile);
}

fn increment_publication_count(ref self: ContractState, profile_address:ContractAddress) -> u256 {
fn increment_publication_count(
ref self: ContractState, profile_address: ContractAddress
) -> u256 {
hub_only(self.karst_hub.read());
let mut profile = self.profile.read(profile_address);
let updated_profile = Profile {
pub_count: profile.pub_count + 1,
metadata_URI: profile.metadata_URI,
pub_count: profile.pub_count + 1, metadata_URI: profile.metadata_URI,
};
self.profile.write(profile_address, updated_profile);
profile.pub_count
Expand All @@ -117,16 +115,19 @@ mod KarstProfile {

/// @notice returns user metadata
/// @params user
fn get_profile_metadata(self: @ContractState, profile_address: ContractAddress) -> ByteArray {
fn get_profile_metadata(
self: @ContractState, profile_address: ContractAddress
) -> ByteArray {
self.profile.read(profile_address).metadata_URI
}


fn get_profile(ref self: ContractState, profile_address: ContractAddress) -> Profile {
self.profile.read(profile_address)
}

fn get_user_publication_count(self: @ContractState, profile_address: ContractAddress) -> u256 {
fn get_user_publication_count(
self: @ContractState, profile_address: ContractAddress
) -> u256 {
self.profile.read(profile_address).pub_count
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/publication/publication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ pub mod Publications {
hub_only(self.karst_hub.read());
let pubIdAssigned = IKarstProfileDispatcher {
contract_address: profile_contract_address
}.increment_publication_count(profile_address);
}
.increment_publication_count(profile_address);
let new_post = Publication {
pointed_profile_address: 0.try_into().unwrap(),
pointed_pub_id: 0,
Expand Down Expand Up @@ -172,7 +173,7 @@ pub mod Publications {
impl Private of PrivateTrait {
fn _fillRootOfPublicationInStorage(
ref self: ContractState,
profile_address:ContractAddress,
profile_address: ContractAddress,
pointed_profile_address: ContractAddress,
pointed_pub_id: u256,
profile_contract_address: ContractAddress
Expand Down Expand Up @@ -212,7 +213,10 @@ pub mod Publications {
.increment_publication_count(profile_address);
let root_profile_address = self
._fillRootOfPublicationInStorage(
profile_address,pointed_profile_address, pointed_pub_id, profile_contract_address
profile_address,
pointed_profile_address,
pointed_pub_id,
profile_contract_address
);
let update_reference = Publication {
pointed_profile_address: profile_address,
Expand Down
15 changes: 13 additions & 2 deletions tests/test_karstnft.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,20 @@ fn test_token_mint() {
HUB_ADDRESS.try_into().unwrap()
);
let dispatcher = IKarstProfileDispatcher { contract_address: profile_contract_address };
dispatcher.create_profile(contract_address, registry_class_hash, acct_class_hash.into(), 2456, HUB_ADDRESS.try_into().unwrap());
dispatcher
.create_profile(
contract_address,
registry_class_hash,
acct_class_hash.into(),
2456,
HUB_ADDRESS.try_into().unwrap()
);
let current_token_id = karstDispatcher.get_current_token_id();
dispatcher.set_profile_metadata_uri(HUB_ADDRESS.try_into().unwrap(), "ipfs://QmSkDCsS32eLpcymxtn1cEn7Rc5hfefLBgfvZyjaYXr4gQ/");
dispatcher
.set_profile_metadata_uri(
HUB_ADDRESS.try_into().unwrap(),
"ipfs://QmSkDCsS32eLpcymxtn1cEn7Rc5hfefLBgfvZyjaYXr4gQ/"
);
let hub_profile_uri = dispatcher.get_profile_metadata(HUB_ADDRESS.try_into().unwrap());
assert(hub_profile_uri == "ipfs://QmSkDCsS32eLpcymxtn1cEn7Rc5hfefLBgfvZyjaYXr4gQ/", 'invalid');

Expand Down
Loading

0 comments on commit 3fd9c14

Please sign in to comment.