Skip to content

Commit 1ea3b27

Browse files
committed
Add integration test for set code transactions
1 parent 3ded3a5 commit 1ea3b27

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

web3/_utils/module_testing/eth_module.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,74 @@ async def test_async_sign_and_send_raw_middleware(
723723
# clean up
724724
async_w3.middleware_onion.remove("signing")
725725

726+
@pytest.mark.asyncio
727+
async def test_async_sign_authorization_and_send_raw_set_code_transaction(
728+
self,
729+
async_w3: "AsyncWeb3",
730+
keyfile_account_pkey: HexStr,
731+
async_math_contract: "AsyncContract",
732+
) -> None:
733+
keyfile_account = async_w3.eth.account.from_key(keyfile_account_pkey)
734+
735+
chain_id = await async_w3.eth.chain_id
736+
nonce = await async_w3.eth.get_transaction_count(keyfile_account.address)
737+
738+
auth = {
739+
"chainId": chain_id,
740+
"address": async_math_contract.address,
741+
"nonce": nonce + 1,
742+
}
743+
signed_auth = keyfile_account.sign_authorization(auth)
744+
745+
txn: TxParams = {
746+
"chainId": chain_id,
747+
"to": keyfile_account.address,
748+
"value": Wei(0),
749+
"gas": 200_000,
750+
"nonce": nonce,
751+
"maxPriorityFeePerGas": Wei(10**9),
752+
"maxFeePerGas": Wei(10**9),
753+
"data": HexBytes("0x"),
754+
"authorizationList": [signed_auth],
755+
}
756+
757+
signed = keyfile_account.sign_transaction(txn)
758+
tx_hash = await async_w3.eth.send_raw_transaction(signed.raw_transaction)
759+
get_tx = await async_w3.eth.get_transaction(tx_hash)
760+
await async_w3.eth.wait_for_transaction_receipt(tx_hash, timeout=10)
761+
762+
code = await async_w3.eth.get_code(keyfile_account.address)
763+
assert code.to_0x_hex() == f"0xef0100{async_math_contract.address[2:].lower()}"
764+
765+
assert len(get_tx["authorizationList"]) == 1
766+
get_auth = get_tx["authorizationList"][0]
767+
assert get_auth["chainId"] == chain_id
768+
assert get_auth["address"] == async_math_contract.address
769+
assert get_auth["nonce"] == nonce + 1
770+
assert isinstance(get_auth["yParity"], int)
771+
assert isinstance(get_auth["r"], HexBytes)
772+
assert isinstance(get_auth["s"], HexBytes)
773+
774+
# reset code
775+
reset_auth = {
776+
"chainId": chain_id,
777+
"address": "0x" + ("00" * 20),
778+
"nonce": nonce + 3,
779+
}
780+
signed_reset_auth = keyfile_account.sign_authorization(reset_auth)
781+
new_txn = dict(txn)
782+
new_txn["authorizationList"] = [signed_reset_auth]
783+
new_txn["nonce"] = nonce + 2
784+
785+
signed_reset = keyfile_account.sign_transaction(new_txn)
786+
reset_tx_hash = await async_w3.eth.send_raw_transaction(
787+
signed_reset.raw_transaction
788+
)
789+
await async_w3.eth.wait_for_transaction_receipt(reset_tx_hash, timeout=10)
790+
791+
reset_code = await async_w3.eth.get_code(keyfile_account.address)
792+
assert reset_code == HexBytes("0x")
793+
726794
@pytest.mark.asyncio
727795
async def test_GasPriceStrategyMiddleware(
728796
self,
@@ -3785,6 +3853,68 @@ def test_sign_and_send_raw_middleware(
37853853
# cleanup
37863854
w3.middleware_onion.remove("signing")
37873855

3856+
def test_sign_authorization_and_send_raw_set_code_transaction(
3857+
self, w3: "Web3", keyfile_account_pkey: HexStr, math_contract: "Contract"
3858+
) -> None:
3859+
keyfile_account = w3.eth.account.from_key(keyfile_account_pkey)
3860+
3861+
chain_id = w3.eth.chain_id
3862+
nonce = w3.eth.get_transaction_count(keyfile_account.address)
3863+
3864+
auth = {
3865+
"chainId": chain_id,
3866+
"address": math_contract.address,
3867+
"nonce": nonce + 1,
3868+
}
3869+
signed_auth = keyfile_account.sign_authorization(auth)
3870+
3871+
txn: TxParams = {
3872+
"chainId": chain_id,
3873+
"to": keyfile_account.address,
3874+
"value": Wei(0),
3875+
"gas": 200_000,
3876+
"nonce": nonce,
3877+
"maxPriorityFeePerGas": Wei(10**9),
3878+
"maxFeePerGas": Wei(10**9),
3879+
"data": HexBytes("0x"),
3880+
"authorizationList": [signed_auth],
3881+
}
3882+
3883+
signed = keyfile_account.sign_transaction(txn)
3884+
tx_hash = w3.eth.send_raw_transaction(signed.raw_transaction)
3885+
get_tx = w3.eth.get_transaction(tx_hash)
3886+
w3.eth.wait_for_transaction_receipt(tx_hash, timeout=10)
3887+
3888+
code = w3.eth.get_code(keyfile_account.address)
3889+
assert code.to_0x_hex() == f"0xef0100{math_contract.address[2:].lower()}"
3890+
3891+
assert len(get_tx["authorizationList"]) == 1
3892+
get_auth = get_tx["authorizationList"][0]
3893+
assert get_auth["chainId"] == chain_id
3894+
assert get_auth["address"] == math_contract.address
3895+
assert get_auth["nonce"] == nonce + 1
3896+
assert isinstance(get_auth["yParity"], int)
3897+
assert isinstance(get_auth["r"], HexBytes)
3898+
assert isinstance(get_auth["s"], HexBytes)
3899+
3900+
# reset code
3901+
reset_auth = {
3902+
"chainId": chain_id,
3903+
"address": "0x" + ("00" * 20),
3904+
"nonce": nonce + 3,
3905+
}
3906+
signed_reset_auth = keyfile_account.sign_authorization(reset_auth)
3907+
new_txn = dict(txn)
3908+
new_txn["authorizationList"] = [signed_reset_auth]
3909+
new_txn["nonce"] = nonce + 2
3910+
3911+
signed_reset = keyfile_account.sign_transaction(new_txn)
3912+
reset_tx_hash = w3.eth.send_raw_transaction(signed_reset.raw_transaction)
3913+
w3.eth.wait_for_transaction_receipt(reset_tx_hash, timeout=10)
3914+
3915+
reset_code = w3.eth.get_code(keyfile_account.address)
3916+
assert reset_code == HexBytes("0x")
3917+
37883918
def test_eth_call(self, w3: "Web3", math_contract: "Contract") -> None:
37893919
txn_params = math_contract._prepare_transaction(
37903920
abi_element_identifier="add",

0 commit comments

Comments
 (0)