Skip to content

Commit

Permalink
feat: whitelist paymaster users
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchand-Nicolas committed Jul 15, 2024
1 parent 4d22d3c commit cb1ac0e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
5 changes: 5 additions & 0 deletions config.template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -119,6 +125,7 @@ struct RawConfig {
databases: Databases,
variables: Variables,
contracts: Contracts,
paymaster: Paymaster,
starkscan: Starkscan,
custom_resolvers: HashMap<String, Vec<String>>,
solana: Solana,
Expand All @@ -135,6 +142,7 @@ pub_struct!(Clone, Deserialize; Config {
databases: Databases,
variables: Variables,
contracts: Contracts,
paymaster: Paymaster,
starkscan: Starkscan,
custom_resolvers: HashMap<String, Vec<String>>,
reversed_resolvers: HashMap<String, String>,
Expand Down Expand Up @@ -253,6 +261,7 @@ impl From<RawConfig> for Config {
databases: raw.databases,
variables: raw.variables,
contracts: raw.contracts,
paymaster: raw.paymaster,
starkscan: raw.starkscan,
custom_resolvers: raw.custom_resolvers,
reversed_resolvers,
Expand Down
64 changes: 55 additions & 9 deletions src/endpoints/campaigns/get_free_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)),
}
}
Expand Down

0 comments on commit cb1ac0e

Please sign in to comment.