From 414445bb7743c9d8f70c5ea4f268b94b24cbfffc Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Mon, 13 Jul 2020 16:17:17 +0200 Subject: [PATCH 1/4] fix simple flow --- README.md | 6 +- README_marketplace_flow.md | 126 ------------------------------------- README_simple_flow.md | 96 ---------------------------- 3 files changed, 3 insertions(+), 225 deletions(-) delete mode 100644 README_marketplace_flow.md delete mode 100644 README_simple_flow.md diff --git a/README.md b/README.md index 3b1b246b1..805a1384d 100644 --- a/README.md +++ b/README.md @@ -26,17 +26,17 @@ This is in alpha state and you can expect running into problems. If you run into This stripped-down flow shows the essence of Ocean. Just downloading, no metadata. -[Go to simple flow](README_simple_flow.md) +[Go to simple flow](doc/README_simple_flow.md) ## Quickstart: Marketplace Flow This batteries-included flow includes metadata, multiple services for one datatoken, and compute-to-data. -[Go to marketplace flow](README_marketplace_flow.md) +[Go to marketplace flow](doc/README_marketplace_flow.md) ## For ocean-lib Developers -[Go to ocean-lib-developers flow](README_ocean-lib-developers.md) +[Go to ocean-lib-developers flow](doc/README_ocean-lib-developers.md) ## License diff --git a/README_marketplace_flow.md b/README_marketplace_flow.md deleted file mode 100644 index 30f759715..000000000 --- a/README_marketplace_flow.md +++ /dev/null @@ -1,126 +0,0 @@ -# Quickstart: Marketplace Flow - -This batteries-included flow includes metadata, multiple services for one datatoken, and compute-to-data. - -It focuses on Alice's experience as a publisher, and Bob's experience as a buyer & consumer. The rest are services used by Alice and Bob. - -Here's the steps. -1. Initialize services -1. Alice publishes assets for data services (= publishes a datatoken contract and metadata) -1. Alice mints 100 tokens -1. Alice allows marketplace to sell her datatokens -1. Marketplace posts asset for sale -1. Value swap: Bob buys datatokens from marketplace -1. Bob uses a service he just purchased (download) - -Let's go through each step. - -## 0. Installation - -If you haven't installed yet: -```console -pip install ocean-lib -``` - -## 1. Initialize services - -This quickstart treats the publisher service, metadata store, and marketplace as -externally-run services. For convenience, we run them locally in default settings. - -``` -docker run @oceanprotocol/provider-py:latest -docker run @oceanprotocol/metadatastore:latest -docker run @oceanprotocol/marketplace:latest -``` - -## 2. Alice publishes assets for data services (= publishes a datatoken contract) - -```python -from ocean_lib import Ocean -from ocean_lib.web3_internal.utils import get_account - -#Alice's config -config = { - 'network' : 'rinkeby', - 'privateKey' :'8da4ef21b864d2cc526dbdb2a120bd2874c36c9d0a1fb7f8c63d7f7a8b41de8f', - 'metadataStoreUri' : 'localhost:5000', - 'providerUri' : 'localhost:8030' -} -ocean = Ocean(config) -account = get_account(0) - -data_token = ocean.create_data_token(ocean.config.metadata_store_url, account) -token_address = data_token.address - -metadata = {"main": { - "type": "dataset", "name": "10 Monkey Species Small", "author": "Mario", - "license": "CC0: Public Domain", "dateCreated": "2012-02-01T10:55:11Z", - "files": [{ "index": 0, "contentType": "application/zip", "url": "https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/training.zip"}, - { "index": 1, "contentType": "text/text", "url": "https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/monkey_labels.txt"}, - { "index": 2, "contentType": "application/zip", "url": "https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/validation.zip"}]}} -asset = ocean.assets.create(metadata, account, data_token_address=token_address) -assert token_address == asset._other_values['dataTokenAddress'] - -did = asset.did -``` - -## 3. Alice mints 100 tokens - -```python -data_token.mint(account.address, 100, account) -``` - -## 4. Alice allows marketplace to sell her datatokens - -```python -marketplace_address = '0x068ed00cf0441e4829d9784fcbe7b9e26d4bd8d0' -data_token.approve(marketplace_address, 20) -``` - -## 5. Marketplace posts asset for sale -Now, you're the marketplace:) - -```python -from ocean_lib import Ocean - -#Market's config -config = { - 'network': 'rinkeby', - 'privateKey':'1234ef21b864d2cc526dbdb2a120bd2874c36c9d0a1fb7f8c63d7f7a8b41de8f', -} -market_ocean = Ocean(config) - -asset = ocean.assets.resolve(did) -service1 = asset.get_service('download') -service2 = asset.get_service('access') -price = 10.0 #marketplace-set price of 10 USD / datatoken - -#Display key asset information, such as the cost of each service -print(f"Service 1 costs {service1.get_num_dt_needed() * price} USD") # 1.5 * 10 = 15 -print(f"Service 2 costs {service2.get_num_dt_needed() * price} USD") # 2.5 * 10 = 25 -``` - -## 6. Value swap: Bob buys datatokens from marketplace - -```python -#Not shown: in marketplace GUI, Bob uses Stripe to send USD to marketplace (or other methods / currencies). - -market_token = market_ocean.get_data_token(token_address) -market_token.transfer(dst_address=bob_address, 1.0) -``` - -## 7. Bob uses a service he just purchased (download) -Now, you're Bob:) - -```python - -#Bob's config -config = { - 'network': 'rinkeby', - 'privateKey':'1234ef21b864d2cc526dbdb2a120bd2874c36c9d0a1fb7f8c63d7f7a8b41de8o', -} -market_ocean = Ocean(config) - -service = asset.get_service('access') -file_path = bob_ocean.assets.download(asset.did, service.index, bob_account, '~/my-datasets') -``` \ No newline at end of file diff --git a/README_simple_flow.md b/README_simple_flow.md deleted file mode 100644 index 26003e923..000000000 --- a/README_simple_flow.md +++ /dev/null @@ -1,96 +0,0 @@ - -# ocean-lib - -`ocean-lib-js` is a Javascript/Typescript library to privately & securely publish, exchange, and consume data. With it, you can: -* **Publish** data services: static data, streaming data, or compute-to-data. Every data service gets its own [ERC20](https://github.com/ethereum/EIPs/blob/7f4f0377730f5fc266824084188cc17cf246932e/EIPS/eip-20.md) token. -* **Mint** data tokens for a given data service -* **Transfer** data tokens to another owner -* **Consume** data tokens, to access the service - -`ocean-lib-js` is part of the [Ocean Protocol](www.oceanprotocol.com) toolset. - -# Installation -``` -// ES6 -import { Ocean, Logger } from '@oceanprotocol/lib' - -// ES2015 -const { Ocean, Logger } = require('@oceanprotocol/lib') - -``` - -# Quickstart - -This section describes a flow with the simplest transfer of value, for static data. - -Here's the steps. -1. Alice publishes a dataset (= publishes a datatoken contract) -1. Alice mints 100 tokens -1. Alice transfers 1 token to Bob -1. Bob consumes dataset - -Let's go through each of these in detail. - - -## 1. Alice publishes a dataset (= publishes a datatoken contract) - -For now, you're Alice:) Let's proceed. - -Run `ganache-cli` locally: -```bash -ganache-cli -``` - -Then proceed in with your code: -```javascript -const tokenAmount = 100 -const transferAmount = 1 -const blob = 'http://localhost:8030/api/v1/provider/services' - -const alice = await ocean.accounts.list()[0] -const bob = await ocean.accounts.list()[0] -// create datatoken class -const datatoken = new DataTokens(contracts.factoryAddress, factoryABI, datatokensABI, web3) -// deploy datatoken -const tokenAddress = await datatoken.create(blob, alice) - -``` - -## 2. Alice hosts the dataset - -Clone [provider-py](https://github.com/oceanprotocol/provider-py) and update your local environment variables: - -``` -export FLASK_APP=ocean_provider/run.py -export PROVIDER_ADDRESS=your_provider_address -export PROVIDER_KEY=your_provider_key -export CONFIG='{"File": "https://raw.githubusercontent.com/oceanprotocol/barge/master/README.md"}' -``` - -## 3. Alice mints 100 tokens - -```javascript -datatoken.mint(tokenAddress, alice, tokenAmount) -``` - -## 4. Alice transfers 1 token to Bob - -```javascript -const ts = await datatoken.transfer(tokenAddress, bob, transferAmount, alice) -const transactionId = ts['transactionHash'] -``` - -## 5. Bob consumes dataset - -Now, you are Bob :) - - -```javascript - -const config = new Config() -const ocean = await Ocean.getInstance() - -await ocean.assets.download(tokenAddress, blob, transactionId, bob) -``` - - From 9368a80e1b7290346f05aa9a16c18ae9d5c804e7 Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Tue, 14 Jul 2020 10:40:00 +0200 Subject: [PATCH 2/4] fix marketplace docs --- docs/README_marketplace_flow.md | 175 ++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 docs/README_marketplace_flow.md diff --git a/docs/README_marketplace_flow.md b/docs/README_marketplace_flow.md new file mode 100644 index 000000000..9aca0e18c --- /dev/null +++ b/docs/README_marketplace_flow.md @@ -0,0 +1,175 @@ +# Quickstart: Marketplace Flow + +This batteries-included flow includes metadata, multiple services for one datatoken, and compute-to-data. + +It focuses on Alice's experience as a publisher, and Bob's experience as a buyer & consumer. The rest are services used by Alice and Bob. + +Here's the steps. +1. Initialize services +1. Alice publishes assets for data services (= publishes a datatoken contract and metadata) +1. Alice mints 100 tokens +1. Alice allows marketplace to sell her datatokens +1. Marketplace posts asset for sale +1. Value swap: Bob buys datatokens from marketplace +1. Bob uses a service he just purchased (download) + +Let's go through each step. + +## 0. Installation + +If you haven't installed yet: +```bash +npm i @oceanprotocol/lib +``` + +## 1. Initialize services + +This quickstart treats the publisher service, ganache-cli, metadata store, and marketplace as +externally-run services. For convenience, we run barge locally in default settings. + +```bash +git clone https://github.com/oceanprotocol/barge.git +cd barge/ +git checkout v3 +export PROVIDER_VERSION=phase2 +./start_ocean.sh --no-dashboard +``` + +## 2. Alice publishes assets for data services (= publishes a datatoken contract) + +1. Create DataToken +```javascript +import { TestContractHandler } from '../TestContractHandler' +import { DataTokens } from '../../src/datatokens/Datatokens' +import { Ocean } from '../../src/ocean/Ocean' +const Web3 = require('web3') +const web3 = new Web3('http://127.0.0.1:8545') +const factory = require('@oceanprotocol/contracts/artifacts/development/Factory.json') +const datatokensTemplate = require('@oceanprotocol/contracts/artifacts/development/DataTokenTemplate.json') + +// Alice's config +const config = { + metadataStoreUri: 'http://aquarius:5000', + providerUri: 'http://localhost:8030', + nodeUri: `http://localhost:${process.env.ETH_PORT || 8545}`, + verbose: LogLevel.Error, + web3Provider: web3, + factoryAddress: '0x123456789...' +} +const ocean = await Ocean.getInstance(config) +const alice = (await ocean.accounts.list())[0] + +datatoken = new DataTokens( + config.factoryAddress, + factory.abi, + datatokensTemplate.abi, + web3 + ) +const data = { t: 1, url: ocean.config.metadataStoreUri } +const blob = JSON.stringify(data) + +const dataTokenAddress = await datatoken.create(blob, alice.getId()) + +``` +2. Publish asset(s) + +```javascript +const asset = { + main: { + type: 'dataset', + name: 'test-dataset', + dateCreated: new Date(Date.now()).toISOString().split('.')[0] + 'Z', // remove milliseconds + author: 'oceanprotocol-team', + license: 'MIT', + files: [ + { + url: + 'https://raw.githubusercontent.com/tbertinmahieux/MSongsDB/master/Tasks_Demos/CoverSongs/shs_dataset_test.txt', + checksum: 'efb2c764274b745f5fc37f97c6b0e761', + contentLength: '4535431', + contentType: 'text/csv', + encoding: 'UTF-8', + compression: 'zip' + } + ] + } + } + +// create a service +service1 = await ocean.assets.createAccessServiceAttributes( + alice, + 10, // set the price in datatoken + new Date(Date.now()).toISOString().split('.')[0] + 'Z', // publishedDate + 0 // timeout +) + +// publish asset +const ddo = await ocean.assets.create(asset, alice, [downloadService], dataTokenAddress) + +const did = ddo.id +``` + +## 3. Alice mints 100 tokens + +```javascript +await datatoken.mint(tokenAddress, alice.getId(), 100) +``` + +## 4. Alice allows marketplace to sell her datatokens + +```javascript +await datatoken.approve( + dataTokenAddress, + '0x068ed00cf0441e4829d9784fcbe7b9e26d4bd8d0', // marketplace address, + 20, // marketplaceAllowance + alice.getId() +) +``` + +## 5. Marketplace posts asset for sale +Now, you're the marketplace:) + +```javascript + +// Market's config +const marketOcean = await Ocean.getInstance(config) +const marketplace = (await ocean.accounts.list())[1] + +const asset = await ocean.assets.resolve(ddo.id) +const accessService = await ocean.assets.getServiceByType(asset.id, 'access') +price = 20 // in USD per dataToken +assert(accessService.attributes.main.cost * price === 200) +``` + +## 6. Value swap: Bob buys datatokens from marketplace + +```javascript +// Not shown: in marketplace GUI, Bob uses Stripe to send USD to marketplace (or other methods / currencies). + +``` + +## 7. Bob uses a service he just purchased (download) +Now, you're Bob:) + +```javascript +const accessService = await ocean.assets.getServiceByType(asset.id, 'access') +const bob = (await ocean.accounts.list())[2] +await ocean.assets.order(ddo.id, accessService.type, bob.getId()).then(async (res: string) => { + res = JSON.parse(res) + return await datatoken.transfer( + res['dataToken'], + res['to'], + res['numTokens'], + res['from'] + ) + }).then(async (tx) => { + await ocean.assets.download( + ddo.id, + tx.transactionHash, + dataTokenAddress, + bob, + '~/my-datasets' + ) + }) + +``` \ No newline at end of file From 4adc824d51ea755c3b0a3f30886b8962f2c47b8d Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Tue, 14 Jul 2020 10:41:30 +0200 Subject: [PATCH 3/4] add simple flow --- docs/README_simple_flow.md | 94 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 docs/README_simple_flow.md diff --git a/docs/README_simple_flow.md b/docs/README_simple_flow.md new file mode 100644 index 000000000..e1affb3ad --- /dev/null +++ b/docs/README_simple_flow.md @@ -0,0 +1,94 @@ + +# ocean-lib + +`ocean-lib-js` is a Javascript/Typescript library to privately & securely publish, exchange, and consume data. With it, you can: +* **Publish** data services: static data, streaming data, or compute-to-data. Every data service gets its own [ERC20](https://github.com/ethereum/EIPs/blob/7f4f0377730f5fc266824084188cc17cf246932e/EIPS/eip-20.md) token. +* **Mint** data tokens for a given data service +* **Transfer** data tokens to another owner +* **Consume** data tokens, to access the service + +`ocean-lib-js` is part of the [Ocean Protocol](www.oceanprotocol.com) toolset. + +# Installation +``` +// ES6 +import { Ocean, Logger } from '@oceanprotocol/lib' + +// ES2015 +const { Ocean, Logger } = require('@oceanprotocol/lib') + +``` + +# Quickstart + +This section describes a flow with the simplest transfer of value, for static data. + +Here's the steps. +1. Alice publishes a dataset (= publishes a datatoken contract) +1. Alice mints 100 tokens +1. Alice transfers 1 token to Bob +1. Bob consumes dataset + +Let's go through each of these in detail. + + +## 1. Alice publishes a dataset (= publishes a datatoken contract) + +For now, you're Alice:) Let's proceed. + +Run `ganache-cli` locally: +```bash +ganache-cli +``` + +Then proceed in with your code: +```javascript +const tokenAmount = 100 +const transferAmount = 1 +const blob = 'http://localhost:8030/api/v1/provider/services' + +const alice = await ocean.accounts.list()[0] +const bob = await ocean.accounts.list()[0] +// create datatoken class +const datatoken = new DataTokens(contracts.factoryAddress, factoryABI, datatokensABI, web3) +// deploy datatoken +const tokenAddress = await datatoken.create(blob, alice) + +``` + +## 2. Alice hosts the dataset + +Clone [provider-py](https://github.com/oceanprotocol/provider-py) and update your local environment variables: + +``` +export FLASK_APP=ocean_provider/run.py +export PROVIDER_ADDRESS=your_provider_address +export PROVIDER_KEY=your_provider_key +export CONFIG='{"File": "https://raw.githubusercontent.com/oceanprotocol/barge/master/README.md"}' +``` + +## 3. Alice mints 100 tokens + +```javascript +datatoken.mint(tokenAddress, alice, tokenAmount) +``` + +## 4. Alice transfers 1 token to Bob + +```javascript +const ts = await datatoken.transfer(tokenAddress, bob, transferAmount, alice) +const transactionId = ts['transactionHash'] +``` + +## 5. Bob consumes dataset + +Now, you are Bob :) + + +```javascript + +const config = new Config() +const ocean = await Ocean.getInstance() + +await ocean.assets.download(tokenAddress, blob, transactionId, bob) +``` \ No newline at end of file From 2669b2be6237247a285e06aef46cfdc8bbb7aae5 Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Tue, 14 Jul 2020 10:42:52 +0200 Subject: [PATCH 4/4] minor fix --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index 805a1384d..9b978868a 100644 --- a/README.md +++ b/README.md @@ -34,10 +34,6 @@ This batteries-included flow includes metadata, multiple services for one datato [Go to marketplace flow](doc/README_marketplace_flow.md) -## For ocean-lib Developers - -[Go to ocean-lib-developers flow](doc/README_ocean-lib-developers.md) - ## License ```