From 516b431a487a4c5f8c30378b9598c2a63d2b9a8b Mon Sep 17 00:00:00 2001 From: Rohit Narurkar Date: Wed, 12 Aug 2020 14:05:33 +0530 Subject: [PATCH] add support for gas estimate and calldata from ContractCall (#53) * add support for gas estimate and calldata from ContractCall * fix celo tests for get_block and get_tx --- ethers-contract/src/call.rs | 12 +++++++++++- ethers-contract/tests/contract.rs | 12 ++++++++---- ethers-providers/tests/provider.rs | 10 +++++----- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/ethers-contract/src/call.rs b/ethers-contract/src/call.rs index 68f2915b..4ca09660 100644 --- a/ethers-contract/src/call.rs +++ b/ethers-contract/src/call.rs @@ -1,6 +1,6 @@ use ethers_core::{ abi::{Detokenize, Error as AbiError, Function, InvalidOutputType}, - types::{Address, BlockNumber, TransactionRequest, TxHash, U256}, + types::{Address, BlockNumber, Bytes, TransactionRequest, TxHash, U256}, }; use ethers_providers::{JsonRpcClient, ProviderError}; use ethers_signers::{Client, ClientError, Signer}; @@ -91,6 +91,16 @@ where P: JsonRpcClient, D: Detokenize, { + /// Returns the underlying transaction's ABI encoded data + pub fn calldata(&self) -> Option { + self.tx.data.clone() + } + + /// Returns the estimated gas cost for the underlying transaction to be executed + pub async fn estimate_gas(&self) -> Result { + Ok(self.client.estimate_gas(&self.tx).await?) + } + /// Queries the blockchain via an `eth_call` for the provided transaction. /// /// If executed on a non-state mutating smart contract function (i.e. `view`, `pure`) diff --git a/ethers-contract/tests/contract.rs b/ethers-contract/tests/contract.rs index 05a29382..a890316a 100644 --- a/ethers-contract/tests/contract.rs +++ b/ethers-contract/tests/contract.rs @@ -46,15 +46,19 @@ mod eth_tests { // need to declare the method first, and only then send it // this is because it internally clones an Arc which would otherwise // get immediately dropped - let _tx_hash = contract + let contract_call = contract .connect(client2.clone()) .method::<_, H256>("setValue", "hi".to_owned()) - .unwrap() - .send() - .await .unwrap(); + let calldata = contract_call.calldata().unwrap(); + let gas_estimate = contract_call.estimate_gas().await.unwrap(); + let tx_hash = contract_call.send().await.unwrap(); + let tx = client.get_transaction(tx_hash).await.unwrap(); + let tx_receipt = client.get_transaction_receipt(tx_hash).await.unwrap(); assert_eq!(last_sender.clone().call().await.unwrap(), client2.address()); assert_eq!(get_value.clone().call().await.unwrap(), "hi"); + assert_eq!(tx.input, calldata); + assert_eq!(tx_receipt.gas_used.unwrap(), gas_estimate); // we can also call contract methods at other addresses with the `at` call // (useful when interacting with multiple ERC20s for example) diff --git a/ethers-providers/tests/provider.rs b/ethers-providers/tests/provider.rs index abc4f425..f78259b6 100644 --- a/ethers-providers/tests/provider.rs +++ b/ethers-providers/tests/provider.rs @@ -102,14 +102,14 @@ mod celo_tests { let provider = Provider::::try_from("https://alfajores-forno.celo-testnet.org").unwrap(); - let tx_hash = "544ea96cddb16aeeaedaf90885c1e02be4905f3eb43d6db3f28cac4dbe76a625" + let tx_hash = "c8496681d0ade783322980cce00c89419fce4b484635d9e09c79787a0f75d450" .parse::() .unwrap(); let tx = provider.get_transaction(tx_hash).await.unwrap(); assert!(tx.gateway_fee_recipient.is_none()); assert_eq!(tx.gateway_fee.unwrap(), 0.into()); assert_eq!(tx.hash, tx_hash); - assert_eq!(tx.block_number.unwrap(), 1100845.into()) + assert_eq!(tx.block_number.unwrap(), 447181.into()) } #[tokio::test] @@ -117,15 +117,15 @@ mod celo_tests { let provider = Provider::::try_from("https://alfajores-forno.celo-testnet.org").unwrap(); - let block = provider.get_block(1342561).await.unwrap(); + let block = provider.get_block(447254).await.unwrap(); assert_eq!( block.randomness, Randomness { - committed: "a3a64b7a29bb4ddd49b7d9e3cf3dd14ecbb7f0321061706c634d14b15425dd30" + committed: "003e12deb86292844274493e9ab6e57ed1e276202c16799d97af723eb0d3253f" .from_hex::>() .unwrap() .into(), - revealed: "3c5f2f71941783cbe7f2dbd387c35503ca0470b300e1613866b988a1db8902a3" + revealed: "1333b3b45e0385da48a01b4459aeda7607867ef6a41167cfdeefa49b9fdce6d7" .from_hex::>() .unwrap() .into(),