Beispiel #1
0
def main(did, pool_address, order_tx_id=None):
    ocean = Ocean(config=Config(options_dict=get_config_dict()))
    publisher = Wallet(
        ocean.web3,
        private_key="0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58",
    )  # 0xe2DD09d719Da89e5a3D0F2549c7E24566e947260
    consumer = Wallet(
        ocean.web3,
        private_key="0x9bf5d7e4978ed5206f760e6daded34d657572bd49fa5b3fe885679329fb16b16",
    )  # 0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0

    if not (did and pool_address):
        metadata_file = "./examples/data/metadata.json"
        with open(metadata_file) as f:
            metadata = json.load(f)

        asset, pool = publish_asset(metadata, publisher)
    else:
        asset = ocean.assets.resolve(did)
        pool = BPool(pool_address)

    if not asset:
        print("publish asset failed, cannot continue with running compute.")
        return

    print(f"Requesting compute using asset {asset.did} and pool {pool.address}")
    algo_file = "./examples/data/algorithm.py"
    job_id, status = run_compute(
        asset.did, consumer, algo_file, pool.address, order_tx_id
    )
    print(f"Compute started on asset {asset.did}: job_id={job_id}, status={status}")
Beispiel #2
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
Beispiel #3
0
def main(did, pool_address, order_tx_id=None):
ocean = Ocean(config=Config(options_dict=get_config_dict()))
publisher = Wallet(ocean.web3, private_key='0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58')  # 0xe2DD09d719Da89e5a3D0F2549c7E24566e947260
#consumer = Wallet(ocean.web3, private_key='0x9bf5d7e4978ed5206f760e6daded34d657572bd49fa5b3fe885679329fb16b16')  # 0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0
publisher_wallet = Wallet(ocean.web3, private_key=os.getenv('Publisher_Key')) #addr: 0xc966Ba2a41888B6B4c5273323075B98E27B9F364
consumer = Wallet(ocean.web3, private_key=os.getenv('Consumer_Key')) #addr: 0xEF5dc33A53DD2ED3F670B53F07cEc5ADD4D80504

    if not (did and pool_address):
        metadata_file = './examples/data/metadata.json'
        with open(metadata_file) as f:
            metadata = json.load(f)

        asset, pool = publish_asset(metadata, publisher)
        #Dataset asset created successfully: did=did:op:784Cc17176533cc962cf659B9f49349ba6F9df3b, datatoken=0x784Cc17176533cc962cf659B9f49349ba6F9df3b
        #pool_address = 0x3490DDd035B2e1DA30Af09AB6090Bf71fdb94898
    else:
        asset = ocean.assets.resolve(did)
        pool = BPool(pool_address)

    if not asset:
        print(f'publish asset failed, cannot continue with running compute.')
        return

    print(f'Requesting compute using asset {asset.did} and pool {pool.address}')
    algo_file = './examples/data/algorithm.py'
    order_tx_id=

    job_id, status = run_compute(asset.did, consumer, algo_file, pool.address, order_tx_id)
    print(f'Compute started on asset {asset.did}: job_id={job_id}, status={status}')
Beispiel #4
0
def make_info(name, private_key_name):
    # assume that this account has ETH with gas.
    class _Info:
        pass

    info = _Info()
    web3 = get_web3()
    config = ExampleConfig.get_config()

    info.web3 = web3

    info.private_key = os.environ.get(private_key_name)
    info.wallet = Wallet(
        web3,
        private_key=info.private_key,
        block_confirmations=config.block_confirmations,
        transaction_timeout=config.transaction_timeout,
    )
    info.address = info.wallet.address
    wallet = get_ganache_wallet()
    if wallet:
        assert get_ether_balance(
            web3, wallet.address) >= to_wei(4), "Ether balance less than 4."
        if get_ether_balance(web3, info.address) < to_wei(2):
            send_ether(wallet, info.address, to_wei(4))

    from ocean_lib.ocean.ocean import Ocean

    info.ocean = Ocean(config)
    info.T1 = _deployAndMintToken(web3, "TOK1", info.address)
    info.T2 = _deployAndMintToken(web3, "TOK2", info.address)

    return info
