Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): mongodb query to return the total count of bridged tokens #181

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading