Skip to content

Commit

Permalink
clean db
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetbout committed Sep 6, 2023
1 parent d04b24d commit 5b15c77
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 25 deletions.
2 changes: 1 addition & 1 deletion db/block.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"lastProcesssedBlockNumber":158830,"twitterRefreshToken":"???"}
{"lastProcesssedBlockNumber":158830}
3 changes: 3 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ pub async fn fetch_events(token: Token) -> Result<(), reqwest::Error> {
let rpc_url = format!("https://starknet-mainnet.infura.io/v3/{api_key}");
let rpc_client = JsonRpcClient::new(HttpTransport::new(Url::parse(&rpc_url).unwrap()));

let bn = rpc_client.block_number().await.unwrap();
println!("Block number {}", bn);

let events = rpc_client
.get_events(
EventFilter {
Expand Down
25 changes: 1 addition & 24 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Deserialize, Serialize, Clone)]
struct Data {
last_processsed_block: u128,
twitter_refresh_token: String,
}

async fn get_db(path: &str) -> FileDatabase<Data, Json> {
Expand All @@ -23,14 +22,6 @@ pub async fn get_last_processsed_block(path: &str) -> u128 {
.last_processsed_block
}

pub async fn get_twitter_refresh_token(path: &str) -> String {
get_db(path)
.await
.read(|data| data.to_owned())
.await
.twitter_refresh_token
}

pub async fn set_last_processsed_block(path: &str, last_processsed_block: u128) {
let db = get_db(path).await;
db.write(|data| {
Expand All @@ -39,32 +30,18 @@ pub async fn set_last_processsed_block(path: &str, last_processsed_block: u128)
.await;
db.save().await.unwrap();
}

pub async fn set_twitter_refresh_token(path: &str, twitter_refresh_token: String) {
let db = get_db(path).await;
db.write(|data| {
data.twitter_refresh_token = twitter_refresh_token;
})
.await;
db.save().await.unwrap();
}
#[cfg(test)]
mod tests {

use super::{
get_last_processsed_block, get_twitter_refresh_token, set_last_processsed_block,
set_twitter_refresh_token,
};
use super::{get_last_processsed_block, set_last_processsed_block};
use std::fs;

#[tokio::test]
async fn test_db() {
let path = "./test_db";
set_last_processsed_block(path, 12).await;
let number = get_last_processsed_block(path).await;
let twitter = get_twitter_refresh_token(path).await;
println!("number {:?}:", number);
println!("twitter {:?}:", twitter);
assert!(fs::metadata(path).is_ok(), "File should exist");
fs::remove_file(path).unwrap();
assert!(fs::metadata(path).is_err(), "File shouldn't exist anymore");
Expand Down

0 comments on commit 5b15c77

Please sign in to comment.