Skip to content

Commit

Permalink
Merge pull request #32 from oceanprotocol/feature/ocean-pool
Browse files Browse the repository at this point in the history
Feature/ocean pool
  • Loading branch information
ssallam authored Aug 8, 2020
2 parents 8c1d275 + 5923e8e commit 358023d
Show file tree
Hide file tree
Showing 21 changed files with 526 additions and 336 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.1.1
current_version = 0.1.2
commit = True
tag = True

Expand Down
70 changes: 53 additions & 17 deletions READMEs/marketplace_flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,36 @@ did = asset.did

alice_wallet = Wallet() # From step 2
data_token = DataToken() # From step 2
data_token.mint_tokens(alice_wallet.address, 100.0, alice_wallet)
data_token.mint_tokens(alice_wallet.address, 1000.0, alice_wallet)
```

## 4. Alice allows marketplace to sell her datatokens
## 4. Alice creates a pool for trading her new data tokens

```python
data_token = DataToken() # From step 2
marketplace_address = '0x068ed00cf0441e4829d9784fcbe7b9e26d4bd8d0'
data_token.approve_tokens(marketplace_address, 20.0)
from ocean_lib.ocean.util import to_base_18

token_address = '' # From step 2

pool = ocean.pool.create(
token_address,
data_token_amount_base=to_base_18(500.0),
OCEAN_amount_base=to_base_18(5.0),
from_wallet=alice_wallet
)
pool_address = pool.address
print(f'DataToken @{data_token.address} has a `pool` available @{pool_address}')
```

## 5. Marketplace posts asset for sale
Now, you're the marketplace:)
## 5. Marketplace posts asset for sale using price obtained from balancer pool

```python
from ocean_utils.agreements.service_types import ServiceTypes

from ocean_lib.ocean import Ocean
from ocean_lib.ocean.util import from_base_18
from ocean_lib.models.spool import SPool

#Market's config
# Market's config
config = {
'network': 'rinkeby',
}
Expand All @@ -124,24 +133,44 @@ market_ocean = Ocean(config)
did = '' # from step 2
asset = market_ocean.assets.resolve(did)
service1 = asset.get_service(ServiceTypes.ASSET_ACCESS)
price = 10.0 # marketplace-set price of 10 USD / datatoken
pool_address = ''
pool = market_ocean.pool.get(pool_address)
# price in OCEAN tokens per data token
price_in_OCEAN = from_base_18(market_ocean.pool.get_token_price_base(pool_address))

# Display key asset information, such as the cost of each service
tokens_amount = from_base_18(service1.get_cost())
print(f"Service 1 costs {tokens_amount * price} USD") # 1.5 * 10 = 15
# Each access to an assets service requires ONE datatoken
tokens_amount = 1.0
print(f"Service 1 costs {tokens_amount * price_in_OCEAN} OCEAN")
OCEAN_address = market_ocean.pool.ocean_address
OCEAN_usd_pool_address = ''
USDT_token_address = ''
ocn_pool = SPool(OCEAN_usd_pool_address)
OCEAN_price = from_base_18(ocn_pool.getSpotPrice(
tokenIn_address=USDT_token_address,
tokenOut_address=OCEAN_address
))
print(f"Service 1 costs {tokens_amount * price_in_OCEAN * OCEAN_price} USD")
```

## 6. Value swap: Bob buys datatokens from marketplace
## 6. Value swap: Bob buys datatokens from marketplace (using datatoken <> OCEAN balancer pool)

```python
# Not shown: in marketplace GUI, Bob uses Stripe to send USD to marketplace (or other methods / currencies).

data_token = market_ocean.get_data_token(token_address)
data_token.transfer_tokens(dst_address=bob_address, 1.0)
# This assumes bob_wallet already has sufficient OCEAN tokens to buy the data token. OCEAN tokens
# can be obtained through a crypto exchange or an on-chain pool such as balancer or uniswap
market_ocean.pool.buy_data_tokens(
pool_address,
amount_base=to_base_18(1.0), # buy one data token
max_OCEAN_amount_base=to_base_18(0.1), # pay maximum 0.1 OCEAN tokens
from_wallet=bob_wallet
)


