Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scx1332/import txs #68

Merged
merged 12 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ panic = "unwind"

[profile.release-lto]
inherits = "release"
codegen-units = 1
lto = "fat"
opt-level = 3
panic = "abort"
incremental = false
opt-level = 1
panic = "unwind"

#[profile.release-lto]
#inherits = "release"
#codegen-units = 1
#lto = "fat"
#opt-level = 3
#panic = "abort"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE UNIQUE INDEX "chain_tx_tx_hash" ON "chain_tx" (tx_hash);

ALTER TABLE "chain_transfer" ADD COLUMN "fee_paid" TEXT NULL;
ALTER TABLE "chain_transfer" ADD COLUMN "blockchain_date" DATETIME NULL;

10 changes: 10 additions & 0 deletions crates/erc20_payment_lib/migrations/20231019000000_scan.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE "scan_info"
(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
chain_id INTEGER NOT NULL,
filter TEXT NOT NULL,
start_block INTEGER NOT NULL,
last_block INTEGER NOT NULL
);

CREATE UNIQUE INDEX "idx_scan_info_chain_id" ON "scan_info" ("chain_id", "filter");
4 changes: 3 additions & 1 deletion crates/erc20_payment_lib/src/db/model.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
mod allowance_dao;
mod chain_transfer_dao;
mod chain_tx_dao;
mod scan_dao;
mod token_transfer_dao;
mod transfer_in_dao;
mod tx_dao;

pub use allowance_dao::AllowanceDao;
pub use chain_transfer_dao::{ChainTransferDao, ChainTransferDaoExt};
pub use chain_transfer_dao::ChainTransferDao;
pub use chain_tx_dao::ChainTxDao;
pub use scan_dao::ScanDao;
pub use token_transfer_dao::TokenTransferDao;
pub use transfer_in_dao::TransferInDao;
pub use tx_dao::TxDao;
8 changes: 5 additions & 3 deletions crates/erc20_payment_lib/src/db/model/chain_transfer_dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ pub struct ChainTransferDao {
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>>,
}

/*
#[derive(Serialize, sqlx::FromRow, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ChainTransferDaoExt {
Expand All @@ -23,5 +25,5 @@ pub struct ChainTransferDaoExt {
pub token_addr: Option<String>,
pub token_amount: String,
pub chain_tx_id: i64,
pub blockchain_date: Option<DateTime<Utc>>,
}

}*/
11 changes: 11 additions & 0 deletions crates/erc20_payment_lib/src/db/model/scan_dao.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use serde::Serialize;

#[derive(Serialize, sqlx::FromRow, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ScanDao {
pub id: i64,
pub chain_id: i64,
pub filter: String,
pub start_block: i64,
pub last_block: i64,
}
2 changes: 2 additions & 0 deletions crates/erc20_payment_lib/src/db/ops.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
mod allowance_ops;
mod chain_transfer_ops;
mod chain_tx_ops;
mod scan_ops;
mod token_transfer_ops;
mod transfer_in_ops;
mod tx_ops;

pub use allowance_ops::*;
pub use chain_transfer_ops::*;
pub use chain_tx_ops::*;
pub use scan_ops::*;
use std::future::Future;
use std::time::Duration;
pub use token_transfer_ops::*;
Expand Down
40 changes: 29 additions & 11 deletions crates/erc20_payment_lib/src/db/ops/chain_transfer_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ where
{
let res = sqlx::query_as::<_, ChainTransferDao>(
r"INSERT INTO chain_transfer
(from_addr, receiver_addr, chain_id, token_addr, token_amount, chain_tx_id)
VALUES ($1, $2, $3, $4, $5, $6) RETURNING *;
(from_addr, receiver_addr, chain_id, token_addr, token_amount, chain_tx_id, fee_paid, blockchain_date)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;
",
)
.bind(&chain_transfer.from_addr)
Expand All @@ -22,21 +22,39 @@ VALUES ($1, $2, $3, $4, $5, $6) RETURNING *;
.bind(&chain_transfer.token_addr)
.bind(&chain_transfer.token_amount)
.bind(chain_transfer.chain_tx_id)
.bind(&chain_transfer.fee_paid)
.bind(chain_transfer.blockchain_date)
.fetch_one(executor)
.await?;
Ok(res)
}