Beispiel #5
0
def make_info(name, private_key_name):
    # assume that this account has ETH with gas.
    class _Info:
        pass

    info = _Info()
    web3 = get_web3()

    info.web3 = web3

    info.private_key = os.environ.get(private_key_name)
    info.wallet = Wallet(web3, private_key=info.private_key)
    info.address = info.wallet.address
    info.account = Account(private_key=info.private_key)
    wallet = get_ganache_wallet()
    if wallet:
        assert (from_wei(get_ether_balance(wallet.address)) >
                4), "Ether balance less than 4."
        if from_wei(get_ether_balance(info.address)) < 2:
            send_ether(wallet, info.address, 4)

    from ocean_lib.ocean.ocean import Ocean

    info.ocean = Ocean()
    info.T1 = _deployAndMintToken("TOK1", info.address)
    info.T2 = _deployAndMintToken("TOK2", info.address)

    return info
Beispiel #6
0
def get_wallet(index):
    name = "PARITY_ADDRESS" if not index else f"PARITY_ADDRESS{index}"
    pswrd_name = "PARITY_PASSWORD" if not index else f"PARITY_PASSWORD{index}"
    key_name = "PARITY_KEY" if not index else f"PARITY_KEY{index}"
    encrypted_key_name = ("PARITY_ENCRYPTED_KEY"
                          if not index else f"PARITY_ENCRYPTED_KEY{index}")
    keyfile_name = "PARITY_KEYFILE" if not index else f"PARITY_KEYFILE{index}"

    address = os.getenv(name)
    if not address:
        return None

    pswrd = os.getenv(pswrd_name)
    key = os.getenv(key_name)
    encr_key = os.getenv(encrypted_key_name)
    key_file = os.getenv(keyfile_name)
    if key_file and not encr_key:
        with open(key_file) as _file:
            encr_key = json.loads(_file.read())

    from ocean_lib.web3_internal.wallet import Wallet

    return Wallet(
        Web3Provider.get_web3(),
        private_key=key,
        encrypted_key=encr_key,
        address=Web3.toChecksumAddress(address),
        password=pswrd,
    )
Beispiel #7
0
    def cancel_or_replace_transaction(from_wallet,
                                      nonce_value,
                                      gas_price=None,
                                      gas_limit=None):
        w3 = Web3Provider.get_web3()
        tx = {
            "from": from_wallet.address,
            "to": from_wallet.address,
            "value": 0
        }
        gas = gas_limit if gas_limit is not None else w3.eth.estimateGas(tx)
        tx = {
            "from": from_wallet.address,
            "to": from_wallet.address,
            "value": 0,
            "gas": gas + 1,
        }

        wallet = Wallet(w3,
                        private_key=from_wallet.key,
                        address=from_wallet.address)
        raw_tx = wallet.sign_tx(tx,
                                fixed_nonce=nonce_value,
                                gas_price=gas_price)
        tx_hash = w3.eth.sendRawTransaction(raw_tx)
        receipt = w3.eth.waitForTransactionReceipt(tx_hash, timeout=30)
        return receipt
Beispiel #8
0
def sell_data(
    ocean,
    private_key,
    data_token,
    amount,
    fixed_price=True,
):
    """Sell a dataset on the Ocean market.
    Mint the datatokens.
    In the create() step below, ganache OCEAN is needed.
    Finally, Approve the datatoken for sale.
    Args:
        ocean ():
        wallet ():
        data_token ():
        amount ():
        fixed_price (bool): Whether or not to sell the data at a fixed price.
    Returns:
        (bool): Returns True if successful.
    """
    wallet = Wallet(ocean.web3, private_key, ocean.config.block_confirmations)
    data_token.mint(wallet.address, to_wei(amount), wallet)
    OCEAN_token = BToken(ocean.web3, ocean.OCEAN_address)
    assert OCEAN_token.balanceOf(wallet.address) > 0, 'need OCEAN'
    data_token.approve(ocean.exchange._exchange_address, to_wei(amount),
                       wallet)
    return True
Beispiel #9
0
def buy_data(
    ocean,
    private_key,
    token_address,
    seller_wallet,
    min_amount,
    max_amount,
):
    """Buy a dataset on the market.
    Define wallet, verify that there is enough ganache ETH and OCEAN.
    Create an exchange_id for a new exchange.
    Args:
        ocean ():
        private_key (str):
        token_address (str):
        seller_wallet (Wallet):
        min_amount (float):
        max_amount (float):
    Returns:

    """
    wallet = Wallet(ocean.web3, private_key, ocean.config.block_confirmations)
    assert ocean.web3.eth.get_balance(wallet.address) > 0, 'need ganache ETH'
    OCEAN_token = BToken(ocean.web3, ocean.OCEAN_address)
    assert OCEAN_token.balanceOf(wallet.address) > 0, 'need ganache OCEAN'
    exchange_id = ocean.exchange.create(token_address, to_wei(min_amount),
                                        seller_wallet)
    tx_result = ocean.exchange.buy_at_fixed_rate(to_wei(min_amount), wallet,
                                                 to_wei(max_amount),
                                                 exchange_id, token_address,
                                                 seller_wallet.address)
    assert tx_result, 'failed buying data tokens at fixed rate.'
