Ejemplo n.º 1
0
def test1(network, alice_wallet):
    bfactory_address = get_bfactory_address(network)
    bfactory = BFactory(bfactory_address)

    pool_address = bfactory.newBPool(from_wallet=alice_wallet)
    pool = BPool(pool_address)
    assert isinstance(pool, BPool)
Ejemplo n.º 2
0
def _deployBPool(network: str, from_wallet: Wallet) -> BPool:
    """Helper function to deploy a pool."""
    factory_address = get_bfactory_address(network)
    factory = BFactory(factory_address)
    pool_address = factory.newBPool(from_wallet=from_wallet)
    pool = BPool(pool_address)

    return pool
Ejemplo n.º 3
0
def test_bpool_creation(web3, config, alice_wallet):
    """Test the creation of a Balancer Pool from BFactory (happy flow)."""
    bfactory_address = get_bfactory_address(config.address_file, web3=web3)
    bfactory = BFactory(web3, bfactory_address)

    pool_address = bfactory.newBPool(from_wallet=alice_wallet)
    pool = BPool(web3, pool_address)
    assert isinstance(pool, BPool)
Ejemplo n.º 4
0
def test_bpool_creation(network, alice_wallet):
    """Test the creation of a Balancer Pool from BFactory (happy flow)."""
    bfactory_address = get_bfactory_address(network)
    bfactory = BFactory(bfactory_address)

    pool_address = bfactory.newBPool(from_wallet=alice_wallet)
    pool = BPool(pool_address)
    assert isinstance(pool, BPool)
Ejemplo n.º 5
0
def _deployBPool(web3: Web3, address_file: str, network: str,
                 from_wallet: Wallet) -> BPool:
    """Helper function to deploy a pool."""
    factory_address = get_bfactory_address(address_file, network)
    factory = BFactory(web3, factory_address)
    pool_address = factory.newBPool(from_wallet=from_wallet)
    pool = BPool(web3, pool_address)

    return pool
Ejemplo n.º 6
0
def test_get_bfactory_address(config):
    addresses = util.get_contracts_addresses(config.address_file, "ganache")
    assert addresses
    assert isinstance(addresses, dict)
    assert "BFactory" in addresses

    address = get_bfactory_address(config.address_file, "ganache")
    assert address[:2] == "0x", "It is not a token address."
    assert address == addresses["BFactory"]
Ejemplo n.º 7
0
def test_setSwapFee_fails(network, alice_wallet, alice_address, bob_wallet,
                          bob_address):
    factory = BFactory(get_bfactory_address(network))
    pool_address = factory.newBPool(alice_wallet)
    pool = BPool(pool_address)
    with pytest.raises(Exception):
        pool.setSwapFee(to_base_18(0.011),
                        from_wallet=bob_wallet)  # not ok, bob isn't controller
    pool.setController(bob_address, from_wallet=alice_wallet)
    pool.setSwapFee(to_base_18(0.011), from_wallet=bob_wallet)  # ok now
Ejemplo n.º 8
0
def test_setSwapFee_fails(network, config, web3, alice_wallet, alice_address,
                          bob_wallet, bob_address):
    """Tests that someone who isn't a controller can not set the swap fee."""
    factory = BFactory(web3, get_bfactory_address(config.address_file,
                                                  network))
    pool_address = factory.newBPool(alice_wallet)
    pool = BPool(web3, pool_address)
    with pytest.raises(Exception):
        pool.setSwapFee(to_wei("0.011"),
                        from_wallet=bob_wallet)  # not ok, bob isn't controller
    pool.setController(bob_address, from_wallet=alice_wallet)
    pool.setSwapFee(to_wei("0.011"), from_wallet=bob_wallet)  # ok now
