Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed Mar 20, 2024
1 parent df38646 commit a0fea2b
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 65 deletions.
121 changes: 70 additions & 51 deletions crates/erc20_payment_lib/src/server/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use actix_web::http::header::HeaderValue;
use actix_web::http::{header, StatusCode};
use actix_web::web::Data;
use actix_web::{web, HttpRequest, HttpResponse, Responder, Scope};
use chrono::{DateTime, Utc};
use erc20_payment_lib_common::ops::*;
use erc20_payment_lib_common::utils::datetime_from_u256_timestamp;
use erc20_payment_lib_common::{export_metrics_to_prometheus, FaucetData};
Expand All @@ -20,7 +21,6 @@ use std::collections::BTreeMap;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
use chrono::{DateTime, Utc};
use tokio::sync::Mutex;
use web3::types::{Address, BlockId, BlockNumber, U256};

Expand Down Expand Up @@ -665,19 +665,18 @@ async fn new_transfer(

#[derive(Deserialize)]
pub struct StatsTransferRequest {
account: Option<String>,
receiver: Option<String>,
from: Option<String>,
to: Option<String>,
chain: Option<String>
chain: Option<String>,
}


#[derive(Debug, Serialize)]
pub struct StatsTransferResult {
request_time: f64,
transfers: Vec<ChainTransferRespObj>,
}


#[derive(Serialize, sqlx::FromRow, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ChainTransferRespObj {
Expand All @@ -694,60 +693,80 @@ pub struct ChainTransferRespObj {
pub block_timestamp: i64,
}


pub async fn stats_transfers(
data: Data<Box<ServerData>>,
info: web::Query<StatsTransferRequest>
info: web::Query<StatsTransferRequest>,
) -> actix_web::Result<web::Json<StatsTransferResult>> {
let account = if info.account.clone() == Some("all".to_string()) {
let time_start = std::time::Instant::now();
let receiver = if info.receiver.clone() == Some("all".to_string()) {
None
} else {
let account = Address::from_str(
&info.account.clone().ok_or(actix_web::error::ErrorBadRequest("account not found"))?,
&info
.receiver
.clone()
.ok_or(actix_web::error::ErrorBadRequest("account not found"))?,
)
.map_err(|err| {
actix_web::error::ErrorBadRequest(format!("account has to be valid address {err}"))
})?;
.map_err(|err| {
actix_web::error::ErrorBadRequest(format!("account has to be valid address {err}"))
})?;
Some(account)
};
let account_str =account.map(|account| format!("{:#x}", account));

let account_str = receiver.map(|account| format!("{:#x}", account));

let from = chrono::DateTime::from_timestamp(
i64::from_str(&info.from.clone().ok_or(actix_web::error::ErrorBadRequest("From not found"))?).map_err(
|err| actix_web::error::ErrorBadRequest(format!("From is not a valid timestamp {err}"))
)?, 0
i64::from_str(
&info
.from
.clone()
.ok_or(actix_web::error::ErrorBadRequest("From not found"))?,
)
.map_err(|err| {
actix_web::error::ErrorBadRequest(format!("From is not a valid timestamp {err}"))
})?,
0,
)
.ok_or(
actix_web::error::ErrorBadRequest("From is not a valid timestamp.")
)?;
.ok_or(actix_web::error::ErrorBadRequest(
"From is not a valid timestamp.",
))?;
let to = chrono::DateTime::from_timestamp(
i64::from_str(&info.to.clone().ok_or(actix_web::error::ErrorBadRequest("To not found"))?).map_err(
|err| actix_web::error::ErrorBadRequest(format!("To is not a valid timestamp {err}"))
)?, 0
i64::from_str(
&info
.to
.clone()
.ok_or(actix_web::error::ErrorBadRequest("To not found"))?,
)
.map_err(|err| {
actix_web::error::ErrorBadRequest(format!("To is not a valid timestamp {err}"))
})?,
0,
)
.ok_or(
actix_web::error::ErrorBadRequest("To is not a valid timestamp.")
)?;

let chain_id =
i64::from_str(&info.chain.clone().ok_or(actix_web::error::ErrorBadRequest("Chain id not found"))?).map_err(
|err| actix_web::error::ErrorBadRequest(format!("Chain id a valid {err}"))
)?;

.ok_or(actix_web::error::ErrorBadRequest(
"To is not a valid timestamp.",
))?;

let chain_id = i64::from_str(
&info
.chain
.clone()
.ok_or(actix_web::error::ErrorBadRequest("Chain id not found"))?,
)
.map_err(|err| actix_web::error::ErrorBadRequest(format!("Chain id a valid {err}")))?;

let conn = data.db_connection.lock().await.clone();
let transf = get_all_chain_transfers(&conn, chain_id, from, to, None).await;
let transf = transf.map_err(|err| {
actix_web::error::ErrorBadRequest(format!("Unknown server error: {}", err))
})?;

let txs = get_chain_txs_by_chain_id_and_dates(&conn, chain_id, from, to, None).await
.map_err(
|err| actix_web::error::ErrorBadRequest(format!("Unknown server error: {}", err))
)?;
let map_txs = txs.iter().map(|tx| (tx.id, tx.clone())).collect::<BTreeMap<_, _>>();

let transf = if let Some(receiver) = account_str.as_ref() {
let transf =
get_all_chain_transfers_by_receiver_ext(&conn, chain_id, from, to, receiver, None)
.await;
transf.map_err(|err| {
actix_web::error::ErrorBadRequest(format!("Unknown server error: {}", err))
})?
} else {
let transf = get_all_chain_transfers_ext(&conn, chain_id, from, to, None).await;
transf.map_err(|err| {
actix_web::error::ErrorBadRequest(format!("Unknown server error: {}", err))
})?
};

let mut resp = Vec::new();
for trans in transf.iter() {
Expand All @@ -764,27 +783,27 @@ pub async fn stats_transfers(
}
}

let tx = map_txs.get(&trans.chain_tx_id).cloned();
let Some(tx) = tx else {
continue;
};
resp.push(ChainTransferRespObj {
id: trans.id,
from_addr: trans.from_addr.clone(),
receiver_addr: trans.receiver_addr.clone(),
chain_id: trans.chain_id,
token_addr: trans.token_addr.clone(),
token_amount: trans.token_amount.clone(),
tx_hash: tx.tx_hash,
block_number: tx.block_number,
tx_hash: trans.tx_hash.clone(),
block_number: trans.block_number,
fee_paid: trans.fee_paid.clone(),
block_date: blockchain_date,
block_timestamp: blockchain_date.timestamp()
block_timestamp: blockchain_date.timestamp(),
})
}

let time_end = time_start.elapsed().as_secs_f64();
//serialize
Ok(web::Json(StatsTransferResult { transfers: resp }))
Ok(web::Json(StatsTransferResult {
request_time: time_end,
transfers: resp,
}))
}

pub async fn transfers(data: Data<Box<ServerData>>, req: HttpRequest) -> impl Responder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DROP INDEX "idx_chain_tx_tx_hash";
CREATE UNIQUE INDEX "idx_chain_tx_tx_hash" ON "chain_tx" ("tx_hash");
CREATE INDEX "idx_chain_tx_tx_hash" ON "chain_tx" ("tx_hash");
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use chrono::{DateTime, Utc};
use serde::{Serialize};
use serde::Serialize;

#[derive(Serialize, sqlx::FromRow, Debug, Clone)]
#[serde(rename_all = "camelCase")]
Expand All @@ -15,8 +15,6 @@ pub struct ChainTransferDbObj {
pub blockchain_date: Option<DateTime<Utc>>,
}


/*
#[derive(Serialize, sqlx::FromRow, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ChainTransferDbObjExt {
Expand All @@ -27,5 +25,8 @@ pub struct ChainTransferDbObjExt {
pub token_addr: Option<String>,
pub token_amount: String,
pub chain_tx_id: i64,
}*/
pub fee_paid: Option<String>,
pub blockchain_date: Option<DateTime<Utc>>,
pub tx_hash: String,
pub block_number: i64,
}
2 changes: 1 addition & 1 deletion crates/erc20_payment_lib_common/src/db/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod transfer_in_dao;
mod tx_dao;

