Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions campaign/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use storage::{
storage_increment_donation_count, storage_increment_unique_donor_count,
storage_set_total_raised,
};

use types::{
AssetInfo, CampaignData, CampaignInitializedEvent, CampaignReport, CampaignStatus,
CampaignStatusResponse, DashboardMetrics, DonorRecord, Error, MilestoneData, MilestoneStatus,
Expand Down Expand Up @@ -210,6 +211,8 @@ impl CampaignContract {
storage_increment_asset_raised(&env, &asset_address, amount);
increment_donor_asset_donation(&env, &donor, &asset_address, amount);

let _donor_record =
get_donor(&env, &donor).unwrap_or(DonorRecord::new_for(donor.clone(), asset.clone()));
// Update donor record
let existing_donor = get_donor(&env, &donor);
let is_new_donor = existing_donor.is_none();
Expand Down Expand Up @@ -395,6 +398,9 @@ impl CampaignContract {
let campaign =
get_campaign(&env).unwrap_or_else(|| panic_with_error(&env, Error::NotInitialized));

let _donor_record =
get_donor(&env, &donor).unwrap_or_else(|| panic_with_error(&env, Error::NoDonorRecord));

let mut donor_record =
get_donor(&env, &donor).unwrap_or_else(|| panic_with_error(&env, Error::NoDonorRecord));

Expand Down Expand Up @@ -615,6 +621,17 @@ impl CampaignContract {
}
}

/// Issue #175 – assert the current invoker is the campaign creator.
///
/// Reads the creator address from campaign storage and calls `require_auth()`.
/// Panics with `Error::Unauthorized` if the campaign is not initialized;
/// Soroban's auth framework panics if the invoker is not the creator.
#[allow(dead_code)]
fn require_creator(env: &Env) {
let campaign = get_campaign(env).unwrap_or_else(|| panic_with_error(env, Error::Unauthorized));
campaign.creator.require_auth();
}

/// Validates that `asset` is in the campaign's accepted list and returns the
/// token contract address needed to construct a `token::Client`.
fn get_token_address_for_asset(env: &Env, asset: &AssetInfo, campaign: &CampaignData) -> Address {
Expand Down
11 changes: 8 additions & 3 deletions campaign/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// src/types.rs

use soroban_sdk::{contracterror, contracttype, Address, BytesN, Env, String, Vec};
use soroban_sdk::{
contracterror, contracttype, panic_with_error, Address, BytesN, Env, String, Vec,
};

// ─── Error enum ───────────────────────────────────────────────────────────────

Expand Down Expand Up @@ -450,6 +452,9 @@ impl DonorRecord {
}
}

/// Apply a new donation to this record. Returns an error string (for
/// debug builds) rather than panicking so the call site can choose how
/// to surface it.
/// Apply a new donation to this record. Panics with `Error::Overflow` if
/// `total_donated` or `donation_count` overflows.
pub fn apply_donation(
Expand All @@ -463,13 +468,13 @@ impl DonorRecord {
self.total_donated = self
.total_donated
.checked_add(amount)
.unwrap_or_else(|| env.panic_with_error(Error::Overflow));
.unwrap_or_else(|| panic_with_error!(&env, Error::Overflow));
self.last_donation_time = time;
self.last_donation_ledger = ledger;
self.donation_count = self
.donation_count
.checked_add(1)
.unwrap_or_else(|| env.panic_with_error(Error::Overflow));
.unwrap_or_else(|| panic_with_error!(&env, Error::Overflow));
self.asset = asset;
}
}
Expand Down
Loading