From b24965df945bfcc6f83f0b88168635bdcbc00cbf Mon Sep 17 00:00:00 2001 From: Nenad Date: Wed, 12 Jun 2024 12:36:48 +0200 Subject: [PATCH] refund users when upgrading campaign --- .../crowdfunding/src/campaign.cairo | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/listings/applications/crowdfunding/src/campaign.cairo b/listings/applications/crowdfunding/src/campaign.cairo index 9659c7ef..acac0297 100644 --- a/listings/applications/crowdfunding/src/campaign.cairo +++ b/listings/applications/crowdfunding/src/campaign.cairo @@ -183,7 +183,8 @@ pub mod Campaign { // no need to set total_contributions to 0, as the campaign has ended // and the field can be used as a testament to how much was raised - let success = token.transfer(get_caller_address(), amount); + let owner = get_caller_address(); + let success = token.transfer(owner, amount); assert(success, Errors::TRANSFER_FAILED); self.emit(Event::Claimed(Claimed { amount })); @@ -195,12 +196,7 @@ pub mod Campaign { self.status.write(Status::CLOSED); - let mut contributions = self.get_contributions(); - while let Option::Some((contributor, amt)) = contributions - .pop_front() { - self.contributions.remove(contributor); - self.token.read().transfer(contributor, amt); - }; + self._refund_all(); self.emit(Event::Closed(Closed { reason })); } @@ -253,6 +249,8 @@ pub mod Campaign { return Result::Err(array![Errors::CLASS_HASH_ZERO]); } + self._refund_all(); + starknet::syscalls::replace_class_syscall(impl_hash)?; self.emit(Event::Upgraded(Upgraded { implementation: impl_hash })); @@ -295,6 +293,15 @@ pub mod Campaign { fn _is_target_reached(self: @ContractState) -> bool { self.total_contributions.read() >= self.target.read() } + + fn _refund_all(ref self: ContractState) { + let mut contributions = self.contributions.get_contributions_as_arr(); + while let Option::Some((contributor, amt)) = contributions + .pop_front() { + self.contributions.remove(contributor); + self.token.read().transfer(contributor, amt); + }; + } } } // ANCHOR_END: contract