Exemplo n.º 1
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
Exemplo n.º 2
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
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
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)
Exemplo n.º 6
0
def setup_all(request, config, web3):
    # a test can skip setup_all() via decorator "@pytest.mark.nosetup_all"
    if "nosetup_all" in request.keywords:
        return

    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.keys_str()}")
    print(
        f"sender balance: {from_wei(get_ether_balance(web3, wallet.address))}")
    assert get_ether_balance(
        web3, wallet.address) >= to_wei(10), "Ether balance less than 10."

    from ocean_lib.models.data_token import DataToken

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

    amt_distribute = to_wei(1000)

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

        if OCEAN_token.balanceOf(w.address) < to_wei(100):
            OCEAN_token.mint(wallet.address,
                             amt_distribute,
                             from_wallet=wallet)
            OCEAN_token.transfer(w.address, amt_distribute, from_wallet=wallet)
Exemplo n.º 7
0
def deploy(network, addresses_file):
    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)

    artifacts_path = ContractHandler.artifacts_path
    if not addresses_file:
        addresses_file = config.address_file
    else:
        addresses_file = Path(addresses_file).expanduser().resolve()

    ocean = get_publisher_ocean_instance()
    web3 = ocean.web3

    addresses = dict()

    if os.path.exists(addresses_file):
        with open(addresses_file) as f:
            network_addresses = json.load(f)
    else:
        network_addresses = {network: {}}

    if network == "ganache" and network not in network_addresses:
        network = "development"

    _addresses = network_addresses[network]

    # ****SET ENVT****
    # grab vars
    factory_deployer_private_key = get_ganache_wallet().private_key

    # corner cases
    if invalidKey(factory_deployer_private_key):
        print("Need valid FACTORY_DEPLOYER_PRIVATE_KEY")
        sys.exit(0)

    # ****SEE FUNDS****
    print("Keys:\n%s" % Wallet(
        web3=get_web3(), private_key=factory_deployer_private_key).keysStr())
    print("")

    # ****DEPLOY****
    deployer_wallet = Wallet(web3, private_key=factory_deployer_private_key)
    minter_addr = deployer_wallet.address
    # cap = 2 ** 255 not used

    if DTFactory.CONTRACT_NAME not in _addresses:
        print("****Deploy DataTokenTemplate: begin****")
        dt_address = DataToken.deploy(
            web3,
            deployer_wallet,
            artifacts_path,
            "Template Contract",
            "TEMPLATE",
            minter_addr,
            DataToken.DEFAULT_CAP_BASE,
            DTFactory.FIRST_BLOB,
            minter_addr,
        )
        addresses[DataToken.CONTRACT_NAME] = dt_address
        print("****Deploy DataTokenTemplate: done****\n")

        print("****Deploy DTFactory: begin****")
        dtfactory = DTFactory(
            DTFactory.deploy(web3, deployer_wallet, artifacts_path, dt_address,
                             minter_addr))
        addresses[DTFactory.CONTRACT_NAME] = dtfactory.address
        print("****Deploy DTFactory: done****\n")

    if BFactory.CONTRACT_NAME not in _addresses:
        print("****Deploy BPool: begin****")
        bpool_address = BPool.deploy(web3, deployer_wallet, artifacts_path)
        bpool_template = BPool(bpool_address)
        addresses[BPool.CONTRACT_NAME] = bpool_address
        print("****Deploy BPool: done****\n")

        print("****Deploy 'BFactory': begin****")
        bfactory_address = BFactory.deploy(web3, deployer_wallet,
                                           artifacts_path,
                                           bpool_template.address)
        _ = BFactory(bfactory_address)
        addresses[BFactory.CONTRACT_NAME] = bfactory_address
        print("****Deploy 'BFactory': done****\n")

    if FixedRateExchange.CONTRACT_NAME not in _addresses:
        print("****Deploy 'FixedRateExchange': begin****")
        addresses[FixedRateExchange.CONTRACT_NAME] = FixedRateExchange.deploy(
            web3, deployer_wallet, artifacts_path)
        print("****Deploy 'FixedRateExchange': done****\n")

    if MetadataContract.CONTRACT_NAME not in _addresses:
        print("****Deploy 'Metadata': begin****")
        addresses[MetadataContract.CONTRACT_NAME] = MetadataContract.deploy(
            web3, deployer_wallet, artifacts_path)
        print("****Deploy 'Metadata': done****\n")

    if network in ("ganache", "development"):
        print("****Deploy fake OCEAN: begin****")
        # For simplicity, hijack DataTokenTemplate.
        minter_addr = deployer_wallet.address
        OCEAN_cap = 1410 * 10**6  # 1.41B
        OCEAN_cap_base = util.to_base_18(float(OCEAN_cap))
        OCEAN_token = DataToken(
            DataToken.deploy(
                web3,
                deployer_wallet,
                artifacts_path,
                "Ocean",
                "OCEAN",
                minter_addr,
                OCEAN_cap_base,
                "",
                minter_addr,
            ))
        addresses["Ocean"] = OCEAN_token.address
        print("****Deploy fake OCEAN: done****\n")

        print("****Mint fake OCEAN: begin****")
        OCEAN_token.mint(minter_addr,
                         OCEAN_cap_base,
                         from_wallet=deployer_wallet)
        print("****Mint fake OCEAN: done****\n")

        print("****Distribute fake OCEAN: begin****")
        amt_distribute = 1000
        amt_distribute_base = util.to_base_18(float(amt_distribute))
        for key_label in ["TEST_PRIVATE_KEY1", "TEST_PRIVATE_KEY2"]:
            key = os.environ.get(key_label)
            if not key:
                continue

            dst_address = privateKeyToAddress(key)
            OCEAN_token.transfer(dst_address,
                                 amt_distribute_base,
                                 from_wallet=deployer_wallet)

        print("****Distribute fake OCEAN: done****\n")

    network_addresses[network].update(addresses)

    with open(addresses_file, "w") as f:
        json.dump(network_addresses, f, indent=2)

    return addresses
