Skip to content

Commit

Permalink
upgrade_campaign_implementation-> upgrade_campaign + comment updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenad authored and julio4 committed Jun 26, 2024
1 parent ab66865 commit cfd7694
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
7 changes: 2 additions & 5 deletions listings/applications/advanced_factory/src/contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait ICampaignFactory<TContractState> {
) -> ContractAddress;
fn get_campaign_class_hash(self: @TContractState) -> ClassHash;
fn update_campaign_class_hash(ref self: TContractState, new_class_hash: ClassHash);
fn upgrade_campaign_implementation(
fn upgrade_campaign(
ref self: TContractState, campaign_address: ContractAddress, new_end_time: Option<u64>
);
}
Expand Down Expand Up @@ -89,7 +89,6 @@ pub mod CampaignFactory {

#[abi(embed_v0)]
impl CampaignFactory of super::ICampaignFactory<ContractState> {
// ANCHOR: deploy
fn create_campaign(
ref self: ContractState,
title: ByteArray,
Expand Down Expand Up @@ -119,7 +118,6 @@ pub mod CampaignFactory {

contract_address
}
// ANCHOR_END: deploy

fn get_campaign_class_hash(self: @ContractState) -> ClassHash {
self.campaign_class_hash.read()
Expand All @@ -129,13 +127,12 @@ pub mod CampaignFactory {
self.ownable._assert_only_owner();
assert(new_class_hash.is_non_zero(), Errors::CLASS_HASH_ZERO);

// update own campaign class hash value
self.campaign_class_hash.write(new_class_hash);

self.emit(Event::ClassHashUpdated(ClassHashUpdated { new_class_hash }));
}

fn upgrade_campaign_implementation(
fn upgrade_campaign(
ref self: ContractState, campaign_address: ContractAddress, new_end_time: Option<u64>
) {
assert(campaign_address.is_non_zero(), Errors::ZERO_ADDRESS);
Expand Down
4 changes: 2 additions & 2 deletions listings/applications/advanced_factory/src/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fn test_uprade_campaign_class_hash() {

// upgrade pending campaign
start_cheat_caller_address(factory.contract_address, pending_campaign_creator);
factory.upgrade_campaign_implementation(pending_campaign, Option::None);
factory.upgrade_campaign(pending_campaign, Option::None);

assert_eq!(get_class_hash(pending_campaign), new_class_hash);
assert_eq!(get_class_hash(active_campaign), old_class_hash);
Expand All @@ -177,7 +177,7 @@ fn test_uprade_campaign_class_hash() {

// upgrade active campaign
start_cheat_caller_address(factory.contract_address, active_campaign_creator);
factory.upgrade_campaign_implementation(active_campaign, Option::None);
factory.upgrade_campaign(active_campaign, Option::None);

assert_eq!(get_class_hash(pending_campaign), new_class_hash);
assert_eq!(get_class_hash(active_campaign), new_class_hash);
Expand Down
9 changes: 4 additions & 5 deletions listings/applications/crowdfunding/src/campaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub mod Campaign {
};
use components::ownable::ownable_component;
use super::pledgeable::pledgeable_component;
use super::{Details};
use super::Details;

component!(path: ownable_component, storage: ownable, event: OwnableEvent);
component!(path: pledgeable_component, storage: pledges, event: PledgeableEvent);
Expand Down Expand Up @@ -199,6 +199,8 @@ pub mod Campaign {
self.emit(Event::Canceled(Canceled { reason }));
}

/// Sends the funds to the campaign creator.
/// It leaves the pledge data intact as a testament to campaign success
fn claim(ref self: ContractState) {
self._assert_only_creator();
assert(self._is_started(), Errors::NOT_STARTED);
Expand All @@ -214,9 +216,6 @@ pub mod Campaign {

self.claimed.write(true);

// no need to reset the pledges, as the campaign has ended
// and the data can be used as a testament to how much was raised

let owner = get_caller_address();
let success = token.transfer(owner, amount);
assert(success, Errors::TRANSFER_FAILED);
Expand Down Expand Up @@ -291,7 +290,7 @@ pub mod Campaign {
self.ownable._assert_only_owner();
assert(impl_hash.is_non_zero(), Errors::CLASS_HASH_ZERO);

// only active campaigns have funds to refund and an end time to update
// only active campaigns have pledges to refund and an end time to update
if self._is_started() {
if let Option::Some(end_time) = new_end_time {
assert(end_time >= get_block_timestamp(), Errors::END_BEFORE_NOW);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ pub mod pledgeable_component {
if self.index_to_pledger.read(pledger_index) == pledger {
break;
}
assert(pledger_index > 0, Errors::INCONSISTENT_STATE);
// if pledger_to_amount contains a pledger, then so does index_to_pledger
// thus this will never underflow
pledger_index -= 1;
};

Expand Down

0 comments on commit cfd7694

Please sign in to comment.