diff --git a/contract/contract/src/base/types.rs b/contract/contract/src/base/types.rs index 6add706..83baae7 100644 --- a/contract/contract/src/base/types.rs +++ b/contract/contract/src/base/types.rs @@ -127,6 +127,8 @@ pub struct CampaignMetrics { pub total_raised: i128, pub contributor_count: u32, pub last_donation_at: u64, + pub max_donation: i128, + pub top_contributor: Option
, } impl Default for CampaignMetrics { @@ -141,6 +143,8 @@ impl CampaignMetrics { total_raised: 0, contributor_count: 0, last_donation_at: 0, + max_donation: 0, + top_contributor: None, } } } diff --git a/contract/contract/src/crowdfunding.rs b/contract/contract/src/crowdfunding.rs index 24d8441..18b9a27 100644 --- a/contract/contract/src/crowdfunding.rs +++ b/contract/contract/src/crowdfunding.rs @@ -172,6 +172,25 @@ impl CrowdfundingTrait for CrowdfundingContract { .unwrap_or(0) } + fn get_top_contributor_for_campaign( + env: Env, + campaign_id: BytesN<32>, + ) -> Result { + // Validate campaign exists + Self::get_campaign(env.clone(), campaign_id.clone())?; + + let metrics_key = StorageKey::CampaignMetrics(campaign_id); + let metrics: CampaignMetrics = env + .storage() + .instance() + .get(&metrics_key) + .unwrap_or_default(); + + metrics + .top_contributor + .ok_or(CrowdfundingError::CampaignNotFound) + } + fn get_all_campaigns(env: Env) -> Vec> { env.storage() .instance() @@ -349,6 +368,12 @@ impl CrowdfundingTrait for CrowdfundingContract { metrics.total_raised += amount; metrics.last_donation_at = env.ledger().timestamp(); + // Track top contributor (whale donor) + if amount > metrics.max_donation { + metrics.max_donation = amount; + metrics.top_contributor = Some(donor.clone()); + } + // Track unique donor let donor_key = StorageKey::CampaignDonor(campaign_id.clone(), donor.clone()); if !env.storage().instance().has(&donor_key) { diff --git a/contract/contract/src/interfaces/crowdfunding.rs b/contract/contract/src/interfaces/crowdfunding.rs index 6e6e320..71b9a45 100644 --- a/contract/contract/src/interfaces/crowdfunding.rs +++ b/contract/contract/src/interfaces/crowdfunding.rs @@ -87,6 +87,11 @@ pub trait CrowdfundingTrait { fn get_global_raised_total(env: Env) -> i128; + fn get_top_contributor_for_campaign( + env: Env, + campaign_id: BytesN<32>, + ) -> Result; + fn initialize( env: Env, admin: Address,