Ejemplo n.º 9
0
def test1(network, OCEAN_address, alice_wallet, alice_ocean, alice_address,
          bob_wallet):
    bfactory_address = get_bfactory_address(network)

    # ===============================================================
    # 1. Alice publishes a dataset (= publishes a datatoken)
    # For now, you're Alice:) Let's proceed.
    DT = alice_ocean.create_data_token("DataToken1",
                                       "DT1",
                                       alice_wallet,
                                       blob="localhost:8030")
    DT_address = DT.address

    # ===============================================================
    # 2. Alice hosts the dataset
    # Do from console:
    # >> touch /var/mydata/myFolder1/file
    # >> ENV DT="{'0x1234':'/var/mydata/myFolder1'}"
    # >> docker run @oceanprotocol/provider-py -e CONFIG=DT

    # ===============================================================
    # 3. Alice mints DTs
    DT.mint(alice_address, to_base_18(1000.0), alice_wallet)

    # ===============================================================
    # 4. Alice creates an OCEAN-DT pool (=a Balancer Pool)
    bfactory = BFactory(bfactory_address)
    pool_address = bfactory.newBPool(from_wallet=alice_wallet)
    pool = BPool(pool_address)

    pool.setPublicSwap(True, from_wallet=alice_wallet)

    pool.setSwapFee(to_base_18(0.1), from_wallet=alice_wallet)  # set 10% fee

    DT.approve(pool_address, to_base_18(90.0), from_wallet=alice_wallet)
    pool.bind(
        DT_address,
        to_base_18(90.0),
        balancer_constants.INIT_WEIGHT_DT_BASE,
        from_wallet=alice_wallet,
    )

    OCEAN_token = BToken(OCEAN_address)
    txid = OCEAN_token.approve(pool_address,
                               to_base_18(10.0),
                               from_wallet=alice_wallet)
    r = OCEAN_token.get_tx_receipt(txid)
    assert r and r.status == 1, f"approve failed, receipt={r}"
    pool.bind(
        OCEAN_address,
        to_base_18(10.0),
        balancer_constants.INIT_WEIGHT_OCEAN_BASE,
        from_wallet=alice_wallet,
    )

    # ===============================================================
    # 5. Alice adds liquidity to pool
    DT.approve(pool_address, to_base_18(9.0), from_wallet=alice_wallet)
    pool.rebind(
        DT_address,
        to_base_18(90.0 + 9.0),
        balancer_constants.INIT_WEIGHT_DT_BASE,
        from_wallet=alice_wallet,
    )

    OCEAN_token.approve(pool_address,
                        to_base_18(1.0),
                        from_wallet=alice_wallet)
    pool.rebind(
        OCEAN_address,
        to_base_18(10.0 + 1.0),
        to_base_18(balancer_constants.INIT_WEIGHT_OCEAN),
        from_wallet=alice_wallet,
    )

    # 6. Bob buys a DT from pool
    OCEAN_token.approve(pool_address, to_base_18(2.0), from_wallet=bob_wallet)
    pool.swapExactAmountOut(
        tokenIn_address=OCEAN_address,
        maxAmountIn_base=to_base_18(2.0),
        tokenOut_address=DT_address,
        tokenAmountOut_base=to_base_18(1.0),
        maxPrice_base=2**255,
        from_wallet=bob_wallet,
    )

    # ===============================================================
    # 7. Bob consumes dataset
    # <don't need to show here>

    # ===============================================================
    # 8. Alice removes liquidity
    pool.rebind(
        DT_address,
        to_base_18(90.0 + 9.0 - 2.0),
        balancer_constants.INIT_WEIGHT_DT_BASE,
        from_wallet=alice_wallet,
    )
    pool.rebind(
        OCEAN_address,
        to_base_18(10.0 + 1.0 - 3.0),
        balancer_constants.INIT_WEIGHT_OCEAN_BASE,
        from_wallet=alice_wallet,
    )

    # ===============================================================
    # 9. Alice sells data tokens
    DT.approve(pool_address, to_base_18(1.0), from_wallet=alice_wallet)
    pool.swapExactAmountIn(
        tokenIn_address=DT_address,
        tokenAmountIn_base=to_base_18(1.0),
        tokenOut_address=OCEAN_address,
        minAmountOut_base=to_base_18(0.0001),
        maxPrice_base=2**255,
        from_wallet=alice_wallet,
    )

    # ===============================================================
    # 10. Alice finalizes pool. Now others can add liquidity.
    pool.finalize(from_wallet=alice_wallet)

    # ===============================================================
    # 11. Bob adds liquidity
    DT.approve(pool_address, to_base_18(1.0), from_wallet=bob_wallet)
    OCEAN_token.approve(pool_address, to_base_18(1.0), from_wallet=bob_wallet)
    pool.joinPool(to_base_18(0.1),
                  [to_base_18(1.0), to_base_18(1.0)],
                  from_wallet=bob_wallet)

    # ===============================================================
    # 12. Bob adds liquidity AGAIN
    DT.approve(pool_address, to_base_18(1.0), from_wallet=bob_wallet)
    OCEAN_token.approve(pool_address, to_base_18(1.0), from_wallet=bob_wallet)
    pool.joinPool(to_base_18(0.1),
                  [to_base_18(1.0), to_base_18(1.0)],
                  from_wallet=bob_wallet)
