diff --git a/tests/setup.cairo b/tests/setup.cairo index af276c86..f4a109a9 100644 --- a/tests/setup.cairo +++ b/tests/setup.cairo @@ -1,7 +1,46 @@ +#[starknet::contract] +mod MyToken { + use openzeppelin::token::erc20::ERC20Component; + use starknet::ContractAddress; + + component!(path: ERC20Component, storage: erc20, event: ERC20Event); + + #[abi(embed_v0)] + impl ERC20Impl = ERC20Component::ERC20Impl; + #[abi(embed_v0)] + impl ERC20MetadataImpl = ERC20Component::ERC20MetadataImpl; + #[abi(embed_v0)] + impl ERC20CamelOnlyImpl = ERC20Component::ERC20CamelOnlyImpl; + impl InternalImpl = ERC20Component::InternalImpl; + + #[storage] + struct Storage { + #[substorage(v0)] + erc20: ERC20Component::Storage + } + + #[event] + #[derive(Drop, starknet::Event)] + enum Event { + #[flat] + ERC20Event: ERC20Component::Event + } + + #[constructor] + fn constructor(ref self: ContractState, fixed_supply: u256, recipient: ContractAddress) { + let name = 'MyToken'; + let symbol = 'MTK'; + + self.erc20.initializer(name, symbol); + self.erc20._mint(recipient, fixed_supply); + } +} + use array::ArrayTrait; use core::traits::TryInto; use debug::PrintTrait; use starknet::ContractAddress; +use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait}; use snforge_std::{BlockId, declare, ContractClassTrait, ContractClass, start_prank, CheatTarget}; use governance::contract::IGovernanceDispatcher; @@ -11,76 +50,56 @@ use governance::proposals::IProposalsDispatcherTrait; use governance::upgrades::IUpgradesDispatcher; use governance::upgrades::IUpgradesDispatcherTrait; -fn test_upgrade_root(new_merkle_root: felt) { - - let gov_contract_addr: ContractAddress = - 0x001405ab78ab6ec90fba09e6116f373cda53b0ba557789a4578d8c1ec374ba0f - .try_into() - .unwrap(); - - //let airdrop_contract_addr: ContractAddress = deploy_contract('Airdrop') - - let proposals_dispatcher = IProposalsDispatcher { contract_address: gov_contract_addr }; - - let mut top_carm_holders = ArrayTrait::new(); - let marek_address: ContractAddress = - 0x0011d341c6e841426448ff39aa443a6dbb428914e05ba2259463c18308b86233 - .try_into() - .unwrap(); - top_carm_holders.append(marek_address); - let scaling_address: ContractAddress = - 0x052df7acdfd3174241fa6bd5e1b7192cd133f8fc30a2a6ed99b0ddbfb5b22dcd - .try_into() - .unwrap(); - top_carm_holders.append(scaling_address); - let ondrej_address: ContractAddress = - 0x0583a9d956d65628f806386ab5b12dccd74236a3c6b930ded9cf3c54efc722a1 - .try_into() - .unwrap(); - top_carm_holders.append(ondrej_address); - let carlote_address: ContractAddress = - 0x021b2b25dd73bc60b0549683653081f8963562cbe5cba2d123ec0cbcbf0913e4 - .try_into() - .unwrap(); - top_carm_holders.append(carlote_address); - let fifth_address: ContractAddress = - 0x02af7135154dc27d9311b79c57ccc7b3a6ed74efd0c2b81116e8eb49dbf6aaf8 - .try_into() - .unwrap(); - top_carm_holders.append(fifth_address); - let sixth_address: ContractAddress = - 0x07824efd915baa421d93909bd7f24e36c022b5cfbc5af6687328848a6490ada7 - .try_into() - .unwrap(); - top_carm_holders.append(sixth_address); - let madman_address: ContractAddress = - 0x06717eaf502baac2b6b2c6ee3ac39b34a52e726a73905ed586e757158270a0af - .try_into() - .unwrap(); - top_carm_holders.append(madman_address); - let eighth_address: ContractAddress = - 0x03d1525605db970fa1724693404f5f64cba8af82ec4aab514e6ebd3dec4838ad - .try_into() - .unwrap(); - top_carm_holders.append(eighth_address); - - start_prank(CheatTarget::One(gov_contract_addr), scaling_address); - let new_prop_id = dispatcher.submit_proposal(new_merkle_root, 3); - - loop { - match top_carm_holders.pop_front() { - Option::Some(holder) => { - start_prank(CheatTarget::One(gov_contract_addr), holder); - dispatcher.vote(new_prop_id, 1); - }, - Option::None(()) => { break (); }, - } - }; - assert(dispatcher.get_proposal_status(new_prop_id) == 1, 'proposal not passed!'); +const GOV_TOKEN_INITIAL_SUPPLY: felt252 = 1000000000000000000; + +const first_address: ContractAddress = 0x1.try_into().unwrap(); +const second_address: ContractAddress = 0x2.try_into().unwrap(); +const admin_addr: ContractAddress = 0x3.try_into().unwrap(); + +fn deploy_governance() -> IGovernanceDispatcher { + let gov_contract = declare('Governance'); + let address = gov_contract.deploy().expect('unable to deploy governance'); + IGovernanceDispatcher { contract_address: address }; +} + + +fn deploy_and_distribute_gov_tokens(recipient: ContractAddress) { + let mut calldata = ArrayTrait::new(); + calldata.append(GOV_TOKEN_INITIAL_SUPPLY); + calldata.append(recipient); + + let gov_token_contract = declare('MyToken'); + let token_addr = gov_token_contract.deploy_at(@calldata).expect('unable to deploy MyToken'); + let token: IERC20Dispatcher = IERC20Dispatcher { contract_address: token_addr }; + + start_prank(CheatTarget::One(token_addr), admin_addr); + + token.transfer(first_address, 100000); + token.transfer(second_address, 100000); +} + + +fn test_vote_upgrade_root(new_merkle_root: felt252) { + let gov_contract = deploy_governance(); + let gov_contract_addr = gov_contract.contract_address; + let token_contract = deploy_and_distribute_gov_tokens(admin_addr); + + let dispatcher = IProposalsDispatcher { contract_address: gov_contract_addr }; + + start_prank(CheatTarget::One(gov_contract_addr), admin_addr); + let prop_id = dispatcher.submit_proposal(new_merkle_root, 3); + + start_prank(CheatTarget::One(gov_contract_addr), first_address); + dispatcher.vote(prop_id, 1); + start_prank(CheatTarget::One(gov_contract_addr), second_address); + dispatcher.vote(prop_id, 1); + start_prank(CheatTarget::One(gov_contract_addr), admin_addr); + dispatcher.vote(prop_id, 1); + + assert(dispatcher.get_proposal_status(prop_id) == 1, 'proposal not passed!'); let upgrade_dispatcher = IUpgradesDispatcher { contract_address: gov_contract_addr }; - upgrade_dispatcher.apply_passed_proposal(new_prop_id); + upgrade_dispatcher.apply_passed_proposal(prop_id); assert(check_if_healthy(gov_contract_addr), 'new gov not healthy'); - }