Skip to content

Commit

Permalink
Add custom proposal implementation (#64)
Browse files Browse the repository at this point in the history
* Add custom proposal implementation

Lacks setting of custom proposal configuration during deployment

* Add arbitrary proposal

* Polish apply_passed_proposal

* Polish scarb fmt

* Cherry pick setup.cairo from 1fab54f

* Move test setup to src/

* Remove unrelated functions from setup.cairo

* Fix setup

* Polish tests

* Fix import naming

* Add addition of custom proposal
  • Loading branch information
tensojka committed May 23, 2024
1 parent 7b4cb3c commit 7cf23cd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
30 changes: 29 additions & 1 deletion src/proposals.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Proposals component. Does not depend on anything. Holds governance token address.

use konoha::types::{ContractType, PropDetails, VoteStatus};
use konoha::types::{ContractType, PropDetails, VoteStatus, CustomProposalConfig};
use starknet::ContractAddress;

#[starknet::interface]
Expand All @@ -19,6 +19,7 @@ trait IProposals<TContractState> {
fn submit_custom_proposal(
ref self: TContractState, custom_proposal_type: u32, calldata: Span<felt252>
) -> u32;
fn get_custom_proposal_type(self: @TContractState, i: u32) -> CustomProposalConfig;
}

#[starknet::component]
Expand Down Expand Up @@ -56,6 +57,7 @@ mod proposals {
use konoha::types::ContractType;
use konoha::types::PropDetails;
use konoha::types::VoteStatus;
use konoha::types::CustomProposalConfig;
use konoha::traits::IERC20Dispatcher;
use konoha::traits::IERC20DispatcherTrait;
use konoha::traits::get_governance_token_address_self;
Expand Down Expand Up @@ -241,6 +243,26 @@ mod proposals {
let res: u256 = (caller_balance * constants::NEW_PROPOSAL_QUORUM).into();
assert(total_supply < res, 'not enough tokens to submit');
}

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()) {
i += 1;
res = self.custom_proposal_type.read(i);
};
i
}

fn add_custom_proposal_config(
ref self: ComponentState<TContractState>, config: CustomProposalConfig
) -> u32 {
let idx = self._find_free_custom_proposal_type();
assert(config.target.is_non_zero(), 'target must be nonzero');
assert(config.selector.is_non_zero(), 'selector must be nonzero');
self.custom_proposal_type.write(idx, config);
idx
}
}

#[embeddable_as(ProposalsImpl)]
Expand Down Expand Up @@ -493,5 +515,11 @@ mod proposals {
return constants::MINUS_ONE; // yay_tally < nay_tally
}
}

fn get_custom_proposal_type(
self: @ComponentState<TContractState>, i: u32
) -> CustomProposalConfig {
self.custom_proposal_type.read(i)
}
}
}
14 changes: 7 additions & 7 deletions src/testing/setup.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use snforge_std::{
};


use governance::contract::IGovernanceDispatcher;
use governance::contract::IGovernanceDispatcherTrait;
use governance::proposals::IProposalsDispatcher;
use governance::proposals::IProposalsDispatcherTrait;
use governance::upgrades::IUpgradesDispatcher;
use governance::upgrades::IUpgradesDispatcherTrait;
use governance::constants;
use konoha::contract::IGovernanceDispatcher;
use konoha::contract::IGovernanceDispatcherTrait;
use konoha::proposals::IProposalsDispatcher;
use konoha::proposals::IProposalsDispatcherTrait;
use konoha::upgrades::IUpgradesDispatcher;
use konoha::upgrades::IUpgradesDispatcherTrait;
use konoha::constants;
use openzeppelin::token::erc20::interface::IERC20;
use starknet::get_block_timestamp;

Expand Down
2 changes: 1 addition & 1 deletion src/upgrades.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod upgrades {
use starknet::ContractAddress;
use starknet::class_hash;

use konoha::types::PropDetails;
use konoha::types::{CustomProposalConfig, PropDetails};
use konoha::contract::Governance;
use konoha::contract::Governance::ContractState;

Expand Down

0 comments on commit 7cf23cd

Please sign in to comment.