From 860dee2dfce11b8aa10728e89c95f4eecf6f53b7 Mon Sep 17 00:00:00 2001 From: gaetbout Date: Sat, 9 Sep 2023 12:59:25 +0200 Subject: [PATCH] handling eth --- db/block.json | 2 +- src/api.rs | 55 +++++++++++++++++++--------------------------- src/logger.rs | 33 +++++++++------------------- src/main.rs | 18 ++++++++++++--- token.example.json | 7 +++++- 5 files changed, 54 insertions(+), 61 deletions(-) diff --git a/db/block.json b/db/block.json index 0d51f0b..3958457 100644 --- a/db/block.json +++ b/db/block.json @@ -1 +1 @@ -{"lastProcesssedBlockNumber":205800} \ No newline at end of file +{ "lastProcesssedBlockNumber": 205800 } diff --git a/src/api.rs b/src/api.rs index 4dc8dd5..97c96ed 100644 --- a/src/api.rs +++ b/src/api.rs @@ -4,7 +4,7 @@ use reqwest::{header, header::HeaderValue, Error, Url}; use serde::{Deserialize, Serialize}; use starknet::{ core::{ - types::{BlockId, BlockTag, EventFilter, FieldElement, FunctionCall}, + types::{BlockId, BlockTag, EmittedEvent, EventFilter, FieldElement, FunctionCall}, utils::get_selector_from_name, }, providers::{ @@ -90,40 +90,29 @@ pub async fn fetch_events( token: &Token, from_block: u64, to_block: u64, -) -> Result<(), reqwest::Error> { - let events = rpc_client - .get_events( - EventFilter { - from_block: Some(BlockId::Number(from_block)), - to_block: Some(BlockId::Number(to_block)), - address: Some(FieldElement::from_hex_be(token.address).unwrap()), - keys: Some(vec![vec![get_selector_from_name(token.selector).unwrap()]]), - }, - None, - 1000, - ) - .await - .unwrap(); +) -> Result, reqwest::Error> { + let mut events = vec![]; + let mut continuation_token = Some("0".to_string()); - println!("{:?}", events.events.len()); - println!( - "Cont token: {:?}", - events - .continuation_token - .unwrap_or("No continuation token".to_string()) - ); - println!("decimals: {:?}", token.decimals); - println!("threshold: {:?}", token.threshold); - let threshold = FieldElement::from((10_u128.pow(token.decimals.into())) * token.threshold); - let filtered_events: Vec<_> = events - .events - .iter() - .filter(|event| event.data[2] > threshold) - .collect(); - // TODO Handle the data part 2? - println!("{:?}", filtered_events); + while let Some(cont_token) = continuation_token { + let event_page = rpc_client + .get_events( + EventFilter { + from_block: Some(BlockId::Number(from_block)), + to_block: Some(BlockId::Number(to_block)), + address: Some(FieldElement::from_hex_be(token.address).unwrap()), + keys: Some(vec![vec![get_selector_from_name(token.selector).unwrap()]]), + }, + Some(cont_token), + 1000, + ) + .await + .unwrap(); - Ok(()) + continuation_token = event_page.continuation_token; + events.extend(event_page.events); + } + Ok(events) } async fn address_to_domain( diff --git a/src/logger.rs b/src/logger.rs index b35862f..f2474fb 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -1,4 +1,4 @@ -use log::{Level, LevelFilter, Metadata, Record}; +use log::{LevelFilter, Metadata, Record}; // use log::{debug, error, info, trace, warn}; pub struct SimpleLogger; @@ -16,28 +16,15 @@ impl log::Log for SimpleLogger { fn log(&self, record: &Record) { if self.enabled(record.metadata()) { - match record.level() { - Level::Trace => { - println!( - "\t{} {:<10}:{} {:<6}{}", - date(), - record.file().unwrap(), - record.line().unwrap(), - record.level(), - record.args() - ); - } - _ => { - println!( - "{} {:<10}:{} {:<6}{}", - date(), - record.file().unwrap(), - record.line().unwrap(), - record.level(), - record.args() - ); - } - } + // println!( + // "{} {:<10}:{} {:<6}{}", + // date(), + // record.file().unwrap(), + // record.line().unwrap(), + // record.level(), + // record.args() + // ); + println!("{} {:<6}{}", date(), record.level(), record.args()); } } diff --git a/src/main.rs b/src/main.rs index fa66908..fb0b470 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,10 @@ use api::Token; use dotenv::dotenv; use log::info; use reqwest::Url; -use starknet::providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider}; +use starknet::{ + core::types::FieldElement, + providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider}, +}; use std::error::Error; use crate::api::fetch_events; @@ -29,17 +32,26 @@ const TOKENS: &'static [Token] = &[ETH]; #[tokio::main] async fn main() -> Result<(), Box> { logger::init(); + info!("Start"); check_valid_env(); let rpc_client = get_infura_client(); let current_block = rpc_client.block_number().await.unwrap(); info!("Current number: {}", current_block); - let events = fetch_events(rpc_client, &TOKENS[0], current_block - 2, current_block - 1) + let token = &TOKENS[0]; + let events = fetch_events(rpc_client, token, current_block - 5, current_block - 1) .await .unwrap(); - info!("Events: {:?}", events); + info!("Events len: {:?}", events.len()); + let threshold = FieldElement::from((10_u128.pow(token.decimals.into())) * token.threshold); + let filtered_events: Vec<_> = events + .iter() + .filter(|event| event.data[2] > threshold) + .collect(); + println!("{:?}", filtered_events); // twitter::tweet("Someteaeazzhing".to_string()).await; + info!("End"); Ok(()) } diff --git a/token.example.json b/token.example.json index 47f5a2c..78bbc66 100644 --- a/token.example.json +++ b/token.example.json @@ -1 +1,6 @@ -{"access_token":"???","refresh_token":"???","expires":"2023-09-06T16:27:53.589878Z","scopes":["tweet.write","users.read","tweet.read","offline.access"]} \ No newline at end of file +{ + "access_token": "???", + "refresh_token": "???", + "expires": "2023-09-06T16:27:53.589878Z", + "scopes": ["tweet.write", "users.read", "tweet.read", "offline.access"] +}