Skip to content

Commit 2008f22

Browse files
authored
Merge pull request #258 from DavisVT/feat/update-allevents-192
2 parents 3cea0b8 + e973ad5 commit 2008f22

6 files changed

Lines changed: 341 additions & 2 deletions

File tree

contract/contract/src/base/events.rs

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
11
#![allow(deprecated)]
2-
use soroban_sdk::{Address, BytesN, Env, String, Symbol};
2+
use soroban_sdk::{Address, BytesN, Env, String, Symbol, Vec};
33

4-
use crate::base::types::PoolState;
4+
use crate::base::types::{EventRecord, PoolState, StorageKey};
5+
6+
// ---------------------------------------------------------------------------
7+
// Global event tracker
8+
// ---------------------------------------------------------------------------
9+
10+
/// Increment the persistent event counter and append a record to `AllEvents`.
11+
///
12+
/// Uses persistent storage so the log survives ledger TTL expiry.
13+
/// Called by every public event emitter in this module.
14+
fn record_event(env: &Env, name: &str) {
15+
let count_key = StorageKey::AllEventsCount;
16+
let list_key = StorageKey::AllEvents;
17+
18+
// Increment counter (starts at 0 if not yet initialised)
19+
let new_index: u64 = env
20+
.storage()
21+
.persistent()
22+
.get::<_, u64>(&count_key)
23+
.unwrap_or(0)
24+
+ 1;
25+
26+
env.storage().persistent().set(&count_key, &new_index);
27+
28+
// Append a lightweight record to the global list
29+
let mut list: Vec<EventRecord> = env
30+
.storage()
31+
.persistent()
32+
.get::<_, Vec<EventRecord>>(&list_key)
33+
.unwrap_or_else(|| Vec::new(env));
34+
35+
list.push_back(EventRecord {
36+
index: new_index,
37+
name: String::from_str(env, name),
38+
timestamp: env.ledger().timestamp(),
39+
});
40+
41+
env.storage().persistent().set(&list_key, &list);
42+
}
43+
44+
// ---------------------------------------------------------------------------
45+
// Event emitters
46+
// ---------------------------------------------------------------------------
547

