-
Notifications
You must be signed in to change notification settings - Fork 44
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 #117 from starknet-id/feat/store_times_db
fix: add deployed timestamp in db, create endpoint & update seniority…
- Loading branch information
Showing
8 changed files
with
147 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::utils::DeployedTimesTrait; | ||
use crate::{ | ||
models::{AppState, DeployedTime}, | ||
utils::to_hex, | ||
}; | ||
use mongodb::{bson::doc, Collection}; | ||
use starknet::core::types::FieldElement; | ||
|
||
pub async fn execute_has_deployed_time( | ||
state: Arc<AppState>, | ||
addr: &FieldElement, | ||
) -> Result<u32, String> { | ||
// Check if we have already a result in the db | ||
let deployed_times_collection: Collection<DeployedTime> = state.db.collection("deployed_times"); | ||
let filter = doc! { "addr": to_hex(*addr) }; | ||
if let Ok(Some(document)) = deployed_times_collection.find_one(filter, None).await { | ||
return Ok(document.timestamp); | ||
} | ||
|
||
// If not we fetch it from the API and store it in the db | ||
let url = format!( | ||
"https://api.starkscan.co/api/v0/transactions?from_block=1&limit=1&contract_address={}&order_by=asc", | ||
to_hex(*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.json::<serde_json::Value>().await { | ||
Ok(json) => { | ||
if let Some(timestamp) = json["data"][0]["timestamp"].as_i64() { | ||
match state | ||
.upsert_deployed_timestamp(*addr, timestamp as u32) | ||
.await | ||
{ | ||
Ok(_) => Ok(timestamp as u32), | ||
Err(e) => Err(format!("{}", e)), | ||
} | ||
} else { | ||
Err("Wallet not deployed.".to_string()) | ||
} | ||
} | ||
Err(e) => Err(format!( | ||
"Failed to get JSON response while fetching user transaction data: {}", | ||
e | ||
)), | ||
}, | ||
Err(e) => Err(format!("Failed to fetch user transactions 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
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,29 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::models::AppState; | ||
use crate::{common::has_deployed_time::execute_has_deployed_time, utils::get_error}; | ||
use axum::{ | ||
extract::{Query, State}, | ||
http::StatusCode, | ||
response::IntoResponse, | ||
Json, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::json; | ||
use starknet::core::types::FieldElement; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
|
||
pub struct GetDeployedTimeQuery { | ||
addr: FieldElement, | ||
} | ||
|
||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<GetDeployedTimeQuery>, | ||
) -> impl IntoResponse { | ||
match execute_has_deployed_time(state, &query.addr).await { | ||
Ok(timestamp) => (StatusCode::OK, Json(json!({ "timestamp": timestamp }))).into_response(), | ||
Err(e) => get_error(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
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