Skip to content

Commit

Permalink
add vote tests (#515)
Browse files Browse the repository at this point in the history
Co-authored-by: Orland0x <[email protected]>
  • Loading branch information
pscott and Orland0x authored Aug 25, 2023
1 parent af2c1e6 commit 36dda67
Show file tree
Hide file tree
Showing 6 changed files with 456 additions and 21 deletions.
46 changes: 26 additions & 20 deletions starknet/src/space/space.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait ISpace<TContractState> {
fn next_voting_strategy_index(self: @TContractState) -> u8;
fn proposal_validation_strategy(self: @TContractState) -> Strategy;
// #[view]
// fn vote_power(proposal_id: u256, choice: u8) -> u256;
fn vote_power(self: @TContractState, proposal_id: u256, choice: Choice) -> u256;
// #[view]
// fn vote_registry(proposal_id: u256, voter: ContractAddress) -> bool;
fn proposals(self: @TContractState, proposal_id: u256) -> Proposal;
Expand Down Expand Up @@ -75,32 +75,33 @@ trait ISpace<TContractState> {
#[starknet::contract]
mod Space {
use super::ISpace;
use starknet::storage_access::{StorePacking, StoreUsingPacking};
use starknet::{ClassHash, ContractAddress, info, Store, syscalls};
use starknet::{
storage_access::{StorePacking, StoreUsingPacking}, ClassHash, ContractAddress, info, Store,
syscalls
};
use zeroable::Zeroable;
use array::{ArrayTrait, SpanTrait};
use clone::Clone;
use option::OptionTrait;
use hash::LegacyHash;
use traits::{Into, TryInto};
use serde::Serde;

use sx::interfaces::{
IProposalValidationStrategyDispatcher, IProposalValidationStrategyDispatcherTrait,
IVotingStrategyDispatcher, IVotingStrategyDispatcherTrait, IExecutionStrategyDispatcher,
IExecutionStrategyDispatcherTrait
};
use sx::types::{
UserAddress, Choice, FinalizationStatus, Strategy, IndexedStrategy, Proposal,
PackedProposal, IndexedStrategyTrait, IndexedStrategyImpl, UpdateSettingsCalldata,
NoUpdateU32, NoUpdateStrategy, NoUpdateArray
use sx::{
interfaces::{
IProposalValidationStrategyDispatcher, IProposalValidationStrategyDispatcherTrait,
IVotingStrategyDispatcher, IVotingStrategyDispatcherTrait, IExecutionStrategyDispatcher,
IExecutionStrategyDispatcherTrait
},
types::{
UserAddress, Choice, FinalizationStatus, Strategy, IndexedStrategy, Proposal,
PackedProposal, IndexedStrategyTrait, IndexedStrategyImpl, UpdateSettingsCalldata,
NoUpdateU32, NoUpdateStrategy, NoUpdateArray
},
utils::{
reinitializable::{Reinitializable}, ReinitializableImpl, bits::BitSetter,
legacy_hash::LegacyHashChoice, constants::INITIALIZE_SELECTOR
},
external::ownable::Ownable
};
use sx::utils::reinitializable::Reinitializable;
use sx::utils::ReinitializableImpl;
use sx::utils::bits::BitSetter;
use sx::utils::legacy_hash::LegacyHashChoice;
use sx::external::ownable::Ownable;
use sx::utils::constants::INITIALIZE_SELECTOR;

#[storage]
struct Storage {
Expand Down Expand Up @@ -304,6 +305,7 @@ mod Space {
_add_authenticators(ref self, authenticators);
self._next_proposal_id.write(1_u256);
}

fn propose(
ref self: ContractState,
author: UserAddress,
Expand Down Expand Up @@ -705,6 +707,10 @@ mod Space {
}
}

fn vote_power(self: @ContractState, proposal_id: u256, choice: Choice) -> u256 {
self._vote_power.read((proposal_id, choice))
}

fn transfer_ownership(ref self: ContractState, new_owner: ContractAddress) {
//TODO: temporary component syntax
let mut state = Ownable::unsafe_new_contract_state();
Expand Down
2 changes: 2 additions & 0 deletions starknet/src/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ mod mocks;
mod setup;

mod utils;

mod vote;
1 change: 1 addition & 0 deletions starknet/src/tests/mocks.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod erc20_votes_preset;
mod executor;
mod no_voting_power;
mod proposal_validation_always_fail;
mod space_v2;
28 changes: 27 additions & 1 deletion starknet/src/tests/mocks/executor.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
mod ExecutorExecutionStrategy {
use sx::interfaces::IExecutionStrategy;
use sx::types::{Proposal, ProposalStatus};
use sx::execution_strategies::simple_quorum::SimpleQuorumExecutionStrategy;
use starknet::ContractAddress;
use core::serde::Serde;
use core::array::ArrayTrait;
Expand Down Expand Up @@ -39,3 +38,30 @@ mod ExecutorExecutionStrategy {
#[constructor]
fn constructor(ref self: ContractState) {}
}


#[starknet::contract]
mod ExecutorWithoutTxExecutionStrategy {
use sx::interfaces::IExecutionStrategy;
use sx::types::{Proposal, ProposalStatus};
use core::array::ArrayTrait;

#[storage]
struct Storage {}

#[external(v0)]
impl ExecutorWithoutTxExecutionStrategy of IExecutionStrategy<ContractState> {
// Dummy function that will do nothing
fn execute(
ref self: ContractState,
proposal: Proposal,
votes_for: u256,
votes_against: u256,
votes_abstain: u256,
payload: Array<felt252>
) {}
}

#[constructor]
fn constructor(ref self: ContractState) {}
}
22 changes: 22 additions & 0 deletions starknet/src/tests/mocks/no_voting_power.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[starknet::contract]
mod NoVotingPowerVotingStrategy {
use sx::interfaces::IVotingStrategy;
use sx::types::UserAddress;
use starknet::ContractAddress;

#[storage]
struct Storage {}

#[external(v0)]
impl NoVotingPowerVotingStrategy of IVotingStrategy<ContractState> {
fn get_voting_power(
self: @ContractState,
timestamp: u32,
voter: UserAddress,
params: Array<felt252>,
user_params: Array<felt252>,
) -> u256 {
0
}
}
}
Loading

0 comments on commit 36dda67

Please sign in to comment.