pub use allowance_dao::AllowanceDbObj;
pub use chain_transfer_dao::ChainTransferDbObj;
pub use chain_transfer_dao::{ChainTransferDbObj, ChainTransferDbObjExt};
pub use chain_tx_dao::ChainTxDbObj;
pub use scan_dao::ScanDaoDbObj;
pub use token_transfer_dao::TokenTransferDbObj;
Expand Down
44 changes: 43 additions & 1 deletion crates/erc20_payment_lib_common/src/db/ops/chain_transfer_ops.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use chrono::{DateTime, Utc};
use super::model::ChainTransferDbObj;
use crate::model::ChainTransferDbObjExt;
use chrono::{DateTime, Utc};
use sqlx::Executor;
use sqlx::Sqlite;
use sqlx::SqlitePool;
Expand Down Expand Up @@ -29,6 +30,47 @@ VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;
.await?;
Ok(res)
}
pub async fn get_all_chain_transfers_ext(
conn: &SqlitePool,
chain_id: i64,
from: DateTime<Utc>,
to: DateTime<Utc>,
limit: Option<i64>,
) -> Result<Vec<ChainTransferDbObjExt>, sqlx::Error> {
let limit = limit.unwrap_or(i64::MAX);
let rows = sqlx::query_as::<_, ChainTransferDbObjExt>(
r"SELECT ct.*, cx.tx_hash, cx.block_number FROM chain_transfer as ct JOIN chain_tx as cx ON ct.chain_tx_id = cx.id WHERE ct.chain_id = $1 AND ct.blockchain_date >= $2 AND ct.blockchain_date <= $3 ORDER by id DESC LIMIT $4",
)
.bind(chain_id)
.bind(from)
.bind(to)
.bind(limit)
.fetch_all(conn)
.await?;
Ok(rows)
}

