def client(tool_solc, server): sources = ContractSourceList() compiler = Compiler(executable=tool_solc.get("solc")) sources.add_string( "TestContract", PRAGMA + textwrap.dedent(""" contract TestContract { uint256 public a; uint256 public b; event Change(uint256 previous, uint256 current); event Create(); constructor() public { a = 42; b = 1; emit Create(); } function a_plus_b() public view returns (uint256) { return a + b; } function a_plus(uint256 value) public view returns (uint256) { return a + value; } function set_b(uint256 value) public { emit Change(b, value); b = value; } } """)) compiled = compiler.compile(sources) client = ETHClient(endpoint=server.endpoint) client.update_contracts(compiled) return client
def rescue_cats(client: ETHClient, shelter_address): owner = client.address(0) shelter = client.use("CatShelter", shelter_address) with client.account(owner): for _ in range(10): time.sleep(2) print("Owner (%s) rescued a new cat" % owner) shelter.transact_sync("rescue")
def test_0001_run(server: ETHTestServer): client = ETHClient(endpoint=server.endpoint) client.mine_block() blocktime = client.get_last_blocktime() now = datetime.datetime.now() time_bound_low = int((now - datetime.timedelta(days=1)).timestamp()) time_bound_high = int((now + datetime.timedelta(days=1)).timestamp()) assert (time_bound_low <= blocktime <= time_bound_high)
def adopt_cats(client: ETHClient, shop_address): adopter = client.address(1) shop = client.use("CatShelter", shop_address) flt = client.add_filter([shop], ["Rescued"]) def watch_and_adopt(): for log in client.iter_filters([flt]): catId = log.data.args["catId"] with client.account(adopter): print("User (%s) is adopting Cat #%d" % (adopter, catId)) shop.transact_sync("adopt", catId) thread = threading.Thread(target=watch_and_adopt) thread.start() time.sleep(10) client.remove_filter(flt) thread.join()
def create_client(self, endpoint=None) -> "ETHClient": """Create a ETHClient object, used to interact with an ethereum node. :param endpoint: if set, it overrides the Client.Endpoint setting in the configuration """ from solitude.client import ETHClient if endpoint is None: endpoint = self._cfg["Client.Endpoint"] client = ETHClient(endpoint=endpoint) client.set_default_gaslimit(self._cfg["Client.GasLimit"]) client.set_default_gasprice(self._cfg["Client.GasPrice"]) return client
def test_0003_pay(server: ETHTestServer, tool_solc): CONTRACT_NAME = "TestContract" sources = ContractSourceList() compiler = Compiler(executable=tool_solc.get("solc")) sources.add_string( "test", TEST_CONTRACT.format(solidity_version=SOLIDITY_VERSION, contract_name=CONTRACT_NAME)) compiled = compiler.compile(sources) client = ETHClient(endpoint=server.endpoint) client.update_contracts(compiled) with client.account(client.address(0)): TestContract = client.deploy(CONTRACT_NAME, args=(), wrapper=MyTestContractWrapper) value = TestContract.pay(100, 110) assert value == 110
def deploy(client: ETHClient): owner = client.address(0) with client.account(owner): shelter = client.deploy("CatShelter", args=()) return shelter
def test_0002_increase_time(server: ETHTestServer, tool_solc): TIME_OFFSET = 10000 # seconds TOLERANCE = 60 # seconds CONTRACT_NAME = "TestContract" sources = ContractSourceList() compiler = Compiler(executable=tool_solc.get("solc")) sources.add_string( "test", TEST_CONTRACT.format(solidity_version=SOLIDITY_VERSION, contract_name=CONTRACT_NAME)) compiled = compiler.compile(sources) client = ETHClient(endpoint=server.endpoint) client.update_contracts(compiled) with client.account(client.address(0)): TestContract = client.deploy(CONTRACT_NAME, args=(), wrapper=MyTestContractWrapper) client.increase_blocktime_offset(10) client.mine_block() t = client.get_last_blocktime() client.increase_blocktime_offset(TIME_OFFSET) client.mine_block() blocktime = client.get_last_blocktime() time_diff = blocktime - t assert abs(time_diff - TIME_OFFSET) < TOLERANCE, ( "time of new block is different from expected increase") soltime = TestContract.getTime() assert abs(soltime - blocktime) < TOLERANCE, ( "Time from contract function is different from last block time")
def test_0002_transactions(client: ETHClient, contracts: Dict[str, ContractBase]): TestContract = contracts["TestContract"] with client.account(client.address(0)): TestContract.transact_sync("set_b", 30) assert TestContract.functions.a_plus_b().call() == 42 + 30
def test_0003_events(client: ETHClient, contracts: Dict[str, ContractBase]): TestContract = contracts["TestContract"] attila = client.address(0) with client.account(attila), client.capture("*:TestContract.Change"): TestContract.transact_sync("set_b", 50) events = client.get_events() assert len(events) == 1 assert event_is(events[0], "TestContract", "Change", (1, 50)) with client.account(attila), client.capture(re.compile(r".*:TestContract\..*")): TestContract.transact_sync("set_b", 40) events = client.get_events() assert len(events) == 1 assert event_is(events[0], "TestContract", "Change", (50, 40)) flt = client.add_filter([TestContract], ["Change"]) with client.account(attila), client.capture("*:TestContract.*"): TestContract.transact_sync("set_b", 30) events = client.get_events() assert len(events) == 1 assert event_is(events[0], "TestContract", "Change", (40, 30)) def remove_filter(): for i in range(50): if not flt.valid: return time.sleep(0.1) client.remove_filter(flt) threading.Thread(target=remove_filter, args=()).start() event = next(client.iter_filters([flt], interval=0.25)) client.remove_filter(flt) assert event_is(event, "TestContract", "Change", (40, 30))