Skip to content

Commit

Permalink
get_live_proposals & get_user_voted (#36)
Browse files Browse the repository at this point in the history
* feat: get_live_proposals

* feat: get_user_voted
  • Loading branch information
DaveVodrazka authored Jan 17, 2024
1 parent ecbd84c commit 3c50c51
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
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 @@ -126,6 +130,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

0 comments on commit 3c50c51

Please sign in to comment.