Skip to content

Commit

Permalink
feat: add claimable endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushtom committed May 3, 2024
1 parent 7d5a940 commit ca3b427
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
5 changes: 3 additions & 2 deletions config.template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,7 @@ options = [
correct_answers = [*]



[quizzes.haiko]
name = "Strategist quiz"
desc = "Take part in our Quiz to test your knowledge about Haiko, and you'll have a chance to win 100 USDC."
Expand All @@ -1102,10 +1103,10 @@ kind = "text_choice"
layout = "default"
question = "What is Haiko?"
options = [
"An automated market making platform featuring custom strategies",
"A lending protocol",
"A stablecoin provider",
"An online e-commerce site",
"An automated market making platform featuring custom strategies",
]
correct_answers = [*]

Expand All @@ -1114,9 +1115,9 @@ kind = "text_choice"
layout = "default"
question = "What key benefits to strategists offer?"
options = [
"Automatically rebalances positions so they keep earning yield",
"Increases transaction fees",
"Doubles my rewards from providing liquidity",
"Automatically rebalances positions so they keep earning yield",
"Restricts me from withdrawing liquidity",
]
correct_answers = [*]
Expand Down
109 changes: 109 additions & 0 deletions src/endpoints/quests/haiko/claimable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use crate::models::{AppState, CompletedTaskDocument, Reward, RewardResponse};
use crate::utils::{get_error, get_nft};
use axum::{
extract::{Query, State},
http::StatusCode,
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use futures::StreamExt;
use mongodb::bson::doc;
use serde::Deserialize;
use starknet::{
core::types::FieldElement,
signers::{LocalWallet, SigningKey},
};
use std::sync::Arc;

const QUEST_ID: u32 = 29;
const TASK_IDS: &[u32] = &[140, 141, 142, 143, 144];
const LAST_TASK: u32 = TASK_IDS[4];
const NFT_LEVEL: u32 = 41;

#[derive(Deserialize)]
pub struct ClaimableQuery {
addr: FieldElement,
}

#[route(
get,
"/quests/haiko/claimable",
crate::endpoints::quests::haiko::claimable
)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<ClaimableQuery>,
) -> impl IntoResponse {
let collection = state
.db
.collection::<CompletedTaskDocument>("completed_tasks");

let pipeline = vec![
doc! {
"$match": {
"address": &query.addr.to_string(),
"task_id": { "$in": TASK_IDS },
},
},
doc! {
"$lookup": {
"from": "tasks",
"localField": "task_id",
"foreignField": "id",
"as": "task",
},
},
doc! {
"$match": {
"task.quest_id": QUEST_ID,
},
},
doc! {
"$group": {
"_id": "$address",
"completed_tasks": { "$push": "$task_id" },
},
},
doc! {
"$match": {
"completed_tasks": { "$all": TASK_IDS },
},
},
];

let completed_tasks = collection.aggregate(pipeline, None).await;
match completed_tasks {
Ok(mut tasks_cursor) => {
if tasks_cursor.next().await.is_none() {
return get_error("User hasn't completed all tasks".into());
}

let signer = LocalWallet::from(SigningKey::from_secret_scalar(
state.conf.nft_contract.private_key,
));

let mut rewards = vec![];

let Ok((token_id, sig)) =
get_nft(QUEST_ID, LAST_TASK, &query.addr, NFT_LEVEL, &signer).await
else {
return get_error("Signature failed".into());
};

rewards.push(Reward {
task_id: LAST_TASK,
nft_contract: state.conf.nft_contract.address.clone(),
token_id: token_id.to_string(),
sig: (sig.r, sig.s),
});

if rewards.is_empty() {
get_error("No rewards found for this user".into())
} else {
(StatusCode::OK, Json(RewardResponse { rewards })).into_response()
}
}
Err(_) => get_error("Error querying rewards".into()),
}
}
3 changes: 2 additions & 1 deletion src/endpoints/quests/haiko/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod discord_fw_callback;
pub mod verify_deposit;
pub mod verify_twitter_fw;
pub mod verify_twitter_rw;
pub mod verify_twitter_rw;
pub mod claimable;
10 changes: 10 additions & 0 deletions src/endpoints/quests/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,16 @@ pub async fn handler(
}),
).into_response(),

Some(41) => (
StatusCode::OK,
Json(TokenURI {
name: "Haiko Strategist NFT ".into(),
description: "A Haiko Strategist NFT won for successfully finishing the Quest".into(),
image: format!("{}/haiko/haikoStrategist.webp", state.conf.variables.app_link),
attributes: None,
}),
).into_response(),


_ => get_error("Error, this level is not correct".into()),
}
Expand Down

0 comments on commit ca3b427

Please sign in to comment.