Exemplo n.º 8
0
def deploy_fake_OCEAN():
    """
    Does the following:
    1. Deploy to ganache a new ERC20 contract having symbol OCEAN
    2. Mints tokens
    3. Distributes tokens to TEST_PRIVATE_KEY1 and TEST_PRIVATE_KEY2
    4. In addresses.json, updates development : Ocean entry with new address
    """
    network = "ganache"
    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)

    artifacts_path = ContractHandler.artifacts_path
    addresses_file = config.address_file

    ocean = get_publisher_ocean_instance()
    web3 = ocean.web3

    addresses = dict()

    if os.path.exists(addresses_file):
        with open(addresses_file) as f:
            network_addresses = json.load(f)
    else:
        network_addresses = {network: {}}

    if network not in network_addresses:
        network = "development"

    # ****SET ENVT****
    deployer_private_key = get_ganache_wallet().private_key

    if invalidKey(deployer_private_key):
        print("Need valid DEPLOYER_PRIVATE_KEY")
        sys.exit(0)

    # ****DEPLOY****
    deployer_wallet = Wallet(web3, private_key=deployer_private_key)

    print("****Deploy fake OCEAN: begin****")
    # For simplicity, hijack DataTokenTemplate.
    deployer_addr = deployer_wallet.address
    OCEAN_cap = 1410 * 10**6  # 1.41B
    OCEAN_cap_base = util.to_base_18(float(OCEAN_cap))
    OCEAN_token = DataToken(
        DataToken.deploy(
            web3,
            deployer_wallet,
            artifacts_path,
            "Ocean",
            "OCEAN",
            deployer_addr,
            OCEAN_cap_base,
            "",
            deployer_addr,
        ))
    addresses["Ocean"] = OCEAN_token.address
    print("****Deploy fake OCEAN: done****\n")

    print("****Mint fake OCEAN: begin****")
    OCEAN_token.mint(deployer_addr,
                     OCEAN_cap_base,
                     from_wallet=deployer_wallet)
    print("****Mint fake OCEAN: done****\n")

    print("****Distribute fake OCEAN: begin****")
    amt_distribute = 1000
    amt_distribute_base = util.to_base_18(float(amt_distribute))
    for key_label in ["TEST_PRIVATE_KEY1", "TEST_PRIVATE_KEY2"]:
        key = os.environ.get(key_label)
        if not key:
            continue

        dst_address = privateKeyToAddress(key)
        OCEAN_token.transfer(dst_address,
                             amt_distribute_base,
                             from_wallet=deployer_wallet)
        print(f"Distributed {amt_distribute} OCEAN to address {dst_address}")

    print("****Distribute fake OCEAN: done****\n")

    print("****Update addresses file: begin****\n")

    print(f"addresses file: {addresses_file}")
    print(f"network: {network}")
    print("")

    network_addresses[network].update(addresses)

    with open(addresses_file, "w") as f:
        json.dump(network_addresses, f, indent=2)

    _s = json.dumps(addresses, indent=4)

    s = "Have deployed to, and updated the following addresses\n" + _s
    print(s)

    print("****Update addresses file: done****\n")