pub async fn get_account_chain_transfers(
pub async fn get_all_chain_transfers(
conn: &SqlitePool,
account: &str,
) -> Result<Vec<ChainTransferDaoExt>, sqlx::Error> {
let rows = sqlx::query_as::<_, ChainTransferDaoExt>(r"
SELECT ct.id, ct.chain_id, ct.from_addr, ct.receiver_addr, ct.token_addr, ct.chain_tx_id, ct.token_amount, cx.blockchain_date
FROM chain_transfer as ct
JOIN chain_tx as cx on ct.chain_tx_id = cx.id
WHERE ct.receiver_addr = $1
").bind(account).fetch_all(conn).await?;
limit: Option<i64>,
) -> Result<Vec<ChainTransferDao>, sqlx::Error> {
let limit = limit.unwrap_or(i64::MAX);
let rows = sqlx::query_as::<_, ChainTransferDao>(
r"SELECT * FROM chain_transfer ORDER by id DESC LIMIT $1",
)
.bind(limit)
.fetch_all(conn)
.await?;
Ok(rows)
}

pub async fn get_chain_transfers_by_chain_id(
conn: &SqlitePool,
chain_id: i64,
limit: Option<i64>,
) -> Result<Vec<ChainTransferDao>, sqlx::Error> {
let limit = limit.unwrap_or(i64::MAX);
let rows = sqlx::query_as::<_, ChainTransferDao>(
r"SELECT * FROM chain_transfer WHERE chain_id = $1 ORDER by id DESC LIMIT $2",
)
.bind(chain_id)
.bind(limit)
.fetch_all(conn)
.await?;
Ok(rows)
}
27 changes: 27 additions & 0 deletions crates/erc20_payment_lib/src/db/ops/chain_tx_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,33 @@ pub async fn get_chain_tx(conn: &SqlitePool, id: i64) -> Result<ChainTxDao, sqlx
Ok(row)
}

pub async fn get_chain_tx_hash<'c, E>(
executor: E,
tx_hash: String,
) -> Result<Option<ChainTxDao>, sqlx::Error>
where
E: Executor<'c, Database = Sqlite>,
{
let row = sqlx::query_as::<_, ChainTxDao>(r"SELECT * FROM chain_tx WHERE tx_hash = $1")
.bind(tx_hash)
.fetch_optional(executor)
.await?;
Ok(row)
}

pub async fn get_last_scanned_block<'c, E>(
executor: E,
chain_id: i64,
) -> Result<Option<i64>, sqlx::Error>
where
E: Executor<'c, Database = Sqlite>,
{
sqlx::query_scalar::<_, i64>(r"SELECT MAX(block_number) FROM chain_tx WHERE chain_id = $1")
.bind(chain_id)
.fetch_optional(executor)
.await
}

#[tokio::test]
async fn tx_chain_test() -> sqlx::Result<()> {
println!("Start tx_chain_test...");
Expand Down
99 changes: 99 additions & 0 deletions crates/erc20_payment_lib/src/db/ops/scan_ops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use crate::db::model::ScanDao;
use sqlx::{Executor, Sqlite};

pub async fn get_scan_info<'c, E>(
executor: E,
chain_id: i64,
filter: &str,
) -> Result<Option<ScanDao>, sqlx::Error>
where
E: Executor<'c, Database = Sqlite>,
{
let row = sqlx::query_as::<_, ScanDao>(
r"SELECT * FROM scan_info WHERE chain_id = $1 AND filter = $2",
)
.bind(chain_id)
.bind(filter)
.fetch_optional(executor)
.await?;
Ok(row)
}

pub async fn delete_scan_info<'c, E>(
executor: E,
chain_id: i64,
filter: &str,
) -> Result<(), sqlx::Error>
where
E: Executor<'c, Database = Sqlite>,
{
sqlx::query(r"DELETE FROM scan_info WHERE chain_id = $1 AND filter = $2")
.bind(chain_id)
.bind(filter)
.execute(executor)
.await?;
Ok(())
}

pub async fn upsert_scan_info<'c, E>(
executor: E,
scan_dao: &ScanDao,
) -> Result<ScanDao, sqlx::Error>
where
E: Executor<'c, Database = Sqlite>,
{
let res = sqlx::query_as::<_, ScanDao>(
r"INSERT OR REPLACE INTO scan_info
(chain_id, filter, start_block, last_block)
VALUES ($1, $2, $3, $4) RETURNING *;
",
)
.bind(scan_dao.chain_id)
.bind(&scan_dao.filter)
.bind(scan_dao.start_block)
.bind(scan_dao.last_block)
.fetch_one(executor)
.await?;
Ok(res)
}
#[tokio::test]
async fn tx_chain_test() -> sqlx::Result<()> {
println!("Start tx_chain_test...");

use crate::db::create_sqlite_connection;
let conn = create_sqlite_connection(None, None, false, true)
.await
.unwrap();

let mut scan_info_to_insert = ScanDao {
id: -1,
chain_id: 25,
filter: "filter".to_string(),
start_block: 77,
last_block: 6666,
};

let scan_info_from_insert = upsert_scan_info(&conn, &scan_info_to_insert).await?;
scan_info_to_insert.id = scan_info_from_insert.id;
assert_eq!(scan_info_to_insert.id, 1);
let scan_info_from_dao = get_scan_info(&conn, 25, "filter").await?.unwrap();

//all three should be equal
assert_eq!(scan_info_to_insert, scan_info_from_dao);
assert_eq!(scan_info_from_insert, scan_info_from_dao);

assert_eq!(None, get_scan_info(&conn, 25, "filter2").await?);
assert_eq!(None, get_scan_info(&conn, 26, "filter").await?);

//this transaction will overwrite id due to conflict in unique index
scan_info_to_insert.id = 2;
let result = upsert_scan_info(&conn, &scan_info_to_insert).await.unwrap();

assert_eq!(result.id, 2);

delete_scan_info(&conn, 25, "filter").await.unwrap();

assert_eq!(None, get_scan_info(&conn, 25, "filter").await?);

Ok(())
}
Loading