From e4e2f7c7cf43f26ffae6af37b9bfd9509ec39afa Mon Sep 17 00:00:00 2001 From: remiroyc Date: Mon, 8 Apr 2024 14:11:17 +0200 Subject: [PATCH] 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) }