diff --git a/crates/torii/core/src/executor.rs b/crates/torii/core/src/executor.rs index 38d137022d..4626daf147 100644 --- a/crates/torii/core/src/executor.rs +++ b/crates/torii/core/src/executor.rs @@ -14,8 +14,8 @@ use tracing::{debug, error}; use crate::simple_broker::SimpleBroker; use crate::types::{ - Entity as EntityUpdated, Event as EventEmitted, EventMessage as EventMessageUpdated, - IndexerUpdate, Model as ModelRegistered, + Contract as ContractUpdated, Entity as EntityUpdated, Event as EventEmitted, + EventMessage as EventMessageUpdated, Model as ModelRegistered, }; pub(crate) const LOG_TARGET: &str = "torii_core::executor"; @@ -31,7 +31,7 @@ pub enum Argument { #[derive(Debug, Clone)] pub enum BrokerMessage { - SetHead(IndexerUpdate), + SetHead(ContractUpdated), ModelRegistered(ModelRegistered), EntityUpdated(EntityUpdated), EventMessageUpdated(EventMessageUpdated), @@ -208,7 +208,13 @@ impl<'c> Executor<'c> { format!("Failed to execute query: {:?}, args: {:?}", statement, arguments) })?; - self.publish_queue.push(BrokerMessage::SetHead(IndexerUpdate { + sqlx::query("UPDATE contracts SET tps = ? WHERE id = ?") + .bind(tps as i64) + .bind(format!("{:#x}", set_head.contract_address)) + .execute(&mut **tx) + .await?; + + self.publish_queue.push(BrokerMessage::SetHead(ContractUpdated { head: set_head.head, tps, last_block_timestamp: set_head.last_block_timestamp, diff --git a/crates/torii/core/src/types.rs b/crates/torii/core/src/types.rs index 9e20b881c2..29dedc8659 100644 --- a/crates/torii/core/src/types.rs +++ b/crates/torii/core/src/types.rs @@ -85,8 +85,9 @@ pub struct Event { pub created_at: DateTime, } -#[derive(Debug, Clone)] -pub struct IndexerUpdate { +#[derive(FromRow, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct Contract { pub head: u64, pub tps: u64, pub last_block_timestamp: u64, diff --git a/crates/torii/grpc/src/server/subscriptions/indexer.rs b/crates/torii/grpc/src/server/subscriptions/indexer.rs index c1008d428e..718f1d7eef 100644 --- a/crates/torii/grpc/src/server/subscriptions/indexer.rs +++ b/crates/torii/grpc/src/server/subscriptions/indexer.rs @@ -11,7 +11,7 @@ use tokio::sync::mpsc::{channel, Receiver, Sender}; use tokio::sync::RwLock; use torii_core::error::Error; use torii_core::simple_broker::SimpleBroker; -use torii_core::types::IndexerUpdate; +use torii_core::types::Contract as ContractUpdated; use tracing::{error, trace}; use crate::proto; @@ -67,17 +67,17 @@ impl IndexerManager { #[allow(missing_debug_implementations)] pub struct Service { subs_manager: Arc, - simple_broker: Pin + Send>>, + simple_broker: Pin + Send>>, } impl Service { pub fn new(subs_manager: Arc) -> Self { - Self { subs_manager, simple_broker: Box::pin(SimpleBroker::::subscribe()) } + Self { subs_manager, simple_broker: Box::pin(SimpleBroker::::subscribe()) } } async fn publish_updates( subs: Arc, - update: &IndexerUpdate, + update: &ContractUpdated, ) -> Result<(), Error> { let mut closed_stream = Vec::new(); diff --git a/crates/torii/migrations/20240923155431_last_block_timestamp.sql b/crates/torii/migrations/20240923155431_last_block_timestamp.sql deleted file mode 100644 index 626ddd4288..0000000000 --- a/crates/torii/migrations/20240923155431_last_block_timestamp.sql +++ /dev/null @@ -1,2 +0,0 @@ --- Add last_block_timestamp column for TPS calculation -ALTER TABLE contracts ADD COLUMN last_block_timestamp INTEGER; \ No newline at end of file diff --git a/crates/torii/migrations/20240923155431_tps.sql b/crates/torii/migrations/20240923155431_tps.sql new file mode 100644 index 0000000000..231b4c4edc --- /dev/null +++ b/crates/torii/migrations/20240923155431_tps.sql @@ -0,0 +1,3 @@ +-- Add last_block_timestamp column for TPS calculation +ALTER TABLE contracts ADD COLUMN last_block_timestamp INTEGER; +ALTER TABLE contracts ADD COLUMN tps INTEGER; \ No newline at end of file