use serde::{Deserialize, Serialize};
use serde_json::Value;
pub const NULL_ACCOUNT: &str = "0x0000000000000000000000000000000000000000";
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NormalizedBlock {
pub height: u64,
pub hash: String,
pub parent_hash: String,
pub timestamp: u64,
pub producer: Option<String>,
pub transactions: Vec<NormalizedTx>,
#[serde(default)]
pub extra: Value,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NormalizedTx {
pub id: String,
pub block_height: Option<u64>,
pub block_hash: Option<String>,
pub index: Option<u64>,
pub from: Option<String>,
pub to: Option<String>,
pub value: u128,
pub fee: Option<u128>,
pub success: Option<bool>,
#[serde(default)]
pub input: Vec<u8>,
#[serde(default)]
pub events: Vec<NormalizedEvent>,
#[serde(default)]
pub extra: Value,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NormalizedEvent {
pub emitter: Option<String>,
pub signature: String,
pub indexed: Vec<String>,
pub values: Vec<EventValue>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum EventValue {
Uint(u128),
Int(i128),
}
impl NormalizedEvent {
pub const TRANSFER: &'static str = "Transfer(address,address,uint256)";
pub const BALANCE_CHANGE: &'static str = "BalanceChange(address,int256)";
pub fn transfer(emitter: Option<String>, from: String, to: String, amount: u128) -> Self {
Self {
emitter,
signature: Self::TRANSFER.into(),
indexed: vec![from, to],
values: vec![EventValue::Uint(amount)],
}
}
pub fn balance_change(emitter: Option<String>, account: String, delta: i128) -> Self {
Self {
emitter,
signature: Self::BALANCE_CHANGE.into(),
indexed: vec![account],
values: vec![EventValue::Int(delta)],
}
}
}