Skip to content

Commit

Permalink
snakecase toWei and fromWei (#2647)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolovim authored Sep 14, 2022
1 parent 28c7751 commit 086c1a8
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 103 deletions.
6 changes: 3 additions & 3 deletions docs/contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ Taking the following contract code as an example:

>>> array_contract.functions.getBytes2Value().call()
[b'b\x00']
>>> array_contract.functions.setBytes2Value([b'a']).transact({'gas': 420000, 'gasPrice': Web3.toWei(1, 'gwei')})
>>> array_contract.functions.setBytes2Value([b'a']).transact({'gas': 420000, 'gasPrice': Web3.to_wei(1, 'gwei')})
HexBytes('0xc5377ba25224bd763ceedc0ee455cc14fc57b23dbc6b6409f40a557a009ff5f4')
>>> array_contract.functions.getBytes2Value().call()
[b'a\x00']
Expand Down Expand Up @@ -1052,7 +1052,7 @@ Event Log Object
alice, bob = w3.eth.accounts[0], w3.eth.accounts[1]
assert alice == '0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf', alice
assert bob == '0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF', bob
tx_hash = my_token_contract.constructor(1000000).transact({'from': alice, 'gas': 899000, 'gasPrice': Web3.toWei(1, 'gwei')})
tx_hash = my_token_contract.constructor(1000000).transact({'from': alice, 'gas': 899000, 'gasPrice': Web3.to_wei(1, 'gwei')})
assert tx_hash == HexBytes('0x49e3da72a95e4074a9eaea7b438c73ca154627d317e58abeae914e3769a15044'), tx_hash
txn_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
assert txn_receipt['contractAddress'] == '0xF2E246BB76DF876Cef8b38ae84130F4F55De395b', txn_receipt['contractAddress']
Expand All @@ -1061,7 +1061,7 @@ Event Log Object
total_supply = contract.functions.totalSupply().call()
decimals = 10 ** 18
assert total_supply == 1000000 * decimals, total_supply
tx_hash = contract.functions.transfer(alice, 10).transact({'gas': 899000, 'gasPrice': Web3.toWei(1, 'gwei')})
tx_hash = contract.functions.transfer(alice, 10).transact({'gas': 899000, 'gasPrice': Web3.to_wei(1, 'gwei')})
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)

.. doctest:: createFilter
Expand Down
14 changes: 7 additions & 7 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,23 @@ Web3 can help you convert between denominations. The following denominations ar
+--------------+---------------------------------+

Picking up from the previous example, the largest account contained
3841357360894980500000001 wei. You can use the :meth:`~web3.fromWei` method
3841357360894980500000001 wei. You can use the :meth:`~web3.from_wei` method
to convert that balance to ether (or another denomination).

.. code-block:: python
>>> web3.fromWei(3841357360894980500000001, 'ether')
>>> web3.from_wei(3841357360894980500000001, 'ether')
Decimal('3841357.360894980500000001')
To convert back to wei, you can use the inverse function, :meth:`~web3.toWei`.
To convert back to wei, you can use the inverse function, :meth:`~web3.to_wei`.
Note that Python's default floating point precision is insufficient for this
use case, so it's necessary to cast the value to a
`Decimal <https://docs.python.org/3/library/decimal.html>`_ if it isn't already.

.. code-block:: python
>>> from decimal import Decimal
>>> web3.toWei(Decimal('3841357.360894980500000001'), 'ether')
>>> web3.to_wei(Decimal('3841357.360894980500000001'), 'ether')
3841357360894980500000001
Best practice: If you need to work with multiple currency denominations, default
Expand All @@ -167,9 +167,9 @@ wei, then from wei to whatever you need.

