Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed Feb 28, 2024
1 parent 034abb4 commit 83e2bc8
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 124 deletions.
8 changes: 4 additions & 4 deletions crates/erc20_payment_lib/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub async fn transaction_from_chain_and_into_db(
glm_address: Address,
get_balances: bool,
) -> Result<Option<ChainTxDbObj>, PaymentError> {
println!("tx_hash: {tx_hash}");
log::debug!("tx_hash: {tx_hash}");
let tx_hash = web3::types::H256::from_str(tx_hash)
.map_err(|_err| ConversionError::from("Cannot parse tx_hash".to_string()))
.map_err(err_from!())?;
Expand All @@ -84,7 +84,7 @@ pub async fn transaction_from_chain_and_into_db(
.await
.map_err(err_from!())?
{
log::info!("Transaction already in DB: {}, skipping...", chain_tx.id);
log::warn!("Transaction already in DB: {}, skipping...", chain_tx.id);
return Ok(Some(chain_tx));
}

Expand Down Expand Up @@ -125,7 +125,7 @@ pub async fn transaction_from_chain_and_into_db(
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
};

log::info!(
log::debug!(
"Balance: {:.5} for block {}",
balance.unwrap_or_default().to_eth().unwrap(),
chain_tx_dao.block_number
Expand Down Expand Up @@ -228,7 +228,7 @@ pub async fn transaction_from_chain_and_into_db(
}

db_transaction.commit().await.map_err(err_from!())?;
log::info!("Transaction found and parsed successfully: {}", tx.id);
log::debug!("Transaction found and parsed successfully: {}", tx.id);
Ok(Some(tx))
}

Expand Down
71 changes: 37 additions & 34 deletions crates/erc20_payment_lib/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ pub struct ImportErc20TxsArgs {
}

pub async fn import_erc20_txs(import_args: ImportErc20TxsArgs) -> Result<Vec<H256>, PaymentError> {
let mut start_block = import_args.start_block;
let start_block = import_args.start_block;
let option_address_to_option_h256 = |val: Option<Vec<Address>>| -> Option<Vec<H256>> {
val.map(|accounts| {
accounts
Expand All @@ -1189,43 +1189,46 @@ pub async fn import_erc20_txs(import_args: ImportErc20TxsArgs) -> Result<Vec<H25
.as_u64() as i64;

let mut txs = HashMap::<H256, u64>::new();
loop {
let end_block = std::cmp::min(
std::cmp::min(start_block + 1000, current_block),
import_args.scan_end_block,

let end_block = import_args.scan_end_block;
if start_block > end_block {
return Err(err_custom_create!("Start block is greater than end block"));
}
if end_block - start_block > import_args.blocks_at_once as i64 {
return Err(err_custom_create!("Too many blocks to scan"));
}
if start_block > current_block {
return Err(err_custom_create!(
"Start block is greater than current block"
));
}
log::debug!("Scanning chain, blocks: {start_block} - {end_block}");
let logs = get_erc20_logs(
import_args.web3.clone(),
import_args.erc20_address,
topic_senders.clone(),
topic_receivers.clone(),
start_block,
end_block,
)
.await?;
for log in logs.into_iter() {
txs.insert(
log.transaction_hash
.ok_or(err_custom_create!("Log without transaction hash"))?,
log.block_number
.ok_or(err_custom_create!("Log without block number"))?
.as_u64(),
);
log::info!(
"Found matching log entry in block: {}, tx: {}",
log.block_number.unwrap(),
log.block_number.unwrap()
);
if start_block > end_block {
break;
}
log::info!("Scanning chain, blocks: {start_block} - {end_block}");
let logs = get_erc20_logs(
import_args.web3.clone(),
import_args.erc20_address,
topic_senders.clone(),
topic_receivers.clone(),
start_block,
end_block,
)
.await?;
for log in logs.into_iter() {
txs.insert(
log.transaction_hash
.ok_or(err_custom_create!("Log without transaction hash"))?,
log.block_number
.ok_or(err_custom_create!("Log without block number"))?
.as_u64(),
);
log::info!(
"Found matching log entry in block: {}, tx: {}",
log.block_number.unwrap(),
log.block_number.unwrap()
);
}
start_block += import_args.blocks_at_once as i64;
}

if txs.is_empty() {
log::info!("No logs found");
log::debug!("No logs found");
} else {
log::info!("Found {} transactions", txs.len());
}
Expand Down
Loading

0 comments on commit 83e2bc8

Please sign in to comment.