From aa1a83e8fca097dda4ad902b0a128ccb0b0e891f Mon Sep 17 00:00:00 2001 From: Nico <60229704+Marchand-Nicolas@users.noreply.github.com> Date: Wed, 24 Jul 2024 14:06:20 +0200 Subject: [PATCH 1/2] feat: strkFarm API route --- src/endpoints/quests/mod.rs | 1 + .../quests/strk_farm/check_balance.rs | 48 +++++++++++++++++++ src/endpoints/quests/strk_farm/mod.rs | 1 + 3 files changed, 50 insertions(+) create mode 100644 src/endpoints/quests/strk_farm/check_balance.rs create mode 100644 src/endpoints/quests/strk_farm/mod.rs diff --git a/src/endpoints/quests/mod.rs b/src/endpoints/quests/mod.rs index d16a997..4cc16c2 100644 --- a/src/endpoints/quests/mod.rs +++ b/src/endpoints/quests/mod.rs @@ -22,6 +22,7 @@ pub mod rhino; pub mod sithswap; pub mod starknet; pub mod starknetid; +pub mod strk_farm; pub mod tribe; pub mod uri; pub mod verify_balance; diff --git a/src/endpoints/quests/strk_farm/check_balance.rs b/src/endpoints/quests/strk_farm/check_balance.rs new file mode 100644 index 0000000..fc24798 --- /dev/null +++ b/src/endpoints/quests/strk_farm/check_balance.rs @@ -0,0 +1,48 @@ +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; + +type StrkFarmAPIResponse = serde_json::Value; + +#[route( + get, + "/quests/strkFarm/check_balance", + crate::endpoints::quests::strk_farm::check_balance +)] +pub async fn handler( + State(state): State>, + Query(query): Query, +) -> impl IntoResponse { + let task_id = 185; + let addr = &query.addr; + let url = format!("https://www.strkfarm.xyz/api/stats/{addr}"); + let res = reqwest::get(&url).await.unwrap().text().await.unwrap(); + // Res in a JSON containing the user's balance + let json: StrkFarmAPIResponse = serde_json::from_str(&res).unwrap(); + println!("{:?}", json); + let usd = json["holdingsUSD"].as_f64().unwrap(); + if usd == 0.0 { + get_error("You didn't invest on StrkFarm.".to_string()) + } else if usd < 10.0 { + get_error(format!( + "You need to invest at least $10 on StrkFarm (You have ${}).", + usd + )) + } else { + match state.upsert_completed_task(query.addr, task_id).await { + Ok(_) => (StatusCode::OK, Json(json!({"res": true}))).into_response(), + Err(e) => get_error(format!("{}", e)), + } + } +} diff --git a/src/endpoints/quests/strk_farm/mod.rs b/src/endpoints/quests/strk_farm/mod.rs new file mode 100644 index 0000000..efaa97b --- /dev/null +++ b/src/endpoints/quests/strk_farm/mod.rs @@ -0,0 +1 @@ +pub mod check_balance; From 2a2350eb2f04610e93573139ad40c3981251758b Mon Sep 17 00:00:00 2001 From: Nico <60229704+Marchand-Nicolas@users.noreply.github.com> Date: Wed, 24 Jul 2024 14:21:19 +0200 Subject: [PATCH 2/2] removing print --- src/endpoints/quests/strk_farm/check_balance.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/endpoints/quests/strk_farm/check_balance.rs b/src/endpoints/quests/strk_farm/check_balance.rs index fc24798..9f1db06 100644 --- a/src/endpoints/quests/strk_farm/check_balance.rs +++ b/src/endpoints/quests/strk_farm/check_balance.rs @@ -30,7 +30,6 @@ pub async fn handler( let res = reqwest::get(&url).await.unwrap().text().await.unwrap(); // Res in a JSON containing the user's balance let json: StrkFarmAPIResponse = serde_json::from_str(&res).unwrap(); - println!("{:?}", json); let usd = json["holdingsUSD"].as_f64().unwrap(); if usd == 0.0 { get_error("You didn't invest on StrkFarm.".to_string())