Skip to content

Commit

Permalink
Merge pull request #31 from CarmineOptions/feat/voting-period-in-seconds
Browse files Browse the repository at this point in the history
feat: voting time one week
  • Loading branch information
DaveVodrazka authored Dec 12, 2023
2 parents 7b0cc27 + e422b39 commit 141800e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +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 PROPOSAL_VOTING_SECONDS: u64 = consteval_int!(60 * 60 * 24 * 7);
1 change: 1 addition & 0 deletions src/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ mod Governance {
struct Storage {
proposal_details: LegacyMap::<felt252, PropDetails>,
proposal_vote_ends: LegacyMap::<felt252, BlockNumber>,
proposal_vote_end_timestamp: LegacyMap::<felt252, u64>,
proposal_voted_by: LegacyMap::<(felt252, ContractAddress), VoteStatus>,
proposal_total_yay: LegacyMap::<felt252, felt252>,
proposal_total_nay: LegacyMap::<felt252, felt252>,
Expand Down
42 changes: 27 additions & 15 deletions src/proposals.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod Proposals {

use starknet::contract_address::ContractAddressZeroable;
use starknet::get_block_info;
use starknet::get_block_timestamp;
use starknet::get_caller_address;
use starknet::BlockInfo;
use starknet::ContractAddress;
Expand All @@ -26,6 +27,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;
Expand Down Expand Up @@ -61,13 +63,12 @@ mod Proposals {

fn assert_voting_in_progress(prop_id: felt252) {
let state = Governance::unsafe_new_contract_state();
let end_block_number_felt: felt252 = state.proposal_vote_ends.read(prop_id);
let end_block_number: u64 = end_block_number_felt.try_into().unwrap();
assert(end_block_number != 0, 'prop_id not found');
let end_timestamp: u64 = state.proposal_vote_end_timestamp.read(prop_id);
assert(end_timestamp != 0, 'prop_id not found');

let current_block_number: u64 = get_block_info().unbox().block_number;
let current_timestamp: u64 = get_block_timestamp();

assert(end_block_number > current_block_number, 'voting concluded');
assert(end_timestamp > current_timestamp, 'voting concluded');
}

fn as_u256(high: u128, low: u128) -> u256 {
Expand All @@ -88,6 +89,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();
Expand All @@ -102,15 +117,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_block_number: u64 = get_block_info().unbox().block_number;
let end_block_number: BlockNumber = (current_block_number
+ constants::PROPOSAL_VOTING_TIME_BLOCKS)
.into();
state.proposal_vote_ends.write(prop_id, end_block_number);
let current_timestamp: u64 = get_block_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
Expand Down Expand Up @@ -303,11 +316,10 @@ mod Proposals {
fn get_proposal_status(prop_id: felt252) -> felt252 {
let state = Governance::unsafe_new_contract_state();

let end_block_number_felt: felt252 = state.proposal_vote_ends.read(prop_id);
let end_block_number: u64 = end_block_number_felt.try_into().unwrap();
let current_block_number: u64 = get_block_info().unbox().block_number;
let end_timestamp: u64 = state.proposal_vote_end_timestamp.read(prop_id);
let current_timestamp: u64 = get_block_timestamp();

if current_block_number <= end_block_number {
if current_timestamp <= end_timestamp {
return check_proposal_passed_express(prop_id).into();
}

Expand Down

0 comments on commit 141800e

Please sign in to comment.