Example #1
0
def init_components(config=None):
    if config is None:
        config = Config(os.getenv(ENV_CONFIG_FILE))

    ConfigProvider.set_config(config)
    Web3Provider.init_web3(provider=get_web3_connection_provider(config.network_url))
    ContractHandler.set_artifacts_path(config.artifacts_path)
Example #2
0
def setup_network(config_file=None):
    config = Config(filename=config_file) if config_file else get_config()
    network_url = config.network_url
    artifacts_path = get_artifacts_path(config)

    ContractHandler.set_artifacts_path(artifacts_path)

    if network_url.startswith('http'):
        provider = CustomHTTPProvider
    elif network_url.startswith('wss'):
        provider = WebsocketProvider
    else:
        raise AssertionError(f'Unsupported network url {network_url}. Must start with http or wss.')

    Web3Provider.init_web3(provider=provider(network_url))
    if network_url.startswith('wss'):
        from web3.middleware import geth_poa_middleware
        Web3Provider.get_web3().middleware_stack.inject(geth_poa_middleware, layer=0)

    init_account_envvars()

    wallet = get_provider_wallet()
    if wallet is None:
        raise AssertionError(f'Ocean Provider cannot run without a valid '
                             f'ethereum account. `PROVIDER_PRIVATE_KEY` was not found in the environment '
                             f'variables. \nENV WAS: {sorted(os.environ.items())}')

    if not wallet.private_key:
        raise AssertionError(f'Ocean Provider cannot run without a valid '
                             f'ethereum private key..')
Example #3
0
def setup_network(config_file=None):
    config = Config(filename=config_file) if config_file else get_config()
    network_url = config.network_url
    artifacts_path = get_artifacts_path(config)

    ContractHandler.set_artifacts_path(artifacts_path)
    w3_connection_provider = get_web3_connection_provider(network_url)
    Web3Provider.init_web3(provider=w3_connection_provider)
    if network_url.startswith("wss"):
        from web3.middleware import geth_poa_middleware

        Web3Provider.get_web3().middleware_stack.inject(geth_poa_middleware,
                                                        layer=0)

    init_account_envvars()

    wallet = get_provider_wallet()
    if wallet is None:
        raise AssertionError(
            f"Ocean Provider cannot run without a valid "
            f"ethereum account. `PROVIDER_PRIVATE_KEY` was not found in the environment "
            f"variables. \nENV WAS: {sorted(os.environ.items())}")

    if not wallet.private_key:
        raise AssertionError(
            "Ocean Provider cannot run without a valid ethereum private key.")
Example #4
0
def setup_all():
    config = ExampleConfig.get_config()
    ConfigProvider.set_config(config)
    Web3Provider.init_web3(
        provider=get_web3_connection_provider(config.network_url))
    ContractHandler.set_artifacts_path(config.artifacts_path)

    network = Web3Helper.get_network_name()
    wallet = get_ganache_wallet()
    if network in ["ganache", "development"] and wallet:

        print(
            f"sender: {wallet.key}, {wallet.address}, {wallet.password}, {wallet.keysStr()}"
        )
        print(
            f"sender balance: {Web3Helper.from_wei(Web3Helper.get_ether_balance(wallet.address))}"
        )
        assert Web3Helper.from_wei(Web3Helper.get_ether_balance(
            wallet.address)) > 10

        from ocean_lib.models.data_token import DataToken

        OCEAN_token = DataToken(get_ocean_token_address(network))
        amt_distribute = 1000
        amt_distribute_base = to_base_18(float(amt_distribute))
        for w in (get_publisher_wallet(), get_consumer_wallet()):
            if Web3Helper.from_wei(Web3Helper.get_ether_balance(
                    w.address)) < 2:
                Web3Helper.send_ether(wallet, w.address, 4)

            if OCEAN_token.token_balance(w.address) < 100:
                OCEAN_token.transfer(w.address,
                                     amt_distribute_base,
                                     from_wallet=wallet)
Example #5
0
def test1():
    # ocean instance
    config = ExampleConfig.get_config()
    ConfigProvider.set_config(config)
    Web3Provider.init_web3(
        provider=get_web3_connection_provider(config.network_url))
    ContractHandler.set_artifacts_path(config.artifacts_path)

    ocean = Ocean(config)
    OCEAN_address_before = ocean.OCEAN_address

    # deploy, distribute, etc
    deploy_fake_OCEAN()

    # test: OCEAN address should have changed
    OCEAN_address_after = ocean.OCEAN_address
    assert OCEAN_address_before != OCEAN_address_after

    # test: TEST_PRIVATE_KEY{1,2} should each hold OCEAN
    wallet1 = Wallet(ocean.web3, private_key=os.getenv("TEST_PRIVATE_KEY1"))
    wallet2 = Wallet(ocean.web3, private_key=os.getenv("TEST_PRIVATE_KEY2"))

    OCEAN_after = BToken(ocean.OCEAN_address)
    assert OCEAN_after.balanceOf(wallet1.address) > 0
    assert OCEAN_after.balanceOf(wallet2.address) > 0
