Skip to content

Commit

Permalink
chore: update clerk to use timestamp based lock for amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
luizstacio committed Mar 14, 2024
1 parent 3e1bc34 commit 32227a6
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 239 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ To run locally, assuming environment variables have already been set:
cargo run
```

You will need a fuel node running. You can run one with the default configuration to make the faucet work:
## Building Frontend

To build the frontend code, you can run the following commands:

```sh
fuel-core run --chain ./chain_config.json --db-type in-memory
cd frontend
bun install
bun run build
```

For development you can run `bun run build:watch`.
212 changes: 0 additions & 212 deletions chain_config.json

This file was deleted.

2 changes: 1 addition & 1 deletion frontend/src/components/faucet-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function FaucetForm({ providerUrl }: { providerUrl: string }) {
onClick={onSubmit}
isHidden={!isSignedIn}
>
{isLoading ? "Loading..." : "Confirm Claim"}
{isLoading ? "Loading..." : "Send me test ETH"}
</Submit>
<Submit
disabled={isDisabled}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/hooks/use-claim.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function useClaim(providerUrl: string) {

function submitAuthText() {
if (isLoading.value) return "Loading";
return "Claim with Auth";
return "Login with to request faucet ETH";
}

useEffect(() => {
Expand Down
39 changes: 27 additions & 12 deletions src/clerk.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use crate::config::Config;
use clerk_rs::{
apis::{sessions_api, users_api},
Expand Down Expand Up @@ -36,6 +38,7 @@ pub struct ClerkResponse {

pub struct ClerkHandler {
pub client: Clerk,
pub dispense_limit_interval: u64,
}

impl ClerkHandler {
Expand All @@ -48,20 +51,22 @@ impl ClerkHandler {
let clerk_key = Some(clerk_secret_key.expose_secret().clone());
let clerk_config = ClerkConfiguration::new(None, None, clerk_key, None);
let client = Clerk::new(clerk_config);
ClerkHandler { client }
ClerkHandler {
client,
dispense_limit_interval: config.dispense_limit_interval,
}
}

pub async fn update_user_claim(
&self,
user_id: &str,
claim_value: &str,
) -> Result<models::User, ClerkError> {
pub async fn update_user_claim(&self, user_id: &str) -> Result<models::User, ClerkError> {
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
let user = self.get_user(user_id).await?;
let update_request = Some(UpdateUserMetadataRequest {
public_metadata: None,
unsafe_metadata: None,
private_metadata: Some(json!({
"claim_value": claim_value
"claim_timestamp": timestamp.as_secs().to_string(),
})),
});

Expand All @@ -79,16 +84,26 @@ impl ClerkHandler {
Ok(user)
}

pub fn check_dispense_interval(&self, seconds_str: &str) -> bool {
if let Ok(seconds) = u64::from_str_radix(seconds_str, 10) {
let given_time = UNIX_EPOCH + Duration::from_secs(seconds);

if let Ok(duration_since_given) = SystemTime::now().duration_since(given_time) {
let diff_seconds = duration_since_given.as_secs();
return diff_seconds >= self.dispense_limit_interval
&& diff_seconds < self.dispense_limit_interval * 2;
}
}
false
}

pub async fn check_user_claim(&self, user_id: &str) -> Result<bool, ClerkError> {
let user = self.get_user(user_id).await?;
match user.private_metadata {
Some(metadata) => {
let value = metadata.unwrap();
if value["claim_value"].is_null() {
Ok(false)
} else {
Ok(true)
}
let claim_timestamp = value["claim_timestamp"].as_str().unwrap_or("0");
return Ok(self.check_dispense_interval(claim_timestamp));
}
None => Ok(false),
}
Expand Down
5 changes: 1 addition & 4 deletions src/routes/dispense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,7 @@ async fn dispense_auth(
dispense_tracker.lock().unwrap().track(address);

clerk
.update_user_claim(
user_id.clone().as_str(),
format!("{}", config.dispense_amount).as_str(),
)
.update_user_claim(user_id.clone().as_str())
.await
.map_err(|e| {
error(
Expand Down
10 changes: 5 additions & 5 deletions static/index.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions tests/dispense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ async fn many_concurrent_requests() {

#[tokio::test]
async fn dispense_once_per_day() {
let mut rng = StdRng::seed_from_u64(42);
let recipient_address: Address = rng.gen();
let rng = StdRng::seed_from_u64(42);
let context = TestContext::new(rng).await;
let addr = context.addr;

Expand Down

0 comments on commit 32227a6

Please sign in to comment.