From 4400ea9a6498417d7a239790b68be560ec834250 Mon Sep 17 00:00:00 2001 From: remiroyc Date: Mon, 8 Apr 2024 14:11:17 +0200 Subject: [PATCH 1/4] feat(api): Enhance MongoDB query for accurate total count of bridged tokens --- apps/indexer/src/storage/mongo/event_store.rs | 32 +++---------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/apps/indexer/src/storage/mongo/event_store.rs b/apps/indexer/src/storage/mongo/event_store.rs index c4b98d2e..d702b553 100644 --- a/apps/indexer/src/storage/mongo/event_store.rs +++ b/apps/indexer/src/storage/mongo/event_store.rs @@ -27,37 +27,15 @@ impl EventStore for MongoStore { &self, eth_contract_address: &str, ) -> Result { - let pipeline = vec![ - doc! { - "$match": { - "collection_src": eth_contract_address, - } - }, - doc! { - "$project": { - "number_of_tokens": { "$size": "$token_ids" } - } - }, - doc! { - "$group": { - "_id": null, - "total_token_ids": { "$sum": "$number_of_tokens" } - } - }, - ]; + let filter = doc! { + "collection_src": eth_contract_address, + }; - let mut cursor = self + let total_count = self .starknet_bridge_requests - .aggregate(pipeline, None) + .count_documents(filter, None) .await?; - let mut total_count: u64 = 0; - if let Some(doc) = cursor.try_next().await? { - if let Ok(count) = doc.get_i64("total_token_ids") { - total_count = count as u64; - } - } - Ok(total_count) } From de17b2c0208aa01446064b237b679e5366d4f54b Mon Sep 17 00:00:00 2001 From: remiroyc Date: Mon, 8 Apr 2024 14:25:52 +0200 Subject: [PATCH 2/4] fix(api): mongo aggregation --- apps/indexer/src/storage/mongo/event_store.rs | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/apps/indexer/src/storage/mongo/event_store.rs b/apps/indexer/src/storage/mongo/event_store.rs index d702b553..51dd5036 100644 --- a/apps/indexer/src/storage/mongo/event_store.rs +++ b/apps/indexer/src/storage/mongo/event_store.rs @@ -1,7 +1,7 @@ use anyhow::Result; use async_trait::async_trait; use futures::TryStreamExt; -use mongodb::bson::doc; +use mongodb::{bson::doc, options::AggregateOptions}; use super::MongoStore; use crate::storage::{store::EventStore, Event}; @@ -27,16 +27,40 @@ impl EventStore for MongoStore { &self, eth_contract_address: &str, ) -> Result { - let filter = doc! { - "collection_src": eth_contract_address, - }; + let pipeline = vec![ + doc! { + "$match": { + "collection_src": eth_contract_address, + } + }, + doc! { + "$unwind": "$token_ids" + }, + doc! { + "$group": { + "_id": null, + "total_tokens": { + "$sum": 1 + } + } + }, + ]; - let total_count = self + let mut cursor = self .starknet_bridge_requests - .count_documents(filter, None) + .aggregate(pipeline, AggregateOptions::default()) .await?; - Ok(total_count) + let mut total_tokens: u64 = 0; + while let Some(doc) = cursor.try_next().await? { + log::info!("doc: {:?}", doc); + + if let Ok(total) = doc.get_i32("total_tokens") { + total_tokens += total as u64; + }; + } + + Ok(total_tokens) } /// From 24638811b26d6ce864b6b6748a9a019701e4ed18 Mon Sep 17 00:00:00 2001 From: remiroyc Date: Mon, 8 Apr 2024 15:13:31 +0200 Subject: [PATCH 3/4] fix: remove log info --- apps/indexer/src/storage/mongo/event_store.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/apps/indexer/src/storage/mongo/event_store.rs b/apps/indexer/src/storage/mongo/event_store.rs index 51dd5036..79e8905f 100644 --- a/apps/indexer/src/storage/mongo/event_store.rs +++ b/apps/indexer/src/storage/mongo/event_store.rs @@ -53,8 +53,6 @@ impl EventStore for MongoStore { let mut total_tokens: u64 = 0; while let Some(doc) = cursor.try_next().await? { - log::info!("doc: {:?}", doc); - if let Ok(total) = doc.get_i32("total_tokens") { total_tokens += total as u64; }; From 7be192c31340821edf45e175d03cafe590f2e286 Mon Sep 17 00:00:00 2001 From: remiroyc Date: Fri, 12 Apr 2024 14:38:41 +0200 Subject: [PATCH 4/4] feat: normalize contract addr --- apps/indexer/src/handlers/requests.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/indexer/src/handlers/requests.rs b/apps/indexer/src/handlers/requests.rs index 0eda43ba..2729561f 100644 --- a/apps/indexer/src/handlers/requests.rs +++ b/apps/indexer/src/handlers/requests.rs @@ -123,9 +123,12 @@ pub async fn contract_stats( Path(eth_contract_address): Path, state: State, ) -> Result, (StatusCode, String)> { + let contract_address = normalize_hex(ð_contract_address) + .expect("Contract address shall be an hexadecimal string"); + let total_tokens_bridged_on_starknet = state .store - .get_total_tokens_bridged_on_starknet(ð_contract_address) + .get_total_tokens_bridged_on_starknet(&contract_address) .await .unwrap_or(0);