@@ -240,6 +240,61 @@ def record_event(
240240 error = str (e ),
241241 )
242242
243+ def record_structured_event (
244+ self ,
245+ target_contract_id : str ,
246+ event_type : str ,
247+ payload_hash_hex : str ,
248+ schema_version : int ,
249+ correlation_id_hex : str ,
250+ ) -> TransactionResult :
251+ """Submit the SC-38 versioned and correlation-safe event invocation."""
252+ if not self .keypair :
253+ return TransactionResult (False , "" , "error" , error = "No keypair configured" )
254+
255+ try :
256+ payload_hash = bytes .fromhex (payload_hash_hex )
257+ correlation_id = bytes .fromhex (correlation_id_hex )
258+ if len (payload_hash ) != 32 or len (correlation_id ) != 32 :
259+ raise ValueError ("Payload hash and correlation ID must be 32 bytes" )
260+ account = self .server .load_account (self .keypair .public_key )
261+ tx = (
262+ TransactionBuilder (
263+ source_account = account ,
264+ network_passphrase = self .network_passphrase ,
265+ base_fee = 100000 ,
266+ )
267+ .append_invoke_contract_function_op (
268+ contract_id = self .contract_id ,
269+ function_name = "record_structured_event" ,
270+ parameters = [
271+ self ._address_to_sc_val (self .keypair .public_key ),
272+ self ._address_to_sc_val (target_contract_id ),
273+ self ._symbol_to_sc_val (event_type ),
274+ self ._bytes_to_sc_val (payload_hash ),
275+ SCVal (type = SCValType .SCV_U32 , u32 = schema_version ),
276+ self ._bytes_to_sc_val (correlation_id ),
277+ ],
278+ )
279+ .set_timeout (30 )
280+ .build ()
281+ )
282+ simulation = self .server .simulate_transaction (tx )
283+ if simulation .error :
284+ return TransactionResult (False , "" , "simulation_failed" , error = simulation .error )
285+ prepared = self .server .prepare_transaction (tx , simulation )
286+ prepared .sign (self .keypair )
287+ response = self .server .send_transaction (prepared )
288+ return TransactionResult (
289+ success = response .status == "PENDING" ,
290+ tx_hash = response .hash ,
291+ status = response .status ,
292+ result_xdr = getattr (response , "result_xdr" , None ),
293+ )
294+ except Exception as e :
295+ logger .exception ("Failed to record SC-38 structured event" )
296+ return TransactionResult (False , "" , "error" , error = str (e ))
297+
243298 def get_total_events (self ) -> Optional [int ]:
244299 """
245300 Query the total_events function on the contract.
0 commit comments