Ejemplo n.º 10
0
    def __init__(self, config=None, data_provider=None):
        """Initialize Ocean class.

           >> # Make a new Ocean instance
           >> ocean = Ocean({...})

        This class provides the main top-level functions in ocean protocol:
         * Publish assets metadata and associated services
            * Each asset is assigned a unique DID and a DID Document (DDO)
            * The DDO contains the asset's services including the metadata
            * The DID is registered on-chain with a URL of the metadata store
              to retrieve the DDO from

            >> asset = ocean.assets.create(metadata, publisher_wallet)

         * Discover/Search assets via the current configured metadata store (Aquarius)
            >> assets_list = ocean.assets.search('search text')

        An instance of Ocean is parameterized by a `Config` instance.

        :param config: Config instance
        :param data_provider: DataServiceProvider instance
        """
        # Configuration information for the market is stored in the Config class
        # config = Config(filename=config_file, options_dict=config_dict)
        if not config:
            try:
                config = ConfigProvider.get_config()
            except AssertionError:
                config = Config(os.getenv(ENV_CONFIG_FILE))
                ConfigProvider.set_config(config)
        if isinstance(config, dict):
            # fallback to metadataStoreUri
            cache_key = ("metadataCacheUri" if ("metadataCacheUri" in config)
                         else "metadataStoreUri")
            aqua_url = config.get(
                cache_key, config.get("aquarius.url", "http://localhost:5000"))
            config_dict = {
                "eth-network": {
                    "network": config.get("network", "")
                },
                "resources": {
                    "aquarius.url":
                    aqua_url,
                    "provider.url":
                    config.get("providerUri", "http://localhost:8030"),
                },
            }
            config = Config(options_dict=config_dict)
        ConfigProvider.set_config(config)
        self._config = config
        ContractHandler.set_artifacts_path(self._config.artifacts_path)
        Web3Provider.init_web3(
            provider=get_web3_connection_provider(self._config.network_url))

        self._web3 = Web3Provider.get_web3()

        if not data_provider:
            data_provider = DataServiceProvider

        network = Web3Helper.get_network_name()
        addresses = get_contracts_addresses(network, self._config)
        self.assets = OceanAssets(
            self._config, data_provider,
            addresses.get(MetadataContract.CONTRACT_NAME))
        self.services = OceanServices()
        self.auth = OceanAuth(self._config.storage_path)
        self.compute = OceanCompute(self.auth, self._config, data_provider)

        ocean_address = get_ocean_token_address(network)
        self.pool = OceanPool(ocean_address, get_bfactory_address(network))
        self.exchange = OceanExchange(
            ocean_address,
            FixedRateExchange.configured_address(
                network or Web3Helper.get_network_name(),
                ConfigProvider.get_config().address_file,
            ),
            self.config,
        )

        logger.debug("Ocean instance initialized: ")
Ejemplo n.º 11
0
def _deployBPool(network: str, from_wallet: Wallet) -> BPool:
    factory_address = get_bfactory_address(network)
    factory = BFactory(factory_address)
    pool_address = factory.newBPool(from_wallet=from_wallet)
    pool = BPool(pool_address)
    return pool
Ejemplo n.º 12
0
    def __init__(
        self, config: Union[Dict, Config], data_provider: Optional[Type] = None
    ) -> None:
        """Initialize Ocean class.

        Usage: Make a new Ocean instance

        `ocean = Ocean({...})`

        This class provides the main top-level functions in ocean protocol:
        1. Publish assets metadata and associated services
            - Each asset is assigned a unique DID and a DID Document (DDO)
            - The DDO contains the asset's services including the metadata
            - The DID is registered on-chain with a URL of the metadata store
              to retrieve the DDO from

            `asset = ocean.assets.create(metadata, publisher_wallet)`

        2. Discover/Search assets via the current configured metadata store (Aquarius)

            - Usage:
            `assets_list = ocean.assets.search('search text')`

        An instance of Ocean is parameterized by a `Config` instance.

        :param config: `Config` instance
        :param data_provider: `DataServiceProvider` instance
        """
        if isinstance(config, dict):
            # fallback to metadataStoreUri
            cache_key = (
                "metadataCacheUri"
                if ("metadataCacheUri" in config)
                else "metadataStoreUri"
            )
            metadata_cache_uri = config.get(
                cache_key, config.get("metadata_cache_uri", "http://localhost:5000")
            )
            config_dict = {
                "eth-network": {"network": config.get("network", "")},
                "resources": {
                    "metadata_cache_uri": metadata_cache_uri,
                    "provider.url": config.get("providerUri", "http://localhost:8030"),
                },
            }
            config = Config(options_dict=config_dict)
        self.config = config
        self.web3 = get_web3(self.config.network_url)

        if not data_provider:
            data_provider = DataServiceProvider

        network = get_network_name(web3=self.web3)
        addresses = get_contracts_addresses(self.config.address_file, network)
        self.assets = OceanAssets(
            self.config,
            self.web3,
            data_provider,
            addresses.get(MetadataContract.CONTRACT_NAME),
        )
        self.compute = OceanCompute(self.config, data_provider)

        ocean_address = get_ocean_token_address(self.config.address_file, network)
        self.pool = OceanPool(
            self.web3,
            ocean_address,
            get_bfactory_address(self.config.address_file, network),
            get_dtfactory_address(self.config.address_file, network),
        )
        self.exchange = OceanExchange(
            self.web3,
            ocean_address,
            FixedRateExchange.configured_address(network, self.config.address_file),
            self.config,
        )

        logger.debug("Ocean instance initialized: ")