Skip to content

Commit

Permalink
add status pending
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenad committed Jun 12, 2024
1 parent 6796467 commit 85e4667
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion listings/applications/advanced_factory/src/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
22 changes: 19 additions & 3 deletions listings/applications/crowdfunding/src/campaign.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -29,6 +30,7 @@ pub trait ICampaign<TContractState> {
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<felt252>>;
fn withdraw(ref self: TContractState);
}
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -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)]
Expand All @@ -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();
Expand Down Expand Up @@ -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<felt252>> {
if get_caller_address() != self.factory.read() {
return Result::Err(array![Errors::NOT_FACTORY]);
Expand Down
2 changes: 1 addition & 1 deletion listings/applications/crowdfunding/src/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 85e4667

Please sign in to comment.