Skip to content

Commit

Permalink
Merge pull request #252 from Oshioke-Salaki/logger_impl
Browse files Browse the repository at this point in the history
feat: logger impl
  • Loading branch information
Marchand-Nicolas authored Aug 26, 2024
2 parents 56e2de6 + e94e777 commit d7909c3
Show file tree
Hide file tree
Showing 113 changed files with 564 additions and 475 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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" }
serde_derive = "1.0.183"
env_logger = "0.10.0"
axum_auto_routes = { git = "https://github.com/Th0rgal/axum_auto_routes.git", rev = "f9e1d2083e887cd264642359c4aa851938da6f09" }
axum = "0.6.17"
toml = "0.5.10"
Expand Down
10 changes: 10 additions & 0 deletions config.template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ connection_string = "xxxxxx"
secret_key = "secret_key"
expiry_duration = 0

[watchtower]
endpoint = "https://api.watchtower.starknet.id/service/add_message"
app_id = "XXXXXXXXXXXXXXXXX"
token = "XXXXXXXXXXXXXXXXX"
enabled = true
[watchtower.types]
info = "goerli/info"
warning = "goerli/warning"
severe = "goerli/severe"

[discover]
pairs_api_endpoint = "XXXXXXXX"
lending_api_endpoint = "XXXXXXXX"
Expand Down
2 changes: 1 addition & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ pub mod has_deployed_time;
pub mod verify_has_nft;
pub mod verify_has_root_domain;
pub mod verify_has_root_or_braavos_domain;
pub mod verify_quiz;
pub mod verify_quiz;
15 changes: 8 additions & 7 deletions src/common/verify_has_root_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ pub async fn execute_has_root_domain(
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
15 changes: 8 additions & 7 deletions src/common/verify_has_root_or_braavos_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ pub async fn verify_has_root_or_braavos_domain(
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
4 changes: 2 additions & 2 deletions src/common/verify_quiz.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::config::{Quiz, QuizQuestionType};
use crate::models::QuizInsertDocument;
use futures::StreamExt;
use mongodb::bson::{doc, from_document};
use mongodb::Database;
use crate::config::{Quiz, QuizQuestionType};
use starknet::core::types::FieldElement;
use crate::models::QuizInsertDocument;

fn match_vectors(vector1: &Vec<usize>, vector2: &Vec<usize>) -> bool {
// Check if vectors have the same length
Expand Down
15 changes: 15 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ use std::collections::HashMap;
use std::env;
use std::fs;

pub_struct!(Clone, Deserialize; Watchtower {
enabled : bool,
endpoint: String,
app_id: String,
token: String,
types: WatchtowerTypes,
});

pub_struct!(Clone, Deserialize; WatchtowerTypes {
info: String,
warning: String,
severe: String,
});

pub_struct!(Clone, Deserialize; Server { port: u16 });

pub_struct!(Clone, Deserialize; Database {
Expand Down Expand Up @@ -193,6 +207,7 @@ pub_struct!(Clone, Deserialize; Config {
discord: Discord,
starkscan: Starkscan,
achievements: Achievements,
watchtower: Watchtower,
quest_boost: QuestBoost,
rhino: PublicApi,
rango: Api,
Expand Down
5 changes: 1 addition & 4 deletions src/endpoints/achievements/batched/verify_tvl_batched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ use axum_auto_routes::route;
use serde_json::json;
use starknet::core::types::FieldElement;

#[route(
get,
"/achievements/batched/verify_tvl_batched"
)]
#[route(get, "/achievements/batched/verify_tvl_batched")]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementBatchedQuery>,
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/achievements/claim/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod quests_achievement;
pub mod quests_achievement;
5 changes: 1 addition & 4 deletions src/endpoints/achievements/claim/quests_achievement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ fn get_number_of_quests(id: u32) -> u32 {
};
}

#[route(
get,
"/achievements/claim/quest_achievement"
)]
#[route(get, "/achievements/claim/quest_achievement")]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
Expand Down
5 changes: 1 addition & 4 deletions src/endpoints/achievements/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ use futures::stream::StreamExt;
use mongodb::bson::{doc, from_document};
use starknet::core::types::FieldElement;

#[route(
get,
"/achievements/fetch"
)]
#[route(get, "/achievements/fetch")]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<AchievementQuery>,
Expand Down
5 changes: 1 addition & 4 deletions src/endpoints/achievements/fetch_buildings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use axum_auto_routes::route;
use futures::stream::StreamExt;
use mongodb::bson::{doc, from_document};

#[route(
get,
"/achievements/fetch_buildings"
)]
#[route(get, "/achievements/fetch_buildings")]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<BuildingQuery>,
Expand Down
4 changes: 2 additions & 2 deletions src/endpoints/achievements/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
pub mod batched;
pub mod claim;
pub mod fetch;
pub mod fetch_buildings;
pub mod verify_achieved_quests;
pub mod verify_avnu;
pub mod verify_briq;
pub mod verify_default;
pub mod verify_has_domain;
pub mod verify_quests;
pub mod verify_seniority;
pub mod verify_tvl;
pub mod verify_whitelisted;
pub mod verify_quests;
pub mod claim;
5 changes: 1 addition & 4 deletions src/endpoints/achievements/verify_achieved_quests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use axum_auto_routes::route;
use serde_json::json;
use starknet::core::types::FieldElement;

