Skip to content

Commit

Permalink
Fix sign-and-send and add some tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
gnpar committed Dec 4, 2024
1 parent 2bab174 commit aad91f8
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 22 deletions.
11 changes: 6 additions & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ gmpy2 =

# Add here test requirements (semicolon/line-separated)
testing =
setuptools
pytest
boto3
factory-boy
gmpy2
pytest
pytest-cov
web3[tester]==7.*
boto3
pytest-mock
pytest-recording
factory-boy
setuptools
web3[tester]==7.*

[options.entry_points]
# Add here console scripts like:
Expand Down
8 changes: 4 additions & 4 deletions src/ethproto/w3wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def transact(provider, function, tx_kwargs):
}
)
signed_tx = from_.sign_transaction(tx)
tx_hash = provider.w3.eth.send_raw_transaction(signed_tx.rawTransaction)
tx_hash = provider.w3.eth.send_raw_transaction(signed_tx.raw_transaction)
elif W3_TRANSACT_MODE == "defender-async":
from .defender_relay import send_transaction

Expand Down Expand Up @@ -211,8 +211,8 @@ def get_account(self, name):
if isinstance(name, (Account, LocalAccount)):
return name
if name is None:
return self.ZERO
if type(name) == str and name.startswith("0x"):
return list(self.signers.values())[0] if self.signers else self.ZERO
if isinstance(name, str) and name.startswith("0x"):
return name
if name in self.name_to_address:
return self.name_to_address[name]
Expand Down Expand Up @@ -483,7 +483,7 @@ def init_eth_wrapper(self, eth_wrapper, owner, init_params, kwargs):
for lib, _ in contract_def.libraries():
if lib not in libraries:
library_def = self.get_contract_factory(lib)
library = self.construct(library_def)
library = self.construct(library_def, transact_kwargs={"from": eth_wrapper.owner})
libraries[lib] = library.address

if libraries:
Expand Down
2 changes: 1 addition & 1 deletion src/ethproto/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class ETHWrapper:
constructor_args = None
initialize_args = None

def __init__(self, owner="owner", *init_params, **kwargs):
def __init__(self, owner=None, *init_params, **kwargs):
self.provider_key = kwargs.get("provider_key", None)
init_params = self._parse_init_params(init_params, kwargs)
self.provider.init_eth_wrapper(self, owner, init_params, kwargs)
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ def local_node_provider(hardhat_node):
if os.environ.get("TEST_ENV", None) == "web3py":
from web3 import Web3

from ethproto import w3wrappers, wrappers
from ethproto import w3wrappers

wrappers.register_provider("w3", w3wrappers.W3Provider(Web3(Web3.HTTPProvider(hardhat_node))))
w3wrappers.register_w3_provider("w3", Web3(Web3.HTTPProvider(hardhat_node)))
yield
wrappers.register_provider("w3", w3wrappers.W3Provider(w3))
w3wrappers.register_w3_provider("w3", w3)
return
yield

Expand Down
53 changes: 48 additions & 5 deletions tests/test_w3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os

import pytest
from web3 import Web3

from ethproto import wrappers
from ethproto import w3wrappers, wrappers

pytestmark = [
pytest.mark.skipif(os.environ.get("TEST_ENV", None) != "web3py", reason="web3py-only tests"),
Expand Down Expand Up @@ -36,7 +37,7 @@ class CounterUpgradeableWithLibrary(Counter):

@pytest.mark.parametrize("contract_class", [Counter, CounterWithLibrary, CounterUpgradeableWithLibrary])
def test_deploy_counter(contract_class):
counter = contract_class(initial_value=0)
counter = contract_class(initial_value=0, owner="owner")
assert counter.value() == 0
counter.increase()
assert counter.value() == 1
Expand All @@ -51,7 +52,7 @@ class Datatypes(wrappers.ETHWrapper):
def test_address_arguments():
from eth_account import Account

wrapper = Datatypes()
wrapper = Datatypes(owner="owner")

account = Account.create("TEST TEST TEST")

Expand All @@ -76,7 +77,7 @@ def test_wrapper_build_from_def():
contract_def = provider.get_contract_def("Counter")
wrapper = wrappers.ETHWrapper.build_from_def(contract_def)

counter = wrapper(initialValue=0)
counter = wrapper(initialValue=0, owner="owner")
assert counter.value() == 0
counter.increase()
assert counter.value() == 1
Expand All @@ -87,7 +88,7 @@ def test_get_events():
contract_def = provider.get_contract_def("EventLauncher")
wrapper = wrappers.ETHWrapper.build_from_def(contract_def)

launcher = wrapper()
launcher = wrapper(owner="owner")

launcher.launchEvent1(1)

Expand All @@ -109,3 +110,45 @@ def test_get_events():
event2 = provider.get_events(launcher, "Event2")
assert len(event2) == 1
assert event2[0].args.value == 2


@pytest.fixture
def sign_and_send(mocker, hardhat_node):
"""Sets up sign-and-send transact mode with a well-known address, returns the address"""
mocker.patch("ethproto.w3wrappers.W3_TRANSACT_MODE", "sign-and-send")
mocker.patch.dict(
os.environ,
{"W3_ADDR_HARDHAT_18": "0xde9be858da4a475276426320d5e9262ecfc3ba460bfac56360bfa6c4c28b4ee0"},
)
# Force recreate the provider and its address book after patching the environment
w3wrappers.register_w3_provider("w3", Web3(Web3.HTTPProvider(hardhat_node)))
return "0xdD2FD4581271e230360230F9337D5c0430Bf44C0"


def test_sign_and_send(sign_and_send):
# Deploy a contract using sign-and-send
wrapper = Datatypes(owner="HARDHAT_18")

assert wrapper.echoAddress("HARDHAT_18") == "HARDHAT_18"
assert wrappers.get_provider("w3").address_book.get_account("HARDHAT_18") == sign_and_send


def test_sign_and_send_upgradeable(sign_and_send):
upgradeable = CounterUpgradeableWithLibrary(initial_value=0, owner="HARDHAT_18")
assert upgradeable.value() == 0
upgradeable.increase()
assert upgradeable.value() == 1


def test_sign_and_send_interact_with_existing_contract(sign_and_send):
counter = Counter(initial_value=0, owner="HARDHAT_18")
assert counter.value() == 0 # sanity check

connected = Counter.connect(counter.contract.address)

# Interactions with the connected contract work as expected
assert connected.value() == 0
connected.increase()
assert connected.value() == 1

assert counter.value() == 1 # sanity check
5 changes: 1 addition & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ setenv =
TOXINIDIR = {toxinidir}
TEST_ENV = web3py
WEB3_PROVIDER_URI = https://polygon-mainnet.g.alchemy.com/v2/FAKEY
W3_POA = no
passenv =
HOME
WADRAY_USE_GMPY2
W3_*
TRANSACT_MODE
WEB3_PROVIDER_URI
WEB3_*
extras =
testing
deps =
Expand Down

0 comments on commit aad91f8

Please sign in to comment.