Skip to content

Commit

Permalink
Add parser code
Browse files Browse the repository at this point in the history
  • Loading branch information
gemcoder21 committed Sep 4, 2023
1 parent f233ae3 commit d44b979
Show file tree
Hide file tree
Showing 21 changed files with 313 additions and 12 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ members = [
"fiat",
"name_resolver",
"api_connector",
"ns_address_codec"
"ns_address_codec",
"parser",
]

default-members = [
"bin/generate",
"deamon",
"api",
"parser"
]

[workspace.dependencies]
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ FROM debian:bullseye AS runtime
WORKDIR app
COPY --from=builder /app/target/release/api /app
COPY --from=builder /app/target/release/deamon /app
COPY --from=builder /app/target/release/parser /app
COPY --from=builder /app/Settings.toml /app
RUN apt-get update && apt-get install -y openssl ca-certificates libpq-dev postgresql

Expand Down
3 changes: 3 additions & 0 deletions Settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ path = "/metrics"

[assets]
url = "https://raw.githubusercontent.com/gemwalletcom/assets/master"

[chains.binance]
url = "https://dex.binance.org"
4 changes: 3 additions & 1 deletion blockchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ version = { workspace = true }
[dependencies]
typeshare = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
serde_json = { workspace = true }
reqwest = { workspace = true }
async-trait = { workspace = true }
54 changes: 54 additions & 0 deletions blockchain/src/bnbchain/client.rs
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![]);
}
}
3 changes: 3 additions & 0 deletions blockchain/src/bnbchain/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod models;
pub mod model;
pub mod client;
36 changes: 36 additions & 0 deletions blockchain/src/bnbchain/model.rs
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,
}
13 changes: 9 additions & 4 deletions blockchain/src/bnbchain/models/bnbchain_account.rs
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,
}
13 changes: 8 additions & 5 deletions blockchain/src/bnbchain/models/bnbchain_balance.rs
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
}
2 changes: 2 additions & 0 deletions blockchain/src/bnbchain/models/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod bnbchain_account;
pub mod bnbchain_balance;
13 changes: 12 additions & 1 deletion blockchain/src/lib.rs
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>>;
}
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ services:
- redis
- postgres

parser:
image: app
container_name: deamon
environment:
BINARY: deamon
POSTGRES_URL: "postgres://username:password@postgres/api"
depends_on:
- app_build
- postgres

redis:
image: redis:7.0-alpine
container_name: redis
Expand Down
13 changes: 13 additions & 0 deletions parser/Cargo.toml
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 }
65 changes: 65 additions & 0 deletions parser/src/main.rs
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;
}
}
}
13 changes: 13 additions & 0 deletions settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Settings {
pub name: Name,
pub metrics: Metrics,
pub assets: Assets,
pub chains: Chains
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -153,6 +154,18 @@ pub struct Assets {
pub url: String,
}

#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct Chains {
pub binance: Chain,
}

#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct Chain {
pub url: String,
}

impl Settings {
pub fn new() -> Result<Self, ConfigError> {
let s = Config::builder()
Expand Down
23 changes: 23 additions & 0 deletions storage/src/database.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use diesel::dsl::sql;
use diesel::{Connection, upsert::excluded};
use diesel::pg::PgConnection;
use primitives::chain::Chain;
use crate::models::*;
use diesel::prelude::*;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
Expand Down Expand Up @@ -222,6 +223,28 @@ impl DatabaseClient {
.execute(&mut self.connection)
}

pub fn get_parser_state(&mut self, _chain: Chain) -> Result<ParserState, diesel::result::Error> {
use crate::schema::parser_state::dsl::*;
parser_state
.filter(chain.eq(_chain.as_str()))
.select(ParserState::as_select())
.first(&mut self.connection)
}

pub fn set_parser_state_latest_block(&mut self, _chain: Chain, block: i32) -> Result<usize, diesel::result::Error> {
use crate::schema::parser_state::dsl::*;
diesel::update(parser_state.find(_chain.as_str()))
.set(latest_block.eq(block))
.execute(&mut self.connection)
}

pub fn set_parser_state_current_block(&mut self, _chain: Chain, block: i32) -> Result<usize, diesel::result::Error> {
use crate::schema::parser_state::dsl::*;
diesel::update(parser_state.find(_chain.as_str()))
.set(current_block.eq(block))
.execute(&mut self.connection)
}

pub fn migrations(&mut self) {
self.connection.run_pending_migrations(MIGRATIONS).unwrap();
}
Expand Down
1 change: 1 addition & 0 deletions storage/src/migrations/2023-09-03-220931_parser/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- This file should undo anything in `up.sql`
Loading

0 comments on commit d44b979

Please sign in to comment.