Beispiel #10
0
def download_data(ocean, private_key, did):
    """Download a dataset that is in a user's possession.
    Points to the service object, send datatoken to the service,
    and then downloads the dataset files. If the connection breaks,
    then the request can be made again with the order_tx_id.
    Args:
        ocean ():
        private_key ():
        did (str): Dataset ID.
    Returns:

    """
    wallet = Wallet(ocean.web3, private_key, ocean.config.block_confirmations)
    fee_receiver = ZERO_ADDRESS  # could also be market address
    asset = ocean.assets.resolve(did)
    service = asset.get_service(ServiceTypes.ASSET_ACCESS)
    quote = ocean.assets.order(asset.did,
                               wallet.address,
                               service_index=service.index)
    order_tx_id = ocean.assets.pay_for_service(ocean.web3, quote.amount,
                                               quote.data_token_address,
                                               asset.did, service.index,
                                               fee_receiver, wallet,
                                               service.get_c2d_address())
    try:
        file_path = ocean.assets.download(asset.did,
                                          service.index,
                                          wallet,
                                          order_tx_id,
                                          destination='./')
        return file_path
    except:
        return order_tx_id
def get_another_consumer_wallet() -> Wallet:
    config = get_example_config()
    return Wallet(
        get_web3(),
        private_key=os.environ.get("TEST_PRIVATE_KEY3"),
        block_confirmations=config.block_confirmations,
        transaction_timeout=config.transaction_timeout,
    )
Beispiel #12
0
def get_factory_deployer_wallet(network):
    if network == "ganache":
        return get_ganache_wallet()

    private_key = os.environ.get("FACTORY_DEPLOYER_PRIVATE_KEY")
    if not private_key:
        return None

    return Wallet(get_web3(), private_key=private_key)
Beispiel #13
0
def get_wallet(ocean, private_key):
    """Get a user's wallet given their private key.
    Args:
        ocean ():
        private_key (str):
    Returns:

    """
    return Wallet(ocean.web3, private_key, ocean.config.block_confirmations)
Beispiel #14
0
def mint_fake_OCEAN(config: Config) -> None:
    """
    Does the following:
    1. Mints tokens
    2. Distributes tokens to TEST_PRIVATE_KEY1 and TEST_PRIVATE_KEY2
    """
    addresses_file = config.address_file

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

    web3 = get_web3(config.network_url)
    deployer_wallet = Wallet(
        web3,
        private_key=os.environ.get("FACTORY_DEPLOYER_PRIVATE_KEY"),
        block_confirmations=config.block_confirmations,
        transaction_timeout=config.transaction_timeout,
    )

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

    amt_distribute = to_wei(1000)

    OCEAN_token.mint(
        deployer_wallet.address, 2 * amt_distribute, 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,
            block_confirmations=config.block_confirmations,
            transaction_timeout=config.transaction_timeout,
        )

        if OCEAN_token.balanceOf(w.address) < amt_distribute:
            OCEAN_token.transfer(w.address, amt_distribute, from_wallet=deployer_wallet)

        if get_ether_balance(web3, w.address) < to_wei(2):
            send_ether(deployer_wallet, w.address, to_wei(4))
Beispiel #15
0
def get_ganache_wallet():
    web3 = get_web3()
    if (web3.eth.accounts and web3.eth.accounts[0].lower()
            == "0xe2DD09d719Da89e5a3D0F2549c7E24566e947260".lower()):
        return Wallet(
            web3,
            private_key=
            "0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58",
        )

    return None
Beispiel #16
0
def get_ganache_wallet():
    web3 = Web3Provider.get_web3()
    if (web3.eth.accounts and web3.eth.accounts[0].lower()
            == "0xe2DD09d719Da89e5a3D0F2549c7E24566e947260".lower()):
        return Wallet(
            web3,
            private_key=
            "0xfd5c1ccea015b6d663618850824154a3b3fb2882c46cefb05b9a93fea8c3d215",
        )

    return None
