-
Notifications
You must be signed in to change notification settings - Fork 7
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
f233ae3
commit d44b979
Showing
21 changed files
with
313 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,54 @@ | ||
use std::error::Error; | ||
|
||
use crate::ChainProvider; | ||
use async_trait::async_trait; | ||
|
||
use super::model::{Block, NodeInfo}; | ||
|
||
pub struct BNBChainClient { | ||
url: String, | ||
api_url: String, | ||
|
||
client: reqwest::Client, | ||
} | ||
|
||
impl BNBChainClient { | ||
pub fn new(url: String, api_url: String) -> Self { | ||
let client = reqwest::Client::new(); | ||
Self { | ||
url, | ||
api_url, | ||
client, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl ChainProvider for BNBChainClient { | ||
async fn get_latest_block(&self) -> Result<i32, Box<dyn Error>> { | ||
let url = format!("{}/api/v1/node-info", self.url); | ||
let response = self.client | ||
.get(url) | ||
.send() | ||
.await? | ||
.json::<NodeInfo>() | ||
.await?; | ||
|
||
return Ok(response.sync_info.latest_block_height); | ||
} | ||
|
||
async fn get_transactions(&self, block: i32) -> Result<Vec<i32>, Box<dyn Error>> { | ||
let url = format!("{}/bc/api/v1/blocks/{}/txs", self.api_url, block); | ||
|
||
let response = self.client | ||
.get(url) | ||
.send() | ||
.await? | ||
.json::<Block>() | ||
.await?; | ||
|
||
let _transactions = response.txs; | ||
|
||
return Ok(vec![]); | ||
} | ||
} |
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,3 @@ | ||
pub mod models; | ||
pub mod model; | ||
pub mod client; |
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,36 @@ | ||
use typeshare::typeshare; | ||
use serde::{Serialize, Deserialize}; | ||
|
||
#[typeshare] | ||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Block { | ||
pub txs: Vec<Transaction> | ||
} | ||
|
||
#[typeshare] | ||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Transaction { | ||
pub hash: String, | ||
pub r#type: String, | ||
pub fee: i64, | ||
pub memo: String, | ||
pub asset: Option<String>, | ||
pub amount: Option<i64>, | ||
pub from_addr: String, | ||
pub to_addr: Option<String>, | ||
pub block_height: i64, | ||
pub sequence: i64, | ||
} | ||
|
||
#[typeshare] | ||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct NodeInfo { | ||
pub sync_info: SyncInfo, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct SyncInfo { | ||
pub latest_block_height: i32, | ||
} |
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 |
---|---|---|
@@ -1,6 +1,11 @@ | ||
use typeshare::typeshare; | ||
|
||
use super::bnbchain_balance::BNBChainBalance; | ||
|
||
#[typeshare] | ||
struct BNBChainAccount { | ||
balances: Vec<BNBChainBalance>, | ||
sequence: Int, | ||
account_number: Int, | ||
#[allow(dead_code)] | ||
pub struct BNBChainAccount { | ||
pub balances: Vec<BNBChainBalance>, | ||
pub sequence: i32, | ||
pub account_number: i32, | ||
} |
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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
use typeshare::typeshare; | ||
|
||
#[typeshare] | ||
struct BNBChainBalance { | ||
free: String, | ||
frozen: String, | ||
locked: String, | ||
symbol: String | ||
#[allow(dead_code)] | ||
pub struct BNBChainBalance { | ||
pub free: String, | ||
pub frozen: String, | ||
pub locked: String, | ||
pub symbol: String | ||
} |
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,2 @@ | ||
pub mod bnbchain_account; | ||
pub mod bnbchain_balance; |
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 |
---|---|---|
@@ -1,3 +1,14 @@ | ||
// lib.rs | ||
|
||
mod aptos; | ||
mod aptos; | ||
mod bnbchain; | ||
use std::error::Error; | ||
|
||
pub use self::bnbchain::client::BNBChainClient; | ||
use async_trait::async_trait; | ||
|
||
#[async_trait] | ||
pub trait ChainProvider { | ||
async fn get_latest_block(&self) -> Result<i32, Box<dyn Error>>; | ||
async fn get_transactions(&self, block: i32) -> Result<Vec<i32>, Box<dyn Error>>; | ||
} |
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,13 @@ | ||
[package] | ||
name = "parser" | ||
version = { workspace = true } | ||
edition = { workspace = true } | ||
|
||
[dependencies] | ||
serde = { workspace = true } | ||
serde_json = { workspace = true } | ||
primitives = { path = "../primitives" } | ||
storage = { path = "../storage" } | ||
settings = { path = "../settings" } | ||
blockchain = { path = "../blockchain" } | ||
tokio = { workspace = true } |
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,65 @@ | ||
use std::{thread::{sleep, self}, time::Duration}; | ||
|
||
use blockchain::ChainProvider; | ||
use primitives::chain::Chain; | ||
use settings::Settings; | ||
use storage::database::DatabaseClient; | ||
|
||
#[tokio::main] | ||
pub async fn main() { | ||
println!("Hello, parser!"); | ||
|
||
let settings: Settings = Settings::new().unwrap(); | ||
let mut database_client: DatabaseClient = DatabaseClient::new(&settings.postgres.url); | ||
let bnbchain_client = blockchain::BNBChainClient::new( | ||
settings.chains.binance.url.to_string(), | ||
"https://api.binance.org".to_string() | ||
); | ||
|
||
loop { | ||
let state = database_client.get_parser_state(Chain::Binance).unwrap(); | ||
|
||
let latest_block = bnbchain_client.get_latest_block().await; | ||
match latest_block { | ||
Ok(latest_block) => { | ||
let _ = database_client.set_parser_state_latest_block(Chain::Binance, latest_block); | ||
if state.current_block >= state.latest_block { | ||
println!("parser ahead. current_block: {}, latest_block: {}", state.current_block, state.latest_block); | ||
|
||
thread::sleep(Duration::from_secs(2)); continue; | ||
} | ||
}, | ||
Err(err) => { | ||
println!("latest_block error: {:?}", err); | ||
|
||
sleep(Duration::from_secs(2)); continue; | ||
} | ||
} | ||
|
||
println!("current_block: {}, latest_block: {}", state.current_block, state.latest_block); | ||
|
||
let mut next_block = state.current_block + 1; | ||
|
||
loop { | ||
println!("next_block: {:?}, to go: {}", next_block, state.latest_block - next_block); | ||
|
||
let transactions = bnbchain_client.get_transactions(next_block).await; | ||
match transactions { | ||
Ok(_) => { | ||
let _ = database_client.set_parser_state_current_block(Chain::Binance, next_block); | ||
|
||
//println!("transactions: {:?}", transactions); | ||
}, | ||
Err(err) => { | ||
println!("get transactions error: {:?}", err); | ||
} | ||
} | ||
|
||
if next_block >= state.latest_block || next_block % 100 == 0 { | ||
break | ||
} | ||
|
||
next_block += 1; | ||
} | ||
} | ||
} |
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
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 @@ | ||
-- This file should undo anything in `up.sql` |
Oops, something went wrong.