-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add discussion component * check if proposal is live * Implement check CRAM token holder functionality * Refactor get_comment to return array of tuples * Refactor: change panic to assert * Add unit test for discussion/add_comment * Add test for discussion/get_comment * Refac * Refac: get_comments to return an array of comments * Polish * Format with scarb fmt * bug fix * Fix camelCase usage * Refac: test_get_comments * refac: clone ipfs_hash variables and ipfs hash in comment response * Polish --------- Co-authored-by: Ondřej Sojka <[email protected]>
- Loading branch information
Showing
7 changed files
with
290 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use starknet::ContractAddress; | ||
use konoha::types::Comment; | ||
|
||
#[starknet::interface] | ||
trait IDiscussion<TContractState> { | ||
fn add_comment(ref self: TContractState, prop_id: u32, ipfs_hash: ByteArray); | ||
fn get_comments(self: @TContractState, prop_id: u32) -> Array<Comment>; | ||
} | ||
|
||
#[starknet::component] | ||
mod discussion { | ||
use array::ArrayTrait; | ||
use core::box::Box; | ||
use core::serde::Serde; | ||
|
||
use starknet::get_caller_address; | ||
use starknet::ContractAddress; | ||
|
||
use konoha::proposals::proposals as proposals_component; | ||
use konoha::proposals::proposals::ProposalsImpl; | ||
use konoha::traits::IERC20Dispatcher; | ||
use konoha::traits::IERC20DispatcherTrait; | ||
use konoha::traits::get_governance_token_address_self; | ||
use konoha::types::Comment; | ||
|
||
#[storage] | ||
struct Storage { | ||
// mapping of (proposal id, index) to comment | ||
comments: LegacyMap::<(u32, u64), Comment>, | ||
// mapping of proposal id to number of comments | ||
comment_count: LegacyMap::<u32, u64> | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event {} | ||
|
||
#[embeddable_as(DiscussionImpl)] | ||
impl Discussions< | ||
TContractState, | ||
+HasComponent<TContractState>, | ||
+Drop<TContractState>, | ||
impl Proposals: proposals_component::HasComponent<TContractState>, | ||
> of super::IDiscussion<ComponentState<TContractState>> { | ||
fn add_comment( | ||
ref self: ComponentState<TContractState>, prop_id: u32, ipfs_hash: ByteArray | ||
) { | ||
//Check if proposal is live | ||
let is_live = self.is_proposal_live(prop_id); | ||
assert(is_live, 'Proposal is not live!'); | ||
|
||
//Check if caller is a governance token holder | ||
let user_address = get_caller_address(); | ||
let govtoken_addr = get_governance_token_address_self(); | ||
let caller_balance: u256 = IERC20Dispatcher { contract_address: govtoken_addr } | ||
.balance_of(user_address); | ||
assert(caller_balance.high != 0 || caller_balance.low != 0, 'Govtoken balance is zero'); | ||
|
||
//get current comment count | ||
let count: u64 = self.comment_count.read(prop_id); | ||
|
||
//store new comment/ipfs_hash at next index | ||
let new_comment = Comment { user: user_address, ipfs_hash: ipfs_hash }; | ||
self.comments.write((prop_id, count), new_comment); | ||
|
||
//Increment comment count for proposal by one | ||
self.comment_count.write(prop_id, count + 1); | ||
} | ||
|
||
fn get_comments(self: @ComponentState<TContractState>, prop_id: u32) -> Array<Comment> { | ||
//Get comment counts | ||
let count: u64 = self.comment_count.read(prop_id); | ||
|
||
//Initialize an array of comments | ||
let mut arr = ArrayTrait::<Comment>::new(); | ||
|
||
// loop over comment count and collect comments | ||
let mut i: u64 = 0; | ||
loop { | ||
if i >= count { | ||
break; | ||
} | ||
|
||
arr.append(self.comments.read((prop_id, i))); | ||
i += 1; | ||
}; | ||
|
||
// return array of comments | ||
arr | ||
} | ||
} | ||
|
||
#[generate_trait] | ||
impl InternalImpl< | ||
TContractState, | ||
+HasComponent<TContractState>, | ||
+Drop<TContractState>, | ||
impl Proposals: proposals_component::HasComponent<TContractState>, | ||
> of InternalTrait<TContractState> { | ||
fn is_proposal_live(ref self: ComponentState<TContractState>, prop_id: u32) -> bool { | ||
let proposals_comp = get_dep_component!(@self, Proposals); | ||
|
||
let live_proposals = proposals_comp.get_live_proposals(); | ||
|
||
let mut is_live = false; | ||
|
||
//loop over the array to check if prop_id is in the array | ||
let mut i = 0; | ||
loop { | ||
if i >= live_proposals.len() { | ||
break; | ||
} | ||
|
||
match live_proposals.get(i) { | ||
Option::Some(_prop_id) => { | ||
is_live = true; | ||
break; | ||
}, | ||
Option::None => i += 1 | ||
} | ||
}; | ||
is_live | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
mod airdrop_tests; | ||
mod basic; | ||
mod test_treasury; | ||
mod proposals_tests; | ||
mod airdrop_tests; | ||
mod upgrades_tests; | ||
mod setup; | ||
mod test_treasury; | ||
mod upgrades_tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters