-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55ca54b
commit 2cd7b36
Showing
3 changed files
with
147 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name = "updater" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
base64 = "0.22.0" | ||
clap = { version = "4.5.1", features = ["derive"] } | ||
crossbeam = "0.8.4" | ||
env_logger = "0.11.2" | ||
ethers = { version = "2.0.13", features = ["abigen","legacy"] } | ||
log = "0.4.20" | ||
num_cpus = "1.16.0" | ||
reqwest = { version = "0.12.0", features = ["json"] } | ||
rustc-hex = "2.1.0" | ||
serde = "1.0.197" | ||
serde_json = "1.0.114" | ||
sha2 = "0.10.8" | ||
sqlx = { version = "0.7.3", features = ["bigdecimal", "runtime-tokio", "postgres", "chrono", "json"]} | ||
tokio = { version = "1.36.0", features = ["full"]} | ||
toml = "0.8.12" | ||
url = "2.5.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
mod db; | ||
mod error; | ||
mod updater; | ||
|
||
use crate::db::Storage; | ||
use crate::error::Result; | ||
use crate::updater::Updater; | ||
use clap::Parser; | ||
use ethers::contract::abigen; | ||
use ethers::prelude::{Http, Provider}; | ||
use ethers::types::Address; | ||
use log::info; | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::pool::PoolOptions; | ||
use sqlx::{Pool, Postgres}; | ||
use std::fs::File; | ||
use std::io::Read; | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
|
||
const DEFAULT_RPC_RETRIES: usize = 3; | ||
const DEFAULT_INTERVAL: u64 = 15; // 15s | ||
|
||
abigen!(RewardContract, "../abi/Reward.json"); | ||
abigen!(StakingContract, "../abi/Staking.json"); | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct UpdaterConfig { | ||
pub evm_rpc: String, | ||
pub staking: String, | ||
pub reward: String, | ||
pub db_url: String, | ||
} | ||
|
||
impl UpdaterConfig { | ||
pub fn new(file_path: &str) -> Result<Self> { | ||
let mut f = File::open(file_path)?; | ||
let mut s = String::new(); | ||
f.read_to_string(&mut s)?; | ||
let c: UpdaterConfig = toml::from_str(&s)?; | ||
Ok(c) | ||
} | ||
} | ||
|
||
#[derive(Parser, Debug)] | ||
struct Args { | ||
/// Node RPC | ||
#[arg(long)] | ||
pub node: String, | ||
/// Block height to start scanning | ||
#[arg(long)] | ||
pub start: Option<u64>, | ||
/// Interval of scanning in seconds | ||
#[arg(long)] | ||
pub interval: Option<u64>, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
env_logger::init(); | ||
|
||
let config = UpdaterConfig::new("./config.toml")?; | ||
info!("EVM RPC: {}", config.evm_rpc); | ||
info!("Staking contract: {}", config.staking); | ||
info!("Reward contract: {}", config.reward); | ||
|
||
let pool: Pool<Postgres> = PoolOptions::new() | ||
.connect(&config.db_url) | ||
.await | ||
.expect("can't connect to database"); | ||
info!("Connecting db...ok"); | ||
|
||
let storage = Storage::new(pool); | ||
let args = Args::parse(); | ||
let interval = if let Some(interval) = args.interval { | ||
Duration::from_secs(interval) | ||
} else { | ||
Duration::from_secs(DEFAULT_INTERVAL) | ||
}; | ||
|
||
let provider = Provider::<Http>::try_from(config.evm_rpc)?; | ||
let staking_addr: Address = config.staking.parse()?; | ||
let staking = StakingContract::new(staking_addr, Arc::new(provider.clone())); | ||
let reward_addr: Address = config.reward.parse()?; | ||
let reward = RewardContract::new(reward_addr, Arc::new(provider.clone())); | ||
info!("Updating interval: {}s", interval.as_secs()); | ||
let updater = Updater::new(DEFAULT_RPC_RETRIES, provider, staking, reward, storage); | ||
let _ = updater.run().await?; | ||
|
||
Ok(()) | ||
} |