Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
iankressin authored Nov 14, 2024
2 parents 600ee1e + 96a8f31 commit 7e7a071
Show file tree
Hide file tree
Showing 12 changed files with 598 additions and 347 deletions.
355 changes: 268 additions & 87 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eql_core"
version = "0.1.19"
version = "0.1.20"
rust-version.workspace = true
edition.workspace = true
authors.workspace = true
Expand All @@ -10,7 +10,8 @@ repository = "https://github.com/iankressin/eql"
readme = "README.md"

[dependencies]
alloy = { version = "0.2", features = ["std", "contract", "provider-http", "network", "rpc-types"] }
alloy = { version = "0.6.4", features = ["std", "contract", "provider-http", "network", "rpc-types"] }
alloy-eip7702 = "0.4.1"
pest = "2.7.10"
pest_derive = "2.6"
tokio = { version = "1", features = ["macros", "rt"] }
Expand Down
43 changes: 19 additions & 24 deletions crates/core/src/common/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::interpreter::frontend::parser::Rule;
use alloy::{
eips::BlockNumberOrTag,
providers::{Provider, RootProvider},
rpc::types::BlockTransactionsKind,
transports::http::{Client, Http},
};
use anyhow::Result;
Expand Down Expand Up @@ -303,15 +304,10 @@ impl BlockRange {
provider: &Arc<RootProvider<Http<Client>>>,
) -> Result<Vec<u64>> {
let (start_block, end_block) = self.range();
let start_block_number = self
.get_block_number_from_tag(provider.clone(), start_block)
.await?;
let start_block_number = get_block_number_from_tag(provider.clone(), &start_block).await?;

let end_block_number = match end_block {
Some(end) => Some(
self.get_block_number_from_tag(provider.clone(), end)
.await?,
),
Some(end) => Some(get_block_number_from_tag(provider.clone(), &end).await?),
None => None,
};

Expand All @@ -329,23 +325,6 @@ impl BlockRange {
None => Ok(vec![start_block_number]),
}
}

async fn get_block_number_from_tag(
&self,
provider: Arc<RootProvider<Http<Client>>>,
number_or_tag: BlockNumberOrTag,
) -> Result<u64> {
match number_or_tag {
BlockNumberOrTag::Number(number) => Ok(number),
block_tag => match provider.get_block_by_number(block_tag, false).await? {
Some(block) => match block.header.number {
Some(number) => Ok(number),
None => Err(BlockRangeError::UnableToFetchBlockNumber(number_or_tag).into()),
},
None => Err(BlockRangeError::UnableToFetchBlockNumber(number_or_tag).into()),
},
}
}
}

impl Display for BlockRange {
Expand All @@ -366,3 +345,19 @@ impl Display for BlockRange {
}
}
}

pub async fn get_block_number_from_tag(
provider: Arc<RootProvider<Http<Client>>>,
number_or_tag: &BlockNumberOrTag,
) -> Result<u64> {
match number_or_tag {
BlockNumberOrTag::Number(number) => Ok(*number),
block_tag => match provider
.get_block_by_number(*block_tag, BlockTransactionsKind::Hashes)
.await?
{
Some(block) => Ok(block.header.number),
None => Err(BlockRangeError::UnableToFetchBlockNumber(number_or_tag.clone()).into()),
},
}
}
184 changes: 165 additions & 19 deletions crates/core/src/common/query_result.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::common::chain::Chain;
use alloy::primitives::{Address, Bloom, Bytes, FixedBytes, B256, U256};
use alloy_eip7702::SignedAuthorization;
use serde::ser::SerializeStruct;
use serde::{Deserialize, Serialize, Serializer};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -43,10 +45,10 @@ pub struct BlockQueryRes {
pub extra_data: Option<Bytes>,
pub mix_hash: Option<B256>,
pub total_difficulty: Option<U256>,
pub base_fee_per_gas: Option<u128>,
pub base_fee_per_gas: Option<u64>,
pub withdrawals_root: Option<B256>,
pub blob_gas_used: Option<u128>,
pub excess_blob_gas: Option<u128>,
pub blob_gas_used: Option<u64>,
pub excess_blob_gas: Option<u64>,
pub parent_beacon_block_root: Option<B256>,
}

Expand Down Expand Up @@ -99,51 +101,195 @@ impl Default for AccountQueryRes {
}

#[serde_with::skip_serializing_none]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
#[derive(Debug, PartialEq, Eq, Deserialize, Clone)]
pub struct TransactionQueryRes {
pub chain: Option<Chain>,
pub transaction_type: Option<u8>,
pub r#type: Option<u8>,
pub hash: Option<FixedBytes<32>>,
pub from: Option<Address>,
pub to: Option<Address>,
pub data: Option<Bytes>,
#[serde(serialize_with = "serialize_option_u256")]
pub value: Option<U256>,
pub gas_price: Option<u128>,
pub gas: Option<u128>,
pub gas_limit: Option<u64>,
pub effective_gas_price: Option<u128>,
pub status: Option<bool>,
pub chain_id: Option<u64>,
pub v: Option<U256>,
pub v: Option<bool>,
pub r: Option<U256>,
pub s: Option<U256>,
pub max_fee_per_blob_gas: Option<u128>,
pub max_fee_per_gas: Option<u128>,
pub max_priority_fee_per_gas: Option<u128>,
pub y_parity: Option<bool>,
pub authorization_list: Option<Vec<SignedAuthorization>>,
}

