use alloc::collections::BTreeMap as HashMap;
use cometbft::{abci, block, Block};
use crate::{prelude::*, query::EventType};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Event {
pub query: String,
pub data: EventData,
pub events: Option<HashMap<String, Vec<String>>>,
}
impl Event {
pub fn event_type(&self) -> Option<EventType> {
match self.data {
EventData::NewBlock { .. } => Some(EventType::NewBlock),
EventData::Tx { .. } => Some(EventType::Tx),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EventData {
NewBlock {
block: Option<Box<Block>>,
block_id: block::Id,
result_finalize_block: Option<abci::response::FinalizeBlock>,
},
LegacyNewBlock {
block: Option<Box<Block>>,
result_begin_block: Option<abci::response::BeginBlock>,
result_end_block: Option<abci::response::EndBlock>,
},
Tx {
tx_result: TxInfo,
},
GenericJsonEvent(serde_json::Value),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TxInfo {
pub height: i64,
pub index: Option<i64>,
pub tx: Vec<u8>,
pub result: TxResult,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TxResult {
pub log: Option<String>,
pub gas_wanted: Option<String>,
pub gas_used: Option<String>,
pub events: Vec<abci::Event>,
}
pub mod v0_34 {
use super::{Event, EventData, TxInfo, TxResult};
use crate::dialect::v0_34::Event as RpcEvent;
use crate::prelude::*;
use crate::{dialect, serializers, Response};
use alloc::collections::BTreeMap as HashMap;
use cometbft::Block;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct DialectEvent {
pub query: String,
pub data: DialectEventData,
pub events: Option<HashMap<String, Vec<String>>>,
}
pub type DeEvent = DialectEvent;
pub type SerEvent = DialectEvent;
impl Response for DialectEvent {}
impl From<DialectEvent> for Event {
fn from(msg: DialectEvent) -> Self {
Event {
query: msg.query,
data: msg.data.into(),
events: msg.events,
}
}
}
impl From<Event> for DialectEvent {
fn from(msg: Event) -> Self {
DialectEvent {
query: msg.query,
data: msg.data.into(),
events: msg.events,
}
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type", content = "value")]
#[allow(clippy::large_enum_variant)]
pub enum DialectEventData {
#[serde(alias = "tendermint/event/NewBlock")]
NewBlock {
block: Option<Box<Block>>,
result_begin_block: Option<dialect::BeginBlock<RpcEvent>>,
result_end_block: Option<dialect::EndBlock<RpcEvent>>,
},
#[serde(alias = "tendermint/event/Tx")]
Tx {
#[serde(rename = "TxResult")]
tx_result: DialectTxInfo,
},
GenericJsonEvent(serde_json::Value),
}
impl From<DialectEventData> for EventData {
fn from(msg: DialectEventData) -> Self {
match msg {
DialectEventData::NewBlock {
block,
result_begin_block,
result_end_block,
} => EventData::LegacyNewBlock {
block,
result_begin_block: result_begin_block.map(Into::into),
result_end_block: result_end_block.map(Into::into),
},
DialectEventData::Tx { tx_result } => EventData::Tx {
tx_result: tx_result.into(),
},
DialectEventData::GenericJsonEvent(v) => EventData::GenericJsonEvent(v),
}
}
}
impl From<EventData> for DialectEventData {
fn from(msg: EventData) -> Self {
match msg {
EventData::LegacyNewBlock {
block,
result_begin_block,
result_end_block,
} => DialectEventData::NewBlock {
block,
result_begin_block: result_begin_block.map(Into::into),
result_end_block: result_end_block.map(Into::into),
},
EventData::NewBlock {
block,
block_id: _,
result_finalize_block: _,
} => DialectEventData::NewBlock {
block,
result_begin_block: None,
result_end_block: None,
},
EventData::Tx { tx_result } => DialectEventData::Tx {
tx_result: tx_result.into(),
},
EventData::GenericJsonEvent(v) => DialectEventData::GenericJsonEvent(v),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DialectTxInfo {
#[serde(with = "serializers::from_str")]
pub height: i64,
pub index: Option<i64>,
#[serde(with = "serializers::bytes::base64string")]
pub tx: Vec<u8>,
pub result: DialectTxResult,
}
impl From<DialectTxInfo> for TxInfo {
fn from(msg: DialectTxInfo) -> Self {
TxInfo {
height: msg.height,
index: msg.index,
tx: msg.tx,
result: msg.result.into(),
}
}
}
impl From<TxInfo> for DialectTxInfo {
fn from(msg: TxInfo) -> Self {
DialectTxInfo {
height: msg.height,
index: msg.index,
tx: msg.tx,
result: msg.result.into(),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DialectTxResult {
pub log: Option<String>,
pub gas_wanted: Option<String>,
pub gas_used: Option<String>,
pub events: Vec<RpcEvent>,
}
impl From<DialectTxResult> for TxResult {
fn from(msg: DialectTxResult) -> Self {
TxResult {
log: msg.log,
gas_wanted: msg.gas_wanted,
gas_used: msg.gas_used,
events: msg.events.into_iter().map(Into::into).collect(),
}
}
}
impl From<TxResult> for DialectTxResult {
fn from(msg: TxResult) -> Self {
DialectTxResult {
log: msg.log,
gas_wanted: msg.gas_wanted,
gas_used: msg.gas_used,
events: msg.events.into_iter().map(Into::into).collect(),
}
}
}
}
pub mod v1 {
use super::{Event, EventData, TxInfo, TxResult};
use crate::prelude::*;
use crate::{serializers, Response};
use alloc::collections::BTreeMap as HashMap;
use cometbft::abci::Event as RpcEvent;
use cometbft::{abci, block, Block};
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Debug)]
pub struct DeEvent {
pub query: String,
pub data: DeEventData,
pub events: Option<HashMap<String, Vec<String>>>,
}
impl Response for DeEvent {}
impl From<DeEvent> for Event {
fn from(msg: DeEvent) -> Self {
Event {
query: msg.query,
data: msg.data.into(),
events: msg.events,
}
}
}
#[derive(Deserialize, Debug)]
#[serde(tag = "type", content = "value")]
#[allow(clippy::large_enum_variant)]
pub enum DeEventData {
#[serde(alias = "tendermint/event/NewBlock")]
NewBlock {
block: Option<Box<Block>>,
#[serde(default)]
result_begin_block: Option<abci::response::BeginBlock>,
#[serde(default)]
result_end_block: Option<abci::response::EndBlock>,
#[serde(default)]
block_id: Option<block::Id>,
#[serde(default)]
result_finalize_block: Option<abci::response::FinalizeBlock>,
},
#[serde(alias = "tendermint/event/Tx")]
Tx {
#[serde(rename = "TxResult")]
tx_result: DialectTxInfo,
},
GenericJsonEvent(serde_json::Value),
}
impl From<DeEventData> for EventData {
fn from(msg: DeEventData) -> Self {
match msg {
DeEventData::NewBlock {
block,
block_id: Some(block_id),
result_finalize_block,
result_begin_block: _,
result_end_block: _,
} => EventData::NewBlock {
block,
block_id,
result_finalize_block,
},
DeEventData::NewBlock {
block,
result_begin_block,
result_end_block,
block_id: None,
result_finalize_block: _,
} => EventData::LegacyNewBlock {
block,
result_begin_block: result_begin_block.map(Into::into),
result_end_block: result_end_block.map(Into::into),
},
DeEventData::Tx { tx_result } => EventData::Tx {
tx_result: tx_result.into(),
},
DeEventData::GenericJsonEvent(v) => EventData::GenericJsonEvent(v),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DialectTxInfo {
#[serde(with = "serializers::from_str")]
pub height: i64,
pub index: Option<i64>,
#[serde(with = "serializers::bytes::base64string")]
pub tx: Vec<u8>,
pub result: DialectTxResult,
}
impl From<DialectTxInfo> for TxInfo {
fn from(msg: DialectTxInfo) -> Self {
TxInfo {
height: msg.height,
index: msg.index,
tx: msg.tx,
result: msg.result.into(),
}
}
}
impl From<TxInfo> for DialectTxInfo {
fn from(msg: TxInfo) -> Self {
DialectTxInfo {
height: msg.height,
index: msg.index,
tx: msg.tx,
result: msg.result.into(),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct DialectTxResult {
pub log: Option<String>,
pub gas_wanted: Option<String>,
pub gas_used: Option<String>,
pub events: Vec<RpcEvent>,
}
impl From<DialectTxResult> for TxResult {
fn from(msg: DialectTxResult) -> Self {
TxResult {
log: msg.log,
gas_wanted: msg.gas_wanted,
gas_used: msg.gas_used,
events: msg.events.into_iter().map(Into::into).collect(),
}
}
}
impl From<TxResult> for DialectTxResult {
fn from(msg: TxResult) -> Self {
DialectTxResult {
log: msg.log,
gas_wanted: msg.gas_wanted,
gas_used: msg.gas_used,
events: msg.events.into_iter().map(Into::into).collect(),
}
}
}
}
pub mod v0_37 {
use super::{Event, EventData};
use crate::prelude::*;
use alloc::collections::BTreeMap as HashMap;
use cometbft::{abci, Block};
use serde::Serialize;
pub use super::v1::*;
#[derive(Serialize, Debug)]
pub struct SerEvent {
pub query: String,
pub data: SerEventData,
pub events: Option<HashMap<String, Vec<String>>>,
}
impl From<Event> for SerEvent {
fn from(msg: Event) -> Self {
SerEvent {
query: msg.query,
data: msg.data.into(),
events: msg.events,
}
}
}
#[derive(Serialize, Debug)]
#[serde(tag = "type", content = "value")]
pub enum SerEventData {
#[serde(alias = "tendermint/event/NewBlock")]
NewBlock {
block: Option<Box<Block>>,
result_begin_block: Option<abci::response::BeginBlock>,
result_end_block: Option<abci::response::EndBlock>,
},
#[serde(alias = "tendermint/event/Tx")]
Tx {
#[serde(rename = "TxResult")]
tx_result: DialectTxInfo,
},
GenericJsonEvent(serde_json::Value),
}
impl From<EventData> for SerEventData {
fn from(msg: EventData) -> Self {
match msg {
EventData::NewBlock {
block,
block_id: _,
result_finalize_block: _,
} => SerEventData::NewBlock {
block,
result_begin_block: None,
result_end_block: None,
},
EventData::LegacyNewBlock {
block,
result_begin_block,
result_end_block,
} => SerEventData::NewBlock {
block,
result_begin_block: result_begin_block.map(Into::into),
result_end_block: result_end_block.map(Into::into),
},
EventData::Tx { tx_result } => SerEventData::Tx {
tx_result: tx_result.into(),
},
EventData::GenericJsonEvent(v) => SerEventData::GenericJsonEvent(v),
}
}
}
}
pub mod v0_38 {
use super::{Event, EventData};
use crate::prelude::*;
use alloc::collections::BTreeMap as HashMap;
use cometbft::{abci, block, Block};
use serde::Serialize;
pub use super::v1::*;
#[derive(Serialize, Debug)]
pub struct SerEvent {
pub query: String,
pub data: SerEventData,
pub events: Option<HashMap<String, Vec<String>>>,
}
impl From<Event> for SerEvent {
fn from(msg: Event) -> Self {
SerEvent {
query: msg.query,
data: msg.data.into(),
events: msg.events,
}
}
}
#[derive(Serialize, Debug)]
#[serde(tag = "type", content = "value")]
pub enum SerEventData {
#[serde(alias = "tendermint/event/NewBlock")]
NewBlock {
block: Option<Box<Block>>,
block_id: block::Id,
result_finalize_block: Option<abci::response::FinalizeBlock>,
},
#[serde(alias = "tendermint/event/Tx")]
Tx {
#[serde(rename = "TxResult")]
tx_result: DialectTxInfo,
},
GenericJsonEvent(serde_json::Value),
}
impl From<EventData> for SerEventData {
fn from(msg: EventData) -> Self {
match msg {
EventData::NewBlock {
block,
block_id,
result_finalize_block,
} => SerEventData::NewBlock {
block,
block_id,
result_finalize_block,
},
EventData::LegacyNewBlock {
block,
result_begin_block: _,
result_end_block: _,
} => SerEventData::NewBlock {
block,
block_id: Default::default(),
result_finalize_block: None,
},
EventData::Tx { tx_result } => SerEventData::Tx {
tx_result: tx_result.into(),
},
EventData::GenericJsonEvent(v) => SerEventData::GenericJsonEvent(v),
}
}
}
}