Skip to content

Commit

Permalink
Switch to fastnear
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreLeGuen committed Mar 25, 2024
1 parent 8f2ab83 commit 9e241c2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ axum = "0.6.2"
tokio = { version = "1.24.1", features = ["full"] }
hyper = { version = "0.14.23", features = ["full"] }
tower = { version = "0.4", features = ["full"] }
serde = { version = "1.0.152", features = ["derive", "serde_derive"] }
serde = { version = "1", features = ["derive", "serde_derive"] }
serde_json = "1.0.91"
tracing = "0.1.37"
tracing-subscriber = "0.3.16"
Expand Down Expand Up @@ -44,6 +44,7 @@ governor = "0.6.0"
tracing-loki = "0.2.4"
reqwest = "0.11.22"
uint = { version = "0.8.3", default-features = false }
quick_cache = "0.4.0"

[dev-dependencies]
axum-test-helper = "0.3.0"
30 changes: 24 additions & 6 deletions src/kitwallet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod models;

use std::{collections::HashMap, num::NonZeroU32, sync::Arc};

use anyhow::bail;
Expand All @@ -6,11 +8,13 @@ use tokio::sync::RwLock;
use tracing::{error, info};
use tta_rust::RateLim;

use crate::kitwallet::models::FastNearFT;

#[derive(Clone)]
pub struct KitWallet {
rate_limiter: Arc<RwLock<RateLim>>,
client: reqwest::Client,
cache: Arc<RwLock<HashMap<String, Vec<String>>>>,
cache: Arc<RwLock<HashMap<String, (i64, Vec<String>)>>>,
}

impl Default for KitWallet {
Expand Down Expand Up @@ -38,7 +42,10 @@ impl KitWallet {
let cache_read = self.cache.read().await;

if let Some(likely_tokens) = cache_read.get(&account) {
return Ok(likely_tokens.clone());
// Check if the cache is expired
if chrono::Utc::now().timestamp() - likely_tokens.0 < 60 {
return Ok(likely_tokens.1.clone());
}
}

drop(cache_read); // Release the read lock
Expand All @@ -50,22 +57,33 @@ impl KitWallet {
"Account {} likely tokens not cached, fetching from API",
account
);
// https://api.fastnear.com/v1/account/here.near/ft
let likely_tokens = self
.client
.get(format!(
"https://api.kitwallet.app/account/{}/likelyTokens",
"https://api.fastnear.com/v1/account/{}/ft",
account
))
.send()
.await?
.json::<Vec<String>>()
.json::<FastNearFT>()
.await?;

// Insert the result into the cache
let mut cache_write = self.cache.write().await;
cache_write.insert(account.clone(), likely_tokens.clone());
cache_write.insert(
account.clone(),
(
chrono::Utc::now().timestamp(),
likely_tokens
.tokens
.iter()
.map(|t| t.contract_id.clone())
.collect(),
),
);

Ok(likely_tokens)
Ok(cache_write.get(&account).unwrap().1.clone())
}

// get all in parallel
Expand Down
19 changes: 19 additions & 0 deletions src/kitwallet/models.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FastNearFT {
#[serde(rename = "account_id")]
pub account_id: String,
pub tokens: Vec<Token>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Token {
#[serde(rename = "contract_id")]
pub contract_id: String,
#[serde(rename = "last_update_block_height")]
pub last_update_block_height: Value,
}

0 comments on commit 9e241c2

Please sign in to comment.