impl Default for TransactionQueryRes {
fn default() -> Self {
Self {
chain: None,
transaction_type: None,
r#type: None,
hash: None,
from: None,
to: None,
data: None,
value: None,
gas_price: None,
gas: None,
gas_limit: None,
status: None,
chain_id: None,
v: None,
r: None,
s: None,
effective_gas_price: None,
max_fee_per_blob_gas: None,
max_fee_per_gas: None,
max_priority_fee_per_gas: None,
y_parity: None,
authorization_list: None,
}
}
}

impl TransactionQueryRes {
pub fn has_value(&self) -> bool {
self.chain.is_some()
|| self.r#type.is_some()
|| self.hash.is_some()
|| self.from.is_some()
|| self.to.is_some()
|| self.data.is_some()
|| self.value.is_some()
|| self.gas_price.is_some()
|| self.gas_limit.is_some()
|| self.effective_gas_price.is_some()
|| self.status.is_some()
|| self.chain_id.is_some()
|| self.v.is_some()
|| self.r.is_some()
|| self.s.is_some()
|| self.max_fee_per_blob_gas.is_some()
|| self.max_fee_per_gas.is_some()
|| self.max_priority_fee_per_gas.is_some()
|| self.y_parity.is_some()
|| self.authorization_list.is_some()
}

fn get_field_values(&self) -> Vec<(&'static str, String)> {
let mut fields = Vec::new();
if let Some(chain) = &self.chain {
fields.push(("chain", Some(chain.to_string())));
}
if let Some(r#type) = self.r#type {
fields.push(("type", Some(r#type.to_string())));
}
if let Some(hash) = &self.hash {
fields.push(("hash", Some(format!("{hash:?}"))));
}
if let Some(from) = &self.from {
fields.push(("from", Some(from.to_string())));
}
if let Some(to) = &self.to {
fields.push(("to", Some(to.to_string())));
}
if let Some(data) = &self.data {
fields.push(("data", Some(format!("{data:?}"))));
}
if let Some(value) = &self.value {
fields.push(("value", Some(value.to_string())));
}
if let Some(gas_price) = self.gas_price {
fields.push(("gas_price", Some(gas_price.to_string())));
}
if let Some(gas_limit) = self.gas_limit {
fields.push(("gas_limit", Some(gas_limit.to_string())));
}
if let Some(effective_gas_price) = self.effective_gas_price {
fields.push(("effective_gas_price", Some(effective_gas_price.to_string())));
}
if let Some(status) = self.status {
fields.push(("status", Some(status.to_string())));
}
if let Some(chain_id) = self.chain_id {
fields.push(("chain_id", Some(chain_id.to_string())));
}
if let Some(v) = self.v {
fields.push(("v", Some(v.to_string())));
}
if let Some(r) = &self.r {
fields.push(("r", Some(r.to_string())));
}
if let Some(s) = &self.s {
fields.push(("s", Some(s.to_string())));
}
if let Some(max_fee_per_blob_gas) = self.max_fee_per_blob_gas {
fields.push((
"max_fee_per_blob_gas",
Some(max_fee_per_blob_gas.to_string()),
));
}
if let Some(max_fee_per_gas) = self.max_fee_per_gas {
fields.push(("max_fee_per_gas", Some(max_fee_per_gas.to_string())));
}
if let Some(max_priority_fee_per_gas) = self.max_priority_fee_per_gas {
fields.push((
"max_priority_fee_per_gas",
Some(max_priority_fee_per_gas.to_string()),
));
}
if let Some(y_parity) = self.y_parity {
fields.push(("y_parity", Some(y_parity.to_string())));
}

if let Some(auths) = &self.authorization_list {
for (i, auth) in auths.iter().enumerate() {
fields.push((
Box::leak(format!("authorization_list_{i}_chain_id").into_boxed_str()),
Some(auth.chain_id.to_string()),
));
fields.push((
Box::leak(format!("authorization_list_{i}_address").into_boxed_str()),
Some(auth.address.to_string()),
));
fields.push((
Box::leak(format!("authorization_list_{i}_r").into_boxed_str()),
Some(format!("{:?}", auth.r())),
));
fields.push((
Box::leak(format!("authorization_list_{i}_s").into_boxed_str()),
Some(format!("{:?}", auth.s())),
));
fields.push((
Box::leak(format!("authorization_list_{i}_y_parity").into_boxed_str()),
Some(format!("{:?}", auth.y_parity())),
));
fields.push((
Box::leak(format!("authorization_list_{i}_nonce").into_boxed_str()),
Some(auth.nonce.to_string()),
));
}
}

fields
.into_iter()
.map(|(name, value)| (name, value.unwrap_or_default()))
.collect()
}
}

impl Serialize for TransactionQueryRes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let fields = self.get_field_values();
let mut state = serializer.serialize_struct("TransactionQueryRes", fields.len())?;
for (field_name, value) in fields {
state.serialize_field(field_name, &value)?;
}
state.end()
}
}

Expand Down Expand Up @@ -187,6 +333,16 @@ impl Default for LogQueryRes {
}
}

fn serialize_option_u256<S>(option: &Option<U256>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match option {
Some(u256) => serializer.serialize_some(&u256.to_string()),
None => serializer.serialize_none(),
}
}

#[cfg(test)]
mod test {
use std::str::FromStr;
Expand All @@ -210,13 +366,3 @@ mod test {
assert_eq!("{\"value\":\"100\"}", u256_str);
}
}

fn serialize_option_u256<S>(option: &Option<U256>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match option {
Some(u256) => serializer.serialize_some(&u256.to_string()),
None => serializer.serialize_none(),
}
}
Loading

0 comments on commit 7e7a071

Please sign in to comment.