-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feat/optimize_leaderboard
- Loading branch information
Showing
21 changed files
with
656 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use crate::{ | ||
models::{AppState, QuestCategoryDocument}, | ||
utils::get_error, | ||
}; | ||
use axum::{ | ||
extract::{Query, State}, | ||
http::StatusCode, | ||
response::{IntoResponse, Json}, | ||
}; | ||
use mongodb::bson::doc; | ||
use serde::Deserialize; | ||
use std::sync::Arc; | ||
|
||
#[derive(Deserialize)] | ||
pub struct GetQuestsQuery { | ||
name: String, | ||
} | ||
|
||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<GetQuestsQuery>, | ||
) -> impl IntoResponse { | ||
let collection = state | ||
.db | ||
.collection::<QuestCategoryDocument>("quest_categories"); | ||
let filter = doc! { | ||
"name": &query.name | ||
}; | ||
|
||
match collection.find_one(filter, None).await { | ||
Ok(option) => match option { | ||
Some(category) => (StatusCode::OK, Json(category)).into_response(), | ||
None => get_error("Category not found".to_string()), | ||
}, | ||
Err(_) => get_error("Error querying quest category data".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,106 @@ | ||
use crate::{models::AppState, utils::get_error}; | ||
use axum::{ | ||
extract::{Query, State}, | ||
response::IntoResponse, | ||
Json, | ||
}; | ||
|
||
use futures::TryStreamExt; | ||
use mongodb::bson::{doc, Document}; | ||
use reqwest::StatusCode; | ||
use serde::{Deserialize, Serialize}; | ||
use starknet::core::types::FieldElement; | ||
use std::sync::Arc; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct HasCompletedQuestsQuery { | ||
addr: FieldElement, | ||
quest_id: u32, | ||
} | ||
|
||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<HasCompletedQuestsQuery>, | ||
) -> impl IntoResponse { | ||
let address = query.addr.to_string(); | ||
let quest_id = query.quest_id; | ||
let pipeline = vec![ | ||
doc! { | ||
"$match": doc! { | ||
"address": address, | ||
} | ||
}, | ||
doc! { | ||
"$lookup": doc! { | ||
"from": "tasks", | ||
"localField": "task_id", | ||
"foreignField": "id", | ||
"as": "associatedTask" | ||
} | ||
}, | ||
doc! { | ||
"$unwind": "$associatedTask" | ||
}, | ||
doc! { | ||
"$project": doc! { | ||
"_id": 0, | ||
"address": 1, | ||
"task_id": 1, | ||
"quest_id": "$associatedTask.quest_id" | ||
} | ||
}, | ||
doc! { | ||
"$group": doc! { | ||
"_id": "$quest_id", | ||
"done": doc! { | ||
"$sum": 1 | ||
} | ||
} | ||
}, | ||
doc! { | ||
"$match": doc! { | ||
"_id": quest_id, | ||
} | ||
}, | ||
doc! { | ||
"$lookup": doc! { | ||
"from": "tasks", | ||
"localField": "_id", | ||
"foreignField": "quest_id", | ||
"as": "tasks" | ||
} | ||
}, | ||
doc! { | ||
"$project": doc! { | ||
"_id": 0, | ||
"result": doc! { | ||
"$cond": doc! { | ||
"if": doc! { | ||
"$eq": [ | ||
doc! { | ||
"$size": "$tasks" | ||
}, | ||
"$done" | ||
] | ||
}, | ||
"then": true, | ||
"else": false | ||
} | ||
} | ||
} | ||
}, | ||
]; | ||
let tasks_collection = state.db.collection::<Document>("completed_tasks"); | ||
match tasks_collection.aggregate(pipeline, None).await { | ||
Ok(cursor) => { | ||
let mut cursor = cursor; | ||
let mut result = false; | ||
while let Some(doc) = cursor.try_next().await.unwrap() { | ||
result = doc.get("result").unwrap().as_bool().unwrap(); | ||
} | ||
let response = serde_json::json!({ "completed": result }); | ||
(StatusCode::OK, Json(response)).into_response() | ||
} | ||
Err(_) => get_error("Error querying status".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
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,62 @@ | ||
use crate::{models::AppState, utils::get_error}; | ||
use axum::{ | ||
extract::{Query, State}, | ||
response::IntoResponse, | ||
Json, | ||
}; | ||
use std::str::FromStr; | ||
|
||
use mongodb::bson::{doc, Document}; | ||
use reqwest::StatusCode; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::json; | ||
use starknet::core::crypto::ecdsa_sign; | ||
use starknet::core::{crypto::pedersen_hash, types::FieldElement}; | ||
use std::sync::Arc; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct GetClaimBoostQuery { | ||
boost_id: u32, | ||
} | ||
|
||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<GetClaimBoostQuery>, | ||
) -> impl IntoResponse { | ||
let boost_id = query.boost_id; | ||
let collection = state.db.collection::<Document>("boosts"); | ||
let res=collection.find_one(doc! {"id":boost_id},None).await.unwrap(); | ||
|
||
// if no boost found with the requested id | ||
if res.is_none() { | ||
return get_error(format!("Boost with id {} not found", boost_id)); | ||
} | ||
|
||
let boost: Document = res.unwrap(); | ||
let amount = boost.get("amount").unwrap().as_i32().unwrap() as u32; | ||
let token = boost.get("token").unwrap().as_str().unwrap(); | ||
let address = boost.get("winner").unwrap().as_str().unwrap(); | ||
|
||
let hashed = pedersen_hash( | ||
&FieldElement::from(boost_id), | ||
&pedersen_hash( | ||
&FieldElement::from(amount), | ||
&pedersen_hash( | ||
&FieldElement::from(0 as u32), | ||
&pedersen_hash( | ||
&FieldElement::from_str(token).unwrap(), | ||
&FieldElement::from_str(address).unwrap(), | ||
), | ||
), | ||
), | ||
); | ||
|
||
match ecdsa_sign(&state.conf.quest_boost.private_key, &hashed) { | ||
Ok(signature) => ( | ||
StatusCode::OK, | ||
Json(json!({"address": address, "r": signature.r, "s": signature.s})), | ||
) | ||
.into_response(), | ||
Err(e) => get_error(format!("Error while generating signature: {}", 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 get_claim_params; |
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
Oops, something went wrong.