Provider Fixes on filters and gas estimation (#23)

* fix(provider): do not pass a param to estimate_gas

* fix(provider): pass correct number of args to new_filter

* test(signer): add test to ensure that the Pending block number works

* docs: fix links
This commit is contained in:
Georgios Konstantopoulos 2020-06-17 16:09:41 +03:00 committed by GitHub
parent ba5ae5a894
commit 4fd656bce1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 25 deletions

View File

@ -2,11 +2,11 @@
**Complete Ethereum and Celo wallet implementation and utilities in Rust**
[![CircleCI](https://circleci.com/gh/circleci/circleci-docs.svg?style=svg)](https://circleci.com/gh/circleci/circleci-docs)
![Github Actions](https://github.com/gakonst/ethers-rs/workflows/Tests/badge.svg)
## Documentation
Extensive documentation and examples are available [here](docs.rs/ethers).
Extensive documentation and examples are available [here](https://docs.rs/ethers).
Alternatively, you may clone the repository and run `cd ethers/ && cargo doc --open`
@ -55,7 +55,7 @@ in the transactions which are fetched over JSON-RPC.
## Getting Help
First, see if the answer to your question can be found in the [API documentation](docs.rs/ethers). If the answer
First, see if the answer to your question can be found in the [API documentation](https://docs.rs/ethers). If the answer
is not there, try opening an [issue](https://github.com/gakonst/ethers-rs/issues/new) with the question.
## Contributing

View File

@ -251,21 +251,12 @@ impl<P: JsonRpcClient> Provider<P> {
/// Sends a transaction to a single Ethereum node and return the estimated amount of gas required (as a U256) to send it
/// This is free, but only an estimate. Providing too little gas will result in a transaction being rejected
/// (while still consuming all provided gas).
pub async fn estimate_gas(
&self,
tx: &TransactionRequest,
block: Option<BlockNumber>,
) -> Result<U256, ProviderError> {
pub async fn estimate_gas(&self, tx: &TransactionRequest) -> Result<U256, ProviderError> {
let tx = utils::serialize(tx);
let args = match block {
Some(block) => vec![tx, utils::serialize(&block)],
None => vec![tx],
};
Ok(self
.0
.request("eth_estimateGas", args)
.request("eth_estimateGas", [tx])
.await
.map_err(Into::into)?)
}
@ -365,14 +356,12 @@ impl<P: JsonRpcClient> Provider<P> {
/// To check if the state has changed, call `get_filter_changes` with the filter id.
pub async fn new_filter(&self, filter: FilterKind<'_>) -> Result<U256, ProviderError> {
let (method, args) = match filter {
FilterKind::NewBlocks => ("eth_newBlockFilter", utils::serialize(&())),
FilterKind::PendingTransactions => {
("eth_newPendingTransactionFilter", utils::serialize(&()))
}
FilterKind::Logs(filter) => ("eth_newFilter", utils::serialize(&filter)),
FilterKind::NewBlocks => ("eth_newBlockFilter", vec![]),
FilterKind::PendingTransactions => ("eth_newPendingTransactionFilter", vec![]),
FilterKind::Logs(filter) => ("eth_newFilter", vec![utils::serialize(&filter)]),
};
Ok(self.0.request(method, [args]).await.map_err(Into::into)?)
Ok(self.0.request(method, args).await.map_err(Into::into)?)
}
/// Uninstalls a filter

View File

@ -25,7 +25,8 @@ async fn pending_txs_with_confirmations_ganache() {
#[cfg(feature = "celo")]
mod celo_tests {
use super::*;
use ethers::types::H256;
use ethers::{providers::FilterStream, types::H256};
use futures_util::stream::StreamExt;
#[tokio::test]
// https://alfajores-blockscout.celo-testnet.org/tx/0x544ea96cddb16aeeaedaf90885c1e02be4905f3eb43d6db3f28cac4dbe76a625/internal_transactions
@ -42,4 +43,19 @@ mod celo_tests {
assert_eq!(tx.hash, tx_hash);
assert_eq!(tx.block_number.unwrap(), 1100845.into())
}
#[tokio::test]
async fn watch_blocks() {
let provider =
Provider::<Http>::try_from("https://alfajores-forno.celo-testnet.org").unwrap();
let stream = provider
.watch_blocks()
.await
.unwrap()
.interval(2000u64)
.stream();
let _blocks = stream.take(3usize).collect::<Vec<H256>>().await;
}
}

View File

@ -151,7 +151,7 @@ where
// will poll and await the futures concurrently
let (gas_price, gas, nonce) = join!(
maybe(tx.gas_price, self.provider.get_gas_price()),
maybe(tx.gas, self.provider.estimate_gas(&tx, block)),
maybe(tx.gas, self.provider.estimate_gas(&tx)),
maybe(
tx.nonce,
self.provider.get_transaction_count(self.address(), block)

View File

@ -8,7 +8,7 @@ use std::convert::TryFrom;
#[cfg(not(feature = "celo"))]
mod eth_tests {
use super::*;
use ethers::utils::{parse_ether, Ganache};
use ethers::{types::BlockNumber, utils::{parse_ether, Ganache}};
#[tokio::test]
async fn pending_txs_with_confirmations_rinkeby_infura() {
@ -25,9 +25,8 @@ mod eth_tests {
.connect(provider);
let tx = TransactionRequest::pay(client.address(), parse_ether(1u64).unwrap());
let pending_tx = client.send_transaction(tx, None).await.unwrap();
let pending_tx = client.send_transaction(tx, Some(BlockNumber::Pending)).await.unwrap();
let hash = *pending_tx;
dbg!(hash);
let receipt = pending_tx.confirmations(3).await.unwrap();
// got the correct receipt