Skip to content

Commit

Permalink
Format contract id in fn_call event topic (#1465)
Browse files Browse the repository at this point in the history
### What

Resolves #1463

Before 
```
Event log (newest first):
0: [Diagnostic Event] contract:CAXDPD4A5ANAASQ2U74RBW7ULGB3TS5SGSAFYPN6O2R2IJKGEX22SWUS, topics:[error, Error(Contract, #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, #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)
```
  • Loading branch information
sisuresh authored Sep 18, 2024
1 parent 02656e6 commit cf8023a
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions soroban-env-host/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cf8023a

Please sign in to comment.