kabu_node_debug_provider/
cachefolder.rs

1use std::path::Path;
2
3use alloy::primitives::B256;
4use eyre::Result;
5use tokio::fs;
6use tokio::io::{AsyncReadExt, AsyncWriteExt};
7
8#[derive(Clone)]
9pub struct CacheFolder {
10    path: String,
11}
12
13impl CacheFolder {
14    pub async fn new(path: &str) -> Self {
15        if !Path::new(path).exists() {
16            fs::create_dir_all(path).await.unwrap();
17        }
18        CacheFolder { path: path.to_string() }
19    }
20
21    pub async fn write(&self, method: String, index: B256, data: String) -> Result<()> {
22        let file_path = format!("{}/{}_{}.json", self.path, method.to_lowercase(), index.to_string().strip_prefix("0x").unwrap());
23        let mut file = fs::File::create(file_path).await?;
24        file.write_all(data.as_bytes()).await?;
25        Ok(())
26    }
27
28    pub async fn read(&self, method: String, index: B256) -> Result<String> {
29        let file_path = format!("{}/{}_{}.json", self.path, method.to_lowercase(), index.to_string().strip_prefix("0x").unwrap());
30        let mut file = fs::File::open(file_path).await?;
31        let mut content = String::new();
32        file.read_to_string(&mut content).await?;
33        Ok(content)
34    }
35}