Beispiel #17
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
Beispiel #18
0
def test_wallet_arguments(web3, config):
    """Tests that a wallet's arguments are correctly setup."""
    private_key = os.environ.get("TEST_PRIVATE_KEY1")
    assert private_key, "envvar TEST_PRIVATE_KEY1 is not set."

    # Create wallet with valid private key
    wallet = Wallet(
        web3,
        private_key=private_key,
        block_confirmations=config.block_confirmations,
        transaction_timeout=config.transaction_timeout,
    )
    assert wallet.private_key == private_key, "Private keys are different."
    assert wallet.address, "The wallet does not have a wallet address."
    signed_message = wallet.sign(encode_defunct(text="msg-to-sign"))
    assert signed_message, "Signed message is None."

    # create wallet with missing arguments
    with pytest.raises(TypeError):
        Wallet(web3)

    # Create wallet with invalid private_key
    invalid_key = "332233444332"
    with pytest.raises(ValueError):
        Wallet(
            web3,
            private_key=invalid_key,
            block_confirmations=config.block_confirmations,
            transaction_timeout=config.transaction_timeout,
        )

    with pytest.raises(TypeError):
        Wallet(
            web3,
            private_key=None,
            block_confirmations=config.block_confirmations,
            transaction_timeout=config.transaction_timeout,
        )
Beispiel #19
0
def transact_with_contract_function(
    address,
    web3,
    function_name=None,
    transaction=None,
    contract_abi=None,
    fn_abi=None,
    *args,
    **kwargs,
):
    """
    Helper function for interacting with a contract function by sending a
    transaction. This is copied from web3 `transact_with_contract_function`
    so we can use `personal_sendTransaction` when possible.
    """
    transact_transaction = prepare_transaction(
        address,
        web3,
        fn_identifier=function_name,
        contract_abi=contract_abi,
        transaction=transaction,
        fn_abi=fn_abi,
        fn_args=args,
        fn_kwargs=kwargs,
    )

    passphrase = None
    account_key = None
    if transaction and "passphrase" in transaction:
        passphrase = transaction["passphrase"]
        transact_transaction.pop("passphrase")
        if "account_key" in transaction:
            account_key = transaction["account_key"]
            transact_transaction.pop("account_key")

    if account_key:
        raw_tx = Wallet(web3,
                        private_key=account_key).sign_tx(transact_transaction)
        logging.debug(
            f"sending raw tx: function: {function_name}, tx hash: {raw_tx.hex()}"
        )
        txn_hash = web3.eth.sendRawTransaction(raw_tx)
    elif passphrase:
        txn_hash = web3.personal.sendTransaction(transact_transaction,
                                                 passphrase)
    else:
        txn_hash = web3.eth.sendTransaction(transact_transaction)

    wait_for_tx(txn_hash, web3, 5)
    return txn_hash
def get_ganache_wallet():
    web3 = get_web3()
    if (web3.eth.accounts and web3.eth.accounts[0].lower()
            == "0xe2DD09d719Da89e5a3D0F2549c7E24566e947260".lower()):
        config = get_example_config()
        return Wallet(
            web3,
            private_key=
            "0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58",
            block_confirmations=config.block_confirmations,
            transaction_timeout=config.transaction_timeout,
        )

    return None
def get_factory_deployer_wallet(network):
    if network == "ganache":
        return get_ganache_wallet()

    private_key = os.environ.get("FACTORY_DEPLOYER_PRIVATE_KEY")
    if not private_key:
        return None

    config = get_example_config()
    return Wallet(
        get_web3(),
        private_key=private_key,
        block_confirmations=config.block_confirmations,
        transaction_timeout=config.transaction_timeout,
    )
Beispiel #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)
Beispiel #23
0
def test_issue185_system(monkeypatch):
    """A system-level test, to replicate original failure seen in #185"""
    setup_issue_185(monkeypatch)

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

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

    wallet = Wallet(ocean.web3, private_key=private_key)

    # this failed before the fix
    datatoken = ocean.create_data_token("Dataset name",
                                        "dtsymbol",
                                        from_wallet=wallet)
    assert datatoken is not None
Beispiel #24
0
def setup_all():
    setup_network()
    web3 = Web3Provider.get_web3()
    if web3.eth.accounts and web3.eth.accounts[0].lower(
    ) == '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260'.lower():
        wallet = Wallet(
            web3,
            private_key=
            '0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58'
        )

        provider = get_provider_wallet()
        if web3.fromWei(Web3Helper.get_ether_balance(provider.address),
                        'ether') < 10:
            Web3Helper.send_ether(wallet, provider.address, 25)

        consumer = get_consumer_wallet()
        if web3.fromWei(Web3Helper.get_ether_balance(consumer.address),
                        'ether') < 10:
            Web3Helper.send_ether(wallet, consumer.address, 25)