pub async fn get_all_chain_transfers_by_receiver_ext(
conn: &SqlitePool,
chain_id: i64,
from: DateTime<Utc>,
to: DateTime<Utc>,
receiver: &str,
limit: Option<i64>,
) -> Result<Vec<ChainTransferDbObjExt>, sqlx::Error> {
let limit = limit.unwrap_or(i64::MAX);
let rows = sqlx::query_as::<_, ChainTransferDbObjExt>(
r"SELECT ct.*, cx.tx_hash, cx.block_number FROM chain_transfer as ct JOIN chain_tx as cx ON ct.chain_tx_id = cx.id WHERE ct.chain_id = $1 AND ct.blockchain_date >= $2 AND ct.blockchain_date <= $3 AND ct.receiver_addr = $4 ORDER by id DESC LIMIT $5",
)
.bind(chain_id)
.bind(from)
.bind(to)
.bind(receiver)
.bind(limit)
.fetch_all(conn)
.await?;
Ok(rows)
}

pub async fn get_all_chain_transfers(
conn: &SqlitePool,
Expand Down
10 changes: 5 additions & 5 deletions crates/erc20_payment_lib_common/src/db/ops/chain_tx_ops.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use chrono::{DateTime, Utc};
use super::model::ChainTxDbObj;
use chrono::{DateTime, Utc};
use sqlx::Executor;
use sqlx::Sqlite;
use sqlx::SqlitePool;
Expand Down Expand Up @@ -51,10 +51,10 @@ pub async fn get_chain_txs_by_chain_id(
let rows = sqlx::query_as::<_, ChainTxDbObj>(
r"SELECT * FROM chain_tx WHERE chain_id = $1 ORDER by id DESC LIMIT $2",
)
.bind(chain_id)
.bind(limit)
.fetch_all(conn)
.await?;
.bind(chain_id)
.bind(limit)
.fetch_all(conn)
.await?;
Ok(rows)
}

Expand Down
1 change: 0 additions & 1 deletion src/actions/scan_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ pub async fn scan_blockchain_local(
}
}


scan_int(
conn.clone(),
&scan_blockchain_options,
Expand Down

0 comments on commit a0fea2b

Please sign in to comment.