From cff96ae1e59cdd4dc1ff8cce6243eacc0720d812 Mon Sep 17 00:00:00 2001 From: vladimir popov Date: Fri, 24 Jun 2022 18:01:59 +0300 Subject: [PATCH 1/6] extends logs with extra metadata and query params --- src/contract/mod.rs | 34 ++++++++++++++++++++++++++++------ src/types/log.rs | 42 +++++++++++++++++++++++++++++++----------- src/types/mod.rs | 2 +- 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/src/contract/mod.rs b/src/contract/mod.rs index 22bcb4fe..0471d41a 100644 --- a/src/contract/mod.rs +++ b/src/contract/mod.rs @@ -6,8 +6,8 @@ use crate::{ contract::tokens::{Detokenize, Tokenize}, futures::Future, types::{ - AccessList, Address, BlockId, Bytes, CallRequest, FilterBuilder, TransactionCondition, TransactionReceipt, - TransactionRequest, H256, U256, U64, + AccessList, Address, BlockId, BlockNumber, Bytes, CallRequest, FilterBuilder, LogWithMeta, + TransactionCondition, TransactionReceipt, TransactionRequest, H256, U256, U64, }, Transport, }; @@ -279,7 +279,16 @@ impl Contract { } /// Find events matching the topics. - pub async fn events(&self, event: &str, topic0: A, topic1: B, topic2: C) -> Result> + pub async fn events( + &self, + event: &str, + from_block: Option, + to_block: Option, + block_hash: Option, + topic0: A, + topic1: B, + topic2: C, + ) -> Result>> where A: Tokenize, B: Tokenize, @@ -310,7 +319,15 @@ impl Contract { let logs = self .eth - .logs(FilterBuilder::default().topic_filter(filter).build()) + .logs( + FilterBuilder::default() + .address(vec![self.address]) + .topic_filter(filter) + .from_block(from_block) + .to_block(to_block) + .block_hash(block_hash) + .build(), + ) .await?; logs.into_iter() .map(move |l| { @@ -319,9 +336,14 @@ impl Contract { data: l.data.0, })?; - R::from_tokens(log.params.into_iter().map(|x| x.value).collect::>()) + let event_data = R::from_tokens(log.params.into_iter().map(|x| x.value).collect::>())?; + + Ok(LogWithMeta { + transaction_hash: l.transaction_hash, + event_data, + }) }) - .collect::>>() + .collect::>>>() } } diff --git a/src/types/log.rs b/src/types/log.rs index 9a1f5087..fc31ffdc 100644 --- a/src/types/log.rs +++ b/src/types/log.rs @@ -1,4 +1,7 @@ -use crate::types::{BlockNumber, Bytes, Index, H160, H256, U256, U64}; +use crate::{ + contract::tokens::Detokenize, + types::{BlockNumber, Bytes, Index, H160, H256, U256, U64}, +}; use serde::{Deserialize, Serialize, Serializer}; /// A log produced by a transaction. @@ -54,6 +57,17 @@ impl Log { } } +/// A log produced when a specefic contract event was emited +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct LogWithMeta { + /// This could be necessary to retrieve calldata + #[serde(rename = "transactionHash")] + pub transaction_hash: Option, + + ///this is a tuple from the event signature (e.g. (uint256, bytes)) + pub event_data: R, +} + #[derive(Default, Debug, PartialEq, Clone)] struct ValueOrArray(Vec); @@ -106,27 +120,33 @@ impl FilterBuilder { /// Sets `from_block`. The fields `from_block` and `block_hash` are /// mutually exclusive. Setting `from_block` will clear a previously set /// `block_hash`. - pub fn from_block(mut self, block: BlockNumber) -> Self { - self.filter.block_hash = None; - self.filter.from_block = Some(block); + pub fn from_block(mut self, block: Option) -> Self { + if let Some(block) = block { + self.filter.block_hash = None; + self.filter.from_block = Some(block); + } self } /// Sets `to_block`. The fields `to_block` and `block_hash` are mutually /// exclusive. Setting `to_block` will clear a previously set `block_hash`. - pub fn to_block(mut self, block: BlockNumber) -> Self { - self.filter.block_hash = None; - self.filter.to_block = Some(block); + pub fn to_block(mut self, block: Option) -> Self { + if let Some(block) = block { + self.filter.block_hash = None; + self.filter.to_block = Some(block); + } self } /// Sets `block_hash`. The field `block_hash` and the pair `from_block` and /// `to_block` are mutually exclusive. Setting `block_hash` will clear a /// previously set `from_block` and `to_block`. - pub fn block_hash(mut self, hash: H256) -> Self { - self.filter.from_block = None; - self.filter.to_block = None; - self.filter.block_hash = Some(hash); + pub fn block_hash(mut self, hash: Option) -> Self { + if let Some(_block_hash) = hash { + self.filter.from_block = None; + self.filter.to_block = None; + self.filter.block_hash = hash; + } self } diff --git a/src/types/mod.rs b/src/types/mod.rs index fb16288c..7834fc26 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -25,7 +25,7 @@ pub use self::{ bytes::Bytes, bytes_array::BytesArray, fee_history::FeeHistory, - log::{Filter, FilterBuilder, Log}, + log::{Filter, FilterBuilder, Log,LogWithMeta}, parity_peers::{ EthProtocolInfo, ParityPeerInfo, ParityPeerType, PeerNetworkInfo, PeerProtocolsInfo, PipProtocolInfo, }, From d838f474d2d40316dd83e277df1cfa622013b706 Mon Sep 17 00:00:00 2001 From: vladimir popov Date: Fri, 24 Jun 2022 19:12:37 +0300 Subject: [PATCH 2/6] formating --- src/types/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/mod.rs b/src/types/mod.rs index 7834fc26..3053fd09 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -25,7 +25,7 @@ pub use self::{ bytes::Bytes, bytes_array::BytesArray, fee_history::FeeHistory, - log::{Filter, FilterBuilder, Log,LogWithMeta}, + log::{Filter, FilterBuilder, Log, LogWithMeta}, parity_peers::{ EthProtocolInfo, ParityPeerInfo, ParityPeerType, PeerNetworkInfo, PeerProtocolsInfo, PipProtocolInfo, }, From 4c51bc1de5b902d9f66b3b820668ef444de17647 Mon Sep 17 00:00:00 2001 From: Alexander Filippov Date: Mon, 15 Aug 2022 19:30:46 +0300 Subject: [PATCH 3/6] Add signed_call_raw, signed_call_raw_with_confirmations --- src/contract/mod.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/contract/mod.rs b/src/contract/mod.rs index 22031ddf..ef6cdfbe 100644 --- a/src/contract/mod.rs +++ b/src/contract/mod.rs @@ -371,6 +371,15 @@ mod contract_signing { // TODO [ToDr] SendTransactionWithConfirmation should support custom error type (so that we can return // `contract::Error` instead of more generic `Error`. .map_err(|err| crate::error::Error::Decoder(format!("{:?}", err)))?; + self.sign_raw(fn_data, options, key).await + } + + async fn sign_raw( + &self, + fn_data: Vec, + options: Options, + key: impl signing::Key, + ) -> crate::Result { let accounts = Accounts::new(self.eth.transport().clone()); let mut tx = TransactionParameters { nonce: options.nonce, @@ -407,10 +416,24 @@ mod contract_signing { self.eth.send_raw_transaction(signed.raw_transaction).await } + /// Submit contract call transaction to the transaction pool. + /// + /// Note this function DOES NOT wait for any confirmations, so there is no guarantees that the call is actually executed. + /// If you'd rather wait for block inclusion, please use [`signed_call_raw_with_confirmations`] instead. + pub async fn signed_call_raw( + &self, + fn_data: Vec, + options: Options, + key: impl signing::Key, + ) -> crate::Result { + let signed = self.sign_raw(fn_data, options, key).await?; + self.eth.send_raw_transaction(signed.raw_transaction).await + } + /// Submit contract call transaction to the transaction pool and wait for the transaction to be included in a block. /// /// This function will wait for block inclusion of the transaction before returning. - // If you'd rather just submit transaction and receive it's hash, please use [`signed_call`] instead. + /// If you'd rather just submit transaction and receive it's hash, please use [`signed_call`] instead. pub async fn signed_call_with_confirmations( &self, func: &str, @@ -430,6 +453,29 @@ mod contract_signing { ) .await } + + /// Submit contract call transaction to the transaction pool and wait for the transaction to be included in a block. + /// + /// This function will wait for block inclusion of the transaction before returning. + /// If you'd rather just submit transaction and receive it's hash, please use [`signed_call`] instead. + pub async fn signed_call_raw_with_confirmations( + &self, + fn_data: Vec, + options: Options, + confirmations: usize, + key: impl signing::Key, + ) -> crate::Result { + let poll_interval = time::Duration::from_secs(1); + let signed = self.sign_raw(fn_data, options, key).await?; + + confirm::send_raw_transaction_with_confirmation( + self.eth.transport().clone(), + signed.raw_transaction, + poll_interval, + confirmations, + ) + .await + } } } From 14ec3461231ed402d3c1dcc177808a21d633ba9c Mon Sep 17 00:00:00 2001 From: r0wdy1 <103738251+r0wdy1@users.noreply.github.com> Date: Wed, 27 Sep 2023 13:04:36 +0400 Subject: [PATCH 4/6] Update src/types/log.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tomasz Drwięga --- src/types/log.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/log.rs b/src/types/log.rs index fc31ffdc..17d7af90 100644 --- a/src/types/log.rs +++ b/src/types/log.rs @@ -57,7 +57,7 @@ impl Log { } } -/// A log produced when a specefic contract event was emited +/// A log produced when a specific contract event was emitted. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct LogWithMeta { /// This could be necessary to retrieve calldata From 959ee977944af3f550cebbbcf8b0410b9e934e03 Mon Sep 17 00:00:00 2001 From: r0wdy1 <103738251+r0wdy1@users.noreply.github.com> Date: Wed, 27 Sep 2023 13:04:54 +0400 Subject: [PATCH 5/6] Update src/types/log.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tomasz Drwięga --- src/types/log.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/log.rs b/src/types/log.rs index 17d7af90..9936ea68 100644 --- a/src/types/log.rs +++ b/src/types/log.rs @@ -64,7 +64,7 @@ pub struct LogWithMeta { #[serde(rename = "transactionHash")] pub transaction_hash: Option, - ///this is a tuple from the event signature (e.g. (uint256, bytes)) + /// A tuple from the event signature (e.g. `(uint256, bytes)`). pub event_data: R, } From ae21f76b6acde1d399467c8468d9aafc4eeeee80 Mon Sep 17 00:00:00 2001 From: r0wdy1 <103738251+r0wdy1@users.noreply.github.com> Date: Wed, 27 Sep 2023 13:05:40 +0400 Subject: [PATCH 6/6] Update src/types/log.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tomasz Drwięga --- src/types/log.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/log.rs b/src/types/log.rs index 9936ea68..436c56b6 100644 --- a/src/types/log.rs +++ b/src/types/log.rs @@ -60,7 +60,7 @@ impl Log { /// A log produced when a specific contract event was emitted. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct LogWithMeta { - /// This could be necessary to retrieve calldata + /// Transaction hash necessary to retrieve `calldata`. #[serde(rename = "transactionHash")] pub transaction_hash: Option,