Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor contracts #82

Merged
merged 3 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified img/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 14 additions & 11 deletions src/base/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
// ERRORS
// *************************************************************************
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';
pub const NOT_FOLLOWING: felt252 = 'USER_NOT_FOLLOWING';
pub const BLOCKED_STATUS: felt252 = 'BLOCKED';
pub const INVALID_POINTED_PUBLICATION: felt252 = 'INVALID_POINTED_PUB';
pub const INVALID_OWNER: felt252 = 'CALLER_NOT_OWNER';
pub const INVALID_PROFILE: felt252 = 'PROFILE_IS_NOT_OWNER';
pub const OWNER_NOT_ZERO: felt252 = 'HANDLE_HAS_ALREADY_BEEN_LINKED';
pub const NOT_PROFILE_OWNER: felt252 = 'Karst: not profile owner!';
pub const ALREADY_MINTED: felt252 = 'Karst: user already minted!';
pub const INITIALIZED: felt252 = 'Karst: already initialized!';
pub const HUB_RESTRICTED: felt252 = 'Karst: caller is not Hub!';
pub const FOLLOWING: felt252 = 'Karst: user already following!';
pub const NOT_FOLLOWING: felt252 = 'Karst: user not following!';
pub const BLOCKED_STATUS: felt252 = 'Karst: user is blocked!';
pub const INVALID_POINTED_PUBLICATION: felt252 = 'Karst: invalid pointed pub!';
pub const INVALID_OWNER: felt252 = 'Karst: caller is not owner!';
pub const INVALID_PROFILE: felt252 = 'Karst: profile is not owner!';
pub const HANDLE_ALREADY_LINKED: felt252 = 'Karst: handle already linked!';
pub const HADLE_DOES_NOT_EXIST: felt252 = 'Karst: handle does not exist!';
pub const INVALID_LOCAL_NAME: felt252 = 'Karst: invalid local name!';
pub const UNSUPPORTED_PUB_TYPE: felt252 = 'Karst: unsupported pub type!';
}
25 changes: 25 additions & 0 deletions src/follownft/follownft.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,16 @@ mod Follow {
// *************************************************************************
#[abi(embed_v0)]
impl FollowImpl of IFollowNFT<ContractState> {
/// @notice initialize follow contract
/// @param profile_address address of profile to initialize contract for
fn initialize(ref self: ContractState, profile_address: ContractAddress) {
assert(!self.initialized.read(), Errors::INITIALIZED);
self.initialized.write(true);
self.followed_profile_address.write(profile_address);
}

/// @notice performs the follow action
/// @param follower_profile_address address of the user trying to perform the follow action
fn follow(ref self: ContractState, follower_profile_address: ContractAddress) -> u256 {
hub_only(self.karst_hub.read());
let follow_id = self
Expand All @@ -86,6 +90,8 @@ mod Follow {
self._follow(follower_profile_address)
}

/// @notice performs the unfollow action
/// @param unfollower_profile_address address of the user trying to perform the unfollow action
fn unfollow(ref self: ContractState, unfollower_profile_address: ContractAddress) {
hub_only(self.karst_hub.read());
let follow_id = self
Expand All @@ -95,6 +101,8 @@ mod Follow {
self._unfollow(unfollower_profile_address, follow_id);
}

/// @notice performs the block action
/// @param follower_profile_address address of the user to be blocked
fn process_block(
ref self: ContractState, follower_profile_address: ContractAddress
) -> bool {
Expand All @@ -119,28 +127,40 @@ mod Follow {
// *************************************************************************
// GETTERS
// *************************************************************************

/// @notice gets the follower profile address for a follow action
/// @param follow_id ID of the follow action
fn get_follower_profile_address(self: @ContractState, follow_id: u256) -> ContractAddress {
let follow_data = self.follow_data_by_follow_id.read(follow_id);
follow_data.follower_profile_address
}

/// @notice gets the follow timestamp for a follow action
/// @param follow_id ID of the follow action
fn get_follow_timestamp(self: @ContractState, follow_id: u256) -> u64 {
let follow_data = self.follow_data_by_follow_id.read(follow_id);
follow_data.follow_timestamp
}

/// @notice gets the entire follow data for a follow action
/// @param follow_id ID of the follow action
fn get_follow_data(self: @ContractState, follow_id: u256) -> FollowData {
self.follow_data_by_follow_id.read(follow_id)
}

/// @notice checks if a particular address is following the followed profile
/// @param follower_profile_address address of the user to check
fn is_following(self: @ContractState, follower_profile_address: ContractAddress) -> bool {
self.follow_id_by_follower_profile_address.read(follower_profile_address) != 0
}

/// @notice gets the follow ID for a follower_profile_address
/// @param follower_profile_address address of the profile
fn get_follow_id(self: @ContractState, follower_profile_address: ContractAddress) -> u256 {
self.follow_id_by_follower_profile_address.read(follower_profile_address)
}

/// @notice gets the total followers for the followed profile
fn get_follower_count(self: @ContractState) -> u256 {
self.follower_count.read()
}
Expand All @@ -165,6 +185,8 @@ mod Follow {
// *************************************************************************
#[generate_trait]
impl Private of PrivateTrait {
/// @notice internal function that performs the follow action
/// @param follower_profile_address address of profile performing the follow action
fn _follow(ref self: ContractState, follower_profile_address: ContractAddress) -> u256 {
let new_follower_id = self.follower_count.read() + 1;
let follow_timestamp: u64 = get_block_timestamp();
Expand All @@ -190,6 +212,9 @@ mod Follow {
return (new_follower_id);
}

/// @notice internal function that performs the unfollow action
/// @param unfollower address of user performing the unfollow action
/// @param follow_id ID of the initial follow action
fn _unfollow(ref self: ContractState, unfollower: ContractAddress, follow_id: u256) {
self.follow_id_by_follower_profile_address.write(unfollower, 0);
self
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IPublication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub trait IKarstPublications<TContractState> {
// GETTERS
// *************************************************************************
fn get_publication(
self: @TContractState, user: ContractAddress, pubIdAssigned: u256
self: @TContractState, profile_address: ContractAddress, pub_id_assigned: u256
) -> Publication;
fn get_publication_type(
self: @TContractState, profile_address: ContractAddress, pub_id_assigned: u256
Expand Down
5 changes: 5 additions & 0 deletions src/karstnft/karstnft.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ pub mod KarstNFT {

#[abi(embed_v0)]
impl KarstImpl of IKarstNFT::IKarstNFT<ContractState> {
/// @notice mints the karst NFT
/// @param address address of user trying to mint the karst NFT
fn mint_karstnft(ref self: ContractState, address: ContractAddress) {
let balance = self.erc721.balance_of(address);
assert(balance.is_zero(), ALREADY_MINTED);
Expand All @@ -129,10 +131,13 @@ pub mod KarstNFT {
self.last_minted_id.write(token_id);
}

/// @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: ContractAddress) -> u256 {
self.user_token_id.read(user)
}

/// @notice gets the last minted NFT
fn get_last_minted_id(self: @ContractState) -> u256 {
self.last_minted_id.read()
}
Expand Down
1 change: 0 additions & 1 deletion src/mocks.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod registry;
pub mod account;
142 changes: 0 additions & 142 deletions src/mocks/account.cairo

This file was deleted.

22 changes: 20 additions & 2 deletions src/namespaces/handle_registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,17 @@ mod HandleRegistry {
// *************************************************************************
#[abi(embed_v0)]
impl HandleRegistryImpl of IHandleRegistry<ContractState> {
/// @notice links a profile address to a handle
/// @param handle_id ID of handle to be linked
/// @param profile_address address of profile to be linked
fn link(ref self: ContractState, handle_id: u256, profile_address: ContractAddress) {
hub_only(self.karst_hub.read());
self._link(handle_id, profile_address);
}

/// @notice unlinks a profile address from a handle
/// @param handle_id ID of handle to be unlinked
/// @param profile_address address of profile to be unlinked
fn unlink(ref self: ContractState, handle_id: u256, profile_address: ContractAddress) {
let caller = get_caller_address();
self._unlink(handle_id, profile_address, caller);
Expand All @@ -80,13 +86,18 @@ mod HandleRegistry {
// *************************************************************************
// GETTERS
// *************************************************************************

/// @notice resolves a handle to a profile address
/// @param handle_id ID of handle to be resolved
fn resolve(self: @ContractState, handle_id: u256) -> ContractAddress {
let it_exists = IHandleDispatcher { contract_address: self.handle_address.read() }
.exists(handle_id);
assert(it_exists, 'Handle ID does not exist');
assert(it_exists, Errors::HADLE_DOES_NOT_EXIST);
self.handle_to_profile_address.read(handle_id)
}

/// @notice returns the handle linked to a profile address
/// @param profile_address address of profile to be queried
fn get_handle(self: @ContractState, profile_address: ContractAddress) -> u256 {
self.profile_address_to_handle.read(profile_address)
}
Expand All @@ -97,13 +108,16 @@ mod HandleRegistry {
// *************************************************************************
#[generate_trait]
impl Private of PrivateTrait {
/// @notice internal function to link a profile address to a handle
/// @param handle_id ID of handle to be linked
/// @param profile_address address of profile to be linked
fn _link(ref self: ContractState, handle_id: u256, profile_address: ContractAddress) {
let owner = IERC721Dispatcher { contract_address: self.handle_address.read() }
.owner_of(handle_id);
let handle_to_profile = self.handle_to_profile_address.read(handle_id);

assert(profile_address == owner, Errors::INVALID_PROFILE);
assert(handle_to_profile.is_zero(), Errors::OWNER_NOT_ZERO);
assert(handle_to_profile.is_zero(), Errors::HANDLE_ALREADY_LINKED);

self.handle_to_profile_address.write(handle_id, profile_address);
self.profile_address_to_handle.write(profile_address, handle_id);
Expand All @@ -119,6 +133,10 @@ mod HandleRegistry {
)
}

/// @notice internal function to unlink a profile address from a handle
/// @param handle_id ID of handle to be unlinked
/// @param profile_address address of profile to be unlinked
/// @param caller address of user calling this function
fn _unlink(
ref self: ContractState,
handle_id: u256,
Expand Down
Loading
Loading