Skip to content

Commit

Permalink
chore: add factory event tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pscott committed Aug 29, 2023
1 parent d75fe00 commit dcc25ce
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 78 deletions.
13 changes: 9 additions & 4 deletions starknet/src/factory/factory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ mod Factory {
#[event]
#[derive(Drop, starknet::Event)]
enum Event {
SpaceDeployed: SpaceDeployed
NewContractDeployed: NewContractDeployed
}

#[derive(Drop, starknet::Event)]
struct SpaceDeployed {
struct NewContractDeployed {
class_hash: ClassHash,
space_address: ContractAddress
contract_address: ContractAddress
}

#[storage]
Expand All @@ -53,7 +53,12 @@ mod Factory {
// Call initializer.
call_contract_syscall(space_address, INITIALIZE_SELECTOR, initialize_calldata).unwrap();

self.emit(Event::SpaceDeployed(SpaceDeployed { class_hash, space_address }));
self
.emit(
Event::NewContractDeployed(
NewContractDeployed { class_hash, contract_address: space_address }
)
);

space_address
}
Expand Down
77 changes: 34 additions & 43 deletions starknet/src/tests/setup/setup.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,31 @@ mod setup {
use starknet::ClassHash;
use sx::space::space::{Space, ISpaceDispatcher, ISpaceDispatcherTrait};

#[derive(Drop)]
#[derive(Drop, Serde)]
struct Config {
owner: ContractAddress,
min_voting_duration: u64,
max_voting_duration: u64,
voting_delay: u64,
min_voting_duration: u32,
max_voting_duration: u32,
voting_delay: u32,
proposal_validation_strategy: Strategy,
proposal_validation_strategy_metadata_uri: Array<felt252>,
voting_strategies: Array<Strategy>,
voting_strategies_metadata_uris: Array<Array<felt252>>,
authenticators: Array<ContractAddress>,
metadata_uri: Array<felt252>,
dao_uri: Array<felt252>,
}

trait ConfigTrait {
fn get_initialize_calldata(self: @Config) -> Array<felt252>;
}

impl ConfigImpl of ConfigTrait {
fn get_initialize_calldata(self: @Config) -> Array<felt252> {
let mut calldata = array![];
self.serialize(ref calldata);
calldata
}
}

fn setup() -> Config {
Expand All @@ -37,9 +53,9 @@ mod setup {

// Space Settings
let owner = contract_address_const::<0x123456789>();
let max_voting_duration = 2_u64;
let min_voting_duration = 1_u64;
let voting_delay = 1_u64;
let max_voting_duration = 2;
let min_voting_duration = 1;
let voting_delay = 1;
let quorum = u256_from_felt252(1);

// Deploy Vanilla Authenticator
Expand Down Expand Up @@ -85,43 +101,26 @@ mod setup {
vanilla_execution_strategy_address
);

let proposal_validation_strategy_metadata_uri = array!['https:://rick.roll'];
let voting_strategies_metadata_uris = array![];
let dao_uri = array!['https://dao.uri'];
let metadata_uri = array!['https://metadata.uri'];

Config {
owner,
min_voting_duration,
max_voting_duration,
voting_delay,
proposal_validation_strategy,
proposal_validation_strategy_metadata_uri,
voting_strategies,
authenticators
voting_strategies_metadata_uris,
authenticators,
metadata_uri,
dao_uri,
}
}

fn get_initialize_calldata(
owner: @ContractAddress,
min_voting_duration: @u64,
max_voting_duration: @u64,
voting_delay: @u64,
proposal_validation_strategy: @Strategy,
voting_strategies: @Array<Strategy>,
authenticators: @Array<ContractAddress>
) -> Array<felt252> {
// Using empty arrays for all the metadata fields
let mut initializer_calldata = array![];
owner.serialize(ref initializer_calldata);
min_voting_duration.serialize(ref initializer_calldata);
max_voting_duration.serialize(ref initializer_calldata);
voting_delay.serialize(ref initializer_calldata);
proposal_validation_strategy.serialize(ref initializer_calldata);
ArrayTrait::<felt252>::new().serialize(ref initializer_calldata);
voting_strategies.serialize(ref initializer_calldata);
ArrayTrait::<felt252>::new().serialize(ref initializer_calldata);
authenticators.serialize(ref initializer_calldata);
ArrayTrait::<felt252>::new().serialize(ref initializer_calldata);
ArrayTrait::<felt252>::new().serialize(ref initializer_calldata);

initializer_calldata
}

fn deploy(config: @Config) -> (IFactoryDispatcher, ISpaceDispatcher) {
let space_class_hash: ClassHash = Space::TEST_CLASS_HASH.try_into().unwrap();
let contract_address_salt = 0;
Expand All @@ -133,15 +132,7 @@ mod setup {

let factory = IFactoryDispatcher { contract_address: factory_address };

let initializer_calldata = get_initialize_calldata(
config.owner,
config.min_voting_duration,
config.max_voting_duration,
config.voting_delay,
config.proposal_validation_strategy,
config.voting_strategies,
config.authenticators
);
let mut initializer_calldata = config.get_initialize_calldata();
let space_address = factory
.deploy(space_class_hash, contract_address_salt, initializer_calldata.span());

Expand Down
69 changes: 55 additions & 14 deletions starknet/src/tests/test_factory.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
mod tests {
use array::ArrayTrait;
use starknet::{syscalls::deploy_syscall, testing, contract_address_const,};
use array::{ArrayTrait, SpanTrait};
use starknet::{syscalls::deploy_syscall, testing, contract_address_const, };
use traits::TryInto;
use sx::factory::factory::{Factory, IFactoryDispatcher, IFactoryDispatcherTrait};
use option::OptionTrait;
Expand All @@ -10,16 +10,65 @@ mod tests {
use sx::types::Strategy;
use starknet::ClassHash;

use sx::tests::setup::setup::setup::{setup, get_initialize_calldata, deploy};
use sx::tests::setup::setup::setup::{setup, ConfigTrait, deploy};
use openzeppelin::tests::utils;
use openzeppelin::tests::utils::constants::ZERO;
use sx::space::space::Space::SpaceCreated;
use sx::factory::factory::Factory::NewContractDeployed;

use traits::{PartialEq};
use clone::Clone;

#[test]
#[available_gas(10000000000)]
fn test_deploy() {
// Deploy Space
let config = setup();

// TODO: check event gets emitted
deploy(@config);
let (factory, space) = deploy(@config);

// Ensure the space emitted the proper event
let space_event = utils::pop_log::<SpaceCreated>(space.contract_address).unwrap();
assert(space_event.space == space.contract_address, 'space');
assert(space_event.owner == config.owner, 'owner');
assert(
space_event.min_voting_duration == config.min_voting_duration, 'min_voting_duration'
);
assert(
space_event.max_voting_duration == config.max_voting_duration, 'max_voting_duration'
);
assert(space_event.voting_delay == config.voting_delay, 'voting_delay');
assert(
space_event.proposal_validation_strategy == config.proposal_validation_strategy,
'proposal_validation_strategy'
);
assert(
space_event
.proposal_validation_strategy_metadata_URI == config
.proposal_validation_strategy_metadata_uri
.span(),
'prop_val_strat_metadata'
);
assert(
space_event.voting_strategies == config.voting_strategies.span(), 'voting_strategies'
);
assert(
space_event
.voting_strategy_metadata_URIs == config
.voting_strategies_metadata_uris
.span(),
'voting_strat_metadata'
);
assert(space_event.authenticators == config.authenticators.span(), 'authenticators');
assert(space_event.metadata_URI == config.metadata_uri.span(), 'metadata_URI');
assert(space_event.dao_URI == config.dao_uri.span(), 'dao_URI');

let factory_event = utils::pop_log::<NewContractDeployed>(factory.contract_address)
.unwrap();
assert(factory_event.contract_address == space.contract_address, 'space_contract_address');
assert(
factory_event.class_hash == Space::TEST_CLASS_HASH.try_into().unwrap(), 'class_hash'
);
}


Expand All @@ -39,15 +88,7 @@ mod tests {
let contract_address_salt = 0;

let config = setup();
let constructor_calldata = get_initialize_calldata(
@config.owner,
@config.min_voting_duration,
@config.max_voting_duration,
@config.voting_delay,
@config.proposal_validation_strategy,
@config.voting_strategies,
@config.authenticators
);
let constructor_calldata = config.get_initialize_calldata();

let space_address = factory
.deploy(space_class_hash, contract_address_salt, constructor_calldata.span());
Expand Down
10 changes: 5 additions & 5 deletions starknet/src/tests/test_space.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ mod tests {
authenticator
.authenticate(space.contract_address, UPDATE_PROPOSAL_SELECTOR, update_calldata);

testing::set_block_timestamp(config.voting_delay);
testing::set_block_timestamp(config.voting_delay.into());

let mut vote_calldata = array![];
let voter = UserAddress::Starknet(contract_address_const::<0x8765>());
Expand All @@ -280,7 +280,7 @@ mod tests {
// Vote on Proposal
authenticator.authenticate(space.contract_address, VOTE_SELECTOR, vote_calldata);

testing::set_block_timestamp(config.voting_delay + config.max_voting_duration);
testing::set_block_timestamp(config.voting_delay.into() + config.max_voting_duration.into());

// Execute Proposal
space.execute(u256_from_felt252(1), new_payload);
Expand Down Expand Up @@ -377,7 +377,7 @@ mod tests {

assert(space.next_proposal_id() == 2_u256, 'next_proposal_id should be 2');

testing::set_block_timestamp(config.voting_delay);
testing::set_block_timestamp(config.voting_delay.into());

let mut vote_calldata = array![];
let voter = UserAddress::Starknet(contract_address_const::<0x8765>());
Expand All @@ -393,7 +393,7 @@ mod tests {
// Vote on Proposal
authenticator.authenticate(space.contract_address, VOTE_SELECTOR, vote_calldata);

testing::set_block_timestamp(config.voting_delay + config.max_voting_duration);
testing::set_block_timestamp(config.voting_delay.into() + config.max_voting_duration.into());

// Execute Proposal
space.execute(u256_from_felt252(1), vanilla_execution_strategy.params.clone());
Expand Down Expand Up @@ -439,7 +439,7 @@ mod tests {
authenticator.authenticate(space.contract_address, PROPOSE_SELECTOR, propose_calldata);
let proposal_id = u256_from_felt252(1);

testing::set_block_timestamp(config.voting_delay);
testing::set_block_timestamp(config.voting_delay.into());
let proposal = space.proposals(proposal_id);
assert(proposal.finalization_status == FinalizationStatus::Pending(()), 'pending');

Expand Down
16 changes: 8 additions & 8 deletions starknet/src/tests/vote.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ mod tests {
create_proposal(authenticator, space, execution_strategy);

// Increasing block timestamp pass voting delay
testing::set_block_timestamp(config.voting_delay);
testing::set_block_timestamp(config.voting_delay.into());

let mut vote_calldata = array![];
let voter = UserAddress::Starknet(contract_address_const::<0x8765>());
Expand Down Expand Up @@ -109,7 +109,7 @@ mod tests {
create_proposal(authenticator, space, execution_strategy);

// Increasing block timestamp pass voting delay
testing::set_block_timestamp(config.voting_delay);
testing::set_block_timestamp(config.voting_delay.into());

let mut vote_calldata = array![];
let voter = UserAddress::Starknet(contract_address_const::<0x8765>());
Expand Down Expand Up @@ -143,7 +143,7 @@ mod tests {
create_proposal(authenticator, space, execution_strategy);

// Increasing block timestamp by voting delay
testing::set_block_timestamp(config.voting_delay);
testing::set_block_timestamp(config.voting_delay.into());

let mut vote_calldata = array![];
let voter = UserAddress::Starknet(contract_address_const::<0x8765>());
Expand Down Expand Up @@ -209,7 +209,7 @@ mod tests {
create_proposal(authenticator, space, execution_strategy);

// Fast forward to end of voting period
testing::set_block_timestamp(config.voting_delay + config.max_voting_duration);
testing::set_block_timestamp(config.voting_delay.into() + config.max_voting_duration.into());

let mut vote_calldata = array![];
let voter = UserAddress::Starknet(contract_address_const::<0x8765>());
Expand Down Expand Up @@ -240,7 +240,7 @@ mod tests {

create_proposal(authenticator, space, execution_strategy);

testing::set_block_timestamp(config.voting_delay);
testing::set_block_timestamp(config.voting_delay.into());

space
.execute(
Expand Down Expand Up @@ -276,7 +276,7 @@ mod tests {
create_proposal(authenticator, space, execution_strategy);

// Fast forward to end of voting period
testing::set_block_timestamp(config.voting_delay + config.max_voting_duration);
testing::set_block_timestamp(config.voting_delay.into() + config.max_voting_duration.into());

let voter = UserAddress::Starknet(contract_address_const::<0x8765>());
let proposal_id = 1_u256;
Expand Down Expand Up @@ -304,7 +304,7 @@ mod tests {
create_proposal(authenticator, space, execution_strategy);

// Increasing block timestamp pass voting delay
testing::set_block_timestamp(config.voting_delay);
testing::set_block_timestamp(config.voting_delay.into());

let mut vote_calldata = array![];
let voter = UserAddress::Starknet(contract_address_const::<0x8765>());
Expand Down Expand Up @@ -352,7 +352,7 @@ mod tests {
create_proposal(authenticator, space, execution_strategy);

// Increasing block timestamp pass voting delay
testing::set_block_timestamp(config.voting_delay);
testing::set_block_timestamp(config.voting_delay.into());

let mut vote_calldata = array![];
let voter = UserAddress::Starknet(contract_address_const::<0x8765>());
Expand Down
8 changes: 4 additions & 4 deletions starknet/src/tests/voting_strategies/erc20_votes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ mod tests {

// Advance to vote start + 1
let current = info::get_block_timestamp();
testing::set_block_timestamp(current + config.voting_delay + 1);
testing::set_block_timestamp(current + config.voting_delay.into() + 1);

let mut vote_calldata = array::ArrayTrait::<felt252>::new();
let voter = *accounts.at(0);
Expand All @@ -196,7 +196,7 @@ mod tests {
// Vote on proposal
authenticator.authenticate(space.contract_address, VOTE_SELECTOR, vote_calldata);

testing::set_block_timestamp(current + config.max_voting_duration);
testing::set_block_timestamp(current + config.max_voting_duration.into());

// Execute proposal
space.execute(1_u256, vanilla_execution_strategy.params);
Expand Down Expand Up @@ -226,7 +226,7 @@ mod tests {

// Move to the exact voting period start so the strategy will revert.
let current = info::get_block_timestamp();
testing::set_block_timestamp(current + config.voting_delay);
testing::set_block_timestamp(current + config.voting_delay.into());

let mut vote_calldata = array::ArrayTrait::<felt252>::new();
let voter = *accounts.at(0);
Expand Down Expand Up @@ -276,7 +276,7 @@ mod tests {

// Move to the exact voting period start + 1
let current = info::get_block_timestamp();
testing::set_block_timestamp(current + config.voting_delay + 1);
testing::set_block_timestamp(current + config.voting_delay.into() + 1);

let mut vote_calldata = array::ArrayTrait::<felt252>::new();
let voter = *accounts.at(0);
Expand Down

0 comments on commit dcc25ce

Please sign in to comment.