Skip to content

Commit

Permalink
start of refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetbout committed Sep 9, 2023
1 parent 70edf2d commit 955aa56
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
5 changes: 5 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
merge_imports = true
reorder_imports = true
reorder_modules = true
# TODO https://rust-lang.github.io/rustfmt/?version=v1.4.36&search=#reorder_modules
# Use cargo +nightly fmt
47 changes: 24 additions & 23 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use bigdecimal::num_traits;
use dotenv::dotenv;
use num_traits::cast::ToPrimitive;
use reqwest::header::HeaderValue;
use reqwest::{header, Url};
use reqwest::{header, header::HeaderValue, Error, Url};
use serde::{Deserialize, Serialize};
use starknet::core::types::{BlockId, BlockTag, EventFilter, FieldElement, FunctionCall};
use starknet::core::utils::get_selector_from_name;
use starknet::providers::Provider;
use starknet::providers::{jsonrpc::HttpTransport, JsonRpcClient};
use starknet::{
core::{
types::{BlockId, BlockTag, EventFilter, FieldElement, FunctionCall},
utils::get_selector_from_name,
},
providers::{
jsonrpc::{HttpTransport, HttpTransportError, JsonRpcClientError},
JsonRpcClient, Provider, ProviderError,
},
};
use std::time::Duration;

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -81,21 +85,17 @@ pub async fn fetch_coin(coin_id: &str) -> Result<f64, reqwest::Error> {
Ok(coin_info.data.price_usd.parse().unwrap())
}

pub async fn fetch_events(token: Token) -> Result<(), reqwest::Error> {
dotenv().ok();
let api_key = dotenv!("NODE_PROVIDER_API_KEY");

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);

pub async fn fetch_events(
rpc_client: JsonRpcClient<HttpTransport>,
token: &Token,
from_block: u64,
to_block: u64,
) -> Result<(), reqwest::Error> {
let events = rpc_client
.get_events(
EventFilter {
from_block: Some(BlockId::Number(181710)),
to_block: Some(BlockId::Number(181711)),
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()]]),
},
Expand Down Expand Up @@ -126,10 +126,11 @@ pub async fn fetch_events(token: Token) -> Result<(), reqwest::Error> {
Ok(())
}

async fn address_to_domain(address: FieldElement, contract_addr: FieldElement) {
let api_key = dotenv!("NODE_PROVIDER_API_KEY");
let rpc_url = format!("https://starknet-mainnet.infura.io/v3/{api_key}");
let rpc_client = JsonRpcClient::new(HttpTransport::new(Url::parse(&rpc_url).unwrap()));
async fn address_to_domain(
rpc_client: JsonRpcClient<HttpTransport>,
address: FieldElement,
contract_addr: FieldElement,
) {
let repsonse = rpc_client
.call(
FunctionCall {
Expand Down
2 changes: 1 addition & 1 deletion src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub struct SimpleLogger;

pub fn init() {
log::set_logger(&SimpleLogger)
.map(|()| log::set_max_level(LevelFilter::Trace))
.map(|()| log::set_max_level(LevelFilter::Info))
.expect("Couldn't setup the logger");
}

Expand Down
20 changes: 20 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use api::Token;
use dotenv::dotenv;
use log::info;
use reqwest::Url;
use starknet::providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider};
use std::error::Error;

use crate::api::fetch_events;
#[macro_use]
extern crate dotenv_codegen;

Expand All @@ -25,6 +30,15 @@ const TOKENS: &'static [Token] = &[ETH];
async fn main() -> Result<(), Box<dyn Error>> {
logger::init();
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)
.await
.unwrap();
info!("Events: {:?}", events);

// twitter::tweet("Someteaeazzhing".to_string()).await;

Ok(())
Expand All @@ -40,3 +54,9 @@ fn check_valid_env() {
std::env::var("TWITTER_OAUTH2_CLIENT_SECRET")
.expect("TWITTER_OAUTH2_CLIENT_SECRET must be set.");
}

fn get_infura_client() -> JsonRpcClient<HttpTransport> {
let api_key = dotenv!("NODE_PROVIDER_API_KEY");
let rpc_url = format!("https://starknet-mainnet.infura.io/v3/{api_key}");
JsonRpcClient::new(HttpTransport::new(Url::parse(&rpc_url).unwrap()))
}

0 comments on commit 955aa56

Please sign in to comment.