diff --git a/src/lib.rs b/src/lib.rs index 549fdd38..8730b1a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,4 +28,5 @@ pub mod types; /// Re-export solc for convenience pub use solc; -mod utils; +/// Various utilities +pub mod utils; diff --git a/src/types/log.rs b/src/types/log.rs index 3865bba1..c2c15b12 100644 --- a/src/types/log.rs +++ b/src/types/log.rs @@ -1,4 +1,4 @@ -use crate::types::{Address, Bytes, H256, U256, U64}; +use crate::types::{Address, BlockNumber, Bytes, H256, U256, U64}; use serde::{Deserialize, Serialize}; /// A log produced by a transaction. @@ -49,3 +49,65 @@ pub struct Log { /// false if its a valid log. pub removed: Option, } + +/// Filter +#[derive(Default, Debug, PartialEq, Clone, Serialize)] +pub struct Filter { + /// From Block + #[serde(rename = "fromBlock", skip_serializing_if = "Option::is_none")] + from_block: Option, + /// To Block + #[serde(rename = "toBlock", skip_serializing_if = "Option::is_none")] + to_block: Option, + /// Address + #[serde(skip_serializing_if = "Option::is_none")] + address: Option>, + /// Topics + #[serde(skip_serializing_if = "Option::is_none")] + topics: Option>>>, + /// Limit + #[serde(skip_serializing_if = "Option::is_none")] + limit: Option, +} + +impl Filter { + pub fn from_block>(mut self, block: BlockNumber) -> Self { + self.from_block = Some(block.into()); + self + } + + pub fn to_block>(mut self, block: BlockNumber) -> Self { + self.to_block = Some(block.into()); + self + } + + // pub fn address>(mut self, block: BlockNumber) -> Self { + // self.to_block = Some(block.into()); + // self + // } + pub fn limit(mut self, limit: usize) -> Self { + self.limit = Some(limit); + self + } +} + +#[derive(Debug, PartialEq, Clone)] +pub enum ValueOrArray { + Value(T), + Array(Vec), +} + +impl Serialize for ValueOrArray +where + T: Serialize, +{ + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + match self { + ValueOrArray::Value(inner) => inner.serialize(serializer), + ValueOrArray::Array(inner) => inner.serialize(serializer), + } + } +} diff --git a/src/types/mod.rs b/src/types/mod.rs index e903cc49..3dffa088 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -20,4 +20,4 @@ mod block; pub use block::{Block, BlockId, BlockNumber}; mod log; -pub use log::Log; +pub use log::{Filter, Log}; diff --git a/src/utils.rs b/src/utils.rs index c8e5551d..7db0cd0e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -31,6 +31,17 @@ pub fn keccak256(bytes: &[u8]) -> [u8; 32] { output } +/// Gets the first 4 bytes +pub fn id(name: &str) -> [u8; 4] { + let mut output = [0u8; 4]; + + let mut hasher = Keccak::v256(); + hasher.update(name.as_bytes()); + hasher.finalize(&mut output); + + output +} + /// Serialize a type. Panics if the type is returns error during serialization. pub fn serialize(t: &T) -> serde_json::Value { serde_json::to_value(t).expect("Types never fail to serialize.")