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

get_live_proposals & get_user_voted #36

Merged
merged 2 commits into from
Jan 17, 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
16 changes: 15 additions & 1 deletion src/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// When Components arrive in Cairo 2.?, it will be refactored to take advantage of them. Random change to rerun CI

use starknet::ContractAddress;
use governance::types::{ContractType, PropDetails};
use governance::types::{ContractType, PropDetails, VoteStatus};

#[starknet::interface]
trait IGovernance<TContractState> {
Expand All @@ -15,6 +15,10 @@ trait IGovernance<TContractState> {
ref self: TContractState, impl_hash: felt252, to_upgrade: ContractType
) -> felt252;
fn get_proposal_status(self: @TContractState, prop_id: felt252) -> felt252;
fn get_live_proposals(self: @TContractState) -> Array<felt252>;
fn get_user_voted(
self: @TContractState, user_address: ContractAddress, prop_id: felt252
) -> VoteStatus;

// UPGRADES

Expand Down Expand Up @@ -130,6 +134,16 @@ mod Governance {
Proposals::get_proposal_status(prop_id)
}

fn get_live_proposals(self: @ContractState) -> Array<felt252> {
Proposals::get_live_proposals()
}

fn get_user_voted(
self: @ContractState, user_address: ContractAddress, prop_id: felt252
) -> VoteStatus {
Proposals::get_user_voted(user_address, prop_id)
}

// UPGRADES

fn get_governance_token_address(self: @ContractState) -> ContractAddress {
Expand Down
29 changes: 29 additions & 0 deletions src/proposals.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ mod Proposals {
use governance::types::BlockNumber;
use governance::types::ContractType;
use governance::types::PropDetails;
use governance::types::VoteStatus;
use governance::traits::IERC20Dispatcher;
use governance::traits::IERC20DispatcherTrait;
use governance::constants;
Expand Down Expand Up @@ -103,6 +104,34 @@ mod Proposals {
}
}

fn get_live_proposals() -> Array<felt252> {
let max: u32 = get_free_prop_id_timestamp().try_into().unwrap();
let mut i: u32 = 0;
let mut arr = ArrayTrait::<felt252>::new();

loop {
if i >= max {
break;
}

let prop_id: felt252 = i.into();
let current_status = get_proposal_status(prop_id);

if current_status == 0 {
arr.append(prop_id);
}

i += 1;
};

arr
}

fn get_user_voted(user_address: ContractAddress, prop_id: felt252) -> VoteStatus {
let state = Governance::unsafe_new_contract_state();
state.proposal_voted_by.read((prop_id, user_address))
}

fn submit_proposal(payload: felt252, to_upgrade: ContractType) -> felt252 {
assert_correct_contract_type(to_upgrade);
let mut state = Governance::unsafe_new_contract_state();
Expand Down