Skip to content

Commit 68a9d8c

Browse files
authored
deprecated estimateGas and buildTransaction in v5 contract #2438 (#2459)
1 parent 3b43d1d commit 68a9d8c

13 files changed

+149
-94
lines changed

docs/contracts.rst

+27-8
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,11 @@ Each Contract Factory exposes the following methods.
247247
.. py:classmethod:: Contract.constructor(*args, **kwargs).estimateGas(transaction=None, block_identifier=None)
248248
:noindex:
249249

250+
.. warning:: Deprecated: This method is deprecated in favor of :py:meth:`Contract.constructor(*args, **kwargs).estimate_gas`
251+
252+
.. py:classmethod:: Contract.constructor(*args, **kwargs).estimate_gas(transaction=None, block_identifier=None)
253+
:noindex:
254+
250255
Estimate gas for constructing and deploying the contract.
251256

252257
This method behaves the same as the
@@ -264,12 +269,18 @@ Each Contract Factory exposes the following methods.
264269

265270
.. code-block:: python
266271
267-
>>> token_contract.constructor(web3.eth.coinbase, 12345).estimateGas()
272+
>>> token_contract.constructor(web3.eth.coinbase, 12345).estimate_gas()
268273
12563
269274
270275
.. py:classmethod:: Contract.constructor(*args, **kwargs).buildTransaction(transaction=None)
271276
:noindex:
272277

278+
.. warning:: Deprecated: This method is deprecated in favor of :py:meth:`Contract.constructor(*args, **kwargs).build_transaction`
279+
280+
281+
.. py:classmethod:: Contract.constructor(*args, **kwargs).build_transaction(transaction=None)
282+
:noindex:
283+
273284
Construct the contract deploy transaction bytecode data.
274285

275286
If the contract takes constructor parameters they should be provided as
@@ -286,7 +297,7 @@ Each Contract Factory exposes the following methods.
286297
'gasPrice': w3.eth.gas_price,
287298
'chainId': None
288299
}
289-
>>> contract_data = token_contract.constructor(web3.eth.coinbase, 12345).buildTransaction(transaction)
300+
>>> contract_data = token_contract.constructor(web3.eth.coinbase, 12345).build_transaction(transaction)
290301
>>> web3.eth.send_transaction(contract_data)
291302
292303
.. _contract_createFilter:
@@ -835,14 +846,18 @@ Methods
835846

836847
.. py:method:: ContractFunction.estimateGas(transaction, block_identifier=None)
837848
849+
.. warning:: Deprecated: This method is deprecated in favor of :class:`~estimate_gas`
850+
851+
.. py:method:: ContractFunction.estimate_gas(transaction, block_identifier=None)
852+
838853
Call a contract function, executing the transaction locally using the
839854
``eth_call`` API. This will not create a new public transaction.
840855

841856
Refer to the following invocation:
842857

843858
.. code-block:: python
844859
845-
myContract.functions.myMethod(*args, **kwargs).estimateGas(transaction)
860+
myContract.functions.myMethod(*args, **kwargs).estimate_gas(transaction)
846861
847862
This method behaves the same as the :py:meth:`ContractFunction.transact` method,
848863
with transaction details being passed into the end portion of the
@@ -853,7 +868,7 @@ Methods
853868

854869
.. code-block:: python
855870
856-
>>> my_contract.functions.multiply7(3).estimateGas()
871+
>>> my_contract.functions.multiply7(3).estimate_gas()
857872
42650
858873
859874
.. note::
@@ -862,14 +877,18 @@ Methods
862877
nodes would result in an error like: ``ValueError: {'code': -32602, 'message': 'too many arguments, want at most 1'}``
863878

864879
.. py:method:: ContractFunction.buildTransaction(transaction)
880+
881+
.. warning:: Deprecated: This method is deprecated in favor of :class:`~build_transaction`
882+
883+
.. py:method:: ContractFunction.build_transaction(transaction)
865884
866885
Builds a transaction dictionary based on the contract function call specified.
867886

868887
Refer to the following invocation:
869888

