From 50148df5313d0be1960200a4443fd0701996039a Mon Sep 17 00:00:00 2001 From: manlikeHB Date: Fri, 25 Oct 2024 17:43:45 +0100 Subject: [PATCH 1/3] add test --- contracts/.snfoundry_cache/.prev_tests_failed | 1 - contracts/tests/test_fund.cairo | 52 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/contracts/.snfoundry_cache/.prev_tests_failed b/contracts/.snfoundry_cache/.prev_tests_failed index 5d28940..e69de29 100644 --- a/contracts/.snfoundry_cache/.prev_tests_failed +++ b/contracts/.snfoundry_cache/.prev_tests_failed @@ -1 +0,0 @@ -tests::test_fund::test_receive_vote_unsuccessful_wrong_state diff --git a/contracts/tests/test_fund.cairo b/contracts/tests/test_fund.cairo index e8ff290..2895728 100644 --- a/contracts/tests/test_fund.cairo +++ b/contracts/tests/test_fund.cairo @@ -9,6 +9,8 @@ use openzeppelin::utils::serde::SerializedAppend; use gostarkme::fund::IFundDispatcher; use gostarkme::fund::IFundDispatcherTrait; +use gostarkme::constants::{funds::{state_constants::FundStates},}; + fn ID() -> u128 { 1 @@ -155,3 +157,53 @@ fn test_receive_donation_unsuccessful_wrong_state() { // Donate dispatcher.receiveDonation(5); } + +#[test] +#[should_panic(expected: ("You are not the owner",))] +fn test_withdraw_with_wrong_owner() { + let contract_address = _setup_(); + + // call withdraw fn with wrong owner + start_cheat_caller_address_global(OTHER_USER()); + IFundDispatcher { contract_address }.withdraw(); +} + +#[test] +#[should_panic(expected: ('Fund not close goal yet.',))] +fn test_withdraw_with_non_closed_state() { + let contract_address = _setup_(); + let fund_dispatcher = IFundDispatcher { contract_address }; + + start_cheat_caller_address_global(OWNER()); + // set goal + fund_dispatcher.setGoal(500_u256); + // withdraw funds + fund_dispatcher.withdraw(); +} + +#[test] +#[should_panic(expected: ('Fund hasnt reached its goal yet',))] +fn test_withdraw_when_fund_has_not_reach_goal() { + let contract_address = _setup_(); + let dispatcher = IFundDispatcher { contract_address }; + start_cheat_caller_address_global(OWNER()); + dispatcher.setGoal(10); + dispatcher.setState(3); + + dispatcher.withdraw(); +} + +// #[test] +// // #[should_panic(expected: ('Fund hasnt reached its goal yet',))] +// fn test_withdraw() { +// let contract_address = _setup_(); +// let fund_dispatcher = IFundDispatcher { contract_address }; + +// start_cheat_caller_address_global(OWNER()); +// // set goal +// fund_dispatcher.setGoal(500_u256); +// // donate +// fund_dispatcher.receiveDonation(500_u256); +// // withdraw funds +// fund_dispatcher.withdraw(); +// } From b25215bff617dd3089a0c44862c4b4f57502e8cb Mon Sep 17 00:00:00 2001 From: manlikeHB Date: Mon, 28 Oct 2024 12:08:58 +0100 Subject: [PATCH 2/3] add test_withdraw --- .gitignore | 4 ++- contracts/Scarb.toml | 5 +++ contracts/tests/test_fund.cairo | 43 ++++++++++++++++--------- contracts/tests/test_fund_manager.cairo | 2 +- 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 6ec4261..7fa5688 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ target -.DS_Store \ No newline at end of file +.DS_Store + +contracts/.snfoundry_cache/https___starknet_mainnet_public_blastapi_io_rpc_v0_7_519354_v3.json \ No newline at end of file diff --git a/contracts/Scarb.toml b/contracts/Scarb.toml index 539d2d6..c045445 100644 --- a/contracts/Scarb.toml +++ b/contracts/Scarb.toml @@ -17,3 +17,8 @@ test = "snforge test" [[target.starknet-contract]] casm = true + +[[tool.snforge.fork]] +name = "Mainnet" +url = "https://starknet-mainnet.public.blastapi.io/rpc/v0_7" +block_id.number = "519354" diff --git a/contracts/tests/test_fund.cairo b/contracts/tests/test_fund.cairo index 2895728..76e803f 100644 --- a/contracts/tests/test_fund.cairo +++ b/contracts/tests/test_fund.cairo @@ -6,10 +6,12 @@ use starknet::{ContractAddress, contract_address_const}; use snforge_std::{declare, ContractClassTrait, start_cheat_caller_address_global}; use openzeppelin::utils::serde::SerializedAppend; +use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait}; use gostarkme::fund::IFundDispatcher; use gostarkme::fund::IFundDispatcherTrait; use gostarkme::constants::{funds::{state_constants::FundStates},}; +use gostarkme::constants::{funds::{starknet_constants::StarknetConstants},}; fn ID() -> u128 { @@ -174,7 +176,7 @@ fn test_withdraw_with_non_closed_state() { let contract_address = _setup_(); let fund_dispatcher = IFundDispatcher { contract_address }; - start_cheat_caller_address_global(OWNER()); + start_cheat_caller_address_global(OWNER()); // set goal fund_dispatcher.setGoal(500_u256); // withdraw funds @@ -193,17 +195,28 @@ fn test_withdraw_when_fund_has_not_reach_goal() { dispatcher.withdraw(); } -// #[test] -// // #[should_panic(expected: ('Fund hasnt reached its goal yet',))] -// fn test_withdraw() { -// let contract_address = _setup_(); -// let fund_dispatcher = IFundDispatcher { contract_address }; - -// start_cheat_caller_address_global(OWNER()); -// // set goal -// fund_dispatcher.setGoal(500_u256); -// // donate -// fund_dispatcher.receiveDonation(500_u256); -// // withdraw funds -// fund_dispatcher.withdraw(); -// } +#[test] +#[fork("Mainnet")] +fn test_withdraw() { + let contract_address = _setup_(); + let fund_dispatcher = IFundDispatcher { contract_address }; + let starknet_dispatcher = IERC20Dispatcher { + contract_address: StarknetConstants::STRK_TOKEN_ADDRESS.try_into().unwrap() + }; + + start_cheat_caller_address_global(OWNER()); + // set goal + fund_dispatcher.setGoal(500_u256); + fund_dispatcher.setState(2); + + // donate + fund_dispatcher.receiveDonation(500_u256); + + // withdraw funds + fund_dispatcher.withdraw(); + + let balance = starknet_dispatcher.balance_of(OWNER()); + + //TODO assert correct balance when logic for depositing strks is complete + assert(balance == 0, 'wrong balance'); +} diff --git a/contracts/tests/test_fund_manager.cairo b/contracts/tests/test_fund_manager.cairo index a9a862b..91c647b 100755 --- a/contracts/tests/test_fund_manager.cairo +++ b/contracts/tests/test_fund_manager.cairo @@ -77,5 +77,5 @@ fn test_new_fund() { let expected_fund_class_hash = get_class_hash(fund_manager_contract.getFund(1)); let current_id = fund_manager_contract.getCurrentId(); assert(expected_fund_class_hash == fund_class_hash, 'Invalid fund address'); - assert(current_id == 2 , 'Invalid current ID'); + assert(current_id == 2, 'Invalid current ID'); } From d60923cfd584150fc9dcafe46a546f3195d72f31 Mon Sep 17 00:00:00 2001 From: manlikeHB Date: Fri, 1 Nov 2024 17:44:25 +0100 Subject: [PATCH 3/3] complete test --- contracts/tests/test_fund.cairo | 57 +++++++++++++++++---------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/contracts/tests/test_fund.cairo b/contracts/tests/test_fund.cairo index 342387e..0936eed 100644 --- a/contracts/tests/test_fund.cairo +++ b/contracts/tests/test_fund.cairo @@ -300,44 +300,45 @@ fn test_withdraw_with_non_closed_state() { } #[test] -#[should_panic(expected: ('Fund hasnt reached its goal yet',))] -fn test_withdraw_when_fund_has_not_reach_goal() { +#[fork("Mainnet")] +fn test_withdraw() { let contract_address = _setup_(); + let goal: u256 = 500; + let dispatcher = IFundDispatcher { contract_address }; + let minter_address = contract_address_const::(); + let token_address = contract_address_const::(); + let token_dispatcher = IERC20Dispatcher { contract_address: token_address }; - start_cheat_caller_address_global(FUND_MANAGER()); + //Set donation state + dispatcher.setState(2); - dispatcher.setGoal(10); - dispatcher.setState(3); + start_cheat_caller_address(contract_address, FUND_MANAGER()); + dispatcher.setGoal(goal); - start_cheat_caller_address_global(OWNER()); - dispatcher.withdraw(); -} + cheat_caller_address(token_address, minter_address, CheatSpan::TargetCalls(1)); + let mut calldata = array![]; + calldata.append_serde(FUND_MANAGER()); + calldata.append_serde(goal); + call_contract_syscall(token_address, selector!("permissioned_mint"), calldata.span()).unwrap(); -#[test] -#[fork("Mainnet")] -fn test_withdraw() { - let contract_address = _setup_(); - let fund_dispatcher = IFundDispatcher { contract_address }; - let starknet_dispatcher = IERC20Dispatcher { - contract_address: StarknetConstants::STRK_TOKEN_ADDRESS.try_into().unwrap() - }; + cheat_caller_address(token_address, FUND_MANAGER(), CheatSpan::TargetCalls(1)); + token_dispatcher.approve(contract_address, goal); - start_cheat_caller_address_global(FUND_MANAGER()); + dispatcher.receiveDonation(goal); - // set goal - fund_dispatcher.setGoal(500_u256); - fund_dispatcher.setState(2); + start_cheat_caller_address_global(OWNER()); + cheat_caller_address(token_address, OWNER(), CheatSpan::TargetCalls(1)); - // donate - fund_dispatcher.receiveDonation(500_u256); + let owner_balance_before = token_dispatcher.balance_of(OWNER()); + let fund_balance_before = token_dispatcher.balance_of(contract_address); - start_cheat_caller_address_global(OWNER()); - // withdraw funds - fund_dispatcher.withdraw(); + // withdraw + dispatcher.withdraw(); - let balance = starknet_dispatcher.balance_of(OWNER()); + let owner_balance_after = token_dispatcher.balance_of(OWNER()); + let fund_balance_after = token_dispatcher.balance_of(contract_address); - //TODO assert correct balance when logic for depositing strks is complete - assert(balance == 0, 'wrong balance'); + assert(owner_balance_after == (owner_balance_before + goal), 'wrong owner balance'); + assert((fund_balance_before - goal) == fund_balance_after, 'wrong fund balance'); }