ccnext_query_builder/abi/field_mapping/
mod.rs

1use crate::abi::models::QueryableFields;
2
3use alloy::{consensus::TxType, dyn_abi::DynSolType};
4use ccnext_abi_encoding::common::EncodingVersion;
5
6mod v1;
7
8pub type Field = (QueryableFields, DynSolType);
9
10pub struct Chunk {
11    pub fields: Vec<Field>,
12}
13
14impl Chunk {
15    pub fn common_fields() -> Self {
16        Self {
17            fields: vec![
18                (QueryableFields::TxNonce, DynSolType::Uint(64)),
19                (QueryableFields::TxGasLimit, DynSolType::Uint(64)),
20                (QueryableFields::TxFrom, DynSolType::Address),
21                (QueryableFields::TxToIsNull, DynSolType::Bool),
22                (QueryableFields::TxTo, DynSolType::Address),
23                (QueryableFields::TxValue, DynSolType::Uint(256)),
24                (QueryableFields::TxData, DynSolType::Bytes),
25            ],
26        }
27    }
28
29    pub fn receipt_fields() -> Self {
30        Self {
31            fields: vec![
32                (QueryableFields::RxStatus, DynSolType::Uint(8)),
33                (QueryableFields::RxGasUsed, DynSolType::Uint(64)),
34                (
35                    QueryableFields::RxLogs,
36                    DynSolType::Array(
37                        DynSolType::Tuple(vec![
38                            DynSolType::Address,
39                            DynSolType::Array(DynSolType::FixedBytes(32).into()),
40                            DynSolType::Bytes,
41                        ])
42                        .into(),
43                    ),
44                ),
45                (QueryableFields::RxLogBlooms, DynSolType::Bytes),
46            ],
47        }
48    }
49
50    pub fn get_fields(&self) -> Vec<QueryableFields> {
51        self.fields.iter().map(|field| field.0.clone()).collect()
52    }
53
54    pub fn get_types(&self) -> Vec<DynSolType> {
55        self.fields.iter().map(|field| field.1.clone()).collect()
56    }
57
58    pub fn get_type(&self) -> DynSolType {
59        DynSolType::Tuple(self.fields.iter().map(|field| field.1.clone()).collect())
60    }
61}
62
63pub struct MappedEncodedFields {
64    pub chunks: Vec<Chunk>,
65}
66
67impl MappedEncodedFields {
68    pub fn get_all_fields(&self) -> Vec<QueryableFields> {
69        let mut all_fields = Vec::new();
70        for chunk in &self.chunks {
71            for field in &chunk.fields {
72                all_fields.push(field.0.clone());
73            }
74        }
75        all_fields
76    }
77
78    pub fn get_all_types(&self) -> Vec<DynSolType> {
79        let mut all_types = Vec::new();
80        for chunk in &self.chunks {
81            for field in &chunk.fields {
82                all_types.push(field.1.clone());
83            }
84        }
85        all_types
86    }
87}
88
89pub fn get_all_fields_for_transaction(
90    tx_type: TxType,
91    encoding: EncodingVersion,
92) -> MappedEncodedFields {
93    match encoding {
94        EncodingVersion::V1 => v1::get_mapped_field_for_type(tx_type),
95    }
96}