Skip to content

Commit

Permalink
Simplify create_datatoken function to take in some defaults. (#842)
Browse files Browse the repository at this point in the history
* Simplify create_datatoken function to take in some defaults.
* Adds flake8 formatting to readmes.
* Make template_index also default, simplify call even more.
* Adjust names and symbols in readme.
* Bump version: 1.0.0-alpha.3 → 1.0.0-alpha.4
  • Loading branch information
calina-c authored May 25, 2022
1 parent 3468a81 commit 83af173
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 153 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 = 1.0.0-alpha.3
current_version = 1.0.0-alpha.4
commit = True
tag = True

Expand Down
2 changes: 1 addition & 1 deletion READMEs/c2d-flow-more-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ALGO_metadata = {
"container": {
"entrypoint": "python $ALGO",
"image": "oceanprotocol/algo_dockers",
"tag": "python-branin", # This image provides all the dependencies of the grayscale.py algorithm
"tag": "python-branin", # This image provides all the dependencies of the grayscale.py algorithm
"checksum": "44e10daa6637893f4276bb8d7301eb35306ece50f61ca34dcab550",
},
}
Expand Down
38 changes: 7 additions & 31 deletions READMEs/c2d-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,8 @@ Please refer to [data-nfts-and-datatokens-flow](data-nfts-and-datatokens-flow.md
In the same python console:

```python
from ocean_lib.web3_internal.constants import ZERO_ADDRESS

# Publish the datatoken
DATA_datatoken = erc721_nft.create_datatoken(
template_index=1,
name="Datatoken 1",
symbol="DT1",
minter=alice_wallet.address,
fee_manager=alice_wallet.address,
publish_market_order_fee_address=ZERO_ADDRESS,
publish_market_order_fee_token=ocean.OCEAN_address,
publish_market_order_fee_amount=0,
bytess=[b""],
from_wallet=alice_wallet,
)
DATA_datatoken = erc721_nft.create_datatoken("DATA 1", "D1", from_wallet=alice_wallet)
print(f"DATA_datatoken address = '{DATA_datatoken.address}'")

# Specify metadata and services, using the Branin test dataset
Expand Down Expand Up @@ -137,18 +124,7 @@ ALGO_nft_token = ocean.create_erc721_nft("NFTToken1", "NFT1", alice_wallet)
print(f"ALGO_nft_token address = '{ALGO_nft_token.address}'")

# Publish the datatoken
ALGO_datatoken = ALGO_nft_token.create_datatoken(
template_index=1,
name="Datatoken 1",
symbol="DT1",
minter=alice_wallet.address,
fee_manager=alice_wallet.address,
publish_market_order_fee_address=ZERO_ADDRESS,
publish_market_order_fee_token=ocean.OCEAN_address,
publish_market_order_fee_amount=0,
bytess=[b""],
from_wallet=alice_wallet,
)
ALGO_datatoken = ALGO_nft_token.create_datatoken("ALGO 1", "A1", from_wallet=alice_wallet)
print(f"ALGO_datatoken address = '{ALGO_datatoken.address}'")

# Specify metadata and services, using the Branin test dataset
Expand Down Expand Up @@ -315,14 +291,14 @@ X0_vec = numpy.linspace(-5., 10., 15)
X1_vec = numpy.linspace(0., 15., 15)
X0, X1 = numpy.meshgrid(X0_vec, X1_vec)
b, c, t = 0.12918450914398066, 1.5915494309189535, 0.039788735772973836
u = X1 - b*X0**2 + c*X0 - 6
r = 10.*(1. - t) * numpy.cos(X0) + 10
Z = u**2 + r
u = X1 - b * X0 ** 2 + c * X0 - 6
r = 10. * (1. - t) * numpy.cos(X0) + 10
Z = u ** 2 + r

fig, ax = pyplot.subplots(subplot_kw={"projection": "3d"})
ax.scatter(X0, X1, model, c="r", label="model")
pyplot.title("Data + model")
pyplot.show() # or pyplot.savefig("test.png") to save the plot as a .png file instead
pyplot.show() # or pyplot.savefig("test.png") to save the plot as a .png file instead
```

You should see something like this:
Expand All @@ -336,7 +312,7 @@ For examples using different datasets and algorithms, please see [c2d-flow-more-
In the "publish algorithm" step, to replace the sample algorithm with another one:

- Use one of the standard [Ocean algo_dockers images](https://github.com/oceanprotocol/algo_dockers) or publish a custom docker image.
- Use the image name and tag in the `container` part of the algorithm metadata.
- Use the image name and tag in the `container` part of the algorithm metadata.
- The image must have basic support for installing dependencies. E.g. "pip" for the case of Python. You can use other languages, of course.
- More info: https://docs.oceanprotocol.com/tutorials/compute-to-data-algorithms/)

Expand Down
4 changes: 2 additions & 2 deletions READMEs/consume-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ erc20_token.mint(

# Bob points to the service object
from ocean_lib.web3_internal.constants import ZERO_ADDRESS
fee_receiver = ZERO_ADDRESS # could also be market address
fee_receiver = ZERO_ADDRESS # could also be market address
service = asset.services[0]

# Bob sends his datatoken to the service
Expand All @@ -92,7 +92,7 @@ file_path = ocean.assets.download_asset(
destination='./',
order_tx_id=order_tx_id
)
print(f"file_path = '{file_path}'") #e.g. datafile.0xAf07...
print(f"file_path = '{file_path}'") # e.g. datafile.0xAf07...
```

In console:
Expand Down
24 changes: 6 additions & 18 deletions READMEs/data-nfts-and-datatokens-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,8 @@ Congrats, you've created your first Ocean data NFT!
In the same python console:
```python
# Create ERC20 token related to the above NFT.
from ocean_lib.web3_internal.constants import ZERO_ADDRESS

erc20_token = erc721_nft.create_datatoken(
template_index=1, # default value
name="ERC20DT1", # name for ERC20 token
symbol="ERC20DT1Symbol", # symbol for ERC20 token
minter=alice_wallet.address, # minter address
fee_manager=alice_wallet.address, # fee manager for this ERC20 token
publish_market_order_fee_address=alice_wallet.address, # publishing Market Address
publish_market_order_fee_token=ZERO_ADDRESS, # publishing Market Fee Token
publish_market_order_fee_amount=0,
bytess=[b""],
from_wallet=alice_wallet
)

erc20_token = erc721_nft.create_datatoken("Datatoken 1", "DT1", from_wallet=alice_wallet)
print(f"Created ERC20 datatoken. Its address is {erc20_token.address}")
```

Expand All @@ -125,20 +113,20 @@ You can combine creating a data NFT and datatoken into a single call: `ocean.cre

To learn more about some of the objects you created, here are some examples.
```python
#config
# config
print(f"config.network_url = '{config.network_url}'")
print(f"config.block_confirmations = {config.block_confirmations.value}")
print(f"config.metadata_cache_uri = '{config.metadata_cache_uri}'")
print(f"config.provider_url = '{config.provider_url}'")

#wallet
# wallet
print(f"alice_wallet.address = '{alice_wallet.address}'")

#data NFT
# data NFT
print(f"data NFT token name: {erc721_nft.token_name()}")
print(f"data NFT token symbol: {erc721_nft.symbol()}")

#datatoken
# datatoken
print(f"datatoken name: {erc20_token.token_name()}")
print(f"datatoken symbol: {erc20_token.symbol()}")
```
16 changes: 2 additions & 14 deletions READMEs/erc20-enterprise.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,9 @@ In the Python console:
from ocean_lib.web3_internal.constants import ZERO_ADDRESS

erc20_enterprise_token = erc721_nft.create_datatoken(
template_index=2, # this is the value for ERC20 Enterprise token
name="ERC20DT1", # name for ERC20 token
symbol="ERC20DT1Symbol", # symbol for ERC20 token
minter=alice_wallet.address, # minter address
fee_manager=alice_wallet.address, # fee manager for this ERC20 token
publish_market_order_fee_address=alice_wallet.address, # publishing Market Address
publish_market_order_fee_token=ZERO_ADDRESS, # publishing Market Fee Token
publish_market_order_fee_amount=0,
bytess=[b""],
template_index=2, # this is the value for ERC20 Enterprise token
from_wallet=alice_wallet,
)
print(f"ERC20 Enterprise address: {erc20_enterprise_token.address}")
Expand Down Expand Up @@ -144,15 +138,9 @@ In the Python console:
from ocean_lib.web3_internal.constants import ZERO_ADDRESS

erc20_enterprise_token = erc721_nft.create_datatoken(
template_index=2, # this is the value for ERC20 Enterprise token
name="ERC20DT1", # name for ERC20 token
symbol="ERC20DT1Symbol", # symbol for ERC20 token
minter=alice_wallet.address, # minter address
fee_manager=alice_wallet.address, # fee manager for this ERC20 token
publish_market_order_fee_address=alice_wallet.address, # publishing Market Address
publish_market_order_fee_token=ZERO_ADDRESS, # publishing Market Fee Token
publish_market_order_fee_amount=0,
bytess=[b""],
template_index=2, # this is the value for ERC20 Enterprise token
from_wallet=alice_wallet,
)
print(f"ERC20 Enterprise address: {erc20_enterprise_token.address}")
Expand Down
2 changes: 1 addition & 1 deletion READMEs/fixed-rate-exchange-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Please refer to [data-nfts-and-datatokens-flow](data-nfts-and-datatokens-flow.md

In the same python console:
```python
#Mint the datatokens
# Mint the datatokens
erc20_token.mint(alice_wallet.address, ocean.to_wei(100), alice_wallet)
```

Expand Down
12 changes: 6 additions & 6 deletions READMEs/key-value-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ From [data-nfts-and-datatokens-flow](data-nfts-and-datatokens-flow.md), do:
## 3. Add key-value pair to data NFT

```python
#Key-value pair
# Key-value pair
key = "fav_color"
value = "blue"

#prep key for setter
key_hash = ocean.web3.keccak(text=key) #Contract/ERC725 requires keccak256 hash
# prep key for setter
key_hash = ocean.web3.keccak(text=key) # Contract/ERC725 requires keccak256 hash

#prep value for setter
value_hex = value.encode('utf-8').hex() #set_new_data() needs hex
# prep value for setter
value_hex = value.encode('utf-8').hex() # set_new_data() needs hex

#set
# set
erc721_nft.set_new_data(key_hash, value_hex, alice_wallet)
```

Expand Down
3 changes: 1 addition & 2 deletions READMEs/marketplace-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ bpool = ocean.create_pool(
from_wallet=alice_wallet
)
print(f"BPool address: {bpool.address}")

```

## 4. Marketplace displays asset for sale
Expand Down Expand Up @@ -142,7 +141,7 @@ assert erc20_token.balanceOf(bob_wallet.address) >= ocean.to_wei(

# Bob points to the service object
from ocean_lib.web3_internal.constants import ZERO_ADDRESS
fee_receiver = ZERO_ADDRESS # could also be market address
fee_receiver = ZERO_ADDRESS # could also be market address
asset = ocean.assets.resolve(did)
service = asset.services[0]

Expand Down
28 changes: 14 additions & 14 deletions READMEs/profile-nfts-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,33 @@ erc721_nft = ocean.create_erc721_nft('NFTToken1', 'NFT1', alice_wallet, transfer
## 3. Alice adds key-value pair to data NFT. 'value' encrypted with a symmetric key 'symkey'

```python
#imports
# imports
from base64 import b64encode
from cryptography.fernet import Fernet
from eth_account.messages import encode_defunct
from hashlib import sha256
web3 = ocean.web3

#Key-value pair
# Key-value pair
profiledata_name = "fav_color"
profiledata_val = "blue"

#Prep key for setter. Contract/ERC725 requires keccak256 hash
# Prep key for setter. Contract/ERC725 requires keccak256 hash
profiledata_name_hash = web3.keccak(text=profiledata_name)

#Choose a symkey where:
# Choose a symkey where:
# - sharing it unlocks only this field: make unique to this data nft & field
# - only Alice can compute it: make it a function of her private key
# - is hardware wallet friendly: uses Alice's digital signature not private key
preimage = erc721_nft.address + profiledata_name
msg = encode_defunct(text=sha256(preimage.encode('utf-8')).hexdigest())
signed_msg = web3.eth.account.sign_message(msg, private_key=alice_wallet.private_key)
symkey = b64encode(signed_msg.signature.hex().encode('ascii'))[:43] + b'=' #bytes
symkey = b64encode(signed_msg.signature.hex().encode('ascii'))[:43] + b'=' # bytes

#Prep value for setter
# Prep value for setter
profiledata_val_encr_hex = Fernet(symkey).encrypt(profiledata_val.encode('utf-8')).hex()

#set
# set
erc721_nft.set_new_data(profiledata_name_hash, profiledata_val_encr_hex, alice_wallet)
```

Expand All @@ -96,8 +96,8 @@ from eth_utils import decode_hex

dapp_private_key = os.getenv('TEST_PRIVATE_KEY2')
dapp_private_key_obj = keys.PrivateKey(decode_hex(dapp_private_key))
dapp_public_key = str(dapp_private_key_obj.public_key) #str
dapp_address = dapp_private_key_obj.public_key.to_address() #str
dapp_public_key = str(dapp_private_key_obj.public_key) # str
dapp_address = dapp_private_key_obj.public_key.to_address() # str
```

## 5. Alice encrypts symkey with Dapp's public key and shares to Dapp
Expand All @@ -107,12 +107,12 @@ There are various ways for Alice to share the encrypted symkey to the Dapp (see
```python
from ecies import encrypt as asymmetric_encrypt

symkey_name = (profiledata_name + ':for:' + dapp_address[:10]) #str
symkey_name = (profiledata_name + ':for:' + dapp_address[:10]) # str
symkey_name_hash = web3.keccak(text=symkey_name)

symkey_val_encr = asymmetric_encrypt(dapp_public_key, symkey) #bytes
symkey_val_encr = asymmetric_encrypt(dapp_public_key, symkey) # bytes

symkey_val_encr_hex = symkey_val_encr.hex() #hex
symkey_val_encr_hex = symkey_val_encr.hex() # hex

# arg types: key=bytes32, value=bytes, wallet=wallet
erc721_nft.set_new_data(symkey_name_hash, symkey_val_encr_hex, alice_wallet)
Expand All @@ -123,11 +123,11 @@ erc721_nft.set_new_data(symkey_name_hash, symkey_val_encr_hex, alice_wallet)
```python
from ecies import decrypt as asymmetric_decrypt

#symkey_name_hash = <Dapp would set like above>
# symkey_name_hash = <Dapp would set like above>
symkey_val_encr2 = erc721_nft.get_data(symkey_name_hash)
symkey2 = asymmetric_decrypt(dapp_private_key, symkey_val_encr2)

#profiledata_name_hash = <Dapp would set like above>
# profiledata_name_hash = <Dapp would set like above>
profiledata_val_encr_hex2 = erc721_nft.get_data(profiledata_name_hash)
profiledata_val2_bytes = Fernet(symkey).decrypt(profiledata_val_encr_hex2)
profiledata_val2 = profiledata_val2_bytes.decode('utf-8')
Expand Down
1 change: 0 additions & 1 deletion READMEs/publish-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ asset = ocean.assets.create(

did = asset.did # did contains the datatoken address
print(f"did = '{did}'")

```

Note on asset encryption: In order to encrypt the entire asset, when using a private market or metadata cache, use the encrypt keyword.
Expand Down
2 changes: 1 addition & 1 deletion READMEs/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ In Python, import and configure the components / services:
import os
from ocean_lib.config import Config

#configure the components
# configure the components
config = Config(os.getenv('OCEAN_CONFIG_FILE'))
```

Expand Down
2 changes: 1 addition & 1 deletion ocean_lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

__author__ = """OceanProtocol"""
# fmt: off
__version__ = '1.0.0-alpha.3'
__version__ = '1.0.0-alpha.4'
# fmt: on
Loading

0 comments on commit 83af173

Please sign in to comment.