Пример #1
0
    def get_event_log(self,
                      event_name: str,
                      block: int,
                      did: str,
                      timeout: int = 45) -> Optional[AttributeDict]:
        """
        :return: Log if event is found else None
        """
        did = remove_0x_prefix(did)
        start = time.time()
        event = getattr(self.events, event_name)
        logs = []
        while not logs:
            logs = ContractBase.getLogs(event(), fromBlock=block)
            if not logs:
                time.sleep(0.2)

            if time.time() - start > timeout:
                break

        if not logs:
            return None

        _log = None
        for log in logs:
            if remove_0x_prefix(log.args.dataToken) == did:
                _log = log
                break
        return _log
Пример #2
0
def test_main(network, alice_wallet, alice_ocean, dtfactory_address, web3):

    # test super-simple functionality of child
    factory = MyFactory(web3, dtfactory_address)
    factory.createToken("foo_blob", "DT1", "DT1", to_wei(1000), alice_wallet)

    # test attributes
    assert factory.name == "DTFactory"
    assert isinstance(factory.contract.caller, ContractCaller)
    assert factory.contract is not None
    assert factory.contract.address == dtfactory_address
    assert ContractBase.to_checksum_address(
        dtfactory_address) == dtfactory_address

    # test methods
    assert "configured_address" in dir(factory)
    assert factory.contract_name == "DTFactory"
    assert factory.address == dtfactory_address
    assert factory.events
    assert str(factory) == f"{factory.contract_name} @ {factory.address}"
    assert ("createToken" in factory.function_names
            ), "The function createToken from the contract does not exist."
    assert "getCurrentTokenCount" in factory.function_names
    assert "getTokenTemplate" in factory.function_names
    assert not factory.is_tx_successful("nohash")
    with pytest.raises(ValueError):
        assert factory.get_event_signature("noevent")

    assert factory.subscribe_to_event("TokenCreated", 30, None) is None
    assert factory.get_event_argument_names("TokenCreated") == ()
    block = web3.eth.block_number
    block_confirmations = alice_ocean.config.block_confirmations.value
    assert (len(
        factory.get_event_logs(
            "TokenCreated",
            block - block_confirmations,
            block - block_confirmations,
            None,
        )) == 1), "The token was not created."
    log = factory.get_event_log("TokenCreated", block - block_confirmations,
                                block - block_confirmations, None)
    assert len(log) == 1, "The token was not created."
    assert log[0]["event"] == "TokenCreated"
    assert log[0]["address"] == dtfactory_address

    with pytest.raises(TypeError):
        ContractBase.getLogs(None)
Пример #3
0
 def get_event_logs(
     self,
     event_name: str,
     filter_args: Optional[Dict[str, str]] = None,
     from_block: Optional[int] = 0,
     to_block: Optional[int] = "latest",
 ) -> Union[Tuple[()], Tuple[AttributeDict]]:
     event = getattr(self.events, event_name)
     filter_params = filter_args or {}
     logs = ContractBase.getLogs(
         event(),
         argument_filters=filter_params,
         fromBlock=from_block,
         toBlock=to_block,
     )
     return logs
Пример #4
0
def test_main(network, alice_wallet, alice_address, dtfactory_address,
              alice_ocean):

    # test super-simple functionality of child
    factory = MyFactory(dtfactory_address)
    factory.createToken("foo_blob", "DT1", "DT1", to_base_18(1000.0),
                        alice_wallet)

    # test attributes
    assert factory.name == "DTFactory"
    assert isinstance(factory.contract_concise, ConciseContract)
    assert factory.contract is not None
    assert factory.contract.address == dtfactory_address
    assert ContractBase.to_checksum_address(
        dtfactory_address) == dtfactory_address

    # test methods
    assert "configured_address" in dir(factory)
    assert factory.contract_name == "DTFactory"
    assert factory.address == dtfactory_address
    assert factory.events
    assert str(factory) == f"{factory.contract_name} @ {factory.address}"
    assert "createToken" in factory.function_names
    assert "getCurrentTokenCount" in factory.function_names
    assert "getTokenTemplate" in factory.function_names
    assert not factory.is_tx_successful("nohash")
    with pytest.raises(ValueError):
        assert factory.get_event_signature("noevent")
    assert factory.subscribe_to_event("TokenCreated", 30, None) is None
    assert factory.get_event_argument_names("TokenCreated") == ()
    block = alice_ocean.web3.eth.blockNumber
    assert len(factory.get_event_logs("TokenCreated", block, block, None)) == 1

    copy = factory.contract.address
    factory.contract.address = None
    with pytest.raises(TypeError):
        factory.getLogs("", alice_ocean.web3)
    factory.contract.address = copy
Пример #5
0
    def get_start_order_logs(
        self,
        consumer_address: Optional[str] = None,
        from_block: Optional[int] = 0,
        to_block: Optional[int] = "latest",
        from_all_tokens: bool = False,
    ) -> Tuple:
        topic0 = self.get_event_signature(self.ORDER_STARTED_EVENT)
        topics = [topic0]
        if consumer_address:
            topic1 = f"0x000000000000000000000000{consumer_address[2:].lower()}"
            topics = [topic0, None, topic1]

        argument_filters = {"topics": topics}

        logs = ContractBase.getLogs(
            self.events.OrderStarted(),
            argument_filters=argument_filters,
            fromBlock=from_block,
            toBlock=to_block,
            from_all_addresses=from_all_tokens,
        )
        return logs
Пример #6
0
 def get_transfer_events_in_range(self, from_block: Optional[int],
                                  to_block: Optional[int]) -> Tuple:
     return ContractBase.getLogs(self.events.Transfer(),
                                 fromBlock=from_block,
                                 toBlock=to_block)
Пример #7
0
def test_nochild():
    with pytest.raises(AssertionError):
        ContractBase(None)
Пример #8
0
def test_name_is_None():
    with pytest.raises(AssertionError):
        # self.name will become None, triggering the error
        ContractBase(None)
Пример #9
0
def test_static_functions(web3):
    assert (ContractBase.get_tx_receipt(web3, "nohash") is
            None), "The transaction receipt exists for the wrong hash."
Пример #10
0
def test_static_functions():
    assert ContractBase.get_tx_receipt("nohash") is None