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
648pub 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
1861pub 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
4792pub 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
5298pub 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
57104pub 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
62110pub 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
67116pub 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
72122pub 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
77128pub 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
82134pub 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
87140pub 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
101155pub 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
112167pub 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
117173pub 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
122179pub 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
127185pub 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
132191pub 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
144204pub 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
149210pub 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
154216pub 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
159222pub 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
164228pub 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
169234pub 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
174240pub 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
179246pub 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
192260pub fn contract_upgraded ( env : & Env , new_wasm_hash : BytesN < 32 > ) {
0 commit comments