Example #6
0
def run_events_monitor():
    setup_logging()
    logger.info('EventsMonitor: preparing')
    required_env_vars = ['EVENTS_RPC', 'CONFIG_FILE']
    for envvar in required_env_vars:
        if not os.getenv(envvar):
            raise AssertionError(
                f'env var {envvar} is missing, make sure to set the following '
                f'environment variables before starting the events monitor: {required_env_vars}'
            )

    network_rpc = os.environ.get('EVENTS_RPC', 'http:127.0.0.1:8545')

    config_file = os.getenv('CONFIG_FILE', 'config.ini')
    logger.info(
        f'EventsMonitor: starting with the following values: rpc={network_rpc}'
    )

    ConfigProvider.set_config(Config(config_file))
    from ocean_lib.ocean.util import get_web3_connection_provider

    Web3Provider.init_web3(provider=get_web3_connection_provider(network_rpc))
    ContractHandler.set_artifacts_path(get_artifacts_path())
    if get_network_name().lower() == 'rinkeby':
        from web3.middleware import geth_poa_middleware
        Web3Provider.get_web3().middleware_stack.inject(geth_poa_middleware,
                                                        layer=0)

    monitor = EventsMonitor(Web3Provider.get_web3(), config_file)
    monitor.start_events_monitor()
    logger.info(f'EventsMonitor: started')
    while True:
        time.sleep(5)
Example #7
0
def test_get_and_has__name_only():
    """Tests get() and has() from name-only queries, which also call _load() and read_abi_from_file()."""
    contract = ContractHandler.get("DataTokenTemplate")
    assert contract.address[:2] == "0x"
    assert "totalSupply" in str(contract.abi)

    assert ContractHandler.has("DataTokenTemplate")
    assert not ContractHandler.has("foo name")
def test_set_artifacts_path__deny_change_to_same():
    path_before = copy.copy(ContractHandler.artifacts_path)
    assert path_before is not None
    assert ContractHandler._contracts

    ContractHandler.set_artifacts_path(path_before)

    assert ContractHandler.artifacts_path == path_before
    assert ContractHandler._contracts  # cache should *not* have reset
def test_set_artifacts_path__allow_change():
    path_before = copy.copy(ContractHandler.artifacts_path)
    assert path_before is not None
    assert ContractHandler._contracts

    ContractHandler.set_artifacts_path("new path")

    assert ContractHandler.artifacts_path == "new path"
    assert not ContractHandler._contracts  # cache should have reset
Example #10
0
def test_set_artifacts_path__allow_change():
    """Tests that a correct artifacts path can be set (happy flow)."""
    path_before = copy.copy(ContractHandler.artifacts_path)
    assert path_before is not None
    assert ContractHandler._contracts

    ContractHandler.set_artifacts_path("new path")

    assert ContractHandler.artifacts_path == "new path"
    assert not ContractHandler._contracts  # cache should have reset
Example #11
0
def test_set_artifacts_path__deny_change_to_empty():
    """Tests can not set empty artifacts path."""
    path_before = copy.copy(ContractHandler.artifacts_path)
    assert path_before is not None
    assert ContractHandler._contracts

    ContractHandler.set_artifacts_path(None)  # it should deny this

    assert ContractHandler.artifacts_path == path_before
    assert ContractHandler._contracts  # cache should *not* have reset
Example #12
0
def test_load__name_and_address(network, example_config):
    """Tests load() from (name, address) query."""
    addresses = ContractHandler.get_contracts_addresses(
        network, example_config.address_file)
    target_address = addresses["DTFactory"]

    test_tuple = ("DTFactory", target_address)

    assert test_tuple not in ContractHandler._contracts

    ContractHandler._load("DTFactory", target_address)

    assert test_tuple in ContractHandler._contracts
