1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::sync::Arc;

use arrow::{
    array::Array,
    datatypes::{Field, Schema},
    error::ArrowError,
    record_batch::RecordBatch,
};
use brontes_types::mev::SearcherTx;
use itertools::Itertools;

use crate::parquet::{
    normalized_actions::{
        gas_details::get_gas_details_array, transfers::get_normalized_transfer_list_array,
    },
    utils::get_string_array_from_owned,
};

pub fn searcher_tx_to_record_batch(
    searcher_txs: Vec<SearcherTx>,
) -> Result<RecordBatch, ArrowError> {
    let tx_hash_array = get_string_array_from_owned(
        searcher_txs
            .iter()
            .map(|tx| Some(tx.tx_hash.to_string()))
            .collect_vec(),
    );

    let transfers_array = get_normalized_transfer_list_array(
        searcher_txs.iter().map(|tx| &tx.transfers).collect_vec(),
    );

    let gas_details_array =
        get_gas_details_array(searcher_txs.iter().map(|tx| tx.gas_details).collect());

    let schema = Schema::new(vec![
        Field::new("tx_hash", tx_hash_array.data_type().clone(), false),
        Field::new("transfers", transfers_array.data_type().clone(), false),
        Field::new("gas_details", gas_details_array.data_type().clone(), false),
    ]);

    RecordBatch::try_new(
        Arc::new(schema),
        vec![Arc::new(tx_hash_array), Arc::new(transfers_array), Arc::new(gas_details_array)],
    )
}