Skip to content

Commit

Permalink
Merge pull request #85 from Darlington02/main
Browse files Browse the repository at this point in the history
fix `get_handle`
  • Loading branch information
Darlington02 authored Jul 11, 2024
2 parents bdd57ba + dc00a8a commit 908a39e
Show file tree
Hide file tree
Showing 26 changed files with 87 additions and 74 deletions.
6 changes: 3 additions & 3 deletions src/base.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod hubrestricted;
pub mod errors;
pub mod types;
pub mod constants;
pub mod token_uris;
pub mod utils;
2 changes: 2 additions & 0 deletions src/base/constants.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod errors;
mod types;
2 changes: 1 addition & 1 deletion src/base/errors.cairo → src/base/constants/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod Errors {
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 HANDLE_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!';
}
File renamed without changes.
1 change: 1 addition & 0 deletions src/base/token_uris.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 3 additions & 0 deletions src/base/token_uris/follow_token_uri.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// TODO: https://github.com/lens-protocol/core/blob/master/contracts/misc/token-uris/FollowTokenURI.sol#L14


3 changes: 3 additions & 0 deletions src/base/token_uris/handle_token_uri.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// TODO: https://github.com/lens-protocol/core/blob/master/contracts/misc/token-uris/HandleTokenURI.sol


3 changes: 3 additions & 0 deletions src/base/token_uris/profile_token_uri.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// TODO: https://github.com/lens-protocol/core/blob/master/contracts/misc/token-uris/ProfileTokenURI.sol


2 changes: 2 additions & 0 deletions src/base/utils.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod byte_array_extra;
mod hubrestricted;
19 changes: 19 additions & 0 deletions src/base/utils/byte_array_extra.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use core::serde::Serde;

