Skip to content

Commit

Permalink
remove generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Nov 6, 2023
1 parent b889862 commit 63e9f86
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
1 change: 1 addition & 0 deletions arbiter-core/data/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"events":{"arbx":{"ApprovalFilter":[{"owner":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","spender":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","amount":"0x1"},{"owner":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","spender":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","amount":"0x1"},{"owner":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","spender":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","amount":"0x1"},{"owner":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","spender":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","amount":"0x1"},{"owner":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","spender":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","amount":"0x1"}]},"arby":{"ApprovalFilter":[{"owner":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","spender":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","amount":"0x1"},{"owner":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","spender":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","amount":"0x1"},{"owner":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","spender":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","amount":"0x1"},{"owner":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","spender":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","amount":"0x1"},{"owner":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","spender":"0x2efdc9eecfee3a776209fcb8e9a83a6b221d74f5","amount":"0x1"}]},"lex":{"PriceChangeFilter":[{"price":"0xde0b6b3a7640000"},{"price":"0xde0b6b3a7640000"},{"price":"0xde0b6b3a7640000"},{"price":"0xde0b6b3a7640000"},{"price":"0xde0b6b3a7640000"}]}},"metadata":{"name":"test"}}
26 changes: 14 additions & 12 deletions arbiter-core/src/data_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ type FilterDecoder =
/// `Sync`, and has a static lifetime.
/// * `E` - Type that implements the `EthLogDecode`, `Debug`, `Serialize`
/// traits, and has a static lifetime.
pub struct EventLogger<M: Serialize + Clone + Send + Sync + 'static> {
pub struct EventLogger {
decoder: FilterDecoder,
receiver: Option<crossbeam_channel::Receiver<Broadcast>>,
directory: Option<String>,
file_name: Option<String>,
metadata: Option<M>,
metadata: Option<Value>,
}

impl<M: Serialize + Clone + Send + Sync + 'static> EventLogger<M> {
impl EventLogger {
/// Constructs a new `EventLogger`.
///
/// # Returns
Expand Down Expand Up @@ -147,14 +147,16 @@ impl<M: Serialize + Clone + Send + Sync + 'static> EventLogger<M> {
///
/// # Arguments
///
/// * `metadata` - The metadata to be stored with the event logs which must implement the `Serialize` trait.
/// * `metadata` - The metadata to be stored with the event logs which must
/// implement the `Serialize` trait.
///
/// # Returns
///
/// The `EventLogger` instance with the specified metadata.
pub fn metadata(mut self, metadata: M) -> Self {
pub fn metadata(mut self, metadata: impl Serialize) -> Result<Self, serde_json::Error> {
let metadata = serde_json::to_value(metadata)?;
self.metadata = Some(metadata);
self
Ok(self)
}

/// Executes the `EventLogger`.
Expand Down Expand Up @@ -183,7 +185,7 @@ impl<M: Serialize + Clone + Send + Sync + 'static> EventLogger<M> {
let file_name = self.file_name.unwrap_or("output".into());
let metadata = self.metadata.clone();
std::thread::spawn(move || {
let mut logs: BTreeMap<String, BTreeMap<String, Vec<Value>>> = BTreeMap::new();
let mut events: BTreeMap<String, BTreeMap<String, Vec<Value>>> = BTreeMap::new();
while let Ok(broadcast) = receiver.recv() {
match broadcast {
Broadcast::StopSignal => {
Expand All @@ -197,11 +199,11 @@ impl<M: Serialize + Clone + Send + Sync + 'static> EventLogger<M> {
// Create a struct to hold both logs and metadata
#[derive(Serialize)]
struct OutputData<T> {
logs: BTreeMap<String, BTreeMap<String, Vec<Value>>>,
events: BTreeMap<String, BTreeMap<String, Vec<Value>>>,
metadata: Option<T>,
}

let data = OutputData { logs, metadata };
let data = OutputData { events, metadata };

// Write the data to the file
serde_json::to_writer(writer, &data).expect("Unable to write data");
Expand All @@ -219,11 +221,11 @@ impl<M: Serialize + Clone + Send + Sync + 'static> EventLogger<M> {
.unwrap();
let event_as_object = event_as_value.as_object().unwrap();

let contract = logs.get(contract_name);
let contract = events.get(contract_name);
if contract.is_none() {
logs.insert(contract_name.clone(), BTreeMap::new());
events.insert(contract_name.clone(), BTreeMap::new());
}
let contract = logs.get_mut(contract_name).unwrap();
let contract = events.get_mut(contract_name).unwrap();

let event_name =
event_as_object.clone().keys().collect::<Vec<&String>>()[0]
Expand Down
17 changes: 15 additions & 2 deletions arbiter-core/src/tests/data_output.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
use serde::Serialize;

use super::*;
use crate::data_collection::EventLogger;

#[derive(Serialize, Clone)]
struct MockMetadata {
pub name: String,
}

#[tokio::test]
async fn data_capture() {
let (env, client) = startup_user_controlled().unwrap();
let (arbx, arby, lex) = deploy_liquid_exchange(client.clone()).await.unwrap();
println!("Deployed contracts");

let metadata = MockMetadata {
name: "test".to_string(),
};

let listener = EventLogger::builder()
.add(arbx.events(), "arbx")
.add(arby.events(), "arby")
.add(lex.events(), "lex");
.add(lex.events(), "lex")
.metadata(metadata)
.unwrap();

listener.run().unwrap();

Expand All @@ -37,5 +50,5 @@ async fn data_capture() {

env.stop().unwrap();
let path = std::env::current_dir().unwrap().join("data");
std::fs::remove_dir_all(path).unwrap();
// std::fs::remove_dir_all(path).unwrap();
}

0 comments on commit 63e9f86

Please sign in to comment.