Skip to content

Commit

Permalink
Merge pull request #262 from starknet-id/feat/carmine-price-protect-task
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal authored Sep 20, 2024
2 parents bc6acef + 92bb487 commit 4f1f28a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/endpoints/quests/carmine/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod verify_price_protect;
52 changes: 52 additions & 0 deletions src/endpoints/quests/carmine/verify_price_protect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::sync::Arc;

use crate::{
models::{AppState, VerifyQuery},
utils::{get_error, CompletedTasksTrait},
};
use axum::{
extract::{Query, State},
http::StatusCode,
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use serde_json::json;
use starknet::core::types::FieldElement;

#[route(get, "/quests/carmine/verify_price_protect")]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyQuery>,
) -> impl IntoResponse {
let task_id = query.task_id.unwrap();
let addr = query.addr;

let api_url = "https://api.carmine.finance/api/v1/mainnet/price-protect-users";

// Check if the addr is in the "data" field of the API response
let response = reqwest::get(api_url)
.await
.unwrap()
.json::<serde_json::Value>()
.await
.unwrap();
let data = response["data"].as_array().unwrap();
let mut found = false;
for address in data {
if FieldElement::from_hex_be(address.as_str().unwrap()).expect("Failed to parse address")
== addr
{
found = true;
break;
}
}
if found {
match state.upsert_completed_task(addr, task_id).await {
Ok(_) => (StatusCode::OK, Json(json!({"res": true}))).into_response(),
Err(e) => get_error(format!("{}", e)),
}
} else {
get_error("You didn't open price protect for at least 10$ on Carmine.".to_string())
}
}
1 change: 1 addition & 0 deletions src/endpoints/quests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod carmine;
pub mod claimable;
pub mod contract_uri;
pub mod discord_fw_callback;
Expand Down
1 change: 1 addition & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ pub_struct!(Serialize; RewardResponse {

pub_struct!(Deserialize; VerifyQuery {
addr: FieldElement,
task_id: Option<u32>,
});

pub_struct!(Deserialize; VerifyNewQuery {
Expand Down

0 comments on commit 4f1f28a

Please sign in to comment.