Skip to content

Commit

Permalink
estimate_gas: do not serialize null block number (#291)
Browse files Browse the repository at this point in the history
* estimate_gas: do not serialize null block number

* Style fix

* Additional test for estimateGas
  • Loading branch information
vorot93 authored and tomusdrw committed Dec 3, 2019
1 parent 0665faf commit dd544d5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions src/api/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ impl<T: Transport> Eth<T> {
/// Call a contract without changing the state of the blockchain to estimate gas usage.
pub fn estimate_gas(&self, req: CallRequest, block: Option<BlockNumber>) -> CallFuture<U256, T::Out> {
let req = helpers::serialize(&req);
let block = helpers::serialize(&block.unwrap_or(BlockNumber::Latest));

CallFuture::new(self.transport.execute("eth_estimateGas", vec![req, block]))
let args = match block {
Some(block) => vec![req, helpers::serialize(&block)],
None => vec![req],
};

CallFuture::new(self.transport.execute("eth_estimateGas", args))
}

/// Get current recommended gas price
Expand Down Expand Up @@ -454,7 +458,18 @@ mod tests {
value: Some(0x1.into()), data: None,
}, None
=>
"eth_estimateGas", vec![r#"{"to":"0x0000000000000000000000000000000000000123","value":"0x1"}"#, r#""latest""#];
"eth_estimateGas", vec![r#"{"to":"0x0000000000000000000000000000000000000123","value":"0x1"}"#];
Value::String("0x123".into()) => 0x123
);

rpc_test! (
Eth:estimate_gas:for_block, CallRequest {
from: None, to: Address::from_low_u64_be(0x123),
gas: None, gas_price: None,
value: Some(0x1.into()), data: None,
}, Some(0x123.into())
=>
"eth_estimateGas", vec![r#"{"to":"0x0000000000000000000000000000000000000123","value":"0x1"}"#, r#""0x123""#];
Value::String("0x123".into()) => 0x123
);

Expand Down
2 changes: 1 addition & 1 deletion src/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ mod tests {
};

// then
transport.assert_request("eth_estimateGas", &["{\"data\":\"0x06fdde03\",\"from\":\"0x0000000000000000000000000000000000000005\",\"to\":\"0x0000000000000000000000000000000000000001\"}".into(), "\"latest\"".into()]);
transport.assert_request("eth_estimateGas", &["{\"data\":\"0x06fdde03\",\"from\":\"0x0000000000000000000000000000000000000005\",\"to\":\"0x0000000000000000000000000000000000000001\"}".into()]);
transport.assert_no_more_requests();
assert_eq!(result, 5.into());
}
Expand Down

0 comments on commit dd544d5

Please sign in to comment.