Skip to content

Commit

Permalink
Added async eth.modify_transaction (#2921)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidRomanovizc authored May 1, 2023
1 parent a87816c commit 201c49f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ Eth
- :meth:`web3.eth.wait_for_transaction_receipt() <web3.eth.Eth.wait_for_transaction_receipt>`
- :meth:`web3.eth.sign() <web3.eth.Eth.sign>`
- :meth:`web3.eth.sign_transaction() <web3.eth.Eth.sign_transaction>`
- :meth:`web3.eth.modify_transaction() <web3.eth.Eth.modify_transaction>`
- :meth:`web3.eth.replace_transaction() <web3.eth.Eth.replace_transaction>`
- :meth:`web3.eth.get_uncle_count() <web3.eth.Eth.get_uncle_count>`

Expand Down
1 change: 1 addition & 0 deletions newsfragments/2825.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add the ``modify_transaction`` method to the ``AsyncEth`` class
66 changes: 66 additions & 0 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,72 @@ async def test_eth_send_transaction_legacy(
assert txn["gas"] == 21000
assert txn["gasPrice"] == txn_params["gasPrice"]

@pytest.mark.asyncio
async def test_eth_modify_transaction_legacy(
self, async_w3: "AsyncWeb3", unlocked_account: ChecksumAddress
) -> None:
txn_params: TxParams = {
"from": unlocked_account,
"to": unlocked_account,
"value": Wei(1),
"gas": 21000,
"gasPrice": async_w3.to_wei(
1, "gwei"
), # must be greater than base_fee post London
}
txn_hash = await async_w3.eth.send_transaction(txn_params)

modified_txn_hash = await async_w3.eth.modify_transaction(
txn_hash, gasPrice=(cast(int, txn_params["gasPrice"]) * 2), value=2
)
modified_txn = await async_w3.eth.get_transaction(modified_txn_hash)

assert is_same_address(
modified_txn["from"], cast(ChecksumAddress, txn_params["from"])
)
assert is_same_address(
modified_txn["to"], cast(ChecksumAddress, txn_params["to"])
)
assert modified_txn["value"] == 2
assert modified_txn["gas"] == 21000
assert modified_txn["gasPrice"] == cast(int, txn_params["gasPrice"]) * 2

@pytest.mark.asyncio
async def test_eth_modify_transaction(
self, async_w3: "AsyncWeb3", unlocked_account: ChecksumAddress
) -> None:
txn_params: TxParams = {
"from": unlocked_account,
"to": unlocked_account,
"value": Wei(1),
"gas": 21000,
"maxPriorityFeePerGas": async_w3.to_wei(1, "gwei"),
"maxFeePerGas": async_w3.to_wei(2, "gwei"),
}
txn_hash = await async_w3.eth.send_transaction(txn_params)

modified_txn_hash = await async_w3.eth.modify_transaction(
txn_hash,
value=2,
maxPriorityFeePerGas=(cast(Wei, txn_params["maxPriorityFeePerGas"]) * 2),
maxFeePerGas=(cast(Wei, txn_params["maxFeePerGas"]) * 2),
)
modified_txn = await async_w3.eth.get_transaction(modified_txn_hash)

assert is_same_address(
modified_txn["from"], cast(ChecksumAddress, txn_params["from"])
)
assert is_same_address(
modified_txn["to"], cast(ChecksumAddress, txn_params["to"])
)
assert modified_txn["value"] == 2
assert modified_txn["gas"] == 21000
assert (
modified_txn["maxPriorityFeePerGas"]
== cast(Wei, txn_params["maxPriorityFeePerGas"]) * 2
)
assert modified_txn["maxFeePerGas"] == cast(Wei, txn_params["maxFeePerGas"]) * 2

@pytest.mark.asyncio
async def test_async_eth_sign_transaction(
self, async_w3: "AsyncWeb3", unlocked_account: ChecksumAddress
Expand Down
27 changes: 27 additions & 0 deletions web3/eth/async_eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Tuple,
Type,
Union,
cast,
overload,
)
import warnings
Expand All @@ -19,6 +20,9 @@
ChecksumAddress,
HexStr,
)
from eth_utils.toolz import (
merge,
)
from hexbytes import (
HexBytes,
)
Expand All @@ -40,6 +44,10 @@
from web3._utils.rpc_abi import (
RPC,
)
from web3._utils.transactions import (
assert_valid_transaction_params,
extract_valid_transaction_params,
)
from web3.contract import (
AsyncContract,
AsyncContractCaller,
Expand Down Expand Up @@ -533,6 +541,25 @@ async def replace_transaction(
self.w3, current_transaction, new_transaction
)

# todo: Update Any to stricter kwarg checking with TxParams
# https://github.com/python/mypy/issues/4441
async def modify_transaction(
self, transaction_hash: _Hash32, **transaction_params: Any
) -> HexBytes:
assert_valid_transaction_params(cast(TxParams, transaction_params))

current_transaction = await async_get_required_transaction(
self.w3, transaction_hash
)
current_transaction_params = extract_valid_transaction_params(
current_transaction
)
new_transaction = merge(current_transaction_params, transaction_params)

return await async_replace_transaction(
self.w3, current_transaction, new_transaction
)

# eth_sign

_sign: Method[Callable[..., Awaitable[HexStr]]] = Method(
Expand Down

0 comments on commit 201c49f

Please sign in to comment.