feat: add receipts

This commit is contained in:
Georgios Konstantopoulos 2020-05-24 23:27:51 +03:00
parent ee03703ed4
commit 946c4912f4
No known key found for this signature in database
GPG Key ID: FA607837CD26EDBC
5 changed files with 111 additions and 3 deletions

View File

@ -22,7 +22,10 @@ async fn main() -> Result<(), failure::Error> {
// get the mined tx // get the mined tx
let tx = client.get_transaction(tx.hash).await?; let tx = client.get_transaction(tx.hash).await?;
let receipt = client.get_transaction_receipt(tx.hash).await?;
println!("{}", serde_json::to_string(&tx)?); println!("{}", serde_json::to_string(&tx)?);
println!("{}", serde_json::to_string(&receipt)?);
Ok(()) Ok(())
} }

View File

@ -7,7 +7,10 @@ mod http;
use crate::{ use crate::{
signers::{Client, Signer}, signers::{Client, Signer},
types::{Address, Block, BlockId, BlockNumber, Transaction, TransactionRequest, TxHash, U256}, types::{
Address, Block, BlockId, BlockNumber, Transaction, TransactionReceipt, TransactionRequest,
TxHash, U256,
},
utils, utils,
}; };
@ -111,6 +114,17 @@ impl<P: JsonRpcClient> Provider<P> {
} }
} }
/// Gets the transaction receipt for tx hash
pub async fn get_transaction_receipt<T: Send + Sync + Into<TxHash>>(
&self,
hash: T,
) -> Result<TransactionReceipt, P::Error> {
let hash = hash.into();
self.0
.request("eth_getTransactionReceipt", Some(hash))
.await
}
/// Gets the transaction which matches the provided hash via the `eth_getTransactionByHash` API /// Gets the transaction which matches the provided hash via the `eth_getTransactionByHash` API
pub async fn get_transaction<T: Send + Sync + Into<TxHash>>( pub async fn get_transaction<T: Send + Sync + Into<TxHash>>(
&self, &self,

51
src/types/log.rs Normal file
View File

@ -0,0 +1,51 @@
use crate::types::{Address, Bytes, H256, U256, U64};
use serde::{Deserialize, Serialize};
/// A log produced by a transaction.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Log {
/// H160
pub address: Address,
/// topics: Array of 0 to 4 32 Bytes of indexed log arguments.
/// (In solidity: The first topic is the hash of the signature of the event
/// (e.g. Deposit(address,bytes32,uint256)), except you declared the event
/// with the anonymous specifier.)
pub topics: Vec<H256>,
/// Data
pub data: Bytes,
/// Block Hash
#[serde(rename = "blockHash")]
pub block_hash: Option<H256>,
/// Block Number
#[serde(rename = "blockNumber")]
pub block_number: Option<U64>,
/// Transaction Hash
#[serde(rename = "transactionHash")]
pub transaction_hash: Option<H256>,
/// Transaction Index
#[serde(rename = "transactionIndex")]
pub transaction_index: Option<U64>,
/// Integer of the log index position in the block. Noe if it's a pending log.
#[serde(rename = "logIndex")]
pub log_index: Option<U256>,
/// Integer of the transactions index position log was created from.
/// None when it's a pending log.
#[serde(rename = "transactionLogIndex")]
pub transaction_log_index: Option<U256>,
/// Log Type
#[serde(rename = "logType")]
pub log_type: Option<String>,
/// True when the log was removed, due to a chain reorganization.
/// false if its a valid log.
pub removed: Option<bool>,
}

View File

@ -5,7 +5,7 @@ pub use ethereum_types::H256 as TxHash;
pub use ethereum_types::{Address, Bloom, H256, U256, U64}; pub use ethereum_types::{Address, Bloom, H256, U256, U64};
mod transaction; mod transaction;
pub use transaction::{Transaction, TransactionRequest}; pub use transaction::{Transaction, TransactionReceipt, TransactionRequest};
mod keys; mod keys;
pub use keys::{PrivateKey, PublicKey, TxError}; pub use keys::{PrivateKey, PublicKey, TxError};
@ -18,3 +18,6 @@ pub use bytes::Bytes;
mod block; mod block;
pub use block::{Block, BlockId, BlockNumber}; pub use block::{Block, BlockId, BlockNumber};
mod log;
pub use log::Log;

View File

@ -1,6 +1,6 @@
//! Transaction types //! Transaction types
use crate::{ use crate::{
types::{Address, Bytes, Signature, H256, U256, U64}, types::{Address, Bloom, Bytes, Log, Signature, H256, U256, U64},
utils::keccak256, utils::keccak256,
}; };
use rlp::RlpStream; use rlp::RlpStream;
@ -231,6 +231,43 @@ impl Transaction {
} }
} }
/// "Receipt" of an executed transaction: details of its execution.
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct TransactionReceipt {
/// Transaction hash.
#[serde(rename = "transactionHash")]
pub transaction_hash: H256,
/// Index within the block.
#[serde(rename = "transactionIndex")]
pub transaction_index: U64,
/// Hash of the block this transaction was included within.
#[serde(rename = "blockHash")]
pub block_hash: Option<H256>,
/// Number of the block this transaction was included within.
#[serde(rename = "blockNumber")]
pub block_number: Option<U64>,
/// Cumulative gas used within the block after this was executed.
#[serde(rename = "cumulativeGasUsed")]
pub cumulative_gas_used: U256,
/// Gas used by this transaction alone.
///
/// Gas used is `None` if the the client is running in light client mode.
#[serde(rename = "gasUsed")]
pub gas_used: Option<U256>,
/// Contract address created, or `None` if not a deployment.
#[serde(rename = "contractAddress")]
pub contract_address: Option<Address>,
/// Logs generated within this transaction.
pub logs: Vec<Log>,
/// Status: either 1 (success) or 0 (failure).
pub status: Option<U64>,
/// State root.
pub root: Option<H256>,
/// Logs bloom
#[serde(rename = "logsBloom")]
pub logs_bloom: Bloom,
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;