From 92bb4876f48a3cded786ecdb6fdf7c656c2a781d Mon Sep 17 00:00:00 2001 From: Nico <60229704+Marchand-Nicolas@users.noreply.github.com> Date: Wed, 18 Sep 2024 18:27:49 +0200 Subject: [PATCH] feat: carmine price protect task --- src/endpoints/quests/carmine/mod.rs | 1 + .../quests/carmine/verify_price_protect.rs | 52 +++++++++++++++++++ src/endpoints/quests/mod.rs | 1 + src/models.rs | 1 + 4 files changed, 55 insertions(+) create mode 100644 src/endpoints/quests/carmine/mod.rs create mode 100644 src/endpoints/quests/carmine/verify_price_protect.rs diff --git a/src/endpoints/quests/carmine/mod.rs b/src/endpoints/quests/carmine/mod.rs new file mode 100644 index 00000000..88b654cd --- /dev/null +++ b/src/endpoints/quests/carmine/mod.rs @@ -0,0 +1 @@ +pub mod verify_price_protect; diff --git a/src/endpoints/quests/carmine/verify_price_protect.rs b/src/endpoints/quests/carmine/verify_price_protect.rs new file mode 100644 index 00000000..55388b5d --- /dev/null +++ b/src/endpoints/quests/carmine/verify_price_protect.rs @@ -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>, + Query(query): Query, +) -> 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::() + .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()) + } +} diff --git a/src/endpoints/quests/mod.rs b/src/endpoints/quests/mod.rs index 8620cc01..3338ed1a 100644 --- a/src/endpoints/quests/mod.rs +++ b/src/endpoints/quests/mod.rs @@ -1,3 +1,4 @@ +pub mod carmine; pub mod claimable; pub mod contract_uri; pub mod discord_fw_callback; diff --git a/src/models.rs b/src/models.rs index ed80cbad..9451b669 100644 --- a/src/models.rs +++ b/src/models.rs @@ -143,6 +143,7 @@ pub_struct!(Serialize; RewardResponse { pub_struct!(Deserialize; VerifyQuery { addr: FieldElement, + task_id: Option, }); pub_struct!(Deserialize; VerifyNewQuery {