|
| 1 | +pub use reqwest::Error; |
| 2 | +use { |
| 3 | + relay_rpc::{auth::cacao::signature::eip1271::get_rpc_url::GetRpcUrl, domain::ProjectId}, |
| 4 | + serde::Deserialize, |
| 5 | + std::{collections::HashSet, convert::Infallible, sync::Arc, time::Duration}, |
| 6 | + tokio::{sync::RwLock, task::JoinHandle}, |
| 7 | + tracing::error, |
| 8 | + url::Url, |
| 9 | +}; |
| 10 | + |
| 11 | +const BLOCKCHAIN_API_SUPPORTED_CHAINS_ENDPOINT_STR: &str = "/v1/supported-chains"; |
| 12 | +const BLOCKCHAIN_API_RPC_ENDPOINT_STR: &str = "/v1"; |
| 13 | +const BLOCKCHAIN_API_RPC_CHAIN_ID_PARAM: &str = "chainId"; |
| 14 | +const BLOCKCHAIN_API_RPC_PROJECT_ID_PARAM: &str = "projectId"; |
| 15 | + |
| 16 | +const SUPPORTED_CHAINS_REFRESH_INTERVAL: Duration = Duration::from_secs(60 * 60 * 4); |
| 17 | + |
| 18 | +#[derive(Debug, Deserialize)] |
| 19 | +struct SupportedChainsResponse { |
| 20 | + pub http: HashSet<String>, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Debug, Clone)] |
| 24 | +pub struct BlockchainApiProvider { |
| 25 | + project_id: ProjectId, |
| 26 | + blockchain_api_rpc_endpoint: Url, |
| 27 | + supported_chains: Arc<RwLock<HashSet<String>>>, |
| 28 | + refresh_job: Arc<JoinHandle<Infallible>>, |
| 29 | +} |
| 30 | + |
| 31 | +impl Drop for BlockchainApiProvider { |
| 32 | + fn drop(&mut self) { |
| 33 | + self.refresh_job.abort(); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +async fn refresh_supported_chains( |
| 38 | + blockchain_api_supported_chains_endpoint: Url, |
| 39 | + supported_chains: &RwLock<HashSet<String>>, |
| 40 | +) -> Result<(), Error> { |
| 41 | + let response = reqwest::get(blockchain_api_supported_chains_endpoint) |
| 42 | + .await? |
| 43 | + .json::<SupportedChainsResponse>() |
| 44 | + .await?; |
| 45 | + *supported_chains.write().await = response.http; |
| 46 | + Ok(()) |
| 47 | +} |
| 48 | + |
| 49 | +impl BlockchainApiProvider { |
| 50 | + pub async fn new(project_id: ProjectId, blockchain_api_endpoint: Url) -> Result<Self, Error> { |
| 51 | + let blockchain_api_rpc_endpoint = blockchain_api_endpoint |
| 52 | + .join(BLOCKCHAIN_API_RPC_ENDPOINT_STR) |
| 53 | + .expect("Safe unwrap: hardcoded URL: BLOCKCHAIN_API_RPC_ENDPOINT_STR"); |
| 54 | + let blockchain_api_supported_chains_endpoint = blockchain_api_endpoint |
| 55 | + .join(BLOCKCHAIN_API_SUPPORTED_CHAINS_ENDPOINT_STR) |
| 56 | + .expect("Safe unwrap: hardcoded URL: BLOCKCHAIN_API_SUPPORTED_CHAINS_ENDPOINT_STR"); |
| 57 | + |
| 58 | + let supported_chains = Arc::new(RwLock::new(HashSet::new())); |
| 59 | + refresh_supported_chains( |
| 60 | + blockchain_api_supported_chains_endpoint.clone(), |
| 61 | + &supported_chains, |
| 62 | + ) |
| 63 | + .await?; |
| 64 | + let mut interval = tokio::time::interval(SUPPORTED_CHAINS_REFRESH_INTERVAL); |
| 65 | + interval.tick().await; |
| 66 | + let refresh_job = tokio::task::spawn({ |
| 67 | + let supported_chains = supported_chains.clone(); |
| 68 | + let blockchain_api_supported_chains_endpoint = |
| 69 | + blockchain_api_supported_chains_endpoint.clone(); |
| 70 | + async move { |
| 71 | + loop { |
| 72 | + interval.tick().await; |
| 73 | + if let Err(e) = refresh_supported_chains( |
| 74 | + blockchain_api_supported_chains_endpoint.clone(), |
| 75 | + &supported_chains, |
| 76 | + ) |
| 77 | + .await |
| 78 | + { |
| 79 | + error!("Failed to refresh supported chains: {e}"); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + }); |
| 84 | + Ok(Self { |
| 85 | + project_id, |
| 86 | + blockchain_api_rpc_endpoint, |
| 87 | + supported_chains, |
| 88 | + refresh_job: Arc::new(refresh_job), |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +fn build_rpc_url(blockchain_api_rpc_endpoint: Url, chain_id: &str, project_id: &str) -> Url { |
| 94 | + let mut url = blockchain_api_rpc_endpoint; |
| 95 | + url.query_pairs_mut() |
| 96 | + .append_pair(BLOCKCHAIN_API_RPC_CHAIN_ID_PARAM, chain_id) |
| 97 | + .append_pair(BLOCKCHAIN_API_RPC_PROJECT_ID_PARAM, project_id); |
| 98 | + url |
| 99 | +} |
| 100 | + |
| 101 | +impl GetRpcUrl for BlockchainApiProvider { |
| 102 | + async fn get_rpc_url(&self, chain_id: String) -> Option<Url> { |
| 103 | + self.supported_chains |
| 104 | + .read() |
| 105 | + .await |
| 106 | + .contains(&chain_id) |
| 107 | + .then(|| { |
| 108 | + build_rpc_url( |
| 109 | + self.blockchain_api_rpc_endpoint.clone(), |
| 110 | + &chain_id, |
| 111 | + self.project_id.as_ref(), |
| 112 | + ) |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +#[cfg(test)] |
| 118 | +mod tests { |
| 119 | + use super::*; |
| 120 | + |
| 121 | + #[tokio::test] |
| 122 | + async fn rpc_endpoint() { |
| 123 | + assert_eq!( |
| 124 | + build_rpc_url( |
| 125 | + "https://rpc.walletconnect.com/v1".parse().unwrap(), |
| 126 | + "eip155:1", |
| 127 | + "my-project-id" |
| 128 | + ) |
| 129 | + .as_str(), |
| 130 | + "https://rpc.walletconnect.com/v1?chainId=eip155%3A1&projectId=my-project-id" |
| 131 | + ); |
| 132 | + } |
| 133 | +} |
0 commit comments