870889
.. code-block:: python
871890
872-
myContract.functions.myMethod(*args, **kwargs).buildTransaction(transaction)
891+
myContract.functions.myMethod(*args, **kwargs).build_transaction(transaction)
873892
874893
This method behaves the same as the :py:meth:`Contract.transact` method,
875894
with transaction details being passed into the end portion of the
@@ -881,15 +900,15 @@ Methods
881900

882901
.. code-block:: python
883902
884-
>>> math_contract.functions.increment(5).buildTransaction({'nonce': 10})
903+
>>> math_contract.functions.increment(5).build_transaction({'nonce': 10})
885904
886905
You may use :meth:`~web3.eth.Eth.getTransactionCount` to get the current nonce
887906
for an account. Therefore a shortcut for producing a transaction dictionary with
888907
nonce included looks like:
889908

890909
.. code-block:: python
891910
892-
>>> math_contract.functions.increment(5).buildTransaction({'nonce': web3.eth.get_transaction_count('0xF5...')})
911+
>>> math_contract.functions.increment(5).build_transaction({'nonce': web3.eth.get_transaction_count('0xF5...')})
893912
894913
Returns a transaction dictionary. This transaction dictionary can then be sent using
895914
:meth:`~web3.eth.Eth.send_transaction`.
@@ -899,7 +918,7 @@ Methods
899918

