Skip to content

Commit

Permalink
Add default proposal parameters and custom proposal configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
tensojka committed Aug 22, 2024
1 parent 010d070 commit 9da3691
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
10 changes: 0 additions & 10 deletions src/constants.cairo
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
const NEW_PROPOSAL_QUORUM: u128 =
200; // 1/200 of totalSupply required to propose an upgrade. Quorums don't take into account investors. at all, they don't count into total eligible voters, but do vote
const QUORUM: u128 = 10; // 1/10 of totalSupply required to participate to pass
const MINUS_ONE: felt252 = 0x800000000000011000000000000000000000000000000000000000000000000;
const TEAM_TOKEN_BALANCE: u128 = 1000000000000000000;
const PROPOSAL_VOTING_SECONDS: u64 = consteval_int!(60 * 60 * 24 * 7);


// ADDRESSES

const USDC_ADDRESS: felt252 = 0x53c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8;
const ETH_ADDRESS: felt252 = 0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7;
const BTC_ADDRESS: felt252 = 0x03fe2b97c1fd336e750087d68b9b867997fd64a2661ff3ca5a7c771641e8e7ac;
14 changes: 13 additions & 1 deletion src/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,22 @@ mod Governance {
let send_tokens_custom_proposal_config: CustomProposalConfig = CustomProposalConfig {
target: treasury_address.into(),
selector: selector!("send_tokens_to_address"),
library_call: false
library_call: false,
proposal_voting_time: 0 // use global default
};

proposals.add_custom_proposal_config(send_tokens_custom_proposal_config);

let set_default_proposal_params_custom_proposal_config: CustomProposalConfig = CustomProposalConfig {
target: governance_address.into(),
selector: selector!("set_default_proposal_params"),
library_call: false,
proposal_voting_time: 0 // use global default
};

proposals.add_custom_proposal_config(set_default_proposal_params_custom_proposal_config);

proposals.set_default_proposal_params(quorum: 10, proposal_voting_seconds: consteval_int!(60 * 60 * 24 * 7)); // can be omitted to keep the default values
}

#[abi(embed_v0)]
Expand Down
43 changes: 39 additions & 4 deletions src/proposals.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ trait IProposals<TContractState> {
);
fn get_total_delegated_to(self: @TContractState, to_addr: ContractAddress) -> u128;
fn add_custom_proposal_config(ref self: TContractState, config: CustomProposalConfig) -> u32;
fn set_default_proposal_params(ref self: TContractState, quorum: u32, proposal_voting_seconds: u32);
}

#[starknet::component]
Expand Down Expand Up @@ -94,7 +95,9 @@ mod proposals {
custom_proposal_type: LegacyMap::<u32, CustomProposalConfig>, // custom proposal type
custom_proposal_payload: LegacyMap::<
(u32, u32), felt252
> // mapping from prop_id and index to calldata
>, // mapping from prop_id and index to calldata
quorum: u32,
proposal_voting_seconds: u32
}

#[derive(starknet::Event, Drop)]
Expand Down Expand Up @@ -270,6 +273,24 @@ mod proposals {
};
i
}

fn get_quorum(self: @ComponentState<TContractState>) -> u32 {
let saved = self.quorum.read();
if (saved == 0) {
10
} else {
saved
}
}

fn get_proposal_voting_seconds(self: @ComponentState<TContractState>) -> u64 {
let saved = self.proposal_voting_seconds.read();
if (saved == 0) {
consteval_int!(60 * 60 * 24 * 7)
} else {
saved.into()
}
}
}

#[embeddable_as(ProposalsImpl)]
Expand Down Expand Up @@ -337,7 +358,7 @@ mod proposals {
self.proposal_details.write(prop_id, prop_details);

let current_timestamp: u64 = get_block_timestamp();
let end_timestamp: u64 = current_timestamp + constants::PROPOSAL_VOTING_SECONDS;
let end_timestamp: u64 = current_timestamp + self.get_proposal_voting_seconds();
self.proposal_vote_end_timestamp.write(prop_id, end_timestamp);

self.emit(Proposed { prop_id, payload, to_upgrade });
Expand Down Expand Up @@ -367,7 +388,11 @@ mod proposals {
self.proposal_details.write(prop_id_felt, prop_details);

let current_timestamp: u64 = get_block_timestamp();
let end_timestamp: u64 = current_timestamp + constants::PROPOSAL_VOTING_SECONDS;
let end_timestamp: u64 = if (config.proposal_voting_time == 0) {
current_timestamp + self.get_proposal_voting_seconds()
} else {
current_timestamp + config.proposal_voting_time.into()
};
self.proposal_vote_end_timestamp.write(prop_id_felt, end_timestamp);
self.emit(Proposed { prop_id: prop_id_felt, payload, to_upgrade: 5 });

Expand Down Expand Up @@ -522,7 +547,7 @@ mod proposals {
assert(total_eligible_votes_u256.high == 0, 'unable to check quorum');
let total_eligible_votes: u128 = total_eligible_votes_u256.low;

let quorum_threshold: u128 = total_eligible_votes * constants::QUORUM;
let quorum_threshold: u128 = total_eligible_votes * self.get_quorum().into();
if total_tally_multiplied < quorum_threshold {
return constants::MINUS_ONE; // didn't meet quorum
}
Expand Down Expand Up @@ -554,5 +579,15 @@ mod proposals {
self.custom_proposal_type.write(idx, config);
idx
}

fn set_default_proposal_params(ref self: ComponentState<TContractState>, quorum: u32, proposal_voting_seconds: u32) {
assert(get_caller_address() == get_contract_address(), 'can only be called by self');
assert(quorum < 30, 'quorum must be <30');
assert(quorum >= 1, 'quorum < 1?');
assert(proposal_voting_seconds > 3600, 'propvoting secs too short');
assert(proposal_voting_seconds < 3000000, 'propvoting secs > 1mo?');
self.quorum.write(quorum);
self.proposal_voting_seconds.write(proposal_voting_seconds);
}
}
}
3 changes: 2 additions & 1 deletion src/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type ContractType =
struct CustomProposalConfig {
target: felt252, //class hash if library call, contract address if regular call
selector: felt252,
library_call: bool
library_call: bool,
proposal_voting_time: u32
}

#[derive(Drop, Serde, starknet::Store)]
Expand Down

0 comments on commit 9da3691

Please sign in to comment.