Skip to content

Commit c18f639

Browse files
dylanjwankitchiplunkar
authored andcommitted
Specify block number for w3.eth.estimateGas
see #1053 added feature for inputing block_identifier in eth_estimateGas command variable inputs in estimateGas and tests formatter for block_identifier and passing tests added feature for inputing block_identifier in eth_estimateGas command variable inputs in estimateGas and tests passing lint
1 parent 3d14361 commit c18f639

File tree

6 files changed

+57
-6
lines changed

6 files changed

+57
-6
lines changed

tests/integration/go_ethereum/common.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ def test_eth_modifyTransaction(self, web3, unlocked_account):
6060
pytest.xfail('Needs ability to efficiently control mining')
6161
super().test_eth_modifyTransaction(web3, unlocked_account)
6262

63+
def test_eth_estimateGas_with_block(self,
64+
web3,
65+
unlocked_account_dual_type):
66+
pytest.xfail('Block identifier has not been implemented in geth')
67+
super().test_eth_estimateGas_with_block(
68+
web3, unlocked_account_dual_type
69+
)
70+
6371

6472
class GoEthereumVersionModuleTest(VersionModuleTest):
6573
pass

tests/integration/test_ethereum_tester.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ def test_eth_getStorageAt(self, web3, emitter_contract_address):
280280
pytest.xfail('json-rpc method is not implemented on eth-tester')
281281
super().test_eth_getStorageAt(web3, emitter_contract_address)
282282

283+
def test_eth_estimateGas_with_block(self,
284+
web3,
285+
unlocked_account_dual_type):
286+
pytest.xfail('Block identifier has not been implemented in eth-tester')
287+
super().test_eth_estimateGas_with_block(
288+
web3, unlocked_account_dual_type
289+
)
290+
283291

284292
class TestEthereumTesterVersionModule(VersionModuleTest):
285293
pass

web3/_utils/abi.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,11 @@ def is_string_type(abi_type):
356356
return abi_type == 'string'
357357

358358

359+
@curry
360+
def is_length(target_length, value):
361+
return len(value) == target_length
362+
363+
359364
def size_of_type(abi_type):
360365
"""
361366
Returns size in bits of abi_type

web3/_utils/module_testing/eth_module.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,17 @@ def test_eth_estimateGas(self, web3, unlocked_account_dual_type):
469469
assert is_integer(gas_estimate)
470470
assert gas_estimate > 0
471471

472+
def test_eth_estimateGas_with_block(self,
473+
web3,
474+
unlocked_account_dual_type):
475+
gas_estimate = web3.eth.estimateGas({
476+
'from': unlocked_account_dual_type,
477+
'to': unlocked_account_dual_type,
478+
'value': 1,
479+
}, 'latest')
480+
assert is_integer(gas_estimate)
481+
assert gas_estimate > 0
482+
472483
def test_eth_getBlockByHash(self, web3, empty_block):
473484
block = web3.eth.getBlock(empty_block['hash'])
474485
assert block['hash'] == empty_block['hash']

web3/eth.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,15 +294,22 @@ def call(self, transaction, block_identifier=None):
294294
[transaction, block_identifier],
295295
)
296296

297-
def estimateGas(self, transaction):
297+
def estimateGas(self, transaction, block_identifier=None):
298298
# TODO: move to middleware
299299
if 'from' not in transaction and is_checksum_address(self.defaultAccount):
300300
transaction = assoc(transaction, 'from', self.defaultAccount)
301301

302-
return self.web3.manager.request_blocking(
303-
"eth_estimateGas",
304-
[transaction],
305-
)
302+
# TODO: move to middleware
303+
if block_identifier is None:
304+
return self.web3.manager.request_blocking(
305+
"eth_estimateGas",
306+
[transaction],
307+
)
308+
else:
309+
return self.web3.manager.request_blocking(
310+
"eth_estimateGas",
311+
[transaction, block_identifier],
312+
)
306313

307314
def filter(self, filter_params=None, filter_id=None):
308315
if filter_id and filter_params:

web3/middleware/pythonic.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
HexBytes,
1717
)
1818

19+
from web3._utils.abi import (
20+
is_length,
21+
)
1922
from web3._utils.encoding import (
2023
hexstr_if_str,
2124
to_hex,
@@ -244,6 +247,12 @@ def to_hexbytes(num_bytes, val, variable_length=False):
244247
apply_formatters_to_dict(TRANSACTION_PARAM_FORMATTERS),
245248
)
246249

250+
estimate_gas_without_block_id = apply_formatter_at_index(transaction_param_formatter, 0)
251+
estimate_gas_with_block_id = combine_argument_formatters(
252+
transaction_param_formatter,
253+
block_number_formatter,
254+
)
255+
247256

248257
pythonic_middleware = construct_formatting_middleware(
249258
request_formatters={
@@ -273,7 +282,10 @@ def to_hexbytes(num_bytes, val, variable_length=False):
273282
transaction_param_formatter,
274283
block_number_formatter,
275284
),
276-
'eth_estimateGas': apply_formatter_at_index(transaction_param_formatter, 0),
285+
'eth_estimateGas': apply_one_of_formatters((
286+
(estimate_gas_without_block_id, is_length(1)),
287+
(estimate_gas_with_block_id, is_length(2)),
288+
)),
277289
'eth_sendTransaction': apply_formatter_at_index(transaction_param_formatter, 0),
278290
# personal
279291
'personal_importRawKey': apply_formatter_at_index(

0 commit comments

Comments
 (0)