impl FeltTryIntoByteArray of TryInto<felt252, ByteArray> {
fn try_into(self: felt252) -> Option<ByteArray> {
let mut res: ByteArray = Default::default();
res.pending_word = self;
let mut length = 0;
let mut data: u256 = self.into();
loop {
if data == 0 {
break;
}
data /= 0x100;
length += 1;
};
res.pending_word_len = length;
Option::Some(res)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// *************************************************************************
pub mod HubRestricted {
use starknet::{ContractAddress, get_caller_address};
use karst::base::errors::Errors;
use karst::base::constants::errors::Errors;

pub fn hub_only(hub: ContractAddress) {
let caller = get_caller_address();
Expand Down
6 changes: 4 additions & 2 deletions src/follownft/follownft.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ mod Follow {
use starknet::{ContractAddress, get_caller_address, get_block_timestamp};
use core::num::traits::zero::Zero;
use karst::interfaces::{IFollowNFT::IFollowNFT};
use karst::base::{errors::Errors, hubrestricted::HubRestricted::hub_only};
use karst::base::types::FollowData;
use karst::base::{
constants::{errors::Errors, types::FollowData},
utils::hubrestricted::HubRestricted::hub_only
};

// *************************************************************************
// STORAGE
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IFollowNFT.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use starknet::ContractAddress;
use karst::base::types::FollowData;
use karst::base::constants::types::FollowData;

// *************************************************************************
// INTERFACE of FollowNFT
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IHandle.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub trait IHandle<TState> {
// *************************************************************************
fn get_namespace(self: @TState) -> felt252;
fn get_local_name(self: @TState, token_id: u256) -> felt252;
fn get_handle(self: @TState, token_id: u256) -> felt252;
fn get_handle(self: @TState, token_id: u256) -> ByteArray;
fn exists(self: @TState, token_id: u256) -> bool;
fn total_supply(self: @TState) -> u256;
fn get_token_id(self: @TState, local_name: felt252) -> u256;
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IProfile.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use starknet::ContractAddress;
use karst::base::types::Profile;
use karst::base::constants::types::Profile;
// *************************************************************************
// INTERFACE of KARST PROFILE
// *************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/IPublication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// INTERFACE of KARST PUBLICATIONS
// *************************************************************************
use starknet::ContractAddress;
use karst::base::types::{
use karst::base::constants::types::{
PostParams, MirrorParams, ReferencePubParams, PublicationType, Publication, QuoteParams
};

Expand Down
4 changes: 3 additions & 1 deletion src/karstnft/karstnft.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ pub mod KarstNFT {
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 karst::base::{
utils::hubrestricted::HubRestricted::hub_only, constants::errors::Errors::ALREADY_MINTED
};
use openzeppelin::{
account, access::ownable::OwnableComponent,
token::erc721::{
Expand Down
4 changes: 3 additions & 1 deletion src/mocks/interfaces/IComposable.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use starknet::ContractAddress;
use karst::base::types::{Profile, PublicationType, Publication, MirrorParams, QuoteParams};
use karst::base::constants::types::{
Profile, PublicationType, Publication, MirrorParams, QuoteParams
};
// *************************************************************************
// INTERFACE of KARST PROFILE
// *************************************************************************
Expand Down
5 changes: 2 additions & 3 deletions src/namespaces/handle_registry.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ mod HandleRegistry {
};
use karst::interfaces::IHandleRegistry::IHandleRegistry;
use karst::interfaces::IERC721::{IERC721Dispatcher, IERC721DispatcherTrait};
use karst::base::{hubrestricted::HubRestricted::hub_only};
use karst::base::{utils::hubrestricted::HubRestricted::hub_only, constants::errors::Errors};
use karst::interfaces::IHandle::{IHandleDispatcher, IHandleDispatcherTrait};
use karst::base::errors::Errors;

// *************************************************************************
// STORAGE
Expand Down Expand Up @@ -92,7 +91,7 @@ mod HandleRegistry {
fn resolve(self: @ContractState, handle_id: u256) -> ContractAddress {
let it_exists = IHandleDispatcher { contract_address: self.handle_address.read() }
.exists(handle_id);
assert(it_exists, Errors::HADLE_DOES_NOT_EXIST);
assert(it_exists, Errors::HANDLE_DOES_NOT_EXIST);
self.handle_to_profile_address.read(handle_id)
}

Expand Down
19 changes: 12 additions & 7 deletions src/namespaces/handles.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mod Handles {
// *************************************************************************
// IMPORT
// *************************************************************************
use core::num::traits::zero::Zero;
use core::traits::TryInto;
use core::poseidon::PoseidonTrait;
use core::hash::{HashStateTrait, HashStateExTrait};
Expand All @@ -48,9 +49,10 @@ mod Handles {
},
introspection::{src5::SRC5Component}
};
use karst::base::errors::Errors;
use karst::interfaces::IKarstNFT::{IKarstNFTDispatcher, IKarstNFTDispatcherTrait};
use karst::interfaces::IHandle::IHandle;
use karst::base::{constants::errors::Errors, utils::byte_array_extra::FeltTryIntoByteArray};
use karst::interfaces::{
IKarstNFT::{IKarstNFTDispatcher, IKarstNFTDispatcherTrait}, IHandle::IHandle
};

component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
component!(path: SRC5Component, storage: src5, event: SRC5Event);
Expand Down Expand Up @@ -94,7 +96,7 @@ mod Handles {
// CONSTANTS
// *************************************************************************
const MAX_LOCAL_NAME_LENGTH: u256 = 26;
const NAMESPACE: felt252 = 'karst';
const NAMESPACE: felt252 = 'kst';
const ASCII_A: u8 = 97;
const ASCII_Z: u8 = 122;
const ASCII_0: u8 = 48;
Expand Down Expand Up @@ -196,9 +198,12 @@ mod Handles {

/// @notice returns the full handle of a user
/// @param token_id ID of handle to retrieve
fn get_handle(self: @ContractState, token_id: u256) -> felt252 {
fn get_handle(self: @ContractState, token_id: u256) -> ByteArray {
let local_name = self.get_local_name(token_id);
let handle = NAMESPACE + '@/' + local_name;
assert(local_name.is_non_zero(), Errors::HANDLE_DOES_NOT_EXIST);
let local_name_in_byte_array: ByteArray = local_name.try_into().unwrap();
let namespace_in_byte_array: ByteArray = NAMESPACE.try_into().unwrap();
let handle = local_name_in_byte_array + "." + namespace_in_byte_array;
handle
}

Expand All @@ -213,7 +218,7 @@ mod Handles {
self.total_supply.read()
}

/// @notice returns the handle ID for a given local name
/// @notice returns the token ID for a given local name
/// @param local_name local name to be queried
fn get_token_id(self: @ContractState, local_name: felt252) -> u256 {
let hash: u256 = PoseidonTrait::new()
Expand Down
7 changes: 4 additions & 3 deletions src/profile/profile.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ mod ProfileComponent {
};
use karst::interfaces::IERC721::{IERC721Dispatcher, IERC721DispatcherTrait};
use karst::interfaces::IProfile::IProfile;
use karst::base::types::Profile;
use karst::base::errors::Errors::NOT_PROFILE_OWNER;
use karst::base::{hubrestricted::HubRestricted::hub_only};
use karst::base::{
constants::types::Profile, constants::errors::Errors::NOT_PROFILE_OWNER,
utils::hubrestricted::HubRestricted::hub_only
};

// *************************************************************************
// STORAGE
Expand Down
15 changes: 9 additions & 6 deletions src/publication/publication.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use starknet::{ContractAddress, get_caller_address};
use karst::base::types::{
use karst::base::constants::types::{
PostParams, PublicationType, CommentParams, ReferencePubParams, Publication, MirrorParams,
QuoteParams
};
Expand All @@ -17,12 +17,15 @@ pub mod PublicationComponent {
use core::option::OptionTrait;
use starknet::{ContractAddress, get_contract_address, get_caller_address, get_block_timestamp};
use karst::interfaces::IPublication::IKarstPublications;
use karst::base::errors::Errors::{NOT_PROFILE_OWNER, UNSUPPORTED_PUB_TYPE};
use karst::base::{hubrestricted::HubRestricted::hub_only};
use karst::base::types::{
PostParams, Publication, PublicationType, ReferencePubParams, CommentParams, QuoteParams,
MirrorParams
use karst::base::{
constants::errors::Errors::{NOT_PROFILE_OWNER, UNSUPPORTED_PUB_TYPE},
utils::hubrestricted::HubRestricted::hub_only,
constants::types::{
PostParams, Publication, PublicationType, ReferencePubParams, CommentParams,
QuoteParams, MirrorParams
}
};

use karst::profile::profile::ProfileComponent;


Expand Down
2 changes: 1 addition & 1 deletion tests/test_follownft.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use snforge_std::{

use karst::interfaces::IFollowNFT::{IFollowNFTDispatcher, IFollowNFTDispatcherTrait};
use karst::follownft::follownft::Follow;
use karst::base::types::FollowData;
use karst::base::constants::types::FollowData;

const HUB_ADDRESS: felt252 = 24205;
const FOLLOWED_ADDRESS: felt252 = 1234;
Expand Down
44 changes: 5 additions & 39 deletions tests/test_handle.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ fn test_cannot_burn_if_not_owner_of() {
dispatcher.burn_handle(handle_id);
}


#[test]
fn test_get_handle() {
let handles_contract_address = __setup__();
Expand All @@ -187,56 +186,23 @@ fn test_get_handle() {

start_prank(CheatTarget::One(handles_contract_address), USER_ONE.try_into().unwrap());

let token_id = handles_dispatcher
.mint_handle(USER_ONE.try_into().unwrap(), TEST_LOCAL_NAME_THREE);

let handle: felt252 = handles_dispatcher.get_handle(token_id);

let namespace: felt252 = handles_dispatcher.get_namespace();

let test_handle: felt252 = namespace + '@/' + TEST_LOCAL_NAME_THREE;

assert(handle == test_handle, 'Invalid handle');

stop_prank(CheatTarget::One(handles_contract_address));
}


#[test]
fn test_get_handle_two() {
let handles_contract_address = __setup__();

let handles_dispatcher = IHandleDispatcher { contract_address: handles_contract_address };

start_prank(CheatTarget::One(handles_contract_address), USER_ONE.try_into().unwrap());

let token_id = handles_dispatcher
.mint_handle(USER_ONE.try_into().unwrap(), TEST_LOCAL_NAME_FOUR);

let handle: felt252 = handles_dispatcher.get_handle(token_id);

let namespace: felt252 = handles_dispatcher.get_namespace();

let test_handle: felt252 = namespace + '@/' + TEST_LOCAL_NAME_FOUR;
let token_id = handles_dispatcher.mint_handle(USER_ONE.try_into().unwrap(), TEST_LOCAL_NAME);

assert(handle == test_handle, 'Invalid handle');
let handle: ByteArray = handles_dispatcher.get_handle(token_id);
assert(handle == "karst.kst", 'Invalid handle');

stop_prank(CheatTarget::One(handles_contract_address));
}


#[test]
#[should_panic()]
#[should_panic(expected: ('Karst: handle does not exist!',))]
fn test_get_handle_should_panic() {
let handles_contract_address = __setup__();

let handles_dispatcher = IHandleDispatcher { contract_address: handles_contract_address };

start_prank(CheatTarget::One(handles_contract_address), USER_ONE.try_into().unwrap());

let invalid_handle: felt252 = handles_dispatcher.get_handle(TEST_TOKEN_ID);

assert(invalid_handle == 0, 'Expected an invalid handle');

handles_dispatcher.get_handle(TEST_TOKEN_ID);
stop_prank(CheatTarget::One(handles_contract_address));
}
2 changes: 1 addition & 1 deletion tests/test_karstnft.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use snforge_std::{declare, ContractClassTrait, CheatTarget, start_prank, stop_pr
use openzeppelin::{token::erc721::interface::{ERC721ABIDispatcher, ERC721ABIDispatcherTrait}};

use karst::interfaces::IKarstNFT::{IKarstNFTDispatcher, IKarstNFTDispatcherTrait};
use karst::base::{errors::Errors::ALREADY_MINTED};
use karst::base::constants::errors::Errors::ALREADY_MINTED;

const ADMIN: felt252 = 'ADMIN';
const USER_ONE: felt252 = 'BOB';
Expand Down
2 changes: 1 addition & 1 deletion tests/test_publication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use karst::mocks::registry::Registry;
use karst::interfaces::IRegistry::{IRegistryDispatcher, IRegistryDispatcherTrait};
use karst::karstnft::karstnft::KarstNFT;
use karst::mocks::interfaces::IComposable::{IComposableDispatcher, IComposableDispatcherTrait};
use karst::base::types::{
use karst::base::constants::types::{
PostParams, MirrorParams, ReferencePubParams, PublicationType, QuoteParams
};

Expand Down

0 comments on commit 908a39e

Please sign in to comment.