Skip to content

Commit

Permalink
Merge pull request #35 from gidson5/test_donate_event
Browse files Browse the repository at this point in the history
Test donate event
  • Loading branch information
mubarak23 authored Nov 30, 2024
2 parents c773ab0 + bf170c7 commit 6ba0de8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/campaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mod TokengiverCampaign {
#[derive(Drop, starknet::Event)]
pub enum Event {
CreateCampaign: CreateCampaign,
DonationCreated: DonationCreated,
DonationMade: DonationMade,
DeployedTokenGiverNFT: DeployedTokenGiverNFT,
}

Expand All @@ -75,7 +75,7 @@ mod TokengiverCampaign {
}

#[derive(Drop, starknet::Event)]
pub struct DonationCreated {
pub struct DonationMade {
#[key]
campaign_id: u256,
#[key]
Expand Down Expand Up @@ -276,7 +276,7 @@ mod TokengiverCampaign {

self
.emit(
DonationCreated {
DonationMade {
campaign_id: token_id,
donor_address: donor,
amount: amount,
Expand Down
41 changes: 38 additions & 3 deletions tests/test_campaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTr
use starknet::{ContractAddress, ClassHash, get_block_timestamp};

use tokengiver::interfaces::ICampaign::{ICampaign, ICampaignDispatcher, ICampaignDispatcherTrait};
use tokengiver::campaign::TokengiverCampaign::{Event, DonationCreated};
use tokengiver::campaign::TokengiverCampaign::{Event, DonationMade};

fn REGISTRY_HASH() -> felt252 {
0x046163525551f5a50ed027548e86e1ad023c44e0eeb0733f0dab2fb1fdc31ed0.try_into().unwrap()
Expand Down Expand Up @@ -105,9 +105,44 @@ fn test_donate() {
assert(strk_dispatcher.balance_of(DONOR()) == 0, 'wrong balance');
assert(token_giver.get_donations(campaign_address) == amount, 'wrong donation amount');
assert(token_giver.get_donation_count(campaign_address) == 1, 'wrong donation amount');
}


#[test]
#[fork("Mainnet")]
fn test_donate_event_emission() {
let (token_giver_address, strk_address) = __setup__();
let token_giver = ICampaignDispatcher { contract_address: token_giver_address };
let strk_dispatcher = IERC20Dispatcher { contract_address: strk_address };
let random_id = 1;
let mut spy = spy_events();

//create campaign
start_cheat_caller_address(token_giver_address, RECIPIENT());
let campaign_address = token_giver
.create_campaign(REGISTRY_HASH(), IMPLEMENTATION_HASH(), SALT());

stop_cheat_caller_address(token_giver_address);

/// Transfer STRK to Donor
start_cheat_caller_address(strk_address, OWNER());
let amount = 2000000; //
strk_dispatcher.transfer(DONOR(), amount);
assert(strk_dispatcher.balance_of(DONOR()) >= amount, 'strk bal too low');
stop_cheat_caller_address(strk_address);

// approve allowance
start_cheat_caller_address(strk_address, DONOR());
strk_dispatcher.approve(token_giver_address, amount);
stop_cheat_caller_address(strk_address);

// donate
start_cheat_caller_address(token_giver_address, DONOR());
token_giver.donate(campaign_address, amount, random_id);
stop_cheat_caller_address(token_giver_address);

let expected_event = Event::DonationCreated(
DonationCreated {
let expected_event = Event::DonationMade(
DonationMade {
campaign_id: random_id,
donor_address: DONOR(),
amount: amount,
Expand Down

0 comments on commit 6ba0de8

Please sign in to comment.