Skip to content

Commit

Permalink
add a method for proposing and describing in a single call
Browse files Browse the repository at this point in the history
fixes #46
  • Loading branch information
moodysalem committed May 11, 2024
1 parent 3d35cc1 commit 80d6465
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/governor.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ pub trait IGovernor<TContractState> {
// Attaches the given text to the proposal. Simply emits an event containing the proposal description.
fn describe(ref self: TContractState, id: felt252, description: ByteArray);

// Combined propose and describe methods
fn propose_and_describe(
ref self: TContractState, calls: Span<Call>, description: ByteArray
) -> felt252;

// Get the staker that is used by this governor contract.
fn get_staker(self: @TContractState) -> IStakerDispatcher;

Expand Down Expand Up @@ -244,6 +249,14 @@ pub mod Governor {
self.emit(Described { id, description });
}

fn propose_and_describe(
ref self: ContractState, calls: Span<Call>, description: ByteArray
) -> felt252 {
let id = self.propose(calls);
self.describe(id, description);
id
}

fn vote(ref self: ContractState, id: felt252, yea: bool) {
let mut proposal = self.proposals.read(id);

Expand Down
28 changes: 28 additions & 0 deletions src/governor_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,34 @@ fn test_describe_proposal_successful() {
);
}


#[test]
fn test_propose_and_describe_successful() {
let (staker, token, governor, config) = setup();
token.approve(staker.contract_address, config.proposal_creation_threshold.into());
staker.stake(proposer());

advance_time(config.voting_weight_smoothing_duration);

let address_before = get_contract_address();
set_contract_address(proposer());
let id = governor
.propose_and_describe(
array![].span(),
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
);
set_contract_address(address_before);

pop_log::<Governor::Proposed>(governor.contract_address).unwrap();
assert_eq!(
pop_log::<Governor::Described>(governor.contract_address).unwrap(),
Governor::Described {
id,
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
}
);
}

#[test]
#[should_panic(expected: ('NOT_PROPOSER', 'ENTRYPOINT_FAILED'))]
fn test_describe_proposal_fails_for_unknown_proposal() {
Expand Down

0 comments on commit 80d6465

Please sign in to comment.