Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Test event emit when user withdraw fund's balance #170

Merged
merged 6 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/src/fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub mod Fund {
// TODO: Calculate balance to deposit in owner address and in fund manager address (95%
// and 5%), also transfer the amount to fund manager address.
self.token_dispatcher().transfer(self.getOwner(), withdrawn_amount);
assert(self.get_current_goal_state() != 0, 'Fund hasnt reached its goal yet');
assert(self.get_current_goal_state() >= 0, 'Fund hasnt reached its goal yet');
EmmanuelAR marked this conversation as resolved.
Show resolved Hide resolved
self.setState(4);
self
.emit(
Expand Down
89 changes: 75 additions & 14 deletions contracts/tests/test_fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ fn _setup_() -> ContractAddress {
let (contract_address, _) = contract.deploy(@calldata).unwrap();
contract_address
}
fn setup_donation(
dispatcher: IFundDispatcher,
token_dispatcher: IERC20Dispatcher,
contract_address: ContractAddress,
token_address: ContractAddress,
minter_address: ContractAddress,
goal: u256
) {
// Put state as recollecting dons
dispatcher.setState(2);
// Put 10 strks as goal, only fund manager
start_cheat_caller_address(contract_address, FUND_MANAGER());
dispatcher.setGoal(goal);
// fund the manager with STRK token
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();
// approve
cheat_caller_address(token_address, FUND_MANAGER(), CheatSpan::TargetCalls(1));
token_dispatcher.approve(contract_address, goal);
}
// ***************************************************************************************
// TEST
// ***************************************************************************************
Expand Down Expand Up @@ -148,20 +171,12 @@ fn test_receive_donation_successful() {
let minter_address = contract_address_const::<StarknetConstants::STRK_TOKEN_MINTER_ADDRESS>();
let token_address = contract_address_const::<StarknetConstants::STRK_TOKEN_ADDRESS>();
let token_dispatcher = IERC20Dispatcher { contract_address: token_address };
// Put state as recollecting dons
dispatcher.setState(2);
// Put 10 strks as goal, only fund manager
start_cheat_caller_address(contract_address, FUND_MANAGER());
dispatcher.setGoal(goal);
// fund the manager with STRK token
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();
// approve
cheat_caller_address(token_address, FUND_MANAGER(), CheatSpan::TargetCalls(1));
token_dispatcher.approve(contract_address, goal);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that you create the setup_donation method to avoid to repeat code but in this case please leave it ass the original

//Set up donation call
setup_donation(
dispatcher, token_dispatcher, contract_address, token_address, minter_address, goal
);

// Donate 5 strks
dispatcher.receiveDonation(goal / 2);
let current_goal_state = dispatcher.get_current_goal_state();
Expand Down Expand Up @@ -217,3 +232,49 @@ fn test_new_vote_received_event_emitted_successful() {
]
);
}


#[test]
#[fork("Mainnet")]
fn test_emit_event_donation_withdraw() {
//Set up contract addresses
let contract_address = _setup_();
let goal: u256 = 10;
let strks: u256 = 11;

let dispatcher = IFundDispatcher { contract_address };
let minter_address = contract_address_const::<StarknetConstants::STRK_TOKEN_MINTER_ADDRESS>();
let token_address = contract_address_const::<StarknetConstants::STRK_TOKEN_ADDRESS>();
let token_dispatcher = IERC20Dispatcher { contract_address: token_address };

//Set up donation call
setup_donation(
dispatcher, token_dispatcher, contract_address, token_address, minter_address, goal
);

dispatcher.receiveDonation(goal);

start_cheat_caller_address_global(OWNER());
cheat_caller_address(token_address, OWNER(), CheatSpan::TargetCalls(1));

// Spy on emitted events and call the withdraw function
let mut spy = spy_events();
dispatcher.withdraw();

// Verify the expected event was emitted with the correct values
spy
.assert_emitted(
@array![
(
contract_address,
Fund::Event::DonationWithdraw(
Fund::DonationWithdraw {
owner_address: OWNER(),
fund_contract_address: contract_address,
withdrawn_amount: 10
}
)
)
]
);
}