wip: filters

This commit is contained in:
Georgios Konstantopoulos 2020-05-25 00:15:45 +03:00
parent 946c4912f4
commit fe554c158c
No known key found for this signature in database
GPG Key ID: FA607837CD26EDBC
4 changed files with 77 additions and 3 deletions

View File

@ -28,4 +28,5 @@ pub mod types;
/// Re-export solc for convenience
pub use solc;
mod utils;
/// Various utilities
pub mod utils;

View File

@ -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<bool>,
}
/// Filter
#[derive(Default, Debug, PartialEq, Clone, Serialize)]
pub struct Filter {
/// From Block
#[serde(rename = "fromBlock", skip_serializing_if = "Option::is_none")]
from_block: Option<BlockNumber>,
/// To Block
#[serde(rename = "toBlock", skip_serializing_if = "Option::is_none")]
to_block: Option<BlockNumber>,
/// Address
#[serde(skip_serializing_if = "Option::is_none")]
address: Option<ValueOrArray<Address>>,
/// Topics
#[serde(skip_serializing_if = "Option::is_none")]
topics: Option<Vec<Option<ValueOrArray<H256>>>>,
/// Limit
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<usize>,
}
impl Filter {
pub fn from_block<T: Into<BlockNumber>>(mut self, block: BlockNumber) -> Self {
self.from_block = Some(block.into());
self
}
pub fn to_block<T: Into<BlockNumber>>(mut self, block: BlockNumber) -> Self {
self.to_block = Some(block.into());
self
}
// pub fn address<T: Into<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<T> {
Value(T),
Array(Vec<T>),
}
impl<T> Serialize for ValueOrArray<T>
where
T: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
ValueOrArray::Value(inner) => inner.serialize(serializer),
ValueOrArray::Array(inner) => inner.serialize(serializer),
}
}
}

View File

@ -20,4 +20,4 @@ mod block;
pub use block::{Block, BlockId, BlockNumber};
mod log;
pub use log::Log;
pub use log::{Filter, Log};

View File

@ -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: serde::Serialize>(t: &T) -> serde_json::Value {
serde_json::to_value(t).expect("Types never fail to serialize.")