Example #13
0
    def __init__(self, address: [str, None], abi_path=None):
        self.name = self.contract_name
        assert self.name, 'contract_name property needs to be implemented in subclasses.'
        if not abi_path:
            abi_path = ContractHandler.artifacts_path

        assert abi_path, f'abi_path is required, got {abi_path}'

        self.contract_concise = ContractHandler.get_concise_contract(self.name, address)
        self.contract = ContractHandler.get(self.name, address)

        assert not address or (self.contract.address == address and self.address == address)
        assert self.contract_concise is not None
def test_load__name_and_address(network, example_config):
    # test load() from (name, address) query
    addresses = ContractHandler.get_contracts_addresses(
        network, example_config.address_file)
    target_address = addresses["DTFactory"]

    tup = ("DTFactory", target_address)

    assert tup not in ContractHandler._contracts

    contract = ContractHandler._load("DTFactory", target_address)

    assert ContractHandler._contracts[tup] == contract
Example #15
0
def initialize(private_key):
    load_dotenv(".env")
    config = Config(os.getenv('config.ini'))
    config = Config(os.getenv('config.ini'))
    print(config.network_url)
    # config.network_url="https://rinkeby.infura.io/v3/31d95be121a545b688a0e07e4de4d256"
    ConfigProvider.set_config(config)
    Web3Provider.init_web3(
        provider=get_web3_connection_provider(config.network_url))
    ContractHandler.set_artifacts_path(config.artifacts_path)
    ocean = Ocean()
    wallet = Wallet(ocean.web3, private_key=private_key)
    return ocean, wallet
def test_load__name_only():
    # test load() from name-only query
    assert "DTFactory" not in ContractHandler._contracts

    contract = ContractHandler._load("DTFactory")

    assert ContractHandler._contracts["DTFactory"] == contract
def test_set():
    contract = ContractHandler.get("DataTokenTemplate")
    address = contract.address

    ContractHandler.set("second_name", contract)

    # did it store in (name) key?
    tup = ContractHandler._contracts[
        "second_name"]  # (contract, contract_concise)
    assert len(tup) == 2
    assert tup[0].address == address
    assert isinstance(tup[1], ConciseContract)

    # did it store in (name, address) key?
    tup2 = ContractHandler._contracts[("second_name", address)]
    assert tup2 == tup
def test_read_abi_from_file__example_config__happy_path(example_config):
    assert "https" not in str(ContractHandler.artifacts_path)

    contract_definition = ContractHandler.read_abi_from_file(
        "DTFactory", ContractHandler.artifacts_path)
    assert contract_definition["contractName"] == "DTFactory"
    assert "createToken" in str(contract_definition["abi"])
Example #19
0
def get_contracts_addresses(network, config):
    addresses = {}
    try:
        addresses = ContractHandler.get_contracts_addresses(
            network, config.address_file
        )
    except Exception as e:
        print(
            f"error reading contract addresses: {e}.\n"
            f"artifacts path is {ContractHandler.artifacts_path}, address file is {config.address_file}"
        )

    if not addresses:
        print(
            f"cannot find contract addresses: \n"
            f"artifacts path is {ContractHandler.artifacts_path}, address file is {config.address_file}"
        )
        print(f"address file exists? {os.path.exists(config.address_file)}")
        print(
            f"artifacts path exists? {os.path.exists(ContractHandler.artifacts_path)}"
        )
        print(
            f"contents of artifacts folder: \n"
            f"{os.listdir(ContractHandler.artifacts_path)}"
        )
    return addresses or {}
Example #20
0
    def deploy(cls, web3, deployer_wallet: Wallet, abi_path: str = '', *args):
        """
        Deploy the DataTokenTemplate and DTFactory contracts to the current network.

        :param web3:
        :param abi_path:
        :param deployer_wallet: Wallet instance

        :return: smartcontract address of this contract
        """
        if not abi_path:
            abi_path = ContractHandler.artifacts_path

        assert abi_path, f'abi_path is required, got {abi_path}'

        w3 = web3
        _json = ContractHandler.read_abi_from_file(cls.CONTRACT_NAME, abi_path)

        _contract = w3.eth.contract(abi=_json['abi'],
                                    bytecode=_json['bytecode'])
        built_tx = _contract.constructor(*args)\
            .buildTransaction({'from': deployer_wallet.address})

        if 'gas' not in built_tx:
            built_tx['gas'] = web3.eth.estimateGas(built_tx)

        raw_tx = deployer_wallet.sign_tx(built_tx)
        logging.debug(
            f'Sending raw tx to deploy contract {cls.CONTRACT_NAME}, signed tx hash: {raw_tx.hex()}'
        )
        tx_hash = web3.eth.sendRawTransaction(raw_tx)

        return cls.get_tx_receipt(tx_hash, timeout=60).contractAddress