900919
.. code-block:: python
901920
902-
>>> math_contract.functions.increment(5).buildTransaction({'maxFeePerGas': 2000000000, 'maxPriorityFeePerGas': 1000000000})
921+
>>> math_contract.functions.increment(5).build_transaction({'maxFeePerGas': 2000000000, 'maxPriorityFeePerGas': 1000000000})
903922
{
904923
'to': '0x6Bc272FCFcf89C14cebFC57B8f1543F5137F97dE',
905924
'data': '0x7cf5dab00000000000000000000000000000000000000000000000000000000000000005',

docs/examples.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ The following example demonstrates a few things:
361361
362362
store_var_contract = w3.eth.contract(address=address, abi=contract_interface["abi"])
363363
364-
gas_estimate = store_var_contract.functions.setVar(255).estimateGas()
364+
gas_estimate = store_var_contract.functions.setVar(255).estimate_gas()
365365
print(f'Gas estimate to transact with setVar: {gas_estimate}')
366366
367367
if gas_estimate < 100000:
@@ -674,7 +674,7 @@ Just remember that you have to sign all transactions locally, as infura does not
674674

675675
.. code-block:: python
676676
677-
transaction = contract.functions.function_Name(params).buildTransaction()
677+
transaction = contract.functions.function_Name(params).build_transaction()
678678
transaction.update({ 'gas' : appropriate_gas_amount })
679679
transaction.update({ 'nonce' : w3.eth.get_transaction_count('Your_Wallet_Address') })
680680
signed_tx = w3.eth.account.sign_transaction(transaction, private_key)

docs/web3.eth.account.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ To sign a transaction locally that will invoke a smart contract:
303303
>>> unicorn_txn = unicorns.functions.transfer(
304304
... '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
305305
... 1,
306-
... ).buildTransaction({
306+
... ).build_transaction({
307307
... 'chainId': 1,
308308
... 'gas': 70000,
309309
... 'maxFeePerGas': w3.toWei('2', 'gwei'),

newsfragments/2459.feature.rst

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deprecated ``buildTransaction`` and ``estimateGas`` in contract.py v5,
2+
in favor of ``estimate_gas`` and ``build_transaction``

tests/core/contracts/conftest.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ def invoke_contract(api_call_desig='call',
10081008
func_args=[],
10091009
func_kwargs={},
10101010
tx_params={}):
1011-
allowable_call_desig = ['call', 'transact', 'estimateGas', 'buildTransaction']
1011+
allowable_call_desig = ['call', 'transact', 'estimate_gas', 'build_transaction']
10121012
if api_call_desig not in allowable_call_desig:
10131013
raise ValueError("allowable_invoke_method must be one of: %s" % allowable_call_desig)
10141014

@@ -1029,10 +1029,10 @@ def call(request):
10291029

10301030

10311031
@pytest.fixture
1032-
def estimateGas(request):
1033-
return functools.partial(invoke_contract, api_call_desig='estimateGas')
1032+
def estimate_gas(request):
1033+
return functools.partial(invoke_contract, api_call_desig='estimate_gas')
10341034

10351035

10361036
@pytest.fixture
1037-
def buildTransaction(request):
1038-
return functools.partial(invoke_contract, api_call_desig='buildTransaction')
1037+
def build_transaction(request):
1038+
return functools.partial(invoke_contract, api_call_desig='build_transaction')

tests/core/contracts/test_contract_ambiguous_functions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ def test_contract_function_methods(string_contract):
181181
get_value_func = string_contract.get_function_by_signature('getValue()')
182182
assert isinstance(set_value_func('Hello').transact(), HexBytes)
183183
assert get_value_func().call() == 'Hello'
184-
assert isinstance(set_value_func('Hello World').estimateGas(), int)
185-
assert isinstance(set_value_func('Hello World').buildTransaction(), dict)
184+
assert isinstance(set_value_func('Hello World').estimate_gas(), int)
185+
assert isinstance(set_value_func('Hello World').build_transaction(), dict)
186186

187187

188188
def test_diff_between_fn_and_fn_called(string_contract):

tests/core/contracts/test_contract_buildTransaction.py renamed to tests/core/contracts/test_contract_build_transaction.py

+35-30
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ def payable_tester_contract(web3, PayableTesterContract, address_conversion_func
4545
def test_build_transaction_not_paying_to_nonpayable_function(
4646
web3,
4747
payable_tester_contract,
48-
buildTransaction):
49-
txn = buildTransaction(contract=payable_tester_contract,
50-
contract_function='doNoValueCall')
48+
build_transaction):
49+
txn = build_transaction(contract=payable_tester_contract,
50+
contract_function='doNoValueCall')
5151
assert dissoc(txn, 'gas') == {
5252
'to': payable_tester_contract.address,
5353
'data': '0xe4cb8f5c',
@@ -61,15 +61,15 @@ def test_build_transaction_not_paying_to_nonpayable_function(
6161
def test_build_transaction_paying_to_nonpayable_function(
6262
web3,
6363
payable_tester_contract,
64-
buildTransaction):
64+
build_transaction):
6565
with pytest.raises(ValidationError):
66-
buildTransaction(contract=payable_tester_contract,
67-
contract_function='doNoValueCall',
68-
tx_params={'value': 1})
66+
build_transaction(contract=payable_tester_contract,
67+
contract_function='doNoValueCall',
68+
tx_params={'value': 1})
6969

7070

71-
def test_build_transaction_with_contract_no_arguments(web3, math_contract, buildTransaction):
72-
txn = buildTransaction(contract=math_contract, contract_function='increment')
71+
def test_build_transaction_with_contract_no_arguments(web3, math_contract, build_transaction):
72+
txn = build_transaction(contract=math_contract, contract_function='increment')
7373
assert dissoc(txn, 'gas') == {
7474
'to': math_contract.address,
7575
'data': '0xd09de08a',
@@ -81,7 +81,7 @@ def test_build_transaction_with_contract_no_arguments(web3, math_contract, build
8181

8282

8383
def test_build_transaction_with_contract_fallback_function(web3, fallback_function_contract):
84-
txn = fallback_function_contract.fallback.buildTransaction()
84+
txn = fallback_function_contract.fallback.build_transaction()
8585
assert dissoc(txn, 'gas') == {
8686
'to': fallback_function_contract.address,
8787
'data': '0x',
@@ -96,8 +96,8 @@ def test_build_transaction_with_contract_class_method(
9696
web3,
9797
MathContract,
9898
math_contract,
99-
buildTransaction):
100-
txn = buildTransaction(
99+
build_transaction):
100+
txn = build_transaction(
101101
contract=MathContract,
102102
contract_function='increment',
103103
tx_params={'to': math_contract.address},
@@ -115,8 +115,8 @@ def test_build_transaction_with_contract_class_method(
115115
def test_build_transaction_with_contract_default_account_is_set(
116116
web3,
117117
math_contract,
118-
buildTransaction):
119-
txn = buildTransaction(contract=math_contract, contract_function='increment')
118+
build_transaction):
119+
txn = build_transaction(contract=math_contract, contract_function='increment')
120120
assert dissoc(txn, 'gas') == {
121121
'to': math_contract.address,
122122
'data': '0xd09de08a',
@@ -127,11 +127,11 @@ def test_build_transaction_with_contract_default_account_is_set(
127127
}
128128

129129

130-
def test_build_transaction_with_gas_price_strategy_set(web3, math_contract, buildTransaction):
130+
def test_build_transaction_with_gas_price_strategy_set(web3, math_contract, build_transaction):
131131
def my_gas_price_strategy(web3, transaction_params):
132132
return 5
133133
web3.eth.set_gas_price_strategy(my_gas_price_strategy)
134-
txn = buildTransaction(contract=math_contract, contract_function='increment')
134+
txn = build_transaction(contract=math_contract, contract_function='increment')
135135
assert dissoc(txn, 'gas') == {
136136
'to': math_contract.address,
137137
'data': '0xd09de08a',
@@ -143,20 +143,20 @@ def my_gas_price_strategy(web3, transaction_params):
143143

144144
def test_build_transaction_with_contract_data_supplied_errors(web3,
145145
math_contract,
146-
buildTransaction):
146+
build_transaction):
147147
with pytest.raises(ValueError):
148-
buildTransaction(contract=math_contract,
149-
contract_function='increment',
150-
tx_params={'data': '0x000'})
148+
build_transaction(contract=math_contract,
149+
contract_function='increment',
150+
tx_params={'data': '0x000'})
151151

152152

153153
def test_build_transaction_with_contract_to_address_supplied_errors(web3,
154154
math_contract,
155-
buildTransaction):
155+
build_transaction):
156156
with pytest.raises(ValueError):
157-
buildTransaction(contract=math_contract,
158-
contract_function='increment',
159-
tx_params={'to': '0xb2930B35844a230f00E51431aCAe96Fe543a0347'})
157+
build_transaction(contract=math_contract,
158+
contract_function='increment',
159+
tx_params={'to': '0xb2930B35844a230f00E51431aCAe96Fe543a0347'})
160160

161161

162162
@pytest.mark.parametrize(
@@ -219,19 +219,24 @@ def test_build_transaction_with_contract_with_arguments(web3, skip_if_testrpc, m
219219
method_kwargs,
220220
expected,
221221
skip_testrpc,
222-
buildTransaction):
222+
build_transaction):
223223
if skip_testrpc:
224224
skip_if_testrpc(web3)
225225

226-
txn = buildTransaction(contract=math_contract,
227-
contract_function='increment',
228-
func_args=method_args,
229-
func_kwargs=method_kwargs,
230-
tx_params=transaction_args)
226+
txn = build_transaction(contract=math_contract,
227+
contract_function='increment',
228+
func_args=method_args,
229+
func_kwargs=method_kwargs,
230+
tx_params=transaction_args)
231231
expected['to'] = math_contract.address
232232
assert txn is not None
233233
if 'gas' in transaction_args:
234234
assert txn['gas'] == transaction_args['gas']
235235
else:
236236
assert 'gas' in txn
237237
assert dissoc(txn, 'gas') == expected
238+
239+
240+
def test_buildTransaction_deprecated(math_contract):
241+
with pytest.warns(DeprecationWarning, match="deprecated in favor of build_transaction"):
242+
math_contract.functions.counter().buildTransaction()

tests/core/contracts/test_contract_class_construction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ def test_error_to_call_non_existent_fallback(web3,
5353
bytecode_runtime=MATH_RUNTIME,
5454
)
5555
with pytest.raises(FallbackNotFound):
56-
math_contract.fallback.estimateGas()
56+
math_contract.fallback.estimate_gas()

0 commit comments

Comments
 (0)