.. code-block:: python
>>> web3.toWei(Decimal('0.000000005'), 'ether')
>>> web3.to_wei(Decimal('0.000000005'), 'ether')
5000000000
>>> web3.fromWei(5000000000, 'gwei')
>>> web3.from_wei(5000000000, 'gwei')
Decimal('5')
Expand Down Expand Up @@ -493,7 +493,7 @@ contract which conforms to this standard.
alice, bob = w3.eth.accounts[0], w3.eth.accounts[1]
assert alice == '0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf', alice
assert bob == '0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF', bob
tx_hash = factory.constructor(1000000).transact({'from': alice, 'gas': 899000, 'gasPrice': Web3.toWei(1, 'gwei')})
tx_hash = factory.constructor(1000000).transact({'from': alice, 'gas': 899000, 'gasPrice': Web3.to_wei(1, 'gwei')})
assert tx_hash == HexBytes('0x49e3da72a95e4074a9eaea7b438c73ca154627d317e58abeae914e3769a15044'), tx_hash
txn_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
assert txn_receipt['contractAddress'] == '0xF2E246BB76DF876Cef8b38ae84130F4F55De395b', txn_receipt['contractAddress']
Expand Down
6 changes: 3 additions & 3 deletions docs/gas_price.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ returns a higher gas price when the value of the transaction is higher than
from web3 import Web3
def value_based_gas_price_strategy(web3, transaction_params):
if transaction_params['value'] > Web3.toWei(1, 'ether'):
return Web3.toWei(20, 'gwei')
if transaction_params['value'] > Web3.to_wei(1, 'ether'):
return Web3.to_wei(20, 'gwei')
else:
return Web3.toWei(5, 'gwei')
return Web3.to_wei(5, 'gwei')
Selecting the gas price strategy
--------------------------------
Expand Down
4 changes: 2 additions & 2 deletions docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ Address Helpers
Currency Conversions
--------------------

- :meth:`Web3.fromWei() <web3.Web3.fromWei>`
- :meth:`Web3.toWei() <web3.Web3.toWei>`
- :meth:`Web3.from_wei() <web3.Web3.from_wei>`
- :meth:`Web3.to_wei() <web3.Web3.to_wei>`


Cryptographic Hashing
Expand Down
4 changes: 2 additions & 2 deletions docs/web3.eth.account.rst
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ To sign a transaction locally that will invoke a smart contract:
... ).build_transaction({
... 'chainId': 1,
... 'gas': 70000,
... 'maxFeePerGas': w3.toWei('2', 'gwei'),
... 'maxPriorityFeePerGas': w3.toWei('1', 'gwei'),
... 'maxFeePerGas': w3.to_wei('2', 'gwei'),
... 'maxPriorityFeePerGas': w3.to_wei('1', 'gwei'),
... 'nonce': nonce,
... })

Expand Down
6 changes: 3 additions & 3 deletions docs/web3.eth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,8 @@ The following methods are available on the ``web3.eth`` namespace.
'from': web3.eth.coinbase,
'value': 12345,
'gas': 21000,
'maxFeePerGas': web3.toWei(250, 'gwei'),
'maxPriorityFeePerGas': web3.toWei(2, 'gwei'),
'maxFeePerGas': web3.to_wei(250, 'gwei'),
'maxPriorityFeePerGas': web3.to_wei(2, 'gwei'),
})
HexBytes('0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331')
Expand All @@ -862,7 +862,7 @@ The following methods are available on the ``web3.eth`` namespace.
'from': web3.eth.coinbase,
'value': 12345,
'gas': 21000,
'gasPrice': web3.toWei(50, 'gwei'),
'gasPrice': web3.to_wei(50, 'gwei'),
})
HexBytes('0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331')
Expand Down
8 changes: 4 additions & 4 deletions docs/web3.main.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,27 +179,27 @@ Encoding and Decoding Helpers
Currency Conversions
~~~~~~~~~~~~~~~~~~~~~

.. py:method:: Web3.toWei(value, currency)
.. py:method:: Web3.to_wei(value, currency)
Returns the value in the denomination specified by the ``currency`` argument
converted to wei.


.. code-block:: python
>>> Web3.toWei(1, 'ether')
>>> Web3.to_wei(1, 'ether')
1000000000000000000
.. py:method:: Web3.fromWei(value, currency)
.. py:method:: Web3.from_wei(value, currency)
Returns the value in wei converted to the given currency. The value is returned
as a ``Decimal`` to ensure precision down to the wei.


