Skip to content

Commit

Permalink
feature: add upvote and downvote function
Browse files Browse the repository at this point in the history
  • Loading branch information
Adegbite Ademola Kelvin authored and Adegbite Ademola Kelvin committed Sep 11, 2024
1 parent 20109d3 commit 5a63f47
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 284 deletions.
3 changes: 3 additions & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ alexandria_bytes = { git = "https://github.com/keep-starknet-strange/alexandria.
[dev-dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.22.0" }

[sncast.default]
url= "https://starknet-sepolia.public.blastapi.io"

[[target.starknet-contract]]
casm = true
build-external-contracts = ["token_bound_accounts::presets::account::Account"]
Expand Down
2 changes: 2 additions & 0 deletions src/base/constants/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ pub mod Errors {
pub const UNSUPPORTED_PUB_TYPE: felt252 = 'Karst: unsupported pub type!';
pub const INVALID_PROFILE_ADDRESS: felt252 = 'Karst: invalid profile address!';
pub const SELF_FOLLOWING: felt252 = 'Karst: self follow is forbidden';
pub const ALREADY_UPVOTED: felt252 = 'Karst: user already upvoted!';
pub const ALREADY_DOWNVOTED: felt252 = 'Karst: user already downvoted!';
}
34 changes: 27 additions & 7 deletions src/base/constants/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ pub struct Publication {
content_URI: ByteArray,
pub_Type: PublicationType,
root_profile_address: ContractAddress,
root_pub_id: u256
root_pub_id: u256,
upvote: Upvote,
downvote: Downvote,
}

// /**
Expand All @@ -66,15 +68,13 @@ pub struct Publication {
// * @param Post A standard post, having an URI, and no pointer to another publication.
// * @param Comment A comment, having an URI, and a pointer to another publication.
// * @param Mirror A mirror, having a pointer to another publication, but no URI.
// * @param Quote A quote, having an URI, and a pointer to another publication.
// */
#[derive(Debug, Drop, Serde, starknet::Store, Clone, PartialEq)]
enum PublicationType {
Nonexistent,
Post,
Comment,
Mirror,
Quote
Repost,
}

// /**
Expand Down Expand Up @@ -103,7 +103,7 @@ struct CommentParams {
content_URI: ByteArray,
pointed_profile_address: ContractAddress,
pointed_pub_id: u256,
reference_pub_type: PublicationType
reference_pub_type: PublicationType,
}


Expand All @@ -124,10 +124,10 @@ pub struct ReferencePubParams {
// * @param pointed_pub_id The publication ID to point the mirror to.
// */
#[derive(Drop, Serde, starknet::Store, Clone)]
pub struct MirrorParams {
pub struct RepostParams {
profile_address: ContractAddress,
pointed_profile_address: ContractAddress,
pointed_pub_id: u256
pointed_pub_id: u256,
}

// /**
Expand All @@ -146,3 +146,23 @@ pub struct QuoteParams {
pointed_pub_id: u256,
reference_pub_type: PublicationType
}
#[derive(Debug, Drop, Serde, starknet::Store, Clone)]
pub struct Upvote {
publication_id: u256,
transaction_executor: ContractAddress,
block_timestamp: u64,
}
#[derive(Debug, Drop, Serde, starknet::Store, Clone)]
pub struct Downvote {
publication_id: u256,
transaction_executor: ContractAddress,
block_timestamp: u64,
}

// Enum to represent vote types
#[derive(Debug, Drop, Serde, starknet::Store, Clone)]
enum VoteStatus {
None, // No vote cast
Upvoted, // User upvoted
Downvoted // User downvoted
}
6 changes: 2 additions & 4 deletions src/interfaces/IHub.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use starknet::ContractAddress;
use karst::base::constants::types::{
Profile, PostParams, MirrorParams, CommentParams, PublicationType, Publication, QuoteParams
Profile, PostParams, RepostParams, CommentParams, PublicationType, Publication, QuoteParams
};

// *************************************************************************
Expand Down Expand Up @@ -36,9 +36,7 @@ pub trait IHub<TState> {

fn comment(ref self: TState, comment_params: CommentParams) -> u256;

fn quote(ref self: TState, quote_params: QuoteParams) -> u256;

fn mirror(ref self: TState, mirror_params: MirrorParams) -> u256;
fn repost(ref self: TState, repost_params: RepostParams) -> u256;

fn get_publication(
self: @TState, profile_address: ContractAddress, pub_id_assigned: u256
Expand Down
11 changes: 8 additions & 3 deletions src/interfaces/IPublication.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// *************************************************************************
use starknet::ContractAddress;
use karst::base::constants::types::{
PostParams, MirrorParams, CommentParams, PublicationType, Publication, QuoteParams
PostParams, RepostParams, CommentParams, PublicationType, Publication, QuoteParams
};

#[starknet::interface]
Expand All @@ -13,8 +13,11 @@ pub trait IKarstPublications<TState> {
// *************************************************************************
fn post(ref self: TState, post_params: PostParams) -> u256;
fn comment(ref self: TState, comment_params: CommentParams) -> u256;
fn quote(ref self: TState, quote_params: QuoteParams) -> u256;
fn mirror(ref self: TState, mirror_params: MirrorParams) -> u256;
fn repost(ref self: TState, repost_params: RepostParams) -> u256;
fn upvote(ref self: TState, profile_address: ContractAddress, pub_id: u256);
fn downvote(ref self: TState, profile_address: ContractAddress, pub_id: u256);
fn collect(ref self: TState, pub_id: u256) -> bool;

// *************************************************************************
// GETTERS
// *************************************************************************
Expand All @@ -27,4 +30,6 @@ pub trait IKarstPublications<TState> {
fn get_publication_content_uri(
self: @TState, profile_address: ContractAddress, pub_id: u256
) -> ByteArray;
fn get_vote_count(self: @TState, pub_id: u256) -> u256;
fn has_user_voted(self: @TState, profile_address: ContractAddress, pub_id: u256) -> bool;
}
10 changes: 7 additions & 3 deletions src/mocks/interfaces/IComposable.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use starknet::ContractAddress;
use karst::base::constants::types::{
Profile, PublicationType, Publication, MirrorParams, QuoteParams, PostParams, CommentParams
Profile, PublicationType, Publication, RepostParams, QuoteParams, PostParams, CommentParams
};
// *************************************************************************
// INTERFACE of KARST PROFILE
Expand Down Expand Up @@ -43,8 +43,10 @@ pub trait IComposable<TState> {
fn initialize(ref self: TState, hub_address: ContractAddress);
fn post(ref self: TState, post_params: PostParams) -> u256;
fn comment(ref self: TState, comment_params: CommentParams) -> u256;
fn quote(ref self: TState, quote_params: QuoteParams) -> u256;
fn mirror(ref self: TState, mirror_params: MirrorParams) -> u256;
fn repost(ref self: TState, mirror_params: RepostParams) -> u256;
fn upvote(ref self: TState, profile_address: ContractAddress, pub_id: u256);
fn downvote(ref self: TState, profile_address: ContractAddress, pub_id: u256);
fn collect(ref self: TState, pub_id: u256) -> bool;
// *************************************************************************
// GETTERS
// *************************************************************************
Expand All @@ -57,4 +59,6 @@ pub trait IComposable<TState> {
fn get_publication_content_uri(
self: @TState, profile_address: ContractAddress, pub_id: u256
) -> ByteArray;
fn get_vote_count(self: @TState, pub_id: u256) -> u256;
fn has_user_voted(self: @TState, profile_address: ContractAddress, pub_id: u256) -> bool;
}
Loading

0 comments on commit 5a63f47

Please sign in to comment.