Skip to content

Commit 97232f5

Browse files
committed
integrates decoding into log extraction
1 parent c626b89 commit 97232f5

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

crates/core/src/instruction.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
2222
use {
2323
crate::{
24-
error::CarbonResult, filter::Filter, metrics::MetricsCollection, processor::Processor,
25-
transaction::TransactionMetadata,
24+
deserialize::CarbonDeserialize, error::CarbonResult, filter::Filter,
25+
metrics::MetricsCollection, processor::Processor, transaction::TransactionMetadata,
2626
},
2727
async_trait::async_trait,
2828
serde::{Deserialize, Serialize},
@@ -70,14 +70,31 @@ enum LogType {
7070
}
7171

7272
impl InstructionMetadata {
73+
/// Decodes the log events `T` thrown by this instruction.
74+
///
75+
/// # Parameters
76+
///
77+
/// - `T`: The event type to decode the logs into.
78+
///
79+
/// # Returns
80+
///
81+
/// All successfull events of the type `T` decoded from the logs of the instruction.
82+
pub fn decode_log_events<T: CarbonDeserialize>(&self) -> Vec<T> {
83+
self.extract_event_log_data()
84+
.into_iter()
85+
.map(|log| <T as CarbonDeserialize>::deserialize(&mut &log[8..]))
86+
.flatten()
87+
.collect()
88+
}
89+
7390
/// Extracts the `data` from log messages associated with this instruction.
7491
///
7592
/// This method filters the transaction's log messages to return only those
7693
/// that correspond to the current instruction, based on its stack height and
7794
/// absolute path within the instruction stack.
7895
///
7996
/// Returns `Vec<Vec<u8>>` containing the `data` bytes (base64 encoded) from log messages.
80-
pub fn extract_event_log_data(&self) -> Vec<Vec<u8>> {
97+
fn extract_event_log_data(&self) -> Vec<Vec<u8>> {
8198
let logs = match &self.transaction_metadata.meta.log_messages {
8299
Some(logs) => logs,
83100
None => return Vec::new(),

examples/log-events-example/src/main.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use {
22
async_trait::async_trait,
33
carbon_core::{
4-
borsh::BorshDeserialize,
54
error::CarbonResult,
65
instruction::{DecodedInstruction, InstructionMetadata, NestedInstructions},
76
metrics::MetricsCollection,
@@ -100,22 +99,10 @@ impl Processor for RaydiumCpmmInstructionProcessor {
10099
(metadata, _, _, _): Self::InputType,
101100
_metrics: Arc<MetricsCollection>,
102101
) -> CarbonResult<()> {
103-
let logs = metadata.extract_event_log_data();
102+
let logs = metadata.decode_log_events::<SwapEvent>();
104103

105104
if !logs.is_empty() {
106-
println!("Signature: {:?}", metadata.transaction_metadata.signature);
107-
println!(
108-
"Processing instruction - Index: {}, Stack Height: {}, Path: {:?}",
109-
metadata.index, metadata.stack_height, metadata.absolute_path
110-
);
111-
for log in logs {
112-
if log.len() > 8 {
113-
if let Ok(swap_event) = SwapEvent::deserialize(&mut &log[8..]) {
114-
println!("SwapEvent: {:?}", swap_event);
115-
}
116-
}
117-
}
118-
println!("--------------------------------");
105+
println!("Swap Events: {:?}", logs);
119106
}
120107

121108
Ok(())

0 commit comments

Comments
 (0)