From 85e4667122f84f8db0d0d326f4a9bcebcf8e148a Mon Sep 17 00:00:00 2001 From: Nenad Date: Wed, 12 Jun 2024 12:26:15 +0200 Subject: [PATCH] add status pending --- .../advanced_factory/src/tests.cairo | 2 +- .../crowdfunding/src/campaign.cairo | 22 ++++++++++++++++--- .../applications/crowdfunding/src/tests.cairo | 2 +- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/listings/applications/advanced_factory/src/tests.cairo b/listings/applications/advanced_factory/src/tests.cairo index d015e006..d4371bda 100644 --- a/listings/applications/advanced_factory/src/tests.cairo +++ b/listings/applications/advanced_factory/src/tests.cairo @@ -74,7 +74,7 @@ fn test_deploy_campaign() { assert_eq!(details.description, description); assert_eq!(details.target, target); assert_eq!(details.end_time, get_block_timestamp() + duration); - assert_eq!(details.status, Status::ACTIVE); + assert_eq!(details.status, Status::PENDING); assert_eq!(details.token, token); assert_eq!(details.total_contributions, 0); diff --git a/listings/applications/crowdfunding/src/campaign.cairo b/listings/applications/crowdfunding/src/campaign.cairo index 89281044..7ca23e8b 100644 --- a/listings/applications/crowdfunding/src/campaign.cairo +++ b/listings/applications/crowdfunding/src/campaign.cairo @@ -6,9 +6,10 @@ use starknet::{ClassHash, ContractAddress}; #[derive(Drop, Debug, Serde, PartialEq, starknet::Store)] pub enum Status { ACTIVE, + CLOSED, + PENDING, SUCCESSFUL, UNSUCCESSFUL, - CLOSED } #[derive(Drop, Serde)] @@ -29,6 +30,7 @@ pub trait ICampaign { fn contribute(ref self: TContractState, amount: u256); fn get_contributions(self: @TContractState) -> Array<(ContractAddress, u256)>; fn get_details(self: @TContractState) -> Details; + fn start(ref self: TContractState); fn upgrade(ref self: TContractState, impl_hash: ClassHash) -> Result<(), Array>; fn withdraw(ref self: TContractState); } @@ -71,12 +73,12 @@ pub mod Campaign { status: Status } - #[event] #[derive(Drop, starknet::Event)] pub enum Event { #[flat] OwnableEvent: ownable_component::Event, + Activated: Activated, ContributableEvent: contributable_component::Event, ContributionMade: ContributionMade, Claimed: Claimed, @@ -97,6 +99,9 @@ pub mod Campaign { pub amount: u256, } + #[derive(Drop, starknet::Event)] + pub struct Activated {} + #[derive(Drop, starknet::Event)] pub struct Closed { pub reason: ByteArray, @@ -117,6 +122,7 @@ pub mod Campaign { pub mod Errors { pub const NOT_FACTORY: felt252 = 'Caller not factory'; pub const ENDED: felt252 = 'Campaign already ended'; + pub const NOT_PENDING: felt252 = 'Campaign not pending'; pub const STILL_ACTIVE: felt252 = 'Campaign not ended'; pub const ZERO_DONATION: felt252 = 'Donation must be > 0'; pub const ZERO_TARGET: felt252 = 'Target must be > 0'; @@ -155,7 +161,7 @@ pub mod Campaign { self.end_time.write(get_block_timestamp() + duration); self.factory.write(get_caller_address()); self.ownable._init(owner); - self.status.write(Status::ACTIVE) + self.status.write(Status::PENDING) } #[abi(embed_v0)] @@ -164,6 +170,7 @@ pub mod Campaign { self.ownable._assert_only_owner(); assert(self._is_active(), Errors::ENDED); assert(self._is_target_reached(), Errors::TARGET_NOT_REACHED); + // no need to check end_time, as the owner can prematurely end the campaign let this = get_contract_address(); let token = self.token.read(); @@ -229,6 +236,15 @@ pub mod Campaign { } } + fn start(ref self: ContractState) { + self.ownable._assert_only_owner(); + assert(self.status.read() == Status::PENDING, Errors::NOT_PENDING); + + self.status.write(Status::ACTIVE); + + self.emit(Event::Activated(Activated {})); + } + fn upgrade(ref self: ContractState, impl_hash: ClassHash) -> Result<(), Array> { if get_caller_address() != self.factory.read() { return Result::Err(array![Errors::NOT_FACTORY]); diff --git a/listings/applications/crowdfunding/src/tests.cairo b/listings/applications/crowdfunding/src/tests.cairo index 5bd710dc..52548c2b 100644 --- a/listings/applications/crowdfunding/src/tests.cairo +++ b/listings/applications/crowdfunding/src/tests.cairo @@ -47,7 +47,7 @@ fn test_deploy() { assert_eq!(details.description, "description 1"); assert_eq!(details.target, 10000); assert_eq!(details.end_time, get_block_timestamp() + 60); - assert_eq!(details.status, Status::ACTIVE); + assert_eq!(details.status, Status::PENDING); assert_eq!(details.token, contract_address_const::<'token'>()); assert_eq!(details.total_contributions, 0);