Example #21
0
def setup_all(request):
    # a test can skip setup_all() via decorator "@pytest.mark.nosetup_all"
    if "nosetup_all" in request.keywords:
        return
    config = ExampleConfig.get_config()
    ConfigProvider.set_config(config)
    Web3Provider.init_web3(
        provider=get_web3_connection_provider(config.network_url))
    ContractHandler.set_artifacts_path(config.artifacts_path)

    wallet = get_ganache_wallet()

    if not wallet:
        return

    addresses_file = config.address_file
    if not os.path.exists(addresses_file):
        return

    with open(addresses_file) as f:
        network_addresses = json.load(f)

    print(
        f"sender: {wallet.key}, {wallet.address}, {wallet.password}, {wallet.keysStr()}"
    )
    print(f"sender balance: {from_wei(get_ether_balance(wallet.address))}")
    assert (from_wei(get_ether_balance(wallet.address)) >
            10), "Ether balance less than 10."

    from ocean_lib.models.data_token import DataToken

    OCEAN_token = DataToken(address=network_addresses["development"]["Ocean"])

    amt_distribute = 1000
    amt_distribute_base = to_base_18(float(amt_distribute))

    for w in (get_publisher_wallet(), get_consumer_wallet()):
        if from_wei(get_ether_balance(w.address)) < 2:
            send_ether(wallet, w.address, 4)

        if OCEAN_token.token_balance(w.address) < 100:
            OCEAN_token.mint(wallet.address,
                             amt_distribute_base,
                             from_wallet=wallet)
            OCEAN_token.transfer(w.address,
                                 amt_distribute_base,
                                 from_wallet=wallet)
Example #22
0
def mint_fake_OCEAN():
    """
    Does the following:
    1. Mints tokens
    2. Distributes tokens to TEST_PRIVATE_KEY1 and TEST_PRIVATE_KEY2
    """
    config = ExampleConfig.get_config()
    ConfigProvider.set_config(config)
    Web3Provider.init_web3(
        provider=get_web3_connection_provider(config.network_url))
    ContractHandler.set_artifacts_path(config.artifacts_path)

    addresses_file = config.address_file

    ocean = get_publisher_ocean_instance()
    web3 = ocean.web3

    with open(addresses_file) as f:
        network_addresses = json.load(f)

    network = "development"
    deployer_wallet = get_ganache_wallet()

    OCEAN_token = DataToken(address=network_addresses[network]["Ocean"])

    amt_distribute = 1000
    amt_distribute_base = to_base_18(float(amt_distribute))

    OCEAN_token.mint(deployer_wallet.address,
                     2 * amt_distribute_base,
                     from_wallet=deployer_wallet)

    for key_label in ["TEST_PRIVATE_KEY1", "TEST_PRIVATE_KEY2"]:
        key = os.environ.get(key_label)
        if not key:
            continue

        w = Wallet(web3, private_key=key)

        if OCEAN_token.token_balance(w.address) < 1000:
            OCEAN_token.transfer(w.address,
                                 amt_distribute_base,
                                 from_wallet=deployer_wallet)

        if from_wei(get_ether_balance(w.address)) < 2:
            send_ether(deployer_wallet, w.address, 4)
Example #23
0
def events_object():
    global EVENTS_INSTANCE
    if not EVENTS_INSTANCE:
        config_file = os.getenv('CONFIG_FILE', 'config.ini')
        network_rpc = os.environ.get('EVENTS_RPC', 'http://127.0.0.1:8545')

        ConfigProvider.set_config(Config(config_file))
        from ocean_lib.ocean.util import get_web3_connection_provider

        Web3Provider.init_web3(
            provider=get_web3_connection_provider(network_rpc))
        ContractHandler.set_artifacts_path(get_artifacts_path())

        EVENTS_INSTANCE = EventsMonitor(Web3Provider.get_web3(),
                                        app.config['CONFIG_FILE'])
        EVENTS_INSTANCE.store_last_processed_block(0)
    return EVENTS_INSTANCE
Example #24
0
def test_set():
    """Tests setting of a DataTokenTemplate on a Contract."""
    contract = ContractHandler.get("DataTokenTemplate")
    address = contract.address

    ContractHandler.set("second_name", contract)

    # result format is a tuple of (contract, contract_concise)
    # did it store in (name) key?
    result = ContractHandler._contracts["second_name"]
    assert len(result) == 2
    assert result[0].address == address
    assert isinstance(result[1], ConciseContract)

    # did it store in (name, address) key?
    result2 = ContractHandler._contracts[("second_name", address)]
    assert result2 == result
