-
Notifications
You must be signed in to change notification settings - Fork 45
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 #273 from Ugo-X/testnet
created advanced contract task type
- Loading branch information
Showing
11 changed files
with
124 additions
and
1 deletion.
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
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 |
---|---|---|
|
@@ -80,4 +80,4 @@ pub async fn handler( | |
} | ||
Err(e) => get_error(format!("{}", 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,106 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::{ | ||
models::{AppState, QuestTaskDocument}, | ||
utils::{get_error, CompletedTasksTrait}, | ||
}; | ||
use axum::{ | ||
extract::{Query, State}, | ||
http::StatusCode, | ||
response::IntoResponse, | ||
Json, | ||
}; | ||
use axum_auto_routes::route; | ||
use mongodb::bson::doc; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::json; | ||
use starknet::{ | ||
core::types::{BlockId, BlockTag, FieldElement, FunctionCall}, | ||
providers::Provider, | ||
}; | ||
use regex::Regex; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Default)] | ||
pub struct VerifyContractQuery { | ||
pub addr: FieldElement, | ||
pub task_id: u32, | ||
} | ||
|
||
#[route(get, "/quests/verify_contract")] | ||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<VerifyContractQuery>, | ||
) -> impl IntoResponse { | ||
let task_id = query.task_id; | ||
// Get task from db | ||
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()); | ||
} | ||
|
||
let addr = &query.addr; | ||
|
||
if let Some(calls) = task.calls { | ||
for call in calls { | ||
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> = match call.call_data | ||
.iter() | ||
.map(|s| FieldElement::from_hex_be(s)) | ||
.collect::<Result<Vec<FieldElement>, _>>() | ||
{ | ||
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, | ||
calldata, | ||
}, | ||
BlockId::Tag(BlockTag::Latest), | ||
) | ||
.await; | ||
|
||
match call_result { | ||
Ok(result) => { | ||
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) { | ||
return get_error("Contract call result does not match the expected pattern.".to_string()); | ||
} | ||
} | ||
Err(e) => return get_error(format!("Contract call failed: {}", e)), | ||
} | ||
} | ||
|
||
// All calls succeeded and matched their regexes | ||
match state.upsert_completed_task(*addr, task_id).await { | ||
Ok(_) => (StatusCode::OK, Json(json!({"res": true}))).into_response(), | ||
Err(e) => get_error(format!("Failed to update completed task: {}", e)), | ||
} | ||
} else { | ||
get_error("No calls specified for this task.".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