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
This commit is contained in:
Rohit Narurkar 2020-08-12 14:05:33 +05:30 committed by GitHub
parent 34550d9211
commit 516b431a48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 10 deletions

View File

@ -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<Bytes> {
self.tx.data.clone()
}
/// Returns the estimated gas cost for the underlying transaction to be executed
pub async fn estimate_gas(&self) -> Result<U256, ContractError> {
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`)

View File

@ -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)

View File

@ -102,14 +102,14 @@ mod celo_tests {
let provider =
Provider::<Http>::try_from("https://alfajores-forno.celo-testnet.org").unwrap();
let tx_hash = "544ea96cddb16aeeaedaf90885c1e02be4905f3eb43d6db3f28cac4dbe76a625"
let tx_hash = "c8496681d0ade783322980cce00c89419fce4b484635d9e09c79787a0f75d450"
.parse::<H256>()
.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::<Http>::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::<Vec<u8>>()
.unwrap()
.into(),
revealed: "3c5f2f71941783cbe7f2dbd387c35503ca0470b300e1613866b988a1db8902a3"
revealed: "1333b3b45e0385da48a01b4459aeda7607867ef6a41167cfdeefa49b9fdce6d7"
.from_hex::<Vec<u8>>()
.unwrap()
.into(),