Skip to content

Commit

Permalink
refactor: use #route macro to clean main.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Jan 18, 2024
1 parent 4bc8fb9 commit f18d549
Show file tree
Hide file tree
Showing 138 changed files with 957 additions and 666 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "c974e5cb42e8d8344cee910b76005ec46b4dd3ed" }
starknet-id = { git = "https://github.com/starknet-id/starknet-id.rs.git", rev = "2b30c2453b96789a628c86d2edebb1023fa2e77d" }
axum_auto_routes = { git = "https://github.com/Th0rgal/axum_auto_routes.git", rev = "f9e1d2083e887cd264642359c4aa851938da6f09" }
axum = "0.6.17"
toml = "0.5.10"
serde = { version = "1.0.152", features = ["derive"] }
Expand All @@ -23,3 +24,4 @@ percent-encoding = "2.3.1"
chrono = "0.4.19"
lazy_static = "1.4.0"
regex = "1.10.0"
ctor = "0.2.6"
6 changes: 6 additions & 0 deletions src/endpoints/achievements/batched/verify_tvl_batched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ use axum::{
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use serde_json::json;
use starknet::core::types::FieldElement;

#[route(
get,
"/achievements/batched/verify_tvl_batched",
crate::endpoints::achievements::batched::verify_tvl_batched
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementBatchedQuery>,
Expand Down
53 changes: 31 additions & 22 deletions src/endpoints/achievements/claim/quests_achievement.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use std::sync::Arc;

use crate::utils::{to_hex, AchievementsTrait};
use crate::{
models::{AppState, VerifyAchievementQuery},
utils::{get_error},
utils::get_error,
};
use axum::{
extract::{Query, State},
http::StatusCode,
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use futures::TryStreamExt;
use mongodb::bson::{doc, Document};
use serde_json::json;
use starknet::core::types::FieldElement;
use crate::utils::{AchievementsTrait, to_hex};
fn get_number_of_quests(id: u32) -> u32 {
return match id {
23 => 1,
Expand All @@ -26,6 +27,11 @@ fn get_number_of_quests(id: u32) -> u32 {
};
}

#[route(
get,
"/achievements/claim/quest_achievement",
crate::endpoints::achievements::claim::quests_achievement
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
Expand Down Expand Up @@ -89,29 +95,29 @@ pub async fn handler(
}
},
doc! {
"$group": doc! {
"_id": null,
"count": doc! {
"$sum": 1
"$group": doc! {
"_id": null,
"count": doc! {
"$sum": 1
}
}
}
},
},
doc! {
"$addFields": doc! {
"result": doc! {
"$cond": doc! {
"if": doc! {
"$gte": [
"$count",
quests_threshold
]
},
"then": true,
"else": false
"$addFields": doc! {
"result": doc! {
"$cond": doc! {
"if": doc! {
"$gte": [
"$count",
quests_threshold
]
},
"then": true,
"else": false
}
}
}
}
},
},
];
let tasks_collection = state.db.collection::<Document>("completed_tasks");

Expand All @@ -125,7 +131,10 @@ pub async fn handler(
return get_error("User hasn't completed required number of quests".into());
}
let addr_hex = to_hex(addr);
match state.upsert_claimed_achievement(addr_hex, achievement_id).await {
match state
.upsert_claimed_achievement(addr_hex, achievement_id)
.await
{
Ok(_) => (StatusCode::OK, Json(json!({"res": true}))).into_response(),
Err(e) => get_error(format!("{}", e)),
}
Expand Down
6 changes: 6 additions & 0 deletions src/endpoints/achievements/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ use axum::{
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use futures::stream::StreamExt;
use mongodb::bson::{doc, from_document};
use starknet::core::types::FieldElement;

#[route(
get,
"/achievements/fetch",
crate::endpoints::achievements::fetch
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<AchievementQuery>,
Expand Down
6 changes: 6 additions & 0 deletions src/endpoints/achievements/fetch_buildings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ use axum::{
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use futures::stream::StreamExt;
use mongodb::bson::{doc, from_document};

#[route(
get,
"/achievements/fetch_buildings",
crate::endpoints::achievements::fetch_buildings
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<BuildingQuery>,
Expand Down
6 changes: 6 additions & 0 deletions src/endpoints/achievements/verify_achieved_quests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ use axum::{
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use serde_json::json;
use starknet::core::types::FieldElement;

#[route(
get,
"/achievements/verify_achieved_quests",
crate::endpoints::achievements::verify_achieved_quests
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
Expand Down
6 changes: 6 additions & 0 deletions src/endpoints/achievements/verify_avnu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ use axum::{
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use serde_json::json;
use starknet::core::types::FieldElement;

#[route(
get,
"/achievements/verify_avnu",
crate::endpoints::achievements::verify_avnu
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
Expand Down
10 changes: 8 additions & 2 deletions src/endpoints/achievements/verify_briq.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use crate::utils::fetch_json_from_url;
use crate::{
models::{AchievedDocument, AppState, VerifyAchievementQuery},
utils::{get_error, to_hex, AchievementsTrait},
Expand All @@ -10,11 +11,16 @@ use axum::{
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use mongodb::bson::doc;
use serde_json::json;
use starknet::core::types::FieldElement;
use crate::utils::fetch_json_from_url;

#[route(
get,
"/achievements/verify_briq",
crate::endpoints::achievements::verify_briq
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
Expand Down Expand Up @@ -47,7 +53,7 @@ pub async fn handler(
Ok(response) => {
if let Some(sets) = response.get("sets") {
match sets {
serde_json::Value::Array(sets_array ) => {
serde_json::Value::Array(sets_array) => {
for set in sets_array.iter() {
if let serde_json::Value::String(set_str) = set {
let url = format!(
Expand Down
6 changes: 6 additions & 0 deletions src/endpoints/achievements/verify_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use axum::{
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use mongodb::bson::doc;
use serde_json::json;
use starknet::core::types::FieldElement;
Expand All @@ -41,6 +42,11 @@ fn get_args(config: Config, achievement_id: u32) -> Result<(FieldElement, u32, N
}
}

#[route(
get,
"/achievements/verify_default",
crate::endpoints::achievements::verify_default
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
Expand Down
21 changes: 14 additions & 7 deletions src/endpoints/achievements/verify_has_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use axum::{
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use serde_json::json;
use starknet::{
core::types::{BlockId, BlockTag, FieldElement, FunctionCall},
Expand All @@ -19,6 +20,11 @@ use std::{
time::{SystemTime, UNIX_EPOCH},
};

#[route(
get,
"/achievements/verify_has_domain",
crate::endpoints::achievements::verify_has_domain
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyQuery>,
Expand Down Expand Up @@ -51,16 +57,17 @@ pub async fn handler(
FunctionCall {
contract_address: state.conf.starknetid_contracts.naming_contract,
entry_point_selector: selector!("domain_to_expiry"),
calldata: vec![ FieldElement::ONE, result[1] ],
calldata: vec![FieldElement::ONE, result[1]],
},
BlockId::Tag(BlockTag::Latest),
)
.await else {
return get_error("error querying expiry".to_string())
};
let Ok(expiry) : Result<u64, _> = expiry_result[0].try_into() else {
return get_error("error reading expiry".to_string())
};
.await
else {
return get_error("error querying expiry".to_string());
};
let Ok(expiry): Result<u64, _> = expiry_result[0].try_into() else {
return get_error("error reading expiry".to_string());
};
let now = match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(n) => n.as_secs(),
Err(_) => return get_error("system time before UNIX EPOCH".to_string()),
Expand Down
6 changes: 6 additions & 0 deletions src/endpoints/achievements/verify_quests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use axum::{
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use futures::TryStreamExt;
use mongodb::bson::{doc, Document};
use serde_json::json;
Expand All @@ -27,6 +28,11 @@ fn get_number_of_quests(id: u32) -> u32 {
};
}

#[route(
get,
"/achievements/verify_quests",
crate::endpoints::achievements::verify_quests
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
Expand Down
6 changes: 6 additions & 0 deletions src/endpoints/achievements/verify_seniority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ use axum::{
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use chrono::{NaiveDateTime, Utc};
use serde_json::json;
use starknet::core::types::FieldElement;

#[route(
get,
"/achievements/verify_seniority",
crate::endpoints::achievements::verify_seniority
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
Expand Down
6 changes: 6 additions & 0 deletions src/endpoints/achievements/verify_tvl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ use axum::{
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use serde_json::json;
use starknet::core::types::FieldElement;

#[route(
get,
"/achievements/verify_tvl",
crate::endpoints::achievements::verify_tvl
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
Expand Down
12 changes: 4 additions & 8 deletions src/endpoints/get_boosted_quests.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use crate::{models::AppState, utils::get_error};
use axum::{
extract::{State},
response::IntoResponse,
Json,
};
use axum::{extract::State, response::IntoResponse, Json};

use axum_auto_routes::route;
use futures::TryStreamExt;
use mongodb::bson::{doc, Document};
use reqwest::StatusCode;
use std::sync::Arc;

pub async fn handler(
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
#[route(get, "/get_boosted_quests", crate::endpoints::get_boosted_quests)]
pub async fn handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
let pipeline = vec![
doc! {
"$unwind": doc! {
Expand Down
2 changes: 2 additions & 0 deletions src/endpoints/get_completed_quests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use axum::{
Json,
};

use axum_auto_routes::route;
use futures::TryStreamExt;
use mongodb::bson::{doc, Document};
use reqwest::StatusCode;
Expand All @@ -18,6 +19,7 @@ pub struct GetCompletedQuestsQuery {
addr: FieldElement,
}

#[route(get, "/get_completed_quests", crate::endpoints::get_completed_quests)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<GetCompletedQuestsQuery>,
Expand Down
2 changes: 2 additions & 0 deletions src/endpoints/get_deployed_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use axum::{
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use serde::{Deserialize, Serialize};
use serde_json::json;
use starknet::core::types::FieldElement;
Expand All @@ -18,6 +19,7 @@ pub struct GetDeployedTimeQuery {
addr: FieldElement,
}

#[route(get, "/get_deployed_time", crate::endpoints::get_deployed_time)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<GetDeployedTimeQuery>,
Expand Down
Loading

0 comments on commit f18d549

Please sign in to comment.