.. code-block:: python
>>> Web3.fromWei(1000000000000000000, 'ether')
>>> Web3.from_wei(1000000000000000000, 'ether')
Decimal('1')
Expand Down
1 change: 1 addition & 0 deletions newsfragments/2647.breaking-change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Snakecase the ``toWei`` and ``fromWei`` methods
4 changes: 2 additions & 2 deletions tests/core/contracts/test_contract_caller_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def transaction_dict(w3, address):
return {
"from": address,
"gas": 210000,
"maxFeePerGas": w3.toWei(1, "gwei"),
"maxPriorityFeePerGas": w3.toWei(1, "gwei"),
"maxFeePerGas": w3.to_wei(1, "gwei"),
"maxPriorityFeePerGas": w3.to_wei(1, "gwei"),
"value": 12345,
}

Expand Down
2 changes: 1 addition & 1 deletion tests/core/middleware/test_transaction_signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def test_gen_normalized_accounts_type_error(w3):
@pytest.fixture()
def fund_account(w3):
# fund local account
tx_value = w3.toWei(10, "ether")
tx_value = w3.to_wei(10, "ether")
for address in (ADDRESS_1, ADDRESS_2):
w3.eth.send_transaction(
{"to": address, "from": w3.eth.accounts[0], "gas": 21000, "value": tx_value}
Expand Down
6 changes: 3 additions & 3 deletions tests/core/tools/pytest_ethereum/test_linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ def callback_fn(package):
deploy(
"Escrow",
recipient,
transaction={"from": sender, "value": w3.toWei("1", "ether")},
transaction={"from": sender, "value": w3.to_wei("1", "ether")},
),
run_python(callback_fn),
)
escrow_deployer.register_strategy("Escrow", escrow_strategy)
assert w3.eth.get_balance(recipient) == w3.toWei("1000000", "ether")
assert w3.eth.get_balance(recipient) == w3.to_wei("1000000", "ether")
linked_escrow_package = escrow_deployer.deploy("Escrow")
escrow_instance = linked_escrow_package.deployments.get_instance("Escrow")
assert escrow_instance.functions.sender().call() == sender
assert w3.eth.get_balance(recipient) == w3.toWei("1000001", "ether")
assert w3.eth.get_balance(recipient) == w3.to_wei("1000001", "ether")
8 changes: 4 additions & 4 deletions tests/ens/test_get_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def test_set_text_pass_in_transaction_dict(ens):
"avatar",
"example.jpeg",
transact={
"maxFeePerGas": Web3.toWei(100, "gwei"),
"maxPriorityFeePerGas": Web3.toWei(100, "gwei"),
"maxFeePerGas": Web3.to_wei(100, "gwei"),
"maxPriorityFeePerGas": Web3.to_wei(100, "gwei"),
},
)
assert ens.get_text("tester.eth", "url") == "http://example.com"
Expand Down Expand Up @@ -120,8 +120,8 @@ async def async_test_set_text_pass_in_transaction_dict(async_ens):
"avatar",
"example.jpeg",
transact={
"maxFeePerGas": Web3.toWei(100, "gwei"),
"maxPriorityFeePerGas": Web3.toWei(100, "gwei"),
"maxFeePerGas": Web3.to_wei(100, "gwei"),
"maxPriorityFeePerGas": Web3.to_wei(100, "gwei"),
},
)
assert await async_ens.get_text("tester.eth", "url") == "http://example.com"
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_ethereum_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def unlockable_account(w3, unlockable_account_pw):
{
"from": w3.eth.coinbase,
"to": account,
"value": w3.toWei(10, "ether"),
"value": w3.to_wei(10, "ether"),
"gas": 21000,
}
)
Expand Down Expand Up @@ -242,7 +242,7 @@ def funded_account_for_raw_txn(w3):
{
"from": w3.eth.coinbase,
"to": account,
"value": w3.toWei(10, "ether"),
"value": w3.to_wei(10, "ether"),
"gas": 21000,
"gas_price": 1,
}
Expand Down
Loading

0 comments on commit 086c1a8

Please sign in to comment.