```

## 7. Bob uses a service he just purchased (download)
Now, you're Bob:)

```python

Expand All @@ -154,6 +183,13 @@ bob_ocean = Ocean(config)
bob_wallet = Wallet(bob_ocean.web3, private_key='1234ef21b864d2cc526dbdb2a120bd2874c36c9d0a1fb7f8c63d7f7a8b41de8o')
service = asset.get_service(ServiceTypes.ASSET_ACCESS)
quote = bob_ocean.assets.order(asset.did, bob_wallet.address, service_index=service.index)
bob_ocean.assets.pay_for_service(quote.amount, quote.data_token_address, quote.receiver_address, bob_wallet)
file_path = bob_ocean.assets.download(asset.did, service.index, bob_wallet, '~/my-datasets')
transfer_tx_id = bob_ocean.assets.pay_for_service(quote.amount, quote.data_token_address, quote.receiver_address, bob_wallet)
file_path = bob_ocean.assets.download(
asset.did,
service.index,
bob_wallet,
transfer_tx_id,
destination='~/my-datasets',
index=0
)
```
4 changes: 1 addition & 3 deletions READMEs/simple_flow.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ocean_lib.ocean.util import get_dtfactory_addressfrom ocean_lib.ocean.util import get_dtfactory_address# Quickstart: Simple Flow
# Quickstart: Simple Flow

This stripped-down flow shows the essence of Ocean. Just downloading, no metadata.

Expand Down Expand Up @@ -80,8 +80,6 @@ token.transfer_tokens(bob_address, 1.0)

```python
from ocean_lib.ocean import Ocean
from ocean_lib.models.dtfactory import DTFactory
from ocean_lib.ocean.util import get_dtfactory_address

dt_address = '' # From first step
bob_config = {
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
author = 'ocean-lib-py contributors'

# The full version, including alpha/beta/rc tags
release = '0.1.1'
release = '0.1.2'
# The short X.Y version
release_parts = release.split('.') # a list
version = release_parts[0] + '.' + release_parts[1]
Expand Down
2 changes: 1 addition & 1 deletion ocean_lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__author__ = """OceanProtocol"""
__version__ = '0.1.1'
__version__ = '0.1.2'

# Copyright 2018 Ocean Protocol Foundation
# SPDX-License-Identifier: Apache-2.0
2 changes: 0 additions & 2 deletions ocean_lib/assets/asset_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def download_asset_files(
token_address,
token_transfer_tx_id,
data_provider,
nonce,
index=None
):
"""
Expand All @@ -31,7 +30,6 @@ def download_asset_files(
:param token_address: hex str the address of the DataToken smart contract
:param token_transfer_tx_id: hex str the token transfer transaction id (hash)
:param data_provider: DataServiceProvider instance
:param nonce: int value to use in the signature
:param index: Index of the document that is going to be downloaded, int
:return: Asset folder path, str
"""
Expand Down
1 change: 1 addition & 0 deletions ocean_lib/data_provider/data_service_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def encrypt_files_dict(files_dict, encrypt_endpoint, asset_id, publisher_address
def sign_message(wallet, msg, config, nonce=None):
if nonce is None:
nonce = DataServiceProvider.get_nonce(wallet.address, config)
print(f'signing message with nonce {nonce}: {msg}, account={wallet.address}')
return Web3Helper.sign_hash(
add_ethereum_prefix_and_hash_msg(f'{msg}{nonce}'),
wallet
Expand Down
2 changes: 1 addition & 1 deletion ocean_lib/ocean/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# configuration file
CONF_FILE_PATH = '~/ocean.conf'

#Toggle runtime type-checking
# Toggle runtime type-checking
import configparser, os
config = configparser.ConfigParser()
config.read(os.path.expanduser(CONF_FILE_PATH))
Expand Down
Loading

0 comments on commit 358023d

Please sign in to comment.