def test_read_abi_from_file__example_config__bad_contract_name(example_config):
    assert "https" not in str(ContractHandler.artifacts_path)

    base_path = ContractHandler.artifacts_path
    target_filename = os.path.join(base_path, "DTFactoryFOO.json")
    assert not os.path.exists(target_filename)  # should fail due to this

    contract_definition = ContractHandler.read_abi_from_file(
        "DTFactoryFOO", ContractHandler.artifacts_path)
    assert contract_definition is None
Example #26
0
    def __init__(self, address: [str, None], abi_path=None):
        """Initialises Contract Base object.

        The contract name attribute and abi_path are required.
        """
        self.name = self.contract_name
        assert (
            self.name
        ), "contract_name property needs to be implemented in subclasses."
        if not abi_path:
            abi_path = ContractHandler.artifacts_path

        assert abi_path, f"abi_path is required, got {abi_path}"

        self.contract_concise = ContractHandler.get_concise_contract(self.name, address)
        self.contract = ContractHandler.get(self.name, address)

        assert not address or (
            self.contract.address == address and self.address == address
        )
        assert self.contract_concise is not None
Example #27
0
def test_issue185_unit(monkeypatch):
    """For #185, unit-test the root cause method, which is load"""
    setup_issue_185(monkeypatch)

    # ensure that conftest::setup_all() was not called
    assert ConfigProvider._config is None
    assert Web3Provider._web3 is None
    assert ContractHandler._contracts == dict()
    assert ContractHandler.artifacts_path is None

    # actual test. Imports only come now, to avoid setting class-level attributes
    # isort: off
    from ocean_lib.ocean.ocean import Ocean
    from ocean_lib.web3_internal.wallet import Wallet

    # isort: on

    private_key = os.getenv("TEST_PRIVATE_KEY1")
    config = {"network": os.getenv("NETWORK_URL")}
    ocean = Ocean(config)

    # Ensure it's using a path like '/home/trentmc/ocean.py/venv/artifacts'
    assert os.path.exists(ocean._config.artifacts_path)
    assert "venv/artifacts" in ocean._config.artifacts_path

    wallet = Wallet(ocean.web3, private_key=private_key)
    assert wallet is not None

    # At this point, shouldn't have any contracts cached
    assert ContractHandler._contracts == {}

    # This is the call that causes problems in system test.
    contract = ContractHandler._get("DataTokenTemplate", None)
    assert contract is not None

    # The first call may have caused caching. So call again:)
    contract = ContractHandler._get("DataTokenTemplate", None)
    assert contract is not None
Example #28
0
def run_scenario():
    config = Config('config.ini')
    ocean = Ocean(config)
    wallet = Wallet(
        ocean.web3,
        0x376e05899a4ae00463a3a607c774069b7d6a647860dba723f39b735c91238ddf,
        None, "EARLYTOBEDANDEARLYTORISE")
    ConfigProvider.set_config(config)
    Web3Provider.init_web3(
        provider=get_web3_connection_provider(config.network_url))
    ContractHandler.set_artifacts_path(config.artifacts_path)
    print(dir(wallet))
    print(wallet.address)
    print(config.network_url)
    print(config.provider_url)
    print(config.network_url)
    print(config.artifacts_path)

    data_token = ocean.create_data_token('S1Seven', 'S1SV', from_wallet=wallet)
    print(f'created new datatoken with address {data_token.address}')
    token_address = data_token.address
    print(token_address)
    '''
def test_get_contracts_addresses_example_config(network, example_config):
    """Tests that an address can be set if using testing config."""
    # ensure we're testing locally
    assert network in ["ganache", "development"]

    # do we get addresses for every contract?
    addresses = ContractHandler.get_contracts_addresses(
        network, example_config.address_file)
    assert set(addresses.keys()) == set(
        ["DTFactory", "BFactory", "FixedRateExchange", "Metadata", "Ocean"])

    # are address values sane?
    for address in addresses.values():
        assert address[0:2] == "0x"
def test_get_contracts_addresses_good_path_use_network_alias(tmp_path):
    """Tests that an address with a network alias can be set on a Contract."""
    assert ContractHandler.network_alias == {"ganache": "development"}

    # create & fill test file
    d = tmp_path / "subdir"
    d.mkdir()
    address_file = d / "address.json"
    address_file.write_text('{"development" : "myvals"}')  # not "ganache"

    # the main test
    addresses = ContractHandler.get_contracts_addresses(
        network="ganache", address_file=address_file)
    assert addresses == "myvals"