Skip to content

Commit

Permalink
add test for withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenad committed Jun 13, 2024
1 parent d6a7d0a commit 2d37c20
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 6 deletions.
7 changes: 4 additions & 3 deletions listings/applications/crowdfunding/src/campaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub trait ICampaign<TContractState> {
fn start(ref self: TContractState, duration: u64);
fn refund(ref self: TContractState, contributor: ContractAddress, reason: ByteArray);
fn upgrade(ref self: TContractState, impl_hash: ClassHash, new_duration: Option<u64>);
fn withdraw(ref self: TContractState);
fn withdraw(ref self: TContractState, reason: ByteArray);
}

#[starknet::contract]
Expand Down Expand Up @@ -136,6 +136,7 @@ pub mod Campaign {
#[key]
pub contributor: ContractAddress,
pub amount: u256,
pub reason: ByteArray,
}

pub mod Errors {
Expand Down Expand Up @@ -330,7 +331,7 @@ pub mod Campaign {
self.emit(Event::Upgraded(Upgraded { implementation: impl_hash }));
}

fn withdraw(ref self: ContractState) {
fn withdraw(ref self: ContractState, reason: ByteArray) {
assert(self.status.read() != Status::DRAFT, Errors::STILL_DRAFT);
assert(self.status.read() != Status::SUCCESSFUL, Errors::ENDED);
assert(self.status.read() != Status::CLOSED, Errors::CLOSED);
Expand All @@ -340,7 +341,7 @@ pub mod Campaign {
let contributor = get_caller_address();
let amount = self._refund(contributor);

self.emit(Event::Withdrawn(Withdrawn { contributor, amount }));
self.emit(Event::Withdrawn(Withdrawn { contributor, amount, reason }));
}
}

Expand Down
10 changes: 7 additions & 3 deletions listings/applications/crowdfunding/src/mock_upgrade.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub mod MockUpgrade {
status: Status
}


#[event]
#[derive(Drop, starknet::Event)]
pub enum Event {
Expand Down Expand Up @@ -70,6 +71,7 @@ pub mod MockUpgrade {
#[derive(Drop, starknet::Event)]
pub struct Closed {
pub reason: ByteArray,
pub status: Status,
}

#[derive(Drop, starknet::Event)]
Expand All @@ -95,6 +97,7 @@ pub mod MockUpgrade {
#[key]
pub contributor: ContractAddress,
pub amount: u256,
pub reason: ByteArray,
}

#[constructor]
Expand Down Expand Up @@ -157,8 +160,9 @@ pub mod MockUpgrade {
}

self._refund_all(reason.clone());
let status = self.status.read();

self.emit(Event::Closed(Closed { reason }));
self.emit(Event::Closed(Closed { reason, status }));
}

fn contribute(ref self: ContractState, amount: u256) {
Expand Down Expand Up @@ -265,7 +269,7 @@ pub mod MockUpgrade {
self.emit(Event::Upgraded(Upgraded { implementation: impl_hash }));
}

fn withdraw(ref self: ContractState) {
fn withdraw(ref self: ContractState, reason: ByteArray) {
assert(self.status.read() != Status::DRAFT, Errors::STILL_DRAFT);
assert(self.status.read() != Status::SUCCESSFUL, Errors::ENDED);
assert(self.status.read() != Status::CLOSED, Errors::CLOSED);
Expand All @@ -281,7 +285,7 @@ pub mod MockUpgrade {
let success = self.token.read().transfer(contributor, amount);
assert(success, Errors::TRANSFER_FAILED);

self.emit(Event::Withdrawn(Withdrawn { contributor, amount }));
self.emit(Event::Withdrawn(Withdrawn { contributor, amount, reason }));
}
}

Expand Down
83 changes: 83 additions & 0 deletions listings/applications/crowdfunding/src/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,86 @@ fn test_close() {
]
);
}

#[test]
fn test_refund() {
// setup
let duration: u64 = 60;
let (campaign, token) = deploy_with_token(
declare("Campaign").unwrap(), declare("ERC20").unwrap()
);
let mut spy = spy_events(SpyOn::One(campaign.contract_address));
let creator = contract_address_const::<'creator'>();
let contributor = contract_address_const::<'contributor_1'>();
let amount: u256 = 3000;
let prev_balance = token.balance_of(contributor);

// donate
start_cheat_caller_address(campaign.contract_address, creator);
campaign.start(duration);
start_cheat_caller_address(campaign.contract_address, contributor);
campaign.contribute(amount);
assert_eq!(campaign.get_details().total_contributions, amount);
assert_eq!(campaign.get_contribution(contributor), amount);
assert_eq!(token.balance_of(contributor), prev_balance - amount);

// refund
start_cheat_caller_address(campaign.contract_address, creator);
campaign.refund(contributor, "testing");
assert_eq!(campaign.get_details().total_contributions, 0);
assert_eq!(campaign.get_contribution(contributor), 0);
assert_eq!(token.balance_of(contributor), prev_balance);

spy
.assert_emitted(
@array![
(
campaign.contract_address,
Campaign::Event::Refunded(
Campaign::Refunded { contributor, amount, reason: "testing" }
)
)
]
);
}

#[test]
fn test_withdraw() {
// setup
let duration: u64 = 60;
let (campaign, token) = deploy_with_token(
declare("Campaign").unwrap(), declare("ERC20").unwrap()
);
let mut spy = spy_events(SpyOn::One(campaign.contract_address));
let creator = contract_address_const::<'creator'>();
let contributor = contract_address_const::<'contributor_1'>();
let amount: u256 = 3000;
let prev_balance = token.balance_of(contributor);
start_cheat_caller_address(campaign.contract_address, creator);
campaign.start(duration);

// donate
start_cheat_caller_address(campaign.contract_address, contributor);
campaign.contribute(amount);
assert_eq!(campaign.get_details().total_contributions, amount);
assert_eq!(campaign.get_contribution(contributor), amount);
assert_eq!(token.balance_of(contributor), prev_balance - amount);

// withdraw
campaign.withdraw("testing");
assert_eq!(campaign.get_details().total_contributions, 0);
assert_eq!(campaign.get_contribution(contributor), 0);
assert_eq!(token.balance_of(contributor), prev_balance);

spy
.assert_emitted(
@array![
(
campaign.contract_address,
Campaign::Event::Withdrawn(
Campaign::Withdrawn { contributor, amount, reason: "testing" }
)
)
]
);
}

0 comments on commit 2d37c20

Please sign in to comment.