use crate::client::types::{Transaction, TransactionReceipt};
use crate::client::BscScanClient;
use crate::error::{Error, Result};
pub trait TransactionEndpoints {
async fn get_transaction(&self, tx_hash: &str) -> Result<Transaction>;
async fn get_transaction_receipt(&self, tx_hash: &str) -> Result<TransactionReceipt>;
async fn get_confirmations(&self, tx_hash: &str) -> Result<u64>;
async fn get_block_number(&self) -> Result<u64>;
}
impl TransactionEndpoints for BscScanClient {
async fn get_transaction(&self, tx_hash: &str) -> Result<Transaction> {
let params = [("txhash", tx_hash)];
let proxy_tx: crate::client::types::ProxyTransaction = self
.request("proxy", "eth_getTransactionByHash", ¶ms)
.await?;
Ok(Transaction::from(proxy_tx))
}
async fn get_transaction_receipt(&self, tx_hash: &str) -> Result<TransactionReceipt> {
let params = [("txhash", tx_hash)];
self.request("proxy", "eth_getTransactionReceipt", ¶ms)
.await
}
async fn get_confirmations(&self, tx_hash: &str) -> Result<u64> {
let tx = self.get_transaction(tx_hash).await?;
let tx_block: u64 = tx
.block_number
.parse()
.map_err(|_| Error::generic("Invalid block number in transaction"))?;
let current_block = self.get_block_number().await?;
if current_block >= tx_block {
Ok(current_block - tx_block + 1)
} else {
Ok(0)
}
}
async fn get_block_number(&self) -> Result<u64> {
let params: [(&str, &str); 0] = [];
let block_hex: String = self
.request_simple("proxy", "eth_blockNumber", ¶ms)
.await?;
let block_num = u64::from_str_radix(block_hex.trim_start_matches("0x"), 16)
.map_err(|_| Error::generic("Invalid block number format"))?;
Ok(block_num)
}
}