Skip to content

Commit

Permalink
fix(api): mongodb query to return the total count of bridged tokens (#…
Browse files Browse the repository at this point in the history
…181)

* feat(api): Enhance MongoDB query for accurate total count of bridged tokens

* fix(api): mongo aggregation

* fix: remove log info

* feat: normalize contract addr
  • Loading branch information
remiroyc authored Apr 12, 2024
1 parent ae66d48 commit 11bf920
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
5 changes: 4 additions & 1 deletion apps/indexer/src/handlers/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,12 @@ pub async fn contract_stats(
Path(eth_contract_address): Path<String>,
state: State<AppState>,
) -> Result<Json<Stats>, (StatusCode, String)> {
let contract_address = normalize_hex(&eth_contract_address)
.expect("Contract address shall be an hexadecimal string");

let total_tokens_bridged_on_starknet = state
.store
.get_total_tokens_bridged_on_starknet(&eth_contract_address)
.get_total_tokens_bridged_on_starknet(&contract_address)
.await
.unwrap_or(0);

Expand Down
24 changes: 12 additions & 12 deletions apps/indexer/src/storage/mongo/event_store.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -34,31 +34,31 @@ impl EventStore for MongoStore {
}
},
doc! {
"$project": {
"number_of_tokens": { "$size": "$token_ids" }
}
"$unwind": "$token_ids"
},
doc! {
"$group": {
"_id": null,
"total_token_ids": { "$sum": "$number_of_tokens" }
"total_tokens": {
"$sum": 1
}
}
},
];

let mut cursor = self
.starknet_bridge_requests
.aggregate(pipeline, None)
.aggregate(pipeline, AggregateOptions::default())
.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;
}
let mut total_tokens: u64 = 0;
while let Some(doc) = cursor.try_next().await? {
if let Ok(total) = doc.get_i32("total_tokens") {
total_tokens += total as u64;
};
}

Ok(total_count)
Ok(total_tokens)
}

///
Expand Down

0 comments on commit 11bf920

Please sign in to comment.