#[route(
get,
"/achievements/verify_achieved_quests"
)]
#[route(get, "/achievements/verify_achieved_quests")]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
Expand Down
5 changes: 1 addition & 4 deletions src/endpoints/achievements/verify_avnu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use axum_auto_routes::route;
use serde_json::json;
use starknet::core::types::FieldElement;

#[route(
get,
"/achievements/verify_avnu"
)]
#[route(get, "/achievements/verify_avnu")]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
Expand Down
5 changes: 1 addition & 4 deletions src/endpoints/achievements/verify_briq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ use mongodb::bson::doc;
use serde_json::json;
use starknet::core::types::FieldElement;

#[route(
get,
"/achievements/verify_briq"
)]
#[route(get, "/achievements/verify_briq")]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
Expand Down
5 changes: 1 addition & 4 deletions src/endpoints/achievements/verify_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ fn get_args(config: Config, achievement_id: u32) -> Result<(FieldElement, u32, N
}
}

#[route(
get,
"/achievements/verify_default"
)]
#[route(get, "/achievements/verify_default")]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
Expand Down
4 changes: 1 addition & 3 deletions src/endpoints/achievements/verify_has_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ use std::{
time::{SystemTime, UNIX_EPOCH},
};

#[route(
get,
"/achievements/verify_has_domain")]
#[route(get, "/achievements/verify_has_domain")]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyQuery>,
Expand Down
5 changes: 1 addition & 4 deletions src/endpoints/achievements/verify_quests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ fn get_number_of_quests(id: u32) -> u32 {
};
}

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

#[route(
get,
"/achievements/verify_seniority"
)]
#[route(get, "/achievements/verify_seniority")]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
Expand Down
5 changes: 1 addition & 4 deletions src/endpoints/achievements/verify_tvl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use axum_auto_routes::route;
use serde_json::json;
use starknet::core::types::FieldElement;

#[route(
get,
"/achievements/verify_tvl"
)]
#[route(get, "/achievements/verify_tvl")]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyAchievementQuery>,
Expand Down
5 changes: 1 addition & 4 deletions src/endpoints/admin/balance/create_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ pub_struct!(Deserialize; CreateBalance {
cta: String,
});

#[route(
post,
"/admin/tasks/balance/create"
)]
#[route(post, "/admin/tasks/balance/create")]
pub async fn handler(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
Expand Down
5 changes: 1 addition & 4 deletions src/endpoints/admin/balance/update_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ fn field_element_to_bson(fe: &FieldElement) -> mongodb::bson::Bson {
mongodb::bson::Bson::String(fe.to_string())
}

#[route(
post,
"/admin/tasks/balance/update"
)]
#[route(post, "/admin/tasks/balance/update")]
pub async fn handler(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
Expand Down
5 changes: 1 addition & 4 deletions src/endpoints/admin/custom/create_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ pub_struct!(Deserialize; CreateCustom {
api: String,
});

#[route(
post,
"/admin/tasks/custom/create"
)]
#[route(post, "/admin/tasks/custom/create")]
pub async fn handler(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/admin/custom/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod create_custom;
pub mod update_custom;
pub mod update_custom;
21 changes: 8 additions & 13 deletions src/endpoints/admin/custom/update_custom.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use crate::models::{QuestTaskDocument,JWTClaims};
use crate::models::{JWTClaims, QuestTaskDocument};
use crate::utils::verify_task_auth;
use crate::{models::AppState, utils::get_error};
use axum::http::HeaderMap;
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Json},
};
use axum_auto_routes::route;
use mongodb::bson::{doc};
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use mongodb::bson::doc;
use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;
use crate::utils::verify_task_auth;
use axum::http::HeaderMap;
use jsonwebtoken::{Validation,Algorithm,decode,DecodingKey};


pub_struct!(Deserialize; CreateCustom {
id: i64,
Expand All @@ -35,8 +34,8 @@ pub async fn handler(
let user = check_authorization!(headers, &state.conf.auth.secret_key.as_ref()) as String;
let collection = state.db.collection::<QuestTaskDocument>("tasks");

let res= verify_task_auth(user,&collection,&(body.id as i32)).await;
if !res{
let res = verify_task_auth(user, &collection, &(body.id as i32)).await;
if !res {
return get_error("Error updating tasks".to_string());
}

Expand Down Expand Up @@ -74,12 +73,8 @@ pub async fn handler(
"$set": update_doc
};


// insert document to boost collection
return match collection
.find_one_and_update(filter, update, None)
.await
{
return match collection.find_one_and_update(filter, update, None).await {
Ok(_) => (
StatusCode::OK,
Json(json!({"message": "Task updated successfully"})).into_response(),
Expand Down
Loading

0 comments on commit d7909c3

Please sign in to comment.