Skip to content

Commit

Permalink
Merge pull request #92 from starknet-id/feat/whitelist-paymaster-users
Browse files Browse the repository at this point in the history
Feat/whitelist paymaster users
  • Loading branch information
Th0rgal authored Jul 17, 2024
2 parents 7e692e1 + 4ec723a commit 3901991
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 @@ -20,6 +20,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 @@ -39,6 +39,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 @@ -120,6 +126,7 @@ struct RawConfig {
databases: Databases,
variables: Variables,
contracts: Contracts,
paymaster: Paymaster,
starkscan: Starkscan,
custom_resolvers: HashMap<String, Vec<String>>,
solana: Solana,
Expand All @@ -136,6 +143,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 @@ -254,6 +262,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 3901991

Please sign in to comment.