-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from starknet-id/feat/add_stats_endpoints
feat: add stats, renewal & starkscan endpoints
- Loading branch information
Showing
17 changed files
with
787 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use crate::{ | ||
models::AppState, | ||
utils::{get_error, to_hex}, | ||
}; | ||
use axum::{ | ||
extract::{Query, State}, | ||
http::{HeaderMap, HeaderValue, StatusCode}, | ||
response::{IntoResponse, Json}, | ||
}; | ||
use futures::StreamExt; | ||
use mongodb::bson::doc; | ||
use serde::{Deserialize, Serialize}; | ||
use starknet::core::types::FieldElement; | ||
use std::sync::Arc; | ||
|
||
#[derive(Serialize)] | ||
pub struct StarknetIdData { | ||
starknet_id: String, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct StarknetIdQuery { | ||
addr: FieldElement, | ||
domain: String, | ||
} | ||
|
||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<StarknetIdQuery>, | ||
) -> impl IntoResponse { | ||
let renew_collection = state | ||
.starknetid_db | ||
.collection::<mongodb::bson::Document>("auto_renew_flows"); | ||
|
||
let documents = renew_collection | ||
.find( | ||
doc! { | ||
"renewer_address": to_hex(&query.addr), | ||
"domain": query.domain, | ||
"_cursor.to": null, | ||
}, | ||
None, | ||
) | ||
.await; | ||
|
||
match documents { | ||
Ok(mut cursor) => { | ||
let mut headers = HeaderMap::new(); | ||
headers.insert("Cache-Control", HeaderValue::from_static("max-age=30")); | ||
|
||
if let Some(result) = cursor.next().await { | ||
match result { | ||
Ok(res) => { | ||
let mut res = res; | ||
res.remove("_id"); | ||
res.remove("_cursor"); | ||
(StatusCode::OK, headers, Json(res)).into_response() | ||
} | ||
Err(e) => get_error(format!("Error while processing the document: {:?}", e)), | ||
} | ||
} else { | ||
get_error("no results founds".to_string()) | ||
} | ||
} | ||
Err(_) => get_error("Error while fetching from database".to_string()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod get_renewal_data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use crate::{ | ||
models::AppState, | ||
utils::{get_error, to_hex}, | ||
}; | ||
use axum::{ | ||
extract::{Query, State}, | ||
http::StatusCode, | ||
response::{IntoResponse, Json}, | ||
}; | ||
use mongodb::bson::doc; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::Value; | ||
use starknet::core::types::FieldElement; | ||
use std::sync::Arc; | ||
|
||
#[derive(Deserialize)] | ||
pub struct FetchNftsQuery { | ||
addr: FieldElement, | ||
next_url: Option<String>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct StarkscanApiResult { | ||
data: Vec<StarkscanNftProps>, | ||
next_url: Option<String>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct StarkscanNftProps { | ||
animation_url: Option<String>, | ||
attributes: Option<Value>, | ||
contract_address: String, | ||
description: Option<String>, | ||
external_url: Option<String>, | ||
image_url: Option<String>, | ||
image_medium_url: Option<String>, | ||
image_small_url: Option<String>, | ||
minted_at_transaction_hash: Option<String>, | ||
minted_by_address: Option<String>, | ||
token_id: String, | ||
name: Option<String>, | ||
nft_id: Option<String>, | ||
token_uri: Option<String>, | ||
minted_at_timestamp: i64, | ||
} | ||
|
||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<FetchNftsQuery>, | ||
) -> impl IntoResponse { | ||
let url = if let Some(next_url) = &query.next_url { | ||
next_url.clone() | ||
} else { | ||
format!( | ||
"{}nfts?owner_address={}", | ||
state.conf.starkscan.api_url, | ||
to_hex(&query.addr) | ||
) | ||
}; | ||
|
||
let client = reqwest::Client::new(); | ||
match client | ||
.get(&url) | ||
.header("accept", "application/json") | ||
.header("x-api-key", state.conf.starkscan.api_key.clone()) | ||
.send() | ||
.await | ||
{ | ||
Ok(response) => match response.text().await { | ||
Ok(text) => match serde_json::from_str::<StarkscanApiResult>(&text) { | ||
Ok(res) => (StatusCode::OK, Json(res)).into_response(), | ||
Err(e) => get_error(format!( | ||
"Failed to deserialize result from Starkscan API: {} for response: {}", | ||
e, text | ||
)), | ||
}, | ||
Err(e) => get_error(format!( | ||
"Failed to get JSON response while fetching user NFT data: {}", | ||
e | ||
)), | ||
}, | ||
Err(e) => get_error(format!("Failed to fetch user NFTs from API: {}", e)), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod fetch_nfts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use crate::{models::AppState, utils::get_error}; | ||
use axum::{ | ||
extract::{Query, State}, | ||
http::{HeaderMap, HeaderValue, StatusCode}, | ||
response::IntoResponse, | ||
Json, | ||
}; | ||
use futures::StreamExt; | ||
use mongodb::bson::doc; | ||
use serde::{Deserialize, Serialize}; | ||
use std::sync::Arc; | ||
|
||
#[derive(Serialize)] | ||
pub struct CountAddrsData { | ||
count: i32, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct CountAddrsQuery { | ||
since: i64, | ||
} | ||
|
||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<CountAddrsQuery>, | ||
) -> impl IntoResponse { | ||
let mut headers = HeaderMap::new(); | ||
headers.insert("Cache-Control", HeaderValue::from_static("max-age=60")); | ||
|
||
let domain_collection = state | ||
.starknetid_db | ||
.collection::<mongodb::bson::Document>("domains"); | ||
let aggregate_cursor = domain_collection | ||
.aggregate( | ||
vec![ | ||
doc! { "$match": { "_cursor.to": null, "creation_date": { "$gte": query.since } }}, | ||
doc! { "$group": { "_id": "$legacy_address" }}, | ||
doc! { "$count": "total" }, | ||
], | ||
None, | ||
) | ||
.await; | ||
|
||
match aggregate_cursor { | ||
Ok(mut cursor) => { | ||
if let Some(result) = cursor.next().await { | ||
match result { | ||
Ok(doc_) => { | ||
let count = doc_.get_i32("total").unwrap_or(0); | ||
let response_data = CountAddrsData { count }; | ||
(StatusCode::OK, headers, Json(response_data)).into_response() | ||
} | ||
Err(e) => get_error(format!("Error while processing the document: {:?}", e)), | ||
} | ||
} else { | ||
get_error("No documents found".to_string()) | ||
} | ||
} | ||
Err(e) => get_error(format!("Error while fetching from database: {:?}", e)), | ||
} | ||
} |
Oops, something went wrong.