From cf8023a3e05f98f7bc5627d1952b4b33e6746495 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Wed, 18 Sep 2024 13:11:12 -0700 Subject: [PATCH] Format contract id in fn_call event topic (#1465) ### What Resolves https://github.com/stellar/rs-soroban-env/issues/1463 Before ``` Event log (newest first): 0: [Diagnostic Event] contract:CAXDPD4A5ANAASQ2U74RBW7ULGB3TS5SGSAFYPN6O2R2IJKGEX22SWUS, topics:[error, Error(Contract, https://github.com/stellar/rs-soroban-env/pull/8)], data:["negative amount is not allowed", -1] 1: [Diagnostic Event] topics:[fn_call, Bytes(2e378f80e81a004a1aa7f910dbf45983b9cbb234805c3dbe76a3a4254625f5a9), mint], data:[GD4YHMIZ7IGW7E4XJ4TXIJPLJU255JXPM2PKTV6H4PA4N5RTM6T2R73D, -1] 2: [Diagnostic Event] contract:CAXDPD4A5ANAASQ2U74RBW7ULGB3TS5SGSAFYPN6O2R2IJKGEX22SWUS, topics:[fn_return, init_asset], data:Void 3: [Diagnostic Event] topics:[fn_call, Bytes(2e378f80e81a004a1aa7f910dbf45983b9cbb234805c3dbe76a3a4254625f5a9), init_asset], data:Bytes(00000001616161610000000000fc065517dbbca3e37ec0e97dbfcd80e6bcdfdac732be1e4152b259f1f7ea88) ``` After ``` Event log (newest first): 0: [Diagnostic Event] contract:CAXDPD4A5ANAASQ2U74RBW7ULGB3TS5SGSAFYPN6O2R2IJKGEX22SWUS, topics:[error, Error(Contract, https://github.com/stellar/rs-soroban-env/pull/8)], data:["negative amount is not allowed", -1] 1: [Diagnostic Event] topics:[fn_call, CAXDPD4A5ANAASQ2U74RBW7ULGB3TS5SGSAFYPN6O2R2IJKGEX22SWUS, mint], data:[GD4YHMIZ7IGW7E4XJ4TXIJPLJU255JXPM2PKTV6H4PA4N5RTM6T2R73D, -1] 2: [Diagnostic Event] contract:CAXDPD4A5ANAASQ2U74RBW7ULGB3TS5SGSAFYPN6O2R2IJKGEX22SWUS, topics:[fn_return, init_asset], data:Void 3: [Diagnostic Event] topics:[fn_call, CAXDPD4A5ANAASQ2U74RBW7ULGB3TS5SGSAFYPN6O2R2IJKGEX22SWUS, init_asset], data:Bytes(00000001616161610000000000fc065517dbbca3e37ec0e97dbfcd80e6bcdfdac732be1e4152b259f1f7ea88) ``` --- soroban-env-host/src/events/mod.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/soroban-env-host/src/events/mod.rs b/soroban-env-host/src/events/mod.rs index fb70e1ac9..b1c880b98 100644 --- a/soroban-env-host/src/events/mod.rs +++ b/soroban-env-host/src/events/mod.rs @@ -123,11 +123,37 @@ impl core::fmt::Display for HostEvent { match &self.event.body { ContractEventBody::V0(ceb) => { write!(f, "topics:[")?; + + let mut is_fn_call = false; for (i, topic) in ceb.topics.iter().enumerate() { if i != 0 { write!(f, ", ")?; } + + // The second topic of the fn_call event is the contract id as ScBytes, + // but we want to display it as a C key instead, so this block + // tries to deduce if the event is the fn_call event. + if i == 1 && is_fn_call { + if let ScVal::Bytes(bytes) = topic { + let try_convert_to_hash = + TryInto::<[u8; 32]>::try_into(bytes.0.clone()); + if let Ok(contract_id) = try_convert_to_hash { + let strkey = stellar_strkey::Contract(contract_id); + write!(f, "{}", strkey)?; + continue; + } + } + } + display_scval(topic, f)?; + + if i == 0 { + if let ScVal::Symbol(first_topic_str) = topic { + if first_topic_str.0.as_slice() == "fn_call".as_bytes() { + is_fn_call = true; + } + } + } } write!(f, "], data:")?; display_scval(&ceb.data, f)