From 92518f3f8a79c48daf77d83e4a42011e741a1211 Mon Sep 17 00:00:00 2001 From: DaveVodrazka Date: Mon, 11 Dec 2023 10:41:34 +0100 Subject: [PATCH] fix: use new storage var for timestamp --- src/constants.cairo | 2 +- src/contract.cairo | 3 ++- src/proposals.cairo | 25 ++++++++++++++++++++----- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/constants.cairo b/src/constants.cairo index fc1e6276..3d0ced4a 100644 --- a/src/constants.cairo +++ b/src/constants.cairo @@ -8,4 +8,4 @@ const OPTION_CALL: felt252 = 0; const OPTION_PUT: felt252 = 1; const TRADE_SIDE_LONG: felt252 = 0; const TRADE_SIDE_SHORT: felt252 = 1; -const ONE_WEEK_SECONDS: u64 = 604800; +const PROPOSAL_VOTING_SECONDS: u64 = consteval_int!(60 * 60 * 24 * 7); diff --git a/src/contract.cairo b/src/contract.cairo index a276a4b7..f47fd004 100644 --- a/src/contract.cairo +++ b/src/contract.cairo @@ -54,7 +54,8 @@ mod Governance { #[storage] struct Storage { proposal_details: LegacyMap::, - proposal_vote_ends: LegacyMap::, + proposal_vote_ends: LegacyMap::, + proposal_vote_end_timestamp: LegacyMap::, proposal_voted_by: LegacyMap::<(felt252, ContractAddress), VoteStatus>, proposal_total_yay: LegacyMap::, proposal_total_nay: LegacyMap::, diff --git a/src/proposals.cairo b/src/proposals.cairo index 6a110bbe..f6ee6b59 100644 --- a/src/proposals.cairo +++ b/src/proposals.cairo @@ -26,6 +26,7 @@ mod Proposals { use governance::contract::Governance::proposal_total_yayContractMemberStateTrait; use governance::contract::Governance::proposal_total_nayContractMemberStateTrait; use governance::contract::Governance::proposal_vote_endsContractMemberStateTrait; + use governance::contract::Governance::proposal_vote_end_timestampContractMemberStateTrait; use governance::contract::Governance::delegate_hashContractMemberStateTrait; use governance::contract::Governance::total_delegated_toContractMemberStateTrait; use governance::contract::Governance::proposal_voted_byContractMemberStateTrait; @@ -61,7 +62,7 @@ mod Proposals { fn assert_voting_in_progress(prop_id: felt252) { let state = Governance::unsafe_new_contract_state(); - let end_timestamp: u64 = state.proposal_vote_ends.read(prop_id); + let end_timestamp: u64 = state.proposal_vote_end_timestamp.read(prop_id); assert(end_timestamp != 0, 'prop_id not found'); let current_timestamp: u64 = get_block_info().unbox().block_timestamp; @@ -87,6 +88,20 @@ mod Proposals { } } + fn get_free_prop_id_timestamp() -> felt252 { + _get_free_prop_id_timestamp(0) + } + + fn _get_free_prop_id_timestamp(currid: felt252) -> felt252 { + let state = Governance::unsafe_new_contract_state(); + let res = state.proposal_vote_end_timestamp.read(currid); + if res == 0 { + currid + } else { + _get_free_prop_id_timestamp(currid + 1) + } + } + fn submit_proposal(payload: felt252, to_upgrade: ContractType) -> felt252 { assert_correct_contract_type(to_upgrade); let mut state = Governance::unsafe_new_contract_state(); @@ -101,13 +116,13 @@ mod Proposals { ); // TODO use such multiplication that u128 * u128 = u256 assert(total_supply < res, 'not enough tokens to submit'); - let prop_id = get_free_prop_id(); + let prop_id = get_free_prop_id_timestamp(); let prop_details = PropDetails { payload: payload, to_upgrade: to_upgrade }; state.proposal_details.write(prop_id, prop_details); let current_timestamp: u64 = get_block_info().unbox().block_timestamp; - let end_timestamp: u64 = current_timestamp + constants::ONE_WEEK_SECONDS; - state.proposal_vote_ends.write(prop_id, end_timestamp); + let end_timestamp: u64 = current_timestamp + constants::PROPOSAL_VOTING_SECONDS; + state.proposal_vote_end_timestamp.write(prop_id, end_timestamp); state.emit(Governance::Proposed { prop_id, payload, to_upgrade }); prop_id @@ -300,7 +315,7 @@ mod Proposals { fn get_proposal_status(prop_id: felt252) -> felt252 { let state = Governance::unsafe_new_contract_state(); - let end_timestamp: u64 = state.proposal_vote_ends.read(prop_id); + let end_timestamp: u64 = state.proposal_vote_end_timestamp.read(prop_id); let current_timestamp: u64 = get_block_info().unbox().block_timestamp; if current_timestamp <= end_timestamp {