From cb1ac0e646fbec1063bbe46699365de7b580847e Mon Sep 17 00:00:00 2001 From: nicolasito1411 <60229704+Marchand-Nicolas@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:35:29 +0200 Subject: [PATCH] feat: whitelist paymaster users --- config.template.toml | 5 ++ src/config.rs | 9 +++ src/endpoints/campaigns/get_free_domain.rs | 64 +++++++++++++++++++--- 3 files changed, 69 insertions(+), 9 deletions(-) diff --git a/config.template.toml b/config.template.toml index caa6e96..10d5735 100644 --- a/config.template.toml +++ b/config.template.toml @@ -17,6 +17,11 @@ old_verifier = "0xXXXXXXXXXXXX" pop_verifier = "0xXXXXXXXXXXXX" pp_verifier = "0xXXXXXXXXXXXX" argent_multicall = "0xXXXXXXXXXXXX" +free_domains = "0xXXXXXXXXXXXX" + +[paymaster] +api_key = "xxxxxx" +api_url = "https://starknet.api.avnu.fi/paymaster/v1" # https://sepolia.api.avnu.fi/paymaster/v1 on testnet [variables] rpc_url = "xxxxxx" diff --git a/src/config.rs b/src/config.rs index 47b42b1..a34f1e3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -38,6 +38,12 @@ pub_struct!(Clone, Deserialize; Contracts { pop_verifier: FieldElement, pp_verifier: FieldElement, argent_multicall: FieldElement, + free_domains: FieldElement, +}); + +pub_struct!(Clone, Deserialize; Paymaster { + api_key: String, + api_url: String, }); pub_struct!(Clone, Deserialize; Starkscan { @@ -119,6 +125,7 @@ struct RawConfig { databases: Databases, variables: Variables, contracts: Contracts, + paymaster: Paymaster, starkscan: Starkscan, custom_resolvers: HashMap>, solana: Solana, @@ -135,6 +142,7 @@ pub_struct!(Clone, Deserialize; Config { databases: Databases, variables: Variables, contracts: Contracts, + paymaster: Paymaster, starkscan: Starkscan, custom_resolvers: HashMap>, reversed_resolvers: HashMap, @@ -253,6 +261,7 @@ impl From for Config { databases: raw.databases, variables: raw.variables, contracts: raw.contracts, + paymaster: raw.paymaster, starkscan: raw.starkscan, custom_resolvers: raw.custom_resolvers, reversed_resolvers, diff --git a/src/endpoints/campaigns/get_free_domain.rs b/src/endpoints/campaigns/get_free_domain.rs index b0e6203..39bae9a 100644 --- a/src/endpoints/campaigns/get_free_domain.rs +++ b/src/endpoints/campaigns/get_free_domain.rs @@ -12,6 +12,8 @@ use starknet::core::types::FieldElement; use starknet_crypto::pedersen_hash; use std::sync::Arc; +use crate::utils::to_hex; + #[derive(Deserialize)] pub struct FreeDomainQuery { addr: FieldElement, @@ -107,15 +109,59 @@ pub async fn handler( ) .await { - Ok(_) => ( - // and return the signature - StatusCode::OK, - Json(json!({ - "r": signature.r, - "s": signature.s, - })), - ) - .into_response(), + Ok(_) => { + // Request paymaster API to add reward to the user + let api_url = format!( + "{}/accounts/{}/rewards", + state.conf.paymaster.api_url, + to_hex(&query.addr) + ); + let api_key = &state.conf.paymaster.api_key; + let starknet_id_contract = &state.conf.contracts.starknetid; + let free_domain_contract = &state.conf.contracts.free_domains; + // Here you can add the actual request to the paymaster API + let client = reqwest::Client::new(); + let res = client + .post(&api_url) + .header("api-key", api_key) + .json(&json!({ + "address": to_hex(&query.addr), + "campaign": "Free Domain", + "protocol": "STARKNETID", + "freeTx": 1, + "whitelistedCalls": [ + { + "contractAddress": starknet_id_contract, + "entrypoint": "*" + }, + { + "contractAddress": free_domain_contract, + "entrypoint": "*" + } + ] + })) + .send() + .await; + + match res { + Ok(response) if response.status().is_success() => ( + StatusCode::OK, + Json(json!({ + "r": signature.r, + "s": signature.s, + })), + ) + .into_response(), + Ok(response) => get_error(format!( + "Paymaster API request failed with status: {}", + response.status() + )), + Err(e) => get_error(format!( + "Error while requesting Paymaster API: {}", + e + )), + } + } Err(e) => get_error(format!("Error while updating coupon code: {}", e)), } }