mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
4.0 KiB
4.0 KiB
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.
- Initialize services
- Alice publishes assets for data services (= publishes a datatoken contract and metadata)
- Alice mints 100 tokens
- Alice allows marketplace to sell her datatokens
- Marketplace posts asset for sale
- Value swap: Bob buys datatokens from marketplace
- Bob uses a service he just purchased (download)
Let's go through each step.
0. Installation
If you haven't installed yet:
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)
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
data_token.mint(account.address, 100, account)
4. Alice allows marketplace to sell her datatokens
marketplace_address = '0x068ed00cf0441e4829d9784fcbe7b9e26d4bd8d0'
data_token.approve(marketplace_address, 20)
5. Marketplace posts asset for sale
Now, you're the marketplace:)
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
#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:)
#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')