diff --git a/config.template.toml b/config.template.toml index c798d8fb..88421ab9 100644 --- a/config.template.toml +++ b/config.template.toml @@ -30,6 +30,8 @@ contract = "0xXXXXXXXXXXXX" [quests.braavos] api_key_user = "xxxxxx" api_key_claimed_mission = "xxxxxx" +[quests.element] +api_key = "xxxxxx" [twitter] oauth2_clientid = "xxxxxx" @@ -660,3 +662,96 @@ options = [ ] correct_answers = [*] +[quizzes.briq] +name = "briquiz 🧱" +desc = "briq is the Starknet project that allows to build anything you want as an NFT, using little construction blocks stored on the blockchain called 'briqs'. Take the quizz to check if you know all about this blocky project!" +intro = "Ready? Set? Briq!" + +[[quizzes.briq.questions]] +kind = "text_choice" +layout = "default" +question = "What are briqs?" +options = [ + "Tokenized real estate shares", + "ERC-1155 compatible construction blocks used to build NFTs", + "ERC-20 tokens used to trade DeFi", + "ERC-721 collectibles used as profile pictures" +] +correct_answers = [*] + +[[quizzes.briq.questions]] +kind = "text_choice" +layout = "default" +question = "What are sets?" +options = [ + "ERC-721 collectibles built with briqs", + "ERC-1155 tokens used to build houses", + "Tokenized real estate shares", + "ERC-20 stablecoins" +] +correct_answers = [*] + +[[quizzes.briq.questions]] +kind = "text_choice" +layout = "default" +question = "What is the briq factory?" +options = [ + "A real factory, based in France, that produces physical bricks", + "A secondary market to briqs", + "A contract issuing briqs on the primary market, according to an emission schedule", + "A marketplace to buy sets" +] +correct_answers = [*] + +[[quizzes.briq.questions]] +kind = "text_choice" +layout = "default" +question = "When was briq deployed on Starknet mainnet for the 1st time?" +options = [ + "December 2021", + "January 2020", + "November 2022", + "June 2023" +] +correct_answers = [*] + +[quizzes.element] +name = "Element - Gemstone Quest Quiz" +desc = "Challenge your Element knowledge with our quiz for a chance to score an exclusive NFT prize." +intro = "Embark on our Element Quest, complete the tasks, and claim an exclusive Gemstone NFT as your reward." + +[[quizzes.element.questions]] +kind = "text_choice" +layout = "default" +question = "What is Element?" +options = [ + "A popular cryptocurrency exchange", + "A mobile gaming app", + "A community-driven aggregated marketplace for NFTs", + "A new social media platform" +] +correct_answers = [*] + +[[quizzes.element.questions]] +kind = "text_choice" +layout = "default" +question = " Which chain does Element NOT support currently?" +options = [ + "ETH", + "ZkSync", + "Starknet", + "Solana" +] +correct_answers = [*] + +[[quizzes.element.questions]] +kind = "text_choice" +layout = "default" +question = "What is the right answer in the context of Element?" +options = [ + "Gas fee savings of up to 49% on NFT Trading.", + "Gas increase of 35%", + "Royalty payments instantly with 50% Gas", + "Whale tracking for high Gas crypto trading" +] +correct_answers = [*] \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index bb9959b0..7db958d6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -50,6 +50,10 @@ pub_struct!(Clone, Deserialize; Braavos { api_key_claimed_mission: String, }); +pub_struct!(Clone, Deserialize; Element { + api_key: String, +}); + pub_struct!(Clone, Deserialize; Quests { sithswap: Pairs, zklend: Contract, @@ -57,6 +61,7 @@ pub_struct!(Clone, Deserialize; Quests { ekubo: Contract, myswap: Contract, braavos: Braavos, + element: Element, }); pub_struct!(Clone, Deserialize; Twitter { diff --git a/src/endpoints/quests/element/briq/claimable.rs b/src/endpoints/quests/element/briq/claimable.rs new file mode 100644 index 00000000..4d61e897 --- /dev/null +++ b/src/endpoints/quests/element/briq/claimable.rs @@ -0,0 +1,101 @@ +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 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 = 17; +const TASK_IDS: &[u32] = &[67, 68, 69]; +const LAST_TASK: u32 = TASK_IDS[2]; +const NFT_LEVEL: u32 = 23; + +#[derive(Deserialize)] +pub struct ClaimableQuery { + addr: FieldElement, +} + +pub async fn handler( + State(state): State>, + Query(query): Query, +) -> impl IntoResponse { + let collection = state + .db + .collection::("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()), + } +} diff --git a/src/endpoints/quests/element/briq/mod.rs b/src/endpoints/quests/element/briq/mod.rs new file mode 100644 index 00000000..1f33954a --- /dev/null +++ b/src/endpoints/quests/element/briq/mod.rs @@ -0,0 +1,3 @@ +pub mod claimable; +pub mod verify_own_briq; +pub mod verify_twitter_fw; diff --git a/src/endpoints/quests/element/briq/verify_own_briq.rs b/src/endpoints/quests/element/briq/verify_own_briq.rs new file mode 100644 index 00000000..456510fe --- /dev/null +++ b/src/endpoints/quests/element/briq/verify_own_briq.rs @@ -0,0 +1,87 @@ +use std::sync::Arc; + +use crate::{ + models::{AppState, VerifyQuery}, + utils::{get_error, to_hex, CompletedTasksTrait}, +}; +use axum::{ + extract::{Query, State}, + http::StatusCode, + response::IntoResponse, + Json, +}; +use serde_json::json; +use starknet::core::types::FieldElement; + +pub async fn handler( + State(state): State>, + Query(query): Query, +) -> impl IntoResponse { + let task_id = 68; + if query.addr == FieldElement::ZERO { + return get_error("Please connect your wallet first".to_string()); + } + + let url = format!( + "https://api.briq.construction/v1/user/data/starknet-mainnet/{}", + to_hex(query.addr) + ); + match fetch_json_from_url(url).await { + Ok(response) => { + if let Some(sets) = response.get("sets") { + match sets { + serde_json::Value::Array(sets_array) => { + for set in sets_array.iter() { + if let serde_json::Value::String(set_str) = set { + let url = format!( + "https://api.briq.construction/v1/metadata/starknet-mainnet/{}", + set_str + ); + match fetch_json_from_url(url).await { + Ok(metadata_response) => { + if let Some(_properties) = + metadata_response.get("properties") + { + match state + .upsert_completed_task(query.addr, task_id) + .await + { + Ok(_) => { + return ( + StatusCode::OK, + Json(json!({"res": true})), + ) + .into_response(); + } + Err(e) => { + return get_error(format!("{}", e)); + } + } + } + } + Err(e) => return get_error(e), + } + } + } + } + _ => { + return get_error("No Briq sets founds".to_string()); + } + } + } + get_error("No Briq sets founds".to_string()) + } + Err(e) => get_error(e), + } +} + +pub async fn fetch_json_from_url(url: String) -> Result { + let client = reqwest::Client::new(); + match client.get(url).send().await { + Ok(response) => match response.json::().await { + Ok(json) => Ok(json), + Err(e) => Err(format!("Failed to get JSON response: {}", e)), + }, + Err(e) => Err(format!("Failed to send request: {}", e)), + } +} diff --git a/src/endpoints/quests/element/briq/verify_twitter_fw.rs b/src/endpoints/quests/element/briq/verify_twitter_fw.rs new file mode 100644 index 00000000..01c1b670 --- /dev/null +++ b/src/endpoints/quests/element/briq/verify_twitter_fw.rs @@ -0,0 +1,28 @@ +use std::sync::Arc; + +use crate::{ + models::{AppState, VerifyQuery}, + utils::{get_error, CompletedTasksTrait}, +}; +use axum::{ + extract::{Query, State}, + http::StatusCode, + response::IntoResponse, + Json, +}; +use serde_json::json; +use starknet::core::types::FieldElement; + +pub async fn handler( + State(state): State>, + Query(query): Query, +) -> impl IntoResponse { + let task_id = 69; + if query.addr == FieldElement::ZERO { + return get_error("Please connect your wallet first".to_string()); + } + 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/element/element/claimable.rs b/src/endpoints/quests/element/element/claimable.rs new file mode 100644 index 00000000..6b3b6c9d --- /dev/null +++ b/src/endpoints/quests/element/element/claimable.rs @@ -0,0 +1,101 @@ +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 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 = 16; +const TASK_IDS: &[u32] = &[64, 65, 66]; +const LAST_TASK: u32 = TASK_IDS[2]; +const NFT_LEVEL: u32 = 22; + +#[derive(Deserialize)] +pub struct ClaimableQuery { + addr: FieldElement, +} + +pub async fn handler( + State(state): State>, + Query(query): Query, +) -> impl IntoResponse { + let collection = state + .db + .collection::("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()), + } +} diff --git a/src/endpoints/quests/element/element/mod.rs b/src/endpoints/quests/element/element/mod.rs new file mode 100644 index 00000000..204183a9 --- /dev/null +++ b/src/endpoints/quests/element/element/mod.rs @@ -0,0 +1,3 @@ +pub mod claimable; +pub mod verify_is_eligible; +pub mod verify_twitter_fw; diff --git a/src/endpoints/quests/element/element/verify_is_eligible.rs b/src/endpoints/quests/element/element/verify_is_eligible.rs new file mode 100644 index 00000000..08954458 --- /dev/null +++ b/src/endpoints/quests/element/element/verify_is_eligible.rs @@ -0,0 +1,69 @@ +use std::sync::Arc; + +use crate::{ + models::{AppState, VerifyQuery}, + utils::{get_error, to_hex, CompletedTasksTrait}, +}; +use axum::{ + extract::{Query, State}, + http::StatusCode, + response::IntoResponse, + Json, +}; +use serde::Deserialize; +use serde_json::json; +use starknet::core::types::FieldElement; + +#[derive(Debug, Deserialize)] +pub struct ElementResponse { + #[allow(dead_code)] + code: u32, + data: bool, +} + +pub async fn handler( + State(state): State>, + Query(query): Query, +) -> impl IntoResponse { + let task_id = 65; + if query.addr == FieldElement::ZERO { + return get_error("Please connect your wallet first".to_string()); + } + + let url = format!( + "https://api.element.market/openapi/v1/qualify/check?address={}&taskId=100120231026231111", + to_hex(query.addr) + ); + let client = reqwest::Client::new(); + match client + .get(&url) + .header("accept", "application/json") + .header("x-api-key", state.conf.quests.element.api_key.clone()) + .send() + .await + { + Ok(response) => match response.text().await { + Ok(text) => match serde_json::from_str::(&text) { + Ok(res) => { + if res.data { + 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)), + } + } else { + get_error("You have not interacted with Element.".to_string()) + } + } + Err(e) => get_error(format!( + "Failed to deserialize result from Element API: {} for response: {}", + e, text + )), + }, + Err(e) => get_error(format!( + "Failed to get JSON response while fetching Element API: {}", + e + )), + }, + Err(e) => get_error(format!("Failed to fetch Element API: {}", e)), + } +} diff --git a/src/endpoints/quests/element/element/verify_twitter_fw.rs b/src/endpoints/quests/element/element/verify_twitter_fw.rs new file mode 100644 index 00000000..e1d40ce9 --- /dev/null +++ b/src/endpoints/quests/element/element/verify_twitter_fw.rs @@ -0,0 +1,28 @@ +use std::sync::Arc; + +use crate::{ + models::{AppState, VerifyQuery}, + utils::{get_error, CompletedTasksTrait}, +}; +use axum::{ + extract::{Query, State}, + http::StatusCode, + response::IntoResponse, + Json, +}; +use serde_json::json; +use starknet::core::types::FieldElement; + +pub async fn handler( + State(state): State>, + Query(query): Query, +) -> impl IntoResponse { + let task_id = 66; + if query.addr == FieldElement::ZERO { + return get_error("Please connect your wallet first".to_string()); + } + 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/element/mod.rs b/src/endpoints/quests/element/mod.rs new file mode 100644 index 00000000..bbdf7626 --- /dev/null +++ b/src/endpoints/quests/element/mod.rs @@ -0,0 +1,2 @@ +pub mod briq; +pub mod element; diff --git a/src/endpoints/quests/mod.rs b/src/endpoints/quests/mod.rs index 97a6c587..41f45016 100644 --- a/src/endpoints/quests/mod.rs +++ b/src/endpoints/quests/mod.rs @@ -3,6 +3,7 @@ pub mod braavos; pub mod carmine; pub mod contract_uri; pub mod ekubo; +pub mod element; pub mod focustree; pub mod jediswap; pub mod morphine; diff --git a/src/endpoints/quests/uri.rs b/src/endpoints/quests/uri.rs index b6b4697b..8d9be5e6 100644 --- a/src/endpoints/quests/uri.rs +++ b/src/endpoints/quests/uri.rs @@ -252,6 +252,28 @@ pub async fn handler( ) .into_response(), + Some(22) => ( + StatusCode::OK, + Json(TokenURI { + name: "The Element Gemstone".into(), + description: "An Element Gemstone NFT can be won for successfully finishing the Quest".into(), + image: format!("{}/element/elementGem.webp", state.conf.variables.app_link), + attributes: None, + }), + ) + .into_response(), + + Some(23) => ( + StatusCode::OK, + Json(TokenURI { + name: "The Briq Element Gemstone".into(), + description: "A Briq Element Gemstone NFT can be won for successfully finishing the Quest".into(), + image: format!("{}/element/briqGem.webp", state.conf.variables.app_link), + attributes: None, + }), + ) + .into_response(), + _ => get_error("Error, this level is not correct".into()), } } diff --git a/src/endpoints/quests/verify_quiz.rs b/src/endpoints/quests/verify_quiz.rs index 1ffbfb13..b7e021ba 100644 --- a/src/endpoints/quests/verify_quiz.rs +++ b/src/endpoints/quests/verify_quiz.rs @@ -24,6 +24,8 @@ fn get_task_id(quiz_name: &str) -> Option { "aa_mastery_2" => Some(59), "aa_mastery_3" => Some(60), "focustree" => Some(61), + "element" => Some(64), + "briq" => Some(67), _ => None, } } diff --git a/src/main.rs b/src/main.rs index aaa44755..83402e56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -270,6 +270,30 @@ async fn main() { "/quests/focustree/claimable", get(endpoints::quests::focustree::claimable::handler), ) + .route( + "/quests/element/element/verify_is_eligible", + get(endpoints::quests::element::element::verify_is_eligible::handler), + ) + .route( + "/quests/element/element/verify_twitter_fw", + get(endpoints::quests::element::element::verify_twitter_fw::handler), + ) + .route( + "/quests/element/element/claimable", + get(endpoints::quests::element::element::claimable::handler), + ) + .route( + "/quests/element/briq/verify_own_briq", + get(endpoints::quests::element::briq::verify_own_briq::handler), + ) + .route( + "/quests/element/briq/verify_twitter_fw", + get(endpoints::quests::element::briq::verify_twitter_fw::handler), + ) + .route( + "/quests/element/briq/claimable", + get(endpoints::quests::element::briq::claimable::handler), + ) .route( "/achievements/verify_default", get(endpoints::achievements::verify_default::handler),