Skip to content

Instantly share code, notes, and snippets.

@araa47
Last active October 9, 2020 08:06
Show Gist options
  • Save araa47/72d2582beebcdca2a3e25860c0f26f8e to your computer and use it in GitHub Desktop.
Save araa47/72d2582beebcdca2a3e25860c0f26f8e to your computer and use it in GitHub Desktop.
MarketPlace Flow OceanPy
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Setup"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Run Barge \n",
"\n",
"```\n",
"git clone https://github.com/oceanprotocol/barge.git\n",
"cd barge/\n",
"git checkout v3\n",
"export PROVIDER_VERSION=latest\n",
"./start_ocean.sh --no-dashboard\n",
"\n",
"```\n",
"\n",
"\n",
"2. Go to `/root/.ocean/ocean-contract/artifacts/` copy this folder to desktop \n",
"\n",
"3. Now find your virtualenv folder for this project and overwrite the artifacts file. To simply find this you can try running this https://gist.github.com/araa47/4f416ce69feffab9fb1ab20df2dc774e snippet. It should error out showing your artifacts path. You need to overwrite this with the barge artifacts from previous step \n",
"\n",
"4. Go to the Tools Fund Alice notebook and fund alice wallet with some eth (her wallet seems empty in barge v3 so we seem to have to manually fund it)\n",
"\n",
"5. Now you are finally ready for this example workflow\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Alice publishes assets for data services (= publishes a DataToken contract)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"from ocean_utils.agreements.service_factory import ServiceDescriptor\n",
"\n",
"from ocean_lib.ocean.ocean import Ocean\n",
"from ocean_lib.web3_internal.wallet import Wallet\n",
"from ocean_lib.ocean.util import to_base_18\n",
"from ocean_lib.data_provider.data_service_provider import DataServiceProvider\n",
"from ocean_lib.config_provider import ConfigProvider\n",
"from ocean_lib.ocean.ocean import Ocean\n",
"from ocean_lib.models.dtfactory import DTFactory\n",
"from ocean_lib.ocean.util import to_base_18\n",
"from ocean_lib.web3_internal.web3helper import Web3Helper\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"config = {\n",
" 'network' : 'ganache',\n",
" 'metadataStoreUri' : 'http://127.0.0.1:5000',\n",
" 'providerUri' : 'http://127.0.0.1:8030'}\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"ocean = Ocean(config)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"alice_wallet = Wallet(ocean.web3, private_key='8da4ef21b864d2cc526dbdb2a120bd2874c36c9d0a1fb7f8c63d7f7a8b41de8f')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"data_token = ocean.create_data_token(ocean.config.metadata_store_url, 'DataToken1', 'DT1', alice_wallet)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'0x794DD81409646338c186037427764C385357e22E'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"token_address = data_token.address\n",
"token_address"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# `ocean.assets.create` will encrypt the URLs using the provider's encrypt service endpoint and update \n",
"# the asset before pushing to metadata store\n",
"# `ocean.assets.create` will require that token_address is a valid DataToken contract address, unless token_address\n",
"# is not provided then the `create` method will first create a new data token and use it in the new\n",
"# asset.\n",
"metadata = {\n",
" \"main\": {\n",
" \"type\": \"dataset\", \"name\": \"10 Monkey Species Small\", \"author\": \"Mario\", \n",
" \"license\": \"CC0: Public Domain\", \"dateCreated\": \"2012-02-01T10:55:11Z\", \n",
" \"files\": [\n",
" { \"index\": 0, \"contentType\": \"application/zip\", \"url\": \"https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/training.zip\"},\n",
" { \"index\": 1, \"contentType\": \"text/text\", \"url\": \"https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/monkey_labels.txt\"},\n",
" { \"index\": 2, \"contentType\": \"application/zip\", \"url\": \"https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/validation.zip\"}]}\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Prepare attributes for the download service including the cost in DataTokens\n",
"service_attributes = {\n",
" \"main\": {\n",
" \"name\": \"dataAssetAccessServiceAgreement\",\n",
" \"creator\": alice_wallet.address,\n",
" \"cost\": 1.0, # service cost is 1.0 tokens \n",
" \"timeout\": 3600 * 24,\n",
" \"datePublished\": metadata[\"main\"]['dateCreated']\n",
" }\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"service_endpoint = DataServiceProvider.get_download_endpoint(ocean.config)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"download_service = ServiceDescriptor.access_service_descriptor(service_attributes, service_endpoint)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"signing message with nonce None: 0x794DD81409646338c186037427764C385357e22E, account=0x63FaC9201494f0bd17B9892B9fae4d52fe3BD377\n"
]
},
{
"ename": "AssertionError",
"evalue": "Encrypting the files failed.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-11-db325cc89704>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0masset\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mocean\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0massets\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcreate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmetadata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0malice_wallet\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mservice_descriptors\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mdownload_service\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata_token_address\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtoken_address\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/.cache/pypoetry/virtualenvs/ocean-FdIsuwtW-py3.8/lib/python3.8/site-packages/ocean_lib/ocean/ocean_assets.py\u001b[0m in \u001b[0;36mcreate\u001b[0;34m(self, metadata, publisher_wallet, service_descriptors, owner_address, data_token_address, provider_uri, dt_name, dt_symbol, dt_blob)\u001b[0m\n\u001b[1;32m 236\u001b[0m \u001b[0mmetadata_copy\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'encryptedFiles'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfiles_encrypted\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 237\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 238\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mAssertionError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Encrypting the files failed.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 239\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 240\u001b[0m logger.debug(\n",
"\u001b[0;31mAssertionError\u001b[0m: Encrypting the files failed."
]
}
],
"source": [
"asset = ocean.assets.create(metadata, alice_wallet, service_descriptors=[download_service], data_token_address=token_address)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Alice mints 1000 tokens"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'0x099700fb47d102833c5c31c671d274a19601c9875d8ad54f53acec99971ae1b6'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data_token.mint_tokens(alice_wallet.address, 1000.0, alice_wallet)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Alice creates a pool for trading her new data tokens "
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"BPool.newBPool(). Begin.\n",
" pool_address = 0xded797b1BbBBfF62088593368f87178Fa2DB61Ff\n",
"BFactory.newBPool(). Done.\n"
]
},
{
"ename": "ValidationError",
"evalue": "\nCould not identify the intended function with name `setup`, positional argument(s) of type `(<class 'str'>, <class 'int'>, <class 'int'>, <class 'NoneType'>, <class 'int'>, <class 'int'>, <class 'int'>)` and keyword argument(s) of type `{}`.\nFound 1 function(s) with the name `setup`: ['setup(address,uint256,uint256,address,uint256,uint256,uint256)']\nFunction invocation failed due to no matching argument types.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValidationError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-14-4e21a366c45d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m pool = ocean.pool.create(\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mtoken_address\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mdata_token_amount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m500.0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mOCEAN_amount\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m5.0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mfrom_wallet\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0malice_wallet\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.cache/pypoetry/virtualenvs/ocean-FdIsuwtW-py3.8/lib/python3.8/site-packages/ocean_lib/ocean/ocean_pool.py\u001b[0m in \u001b[0;36mcreate\u001b[0;34m(self, data_token_address, data_token_amount, OCEAN_amount, from_wallet, data_token_weight, swap_fee)\u001b[0m\n\u001b[1;32m 70\u001b[0m \u001b[0mot\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapprove_tokens\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpool_address\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mOCEAN_amount\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfrom_wallet\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 71\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 72\u001b[0;31m tx_id = pool.setup(\n\u001b[0m\u001b[1;32m 73\u001b[0m \u001b[0mdata_token_address\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 74\u001b[0m \u001b[0mto_base_18\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata_token_amount\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.cache/pypoetry/virtualenvs/ocean-FdIsuwtW-py3.8/lib/python3.8/site-packages/ocean_lib/models/bpool.py\u001b[0m in \u001b[0;36msetup\u001b[0;34m(self, data_token, data_token_amount, data_token_weight, base_token, base_token_amount, base_token_weight, swap_fee, from_wallet)\u001b[0m\n\u001b[1;32m 66\u001b[0m from_wallet: Wallet) -> str:\n\u001b[1;32m 67\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 68\u001b[0;31m tx_id = self.send_transaction(\n\u001b[0m\u001b[1;32m 69\u001b[0m \u001b[0;34m'setup'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 70\u001b[0m (data_token, data_token_amount, data_token_weight,\n",
"\u001b[0;32m~/.cache/pypoetry/virtualenvs/ocean-FdIsuwtW-py3.8/lib/python3.8/site-packages/ocean_lib/web3_internal/contract_base.py\u001b[0m in \u001b[0;36msend_transaction\u001b[0;34m(self, fn_name, fn_args, from_wallet, transact)\u001b[0m\n\u001b[1;32m 144\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;32mreturn\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 145\u001b[0m \"\"\"\n\u001b[0;32m--> 146\u001b[0;31m \u001b[0mcontract_fn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcontract\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfunctions\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfn_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mfn_args\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 147\u001b[0m contract_function = CustomContractFunction(\n\u001b[1;32m 148\u001b[0m \u001b[0mcontract_fn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.cache/pypoetry/virtualenvs/ocean-FdIsuwtW-py3.8/lib/python3.8/site-packages/web3/contract.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1037\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1038\u001b[0m \u001b[0mclone\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkwargs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1039\u001b[0;31m \u001b[0mclone\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_set_function_info\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1040\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mclone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1041\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.cache/pypoetry/virtualenvs/ocean-FdIsuwtW-py3.8/lib/python3.8/site-packages/web3/contract.py\u001b[0m in \u001b[0;36m_set_function_info\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1042\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_set_function_info\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1043\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mabi\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1044\u001b[0;31m self.abi = find_matching_fn_abi(\n\u001b[0m\u001b[1;32m 1045\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcontract_abi\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1046\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfunction_identifier\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.cache/pypoetry/virtualenvs/ocean-FdIsuwtW-py3.8/lib/python3.8/site-packages/web3/utils/contracts.py\u001b[0m in \u001b[0;36mfind_matching_fn_abi\u001b[0;34m(abi, fn_identifier, args, kwargs)\u001b[0m\n\u001b[1;32m 126\u001b[0m \u001b[0mdiagnosis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mdiagnosis\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 127\u001b[0m )\n\u001b[0;32m--> 128\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValidationError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 129\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 130\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValidationError\u001b[0m: \nCould not identify the intended function with name `setup`, positional argument(s) of type `(<class 'str'>, <class 'int'>, <class 'int'>, <class 'NoneType'>, <class 'int'>, <class 'int'>, <class 'int'>)` and keyword argument(s) of type `{}`.\nFound 1 function(s) with the name `setup`: ['setup(address,uint256,uint256,address,uint256,uint256,uint256)']\nFunction invocation failed due to no matching argument types."
]
}
],
"source": [
"pool = ocean.pool.create(\n",
" token_address,\n",
" data_token_amount=500.0,\n",
" OCEAN_amount=5.0,\n",
" from_wallet=alice_wallet\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment