Skip to content

Commit

Permalink
fix reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
Ugo-X committed Sep 28, 2024
1 parent ce118c6 commit c1367a8
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/endpoints/admin/balance/create_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub async fn handler(
contracts: Some(parsed_contracts),
api_url: None,
regex: None,
calls: None,
};

// insert document to boost collection
Expand Down
1 change: 1 addition & 0 deletions src/endpoints/admin/custom/create_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub async fn handler(
contracts: None,
api_url: None,
regex: None,
calls: None,
};

// insert document to boost collection
Expand Down
1 change: 1 addition & 0 deletions src/endpoints/admin/discord/create_discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub async fn handler(
contracts: None,
api_url: None,
regex: None,
calls: None,
};

// insert document to boost collection
Expand Down
1 change: 1 addition & 0 deletions src/endpoints/admin/domain/create_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub async fn handler(
contracts: None,
api_url: None,
regex: None,
calls: None,
};

// insert document to boost collection
Expand Down
1 change: 1 addition & 0 deletions src/endpoints/admin/quiz/create_quiz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub async fn handler(
contracts: None,
api_url: None,
regex: None,
calls: None,
};

return match tasks_collection.insert_one(new_document, None).await {
Expand Down
1 change: 1 addition & 0 deletions src/endpoints/admin/twitter/create_twitter_fw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub async fn handler(
contracts: None,
api_url: None,
regex: None,
calls: None,
};

// insert document to boost collection
Expand Down
1 change: 1 addition & 0 deletions src/endpoints/admin/twitter/create_twitter_rw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub async fn handler(
contracts: None,
api_url: None,
regex: None,
calls: None,
};

// insert document to boost collection
Expand Down
1 change: 1 addition & 0 deletions src/endpoints/quests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ pub mod verify_custom_api;
pub mod verify_quiz;
pub mod verify_twitter_fw;
pub mod verify_twitter_rw;
pub mod verify_contract;
38 changes: 26 additions & 12 deletions src/endpoints/quests/verify_contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use crate::{
models::{AppState, QuestTaskDocument, Call},
models::{AppState, QuestTaskDocument},
utils::{get_error, CompletedTasksTrait},
};
use axum::{
Expand Down Expand Up @@ -33,12 +33,12 @@ pub async fn handler(
) -> impl IntoResponse {
let task_id = query.task_id;
// Get task from db
let task_collection = state.db.collection("tasks");
let task: QuestTaskDocument = task_collection
.find_one(doc! {"id": task_id}, None)
.await
.unwrap()
.unwrap();
let task_collection = state.db.collection::<QuestTaskDocument>("tasks");
let task = match task_collection.find_one(doc! {"id": task_id}, None).await {
Ok(Some(task)) => task,
Ok(None) => return get_error("Task not found".to_string()),
Err(e) => return get_error(format!("Database error: {}", e)),
};

if task.task_type != Some("contract".to_string()) {
return get_error("Invalid task type.".to_string());
Expand All @@ -48,20 +48,31 @@ pub async fn handler(

if let Some(calls) = task.calls {
for call in calls {
let contract_address = FieldElement::from_hex_be(&call.contract).map_err(|e| get_error(format!("Invalid contract address: {}", e)))?;
let contract_address = match FieldElement::from_hex_be(&call.contract) {
Ok(address) => address,
Err(e) => return get_error(format!("Invalid contract address: {}", e)),
};

let calldata: Vec<FieldElement> = call.call_data
let calldata: Vec<FieldElement> = match call.call_data
.iter()
.map(|s| FieldElement::from_hex_be(s))
.collect::<Result<Vec<FieldElement>, _>>()
.map_err(|e| get_error(format!("Invalid calldata: {}", e)))?;
{
Ok(data) => data,
Err(e) => return get_error(format!("Invalid calldata: {}", e)),
};

let entry_point_selector = match FieldElement::from_hex_be(&call.entry_point) {
Ok(selector) => selector,
Err(e) => return get_error(format!("Invalid entry point: {}", e)),
};

let call_result = state
.provider
.call(
FunctionCall {
contract_address,
entry_point_selector: call.entry_point.into(),
entry_point_selector,
calldata,
},
BlockId::Tag(BlockTag::Latest),
Expand All @@ -70,7 +81,10 @@ pub async fn handler(

match call_result {
Ok(result) => {
let regex = Regex::new(&call.regex).map_err(|e| get_error(format!("Invalid regex: {}", e)))?;
let regex = match Regex::new(&call.regex) {
Ok(re) => re,
Err(e) => return get_error(format!("Invalid regex: {}", e)),
};
let result_str = result.iter().map(|&r| r.to_string()).collect::<Vec<String>>().join(",");

if !regex.is_match(&result_str) {
Expand Down
9 changes: 9 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ pub struct CompletedTaskDocument {
timestamp: i64,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Call {
pub contract: String,
pub call_data: Vec<String>,
pub entry_point: String,
pub regex: String,
}

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct QuestTaskDocument {
pub(crate) id: i32,
Expand All @@ -120,6 +128,7 @@ pub struct QuestTaskDocument {
pub verify_endpoint_type: String,
pub api_url: Option<String>,
pub regex: Option<String>,
pub calls: Option<Vec<Call>>,
#[serde(default)]
pub verify_redirect: Option<String>,
#[serde(default)]
Expand Down

0 comments on commit c1367a8

Please sign in to comment.