Skip to content

Commit

Permalink
Merge pull request #235 from starknet-id/feat/strkFarm-API-route
Browse files Browse the repository at this point in the history
feat: strkFarm API route
  • Loading branch information
Th0rgal authored Jul 24, 2024
2 parents 8277303 + 2a2350e commit 4c8c242
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/endpoints/quests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 47 additions & 0 deletions src/endpoints/quests/strk_farm/check_balance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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<Arc<AppState>>,
Query(query): Query<VerifyQuery>,
) -> 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();
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)),
}
}
}
1 change: 1 addition & 0 deletions src/endpoints/quests/strk_farm/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod check_balance;

0 comments on commit 4c8c242

Please sign in to comment.