Skip to content

Commit

Permalink
Merge pull request #28 from EjembiEmmanuel/feat/quote
Browse files Browse the repository at this point in the history
feat: implement quote function in publication contract
  • Loading branch information
codeWhizperer authored Jun 14, 2024
2 parents c470ca8 + 3017b42 commit 2604a06
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/base/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub struct MirrorParams {
// * @param pointed_profile_address The profile address of the publication author that is quoted.
// * @param pointed_pub_id The publication ID that is quoted.
// */
#[derive(Drop, Serde, starknet::Store)]
#[derive(Drop, Serde, starknet::Store, Clone)]
pub struct QuoteParams {
profile_address: ContractAddress,
content_URI: ByteArray,
Expand Down
4 changes: 3 additions & 1 deletion src/interfaces/IPublication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ pub trait IKarstPublications<T> {
pointed_pub_id: u256,
profile_contract_address: ContractAddress,
) -> u256;
fn quote(
ref self: T, quoteParams: QuoteParams, profile_contract_address: ContractAddress
) -> u256;
fn mirror(
ref self: T, mirrorParams: MirrorParams, profile_contract_address: ContractAddress
) -> u256;
fn quote(ref self: T, quoteParams: QuoteParams) -> u256;
// *************************************************************************
// GETTERS
// *************************************************************************
Expand Down
76 changes: 73 additions & 3 deletions src/publication/publication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,42 @@ use karst::base::types::{
use karst::interfaces::IProfile::{IKarstProfileDispatcher, IKarstProfileDispatcherTrait};
use core::option::OptionTrait;

// *************************************************************************
// INTERFACE of KARST PUBLICATIONS
// *************************************************************************
#[starknet::interface]
pub trait IKarstPublications<T> {
// *************************************************************************
// PUBLISHING FUNCTIONS
// *************************************************************************

fn post(
ref self: T,
contentURI: ByteArray,
profile_address: ContractAddress,
profile_contract_address: ContractAddress
) -> u256;
fn comment(
ref self: T,
profile_address: ContractAddress,
content_URI: ByteArray,
pointed_profile_address: ContractAddress,
pointed_pub_id: u256,
profile_contract_address: ContractAddress,
) -> u256;
fn mirror(ref self: T, mirrorParams: MirrorParams) -> u256;
fn quote(
ref self: T, quoteParams: QuoteParams, profile_contract_address: ContractAddress
) -> u256;
////// Getters//////
fn get_publication(self: @T, user: ContractAddress, pubIdAssigned: u256) -> Publication;
fn get_publication_type(
self: @T, profile_address: ContractAddress, pub_id_assigned: u256
) -> PublicationType;
fn get_publication_content_uri(
self: @T, profile_address: ContractAddress, pub_id: u256
) -> ByteArray;
}

#[starknet::contract]
pub mod Publications {
Expand Down Expand Up @@ -43,6 +79,7 @@ pub mod Publications {
pub enum Event {
Post: Post,
MirrorCreated: MirrorCreated,
QuoteCreated: QuoteCreated,
}

// *************************************************************************
Expand All @@ -65,6 +102,14 @@ pub mod Publications {
pub block_timestamp: u64,
}

#[derive(Drop, starknet::Event)]
pub struct QuoteCreated {
pub quoteParams: QuoteParams,
pub publication_id: u256,
pub transaction_executor: ContractAddress,
pub block_timestamp: u64,
}


// *************************************************************************
// EXTERNAL FUNCTIONS
Expand Down Expand Up @@ -176,8 +221,33 @@ pub mod Publications {
pub_id_assigned
}

fn quote(ref self: ContractState, quoteParams: QuoteParams) -> u256 {
0
fn quote(
ref self: ContractState,
quoteParams: QuoteParams,
profile_contract_address: ContractAddress
) -> u256 {
let ref_quoteParams = quoteParams.clone();

let pub_id_assigned = self
._createReferencePublication(
quoteParams.profile_address,
quoteParams.content_URI,
quoteParams.pointed_profile_address,
quoteParams.pointed_pub_id,
PublicationType::Quote,
profile_contract_address
);

self
.emit(
QuoteCreated {
quoteParams: ref_quoteParams,
publication_id: pub_id_assigned,
transaction_executor: quoteParams.profile_address,
block_timestamp: get_block_timestamp(),
}
);
pub_id_assigned
}
// *************************************************************************
// GETTERS
Expand Down Expand Up @@ -280,7 +350,7 @@ pub mod Publications {
content_URI,
pointed_profile_address,
pointed_pub_id,
PublicationType::Comment,
referencePubType,
profile_contract_address
);

Expand Down
126 changes: 123 additions & 3 deletions tests/test_publication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use karst::publication::publication::Publications;
use karst::interfaces::IPublication::{
IKarstPublicationsDispatcher, IKarstPublicationsDispatcherTrait
};
use karst::base::types::{PostParams, MirrorParams, ReferencePubParams, PublicationType};
use karst::base::types::{
PostParams, MirrorParams, ReferencePubParams, PublicationType, QuoteParams
};

const HUB_ADDRESS: felt252 = 'HUB';
const USER_ONE: felt252 = 'BOB';
Expand Down Expand Up @@ -243,7 +245,6 @@ fn test_comment() {
user_one_first_post_pointed_pub_id,
profile_contract_address
);

let user_one_publication_root_id = publication_dispatcher
.get_publication(user_one_profile_address, user_one_comment_assigned_pub_id_1)
.root_profile_address;
Expand All @@ -260,6 +261,126 @@ fn test_comment() {
);
}

#[test]
fn test_quote() {
let (
_,
_,
profile_contract_address,
publication_contract_address,
_,
_,
user_one_profile_address,
user_two_profile_address,
user_three_profile_address,
user_one_first_post_pointed_pub_id,
) =
__setup__();
let publication_dispatcher = IKarstPublicationsDispatcher {
contract_address: publication_contract_address
};
start_prank(
CheatTarget::Multiple(array![publication_contract_address, profile_contract_address]),
USER_ONE.try_into().unwrap()
);
let quote_content_URI = "ipfs://QmSkDCsS32eLpcymxtn1cEn7Rc5hfefLBgfvZysddefzp/";

let quote_params = QuoteParams {
profile_address: user_two_profile_address,
content_URI: quote_content_URI,
pointed_profile_address: user_one_profile_address,
pointed_pub_id: user_one_first_post_pointed_pub_id
};
// user one publish quote to user 2 profile
let quote_pub_id = publication_dispatcher.quote(quote_params, profile_contract_address);

let publication_type = publication_dispatcher
.get_publication_type(user_two_profile_address, quote_pub_id);
assert(publication_type == PublicationType::Quote, 'invalid pub_type');

stop_prank(
CheatTarget::Multiple(array![publication_contract_address, profile_contract_address]),
);

start_prank(
CheatTarget::Multiple(array![publication_contract_address, profile_contract_address]),
USER_TWO.try_into().unwrap()
);
let user_two_quote_content_URI = "ipfs://QmSkDCsS32eLpcymxtn1cEn7Rc5hfefLBgfvZysdjbezo/";

let user_two_quote_params = QuoteParams {
profile_address: user_three_profile_address,
content_URI: user_two_quote_content_URI,
pointed_profile_address: user_one_profile_address,
pointed_pub_id: user_one_first_post_pointed_pub_id
};
// user 2 publish quote to user 3 profile
let user_two_quote_pub_id = publication_dispatcher
.quote(user_two_quote_params, profile_contract_address);

let publication_type = publication_dispatcher
.get_publication_type(user_three_profile_address, user_two_quote_pub_id);
assert(publication_type == PublicationType::Quote, 'invalid pub_type');

let user_one_publication_root_id = publication_dispatcher
.get_publication(user_two_profile_address, quote_pub_id)
.root_profile_address;
let user_two_publication_root_id = publication_dispatcher
.get_publication(user_three_profile_address, user_two_quote_pub_id)
.root_profile_address;
assert(user_one_publication_root_id == user_two_publication_root_id, 'Invalid root_id');

stop_prank(
CheatTarget::Multiple(array![publication_contract_address, profile_contract_address]),
);
}

#[test]
fn test_quote_pointed_profile_address() {
let (
_,
_,
profile_contract_address,
publication_contract_address,
_,
_,
user_one_profile_address,
user_two_profile_address,
_,
user_one_first_post_pointed_pub_id,
) =
__setup__();
let publication_dispatcher = IKarstPublicationsDispatcher {
contract_address: publication_contract_address
};
start_prank(
CheatTarget::Multiple(array![publication_contract_address, profile_contract_address]),
USER_ONE.try_into().unwrap()
);
let quote_content_URI = "ipfs://QmSkDCsS32eLpcymxtn1cEn7Rc5hfefLBgfvZysddefzp/";

let quote_params = QuoteParams {
profile_address: user_two_profile_address,
content_URI: quote_content_URI,
pointed_profile_address: user_one_profile_address,
pointed_pub_id: user_one_first_post_pointed_pub_id
};

start_prank(CheatTarget::One(publication_contract_address), USER_ONE.try_into().unwrap());
let profileDispatcher = IKarstProfileDispatcher { contract_address: profile_contract_address };

publication_dispatcher.quote(quote_params, profile_contract_address);

let pointed_profile = profileDispatcher.get_profile(user_one_profile_address);

assert(
pointed_profile.profile_address == user_one_profile_address,
'Invalid Pointed Profile Address'
);

stop_prank(CheatTarget::One(publication_contract_address),);
}

#[test]
fn test_publish_mirror() {
let (
Expand Down Expand Up @@ -443,7 +564,6 @@ fn test_mirror_root_profile_address() {
stop_prank(CheatTarget::One(publication_contract_address),);
}


fn to_address(name: felt252) -> ContractAddress {
name.try_into().unwrap()
}

0 comments on commit 2604a06

Please sign in to comment.