Skip to content

Commit

Permalink
feat: add get_appliable_proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveVodrazka committed Jan 17, 2025
1 parent 8588ce9 commit 6710949
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/proposals.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ trait IProposals<TContractState> {
) -> felt252;
fn get_proposal_status(self: @TContractState, prop_id: felt252) -> felt252;
fn get_live_proposals(self: @TContractState) -> Array<felt252>;
fn get_appliable_proposals(self: @TContractState) -> Array<felt252>;
fn get_user_voted(
self: @TContractState, user_address: ContractAddress, prop_id: felt252
) -> VoteStatus;
Expand Down Expand Up @@ -269,7 +270,8 @@ mod proposals {
fn _find_free_custom_proposal_type(self: @ComponentState<TContractState>) -> u32 {
let mut i = 0;
let mut res = self.custom_proposal_type.read(i);
while (res.target.is_non_zero()) {
while(res.target.is_non_zero())
{
i += 1;
res = self.custom_proposal_type.read(i);
};
Expand Down Expand Up @@ -337,6 +339,34 @@ mod proposals {
arr
}

fn get_appliable_proposals(self: @ComponentState<TContractState>) -> Array<felt252> {
let max: u32 = self.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 = self.get_proposal_status(prop_id);

// is passed
if current_status == 1 {
let applied: felt252 = self.proposal_applied.read(prop_id);

// is not applied
if applied == 0 {
arr.append(prop_id);
}
}
i += 1;
};

arr
}

fn get_user_voted(
self: @ComponentState<TContractState>, user_address: ContractAddress, prop_id: felt252
) -> VoteStatus {
Expand Down
47 changes: 47 additions & 0 deletions tests/proposals_tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,50 @@ fn test_get_comments() {
assert_eq!(res_span.at(2).ipfs_hash.clone(), ipfs_hash_3);
}

#[test]
fn test_get_appliable_proposals() {
let (gov, _voting, floating) = deploy_governance_and_both_tokens();
set_staking_curve(gov.contract_address);
let gov_contract_addr = gov.contract_address;

let dispatcher = IProposalsDispatcher { contract_address: gov_contract_addr };
stake_all(gov.contract_address, floating, admin_addr.try_into().unwrap());
start_prank(CheatTarget::One(gov_contract_addr), admin_addr.try_into().unwrap());

// create proposals 1, 2, 3
let prop_id_1: u128 = dispatcher.submit_proposal(123, 4).try_into().unwrap();
let prop_id_2: u128 = dispatcher.submit_proposal(124, 4).try_into().unwrap();
let prop_id_3: u128 = dispatcher.submit_proposal(125, 4).try_into().unwrap();

// vote on proposals 1, 2
dispatcher.vote(prop_id_1, 1);
dispatcher.vote(prop_id_2, 1);

//simulate passage of time
let current_timestamp = get_block_timestamp();
let end_timestamp = current_timestamp + PROPOSAL_VOTING_SECONDS;
start_warp(CheatTarget::One(gov.contract_address), end_timestamp + 1);

// 1, 2 should be passed, 3 should be expired
assert!(dispatcher.get_proposal_status(prop_id_1) == 1, "proposal 1 not passed!");
assert!(dispatcher.get_proposal_status(prop_id_2) == 1, "proposal 2 not passed!");
assert!(
dispatcher.get_proposal_status(prop_id_3) == constants::MINUS_ONE, "proposal 3 not expired!"
);

let upgrades_dispatcher = IUpgradesDispatcher { contract_address: gov_contract_addr };

let appliable_proposals_before = dispatcher.get_appliable_proposals();
assert!(appliable_proposals_before.len() == 2, "unexpected number of appliable proposals!");

// apply proposal 2
upgrades_dispatcher.apply_passed_proposal(prop_id_2);

// should be only proposal 1
let appliable_proposals_after = dispatcher.get_appliable_proposals();
assert!(appliable_proposals_after.len() == 1, "more than 1 appliable proposal!");

assert!(
appliable_proposals_after.pop_front().unwrap() == prop_id_1, "wrong appliable proposal id!"
);
}

0 comments on commit 6710949

Please sign in to comment.