648
pub fn campaign_created(
749
env: &Env,
@@ -13,11 +55,13 @@ pub fn campaign_created(
1355
) {
1456
let topics = (Symbol::new(env, "campaign_created"), id, creator);
1557
env.events().publish(topics, (title, goal, deadline));
58+
record_event(env, "campaign_created");
1659
}
1760

1861
pub fn campaign_goal_updated(env: &Env, id: BytesN<32>, new_goal: i128) {
1962
let topics = (Symbol::new(env, "campaign_goal_updated"), id);
2063
env.events().publish(topics, new_goal);
64+
record_event(env, "campaign_goal_updated");
2165
}
2266

2367
#[allow(clippy::too_many_arguments)]
@@ -42,46 +86,55 @@ pub fn event_created(
4286
let topics = (Symbol::new(env, "event_created"), pool_id, creator);
4387
env.events()
4488
.publish(topics, (name, target_amount, deadline));
89+
record_event(env, "event_created");
4590
}
4691

4792
pub fn pool_state_updated(env: &Env, pool_id: u64, new_state: PoolState) {
4893
let topics = (Symbol::new(env, "pool_state_updated"), pool_id);
4994
env.events().publish(topics, new_state);
95+
record_event(env, "pool_state_updated");
5096
}
5197

5298
pub fn contract_paused(env: &Env, admin: Address, timestamp: u64) {
5399
let topics = (Symbol::new(env, "contract_paused"), admin);
54100
env.events().publish(topics, timestamp);
101+
record_event(env, "contract_paused");
55102
}
56103

57104
pub fn contract_unpaused(env: &Env, admin: Address, timestamp: u64) {
58105
let topics = (Symbol::new(env, "contract_unpaused"), admin);
59106
env.events().publish(topics, timestamp);
107+
record_event(env, "contract_unpaused");
60108
}
61109

62110
pub fn admin_renounced(env: &Env, admin: Address) {
63111
let topics = (Symbol::new(env, "admin_renounced"), admin);
64112
env.events().publish(topics, ());
113+
record_event(env, "admin_renounced");
65114
}
66115

67116
pub fn emergency_contact_updated(env: &Env, admin: Address, contact: Address) {
68117
let topics = (Symbol::new(env, "emergency_contact_updated"), admin);
69118
env.events().publish(topics, contact);
119+
record_event(env, "emergency_contact_updated");
70120
}
71121

72122
pub fn donation_made(env: &Env, campaign_id: BytesN<32>, contributor: Address, amount: i128) {
73123
let topics = (Symbol::new(env, "donation_made"), campaign_id);
74124
env.events().publish(topics, (contributor, amount));
125+
record_event(env, "donation_made");
75126
}
76127

77128
pub fn campaign_cancelled(env: &Env, id: BytesN<32>) {
78129
let topics = (Symbol::new(env, "campaign_cancelled"), id);
79130
env.events().publish(topics, ());
131+
record_event(env, "campaign_cancelled");
80132
}
81133

82134
pub fn campaign_refunded(env: &Env, id: BytesN<32>, contributor: Address, amount: i128) {
83135
let topics = (Symbol::new(env, "campaign_refunded"), id, contributor);
84136
env.events().publish(topics, amount);
137+
record_event(env, "campaign_refunded");
85138
}
86139

87140
pub fn contribution(
@@ -96,6 +149,7 @@ pub fn contribution(
96149
let topics = (Symbol::new(env, "contribution"), pool_id, contributor);
97150
env.events()
98151
.publish(topics, (asset, amount, timestamp, is_private));
152+
record_event(env, "contribution");
99153
}
100154

101155
pub fn emergency_withdraw_requested(
@@ -107,26 +161,31 @@ pub fn emergency_withdraw_requested(
107161
) {
108162
let topics = (Symbol::new(env, "emergency_withdraw_requested"), admin);
109163
env.events().publish(topics, (token, amount, unlock_time));
164+
record_event(env, "emergency_withdraw_requested");
110165
}
111166

112167
pub fn emergency_withdraw_executed(env: &Env, admin: Address, token: Address, amount: i128) {
113168
let topics = (Symbol::new(env, "emergency_withdraw_executed"), admin);
114169
env.events().publish(topics, (token, amount));
170+
record_event(env, "emergency_withdraw_executed");
115171
}
116172

117173
pub fn crowdfunding_token_set(env: &Env, admin: Address, token: Address) {
118174
let topics = (Symbol::new(env, "crowdfunding_token_set"), admin);
119175
env.events().publish(topics, token);
176+
record_event(env, "crowdfunding_token_set");
120177
}
121178

122179
pub fn creation_fee_set(env: &Env, admin: Address, fee: i128) {
123180
let topics = (Symbol::new(env, "creation_fee_set"), admin);
124181
env.events().publish(topics, fee);
182+
record_event(env, "creation_fee_set");
125183
}
126184

127185
pub fn creation_fee_paid(env: &Env, creator: Address, amount: i128) {
128186
let topics = (Symbol::new(env, "creation_fee_paid"), creator);
129187
env.events().publish(topics, amount);
188+
record_event(env, "creation_fee_paid");
130189
}
131190

132191
pub fn refund(
@@ -139,41 +198,49 @@ pub fn refund(
139198
) {
140199
let topics = (Symbol::new(env, "refund"), pool_id, contributor);
141200
env.events().publish(topics, (asset, amount, timestamp));
201+
record_event(env, "refund");
142202
}
143203

144204
pub fn pool_closed(env: &Env, pool_id: u64, closed_by: Address, timestamp: u64) {
145205
let topics = (Symbol::new(env, "pool_closed"), pool_id, closed_by);
146206
env.events().publish(topics, timestamp);
207+
record_event(env, "pool_closed");
147208
}
148209

149210
pub fn platform_fees_withdrawn(env: &Env, to: Address, amount: i128) {
150211
let topics = (Symbol::new(env, "platform_fees_withdrawn"), to);
151212
env.events().publish(topics, amount);
213+
record_event(env, "platform_fees_withdrawn");
152214
}
153215

154216
pub fn event_fees_withdrawn(env: &Env, admin: Address, to: Address, amount: i128) {
155217
let topics = (Symbol::new(env, "event_fees_withdrawn"), admin, to);
156218
env.events().publish(topics, amount);
219+
record_event(env, "event_fees_withdrawn");
157220
}
158221

159222
pub fn address_blacklisted(env: &Env, admin: Address, address: Address) {
160223
let topics = (Symbol::new(env, "address_blacklisted"), admin);
161224
env.events().publish(topics, address);
225+
record_event(env, "address_blacklisted");
162226
}
163227

164228
pub fn address_unblacklisted(env: &Env, admin: Address, address: Address) {
165229
let topics = (Symbol::new(env, "address_unblacklisted"), admin);
166230
env.events().publish(topics, address);
231+
record_event(env, "address_unblacklisted");
167232
}
168233

169234
pub fn pool_metadata_updated(env: &Env, pool_id: u64, updater: Address, new_metadata_hash: String) {
170235
let topics = (Symbol::new(env, "pool_metadata_updated"), pool_id, updater);
171236
env.events().publish(topics, new_metadata_hash);
237+
record_event(env, "pool_metadata_updated");
172238
}
173239

174240
pub fn platform_fee_bps_set(env: &Env, admin: Address, fee_bps: u32) {
175241
let topics = (Symbol::new(env, "platform_fee_bps_set"), admin);
176242
env.events().publish(topics, fee_bps);
243+
record_event(env, "platform_fee_bps_set");
177244
}
178245

179246
pub fn ticket_sold(
@@ -187,6 +254,7 @@ pub fn ticket_sold(
187254
let topics = (Symbol::new(env, "ticket_sold"), pool_id, buyer);
188255
env.events()
189256
.publish(topics, (price, event_amount, fee_amount));
257+
record_event(env, "ticket_sold");
190258
}
191259

192260
pub fn contract_upgraded(env: &Env, new_wasm_hash: BytesN<32>) {

contract/contract/src/base/types.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
use soroban_sdk::{contracttype, Address, BytesN, String, Vec};
22

3+
/// A lightweight record stored for every emitted contract event.
4+
/// Used to populate the global `AllEvents` list.
5+
#[contracttype]
6+
#[derive(Clone, Debug, Eq, PartialEq)]
7+
pub struct EventRecord {
8+
/// Sequential index (1-based) assigned at emission time.
9+
pub index: u64,
10+
/// Short name matching the event's topic Symbol (e.g. "pool_created").
11+
pub name: String,
12+
/// Ledger timestamp at the moment the event was emitted.
13+
pub timestamp: u64,
14+
}
15+
316
#[contracttype]
417
#[derive(Clone, Debug, Eq, PartialEq)]
518
pub struct CampaignDetails {

contract/contract/src/crowdfunding.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,20 @@ impl CrowdfundingTrait for CrowdfundingContract {
19361936
events::contract_upgraded(&env, new_wasm_hash);
19371937
Ok(())
19381938
}
1939+
1940+
fn get_all_events_count(env: Env) -> u64 {
1941+
env.storage()
1942+
.persistent()
1943+
.get::<_, u64>(&StorageKey::AllEventsCount)
1944+
.unwrap_or(0)
1945+
}
1946+
1947+
fn get_all_events(env: Env) -> Vec<crate::base::types::EventRecord> {
1948+
env.storage()
1949+
.persistent()
1950+
.get::<_, Vec<crate::base::types::EventRecord>>(&StorageKey::AllEvents)
1951+
.unwrap_or_else(|| Vec::new(&env))
1952+
}
19391953
}
19401954

19411955
impl CrowdfundingContract {

contract/contract/src/interfaces/crowdfunding.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,10 @@ pub trait CrowdfundingTrait {
221221
) -> Result<(i128, i128), CrowdfundingError>;
222222

223223
fn upgrade_contract(env: Env, new_wasm_hash: BytesN<32>) -> Result<(), CrowdfundingError>;
224+
225+
/// Returns the total number of events ever emitted by this contract.
226+
fn get_all_events_count(env: Env) -> u64;
227+
228+
/// Returns the full list of emitted event records (index, name, timestamp).
229+
fn get_all_events(env: Env) -> Vec<crate::base::types::EventRecord>;
224230
}

0 commit comments

Comments
 (0)