Beispiel #25
0
def setup_all():
    setup_network()
    web3 = Web3Provider.get_web3()
    if (
        web3.eth.accounts
        and web3.eth.accounts[0].lower()
        == "0xe2DD09d719Da89e5a3D0F2549c7E24566e947260".lower()
    ):
        wallet = Wallet(
            web3,
            private_key="0xfd5c1ccea015b6d663618850824154a3b3fb2882c46cefb05b9a93fea8c3d215",
        )

        provider = get_provider_wallet()
        if web3.fromWei(Web3Helper.get_ether_balance(provider.address), "ether") < 10:
            Web3Helper.send_ether(wallet, provider.address, 25)

        consumer = get_consumer_wallet()
        if web3.fromWei(Web3Helper.get_ether_balance(consumer.address), "ether") < 10:
            Web3Helper.send_ether(wallet, consumer.address, 25)
Beispiel #26
0
def send_ether(from_wallet: Wallet, to_address: str, ether_amount: int):
    w3 = Web3Provider.get_web3()
    if not w3.isChecksumAddress(to_address):
        to_address = w3.toChecksumAddress(to_address)

    tx = {
        "from": from_wallet.address,
        "to": to_address,
        "value": w3.toWei(ether_amount, "ether"),
    }
    _ = w3.eth.estimateGas(tx)
    tx = {
        "from": from_wallet.address,
        "to": to_address,
        "value": w3.toWei(ether_amount, "ether"),
        "gas": 500000,
    }
    wallet = Wallet(w3, private_key=from_wallet.key, address=from_wallet.address)
    raw_tx = wallet.sign_tx(tx)
    tx_hash = w3.eth.sendRawTransaction(raw_tx)
    receipt = w3.eth.waitForTransactionReceipt(tx_hash, timeout=30)
    return receipt
Beispiel #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
Beispiel #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)
    '''
Beispiel #29
0
def test_wallet_arguments():
    """Tests that a wallet's arguments are correctly setup."""
    web3 = Web3Provider.get_web3()

    private_key = os.environ.get("TEST_PRIVATE_KEY1")
    assert private_key, "envvar TEST_PRIVATE_KEY1 is not set."

    # Create wallet with valid private key
    wallet = Wallet(web3, private_key=private_key)
    assert wallet.private_key == private_key
    assert wallet.address
    signed_message = wallet.sign(
        add_ethereum_prefix_and_hash_msg("msg-to-sign"))
    assert signed_message

    # Create wallet with encrypted key and password
    password = "******"
    encrypted_key = web3.eth.account.encrypt(private_key, password)
    w2 = Wallet(web3, encrypted_key=encrypted_key, password=password)
    assert w2.address == wallet.address
    assert w2.private_key == wallet.private_key
    assert w2.sign(
        add_ethereum_prefix_and_hash_msg("msg-to-sign")) == signed_message

    # create wallet with missing arguments
    with pytest.raises(AssertionError):
        Wallet(web3)
    with pytest.raises(AssertionError):
        Wallet(web3, encrypted_key=encrypted_key)
    with pytest.raises(AssertionError):
        Wallet(web3, password=password)

    # Create wallet with invalid private_key
    invalid_key = "332233444332"
    with pytest.raises(ValueError):
        Wallet(web3, private_key=invalid_key)

    with pytest.raises(AssertionError):
        Wallet(web3, private_key=None)
Beispiel #30
0
    def send_ether(from_wallet, to_address, ether_amount):
        w3 = Web3Provider.get_web3()
        if not w3.isChecksumAddress(to_address):
            to_address = w3.toChecksumAddress(to_address)

        tx = {
            'from': from_wallet.address,
            'to': to_address,
            'value': w3.toWei(ether_amount, 'ether')
        }
        gas = w3.eth.estimateGas(tx)
        tx = {
            'from': from_wallet.address,
            'to': to_address,
            'value': w3.toWei(ether_amount, 'ether'),
            'gas': 500000
        }
        wallet = Wallet(w3,
                        private_key=from_wallet.key,
                        address=from_wallet.address)
        raw_tx = wallet.sign_tx(tx)
        tx_hash = w3.eth.sendRawTransaction(raw_tx)
        receipt = w3.eth.waitForTransactionReceipt(tx_hash, timeout=30)
        return receipt