2023-05-29 15:58:59 +02:00
---
description: Technical details about most used ocean.py functions
---
2023-06-08 12:19:46 +02:00
# Ocean Instance Tech Details
2023-05-29 14:47:32 +02:00
2023-06-29 17:52:01 +02:00
At the beginning of most flows, we create an `ocean` object, which is an instance of class [`Ocean` ](https://github.com/oceanprotocol/ocean.py/blob/main/ocean_lib/ocean/ocean.py ). It exposes useful information, including the following:
2023-06-13 14:13:08 +02:00
2023-10-19 14:54:19 +02:00
* properties for config & OCEAN
2023-06-13 14:13:08 +02:00
* contract objects retrieval
* users' orders
* provider fees
2023-05-29 15:58:59 +02:00
2023-06-07 15:09:44 +02:00
### Constructor
2023-05-29 15:58:59 +02:00
2023-06-06 15:29:23 +02:00
* **\_\_init\_\_**(`self`, `config_dict: Dict` , `data_provider: Optional[Type] = None` )
2023-05-29 15:58:59 +02:00
2023-06-06 15:29:23 +02:00
The Ocean class is the entry point into Ocean Procol.
2023-05-29 15:58:59 +02:00
In order to initialize a Ocean object, you must provide `config_dict` which is a `Dictionary` instance and optionally a `DataServiceProvider` instance.
2023-06-06 15:29:23 +02:00
**Parameters**
2023-06-07 10:49:43 +02:00
* `config_dict` : `dict` which is mandatory and it contains the configuration as dictionary format.
* `data_provider` : `Optional[DataProvider]` which is optional with a default value of None. If it is not provided, the constructor will instantiate a new one from scratch.
2023-06-06 15:29:23 +02:00
**Returns**
2023-06-06 15:55:41 +02:00
`None`
2023-06-06 15:29:23 +02:00
**Defined in**
2023-06-29 17:52:01 +02:00
[ocean/ocean.py ](https://github.com/oceanprotocol/ocean.py/blob/main/ocean_lib/ocean/ocean.py#L43 )
2023-06-06 15:29:23 +02:00
< details >
< summary > Source code< / summary >
2023-05-29 15:58:59 +02:00
{% code overflow="wrap" %}
```python
class Ocean:
"""The Ocean class is the entry point into Ocean Protocol."""
@enforce_types
def __init__ (self, config_dict: Dict, data_provider: Optional[Type] = None) -> None:
"""Initialize Ocean class.
Usage: Make a new Ocean instance
`ocean = Ocean({...})`
This class provides the main top-level functions in ocean protocol:
1. Publish assets metadata and associated services
- Each asset is assigned a unique DID and a DID Document (DDO)
- The DDO contains the asset's services including the metadata
- The DID is registered on-chain with a URL of the metadata store
to retrieve the DDO from
`ddo = ocean.assets.create(metadata, publisher_wallet)`
2. Discover/Search ddos via the current configured metadata store (Aquarius)
- Usage:
`ddos_list = ocean.assets.search('search text')`
An instance of Ocean is parameterized by a `Config` instance.
:param config_dict: variable definitions
:param data_provider: `DataServiceProvider` instance
"""
config_errors = {}
for key, value in config_defaults.items():
if key not in config_dict:
config_errors[key] = "required"
continue
if not isinstance(config_dict[key], type(value)):
config_errors[key] = f"must be {type(value).__name__}"
if config_errors:
raise Exception(json.dumps(config_errors))
self.config_dict = config_dict
network_name = config_dict["NETWORK_NAME"]
check_network(network_name)
if not data_provider:
data_provider = DataServiceProvider
self.assets = OceanAssets(self.config_dict, data_provider)
self.compute = OceanCompute(self.config_dict, data_provider)
logger.debug("Ocean instance initialized: ")
```
{% endcode %}
< / details >
2023-06-07 15:09:44 +02:00
### Config Getter
2023-05-29 15:58:59 +02:00
2023-06-07 15:09:44 +02:00
* **config**(`self`) -> `dict`
2023-05-29 15:58:59 +02:00
It is a helper method for retrieving the user's configuration for ocean.py.\
It can be called only by Ocean object and returns a python dictionary.
2023-06-06 15:29:23 +02:00
**Returns**
`dict`
Configuration fields as dictionary.
**Defined in**
2023-06-29 17:52:01 +02:00
[ocean/ocean.py ](https://github.com/oceanprotocol/ocean.py/blob/main/ocean_lib/ocean/ocean.py#LL265C1-L268C32 )
2023-06-06 15:29:23 +02:00
< details >
< summary > Source code< / summary >
2023-05-29 15:58:59 +02:00
```python
2023-06-06 15:29:23 +02:00
@property
2023-05-29 15:58:59 +02:00
@enforce_types
def config(self) -> dict: # alias for config_dict
return self.config_dict
```
< / details >
2023-10-19 14:54:19 +02:00
### OCEAN Address
2023-05-29 15:58:59 +02:00
2023-06-29 17:52:01 +02:00
* **ocean_address**(`self`) -> `str`
2023-05-29 15:58:59 +02:00
It is a helper method for retrieving the OCEAN's token address.\
It can be called only by Ocean object and returns the address as a `string` .
2023-06-07 15:09:44 +02:00
**Returns**
`str`
2023-10-19 14:54:19 +02:00
OCEAN address for that network.
2023-06-07 15:09:44 +02:00
**Defined in**
2023-06-29 17:52:01 +02:00
[ocean/ocean.py ](https://github.com/oceanprotocol/ocean.py/blob/main/ocean_lib/ocean/ocean.py#LL100C1-L103C52 )
2023-06-07 15:09:44 +02:00
< details >
< summary > Source code< / summary >
2023-05-29 15:58:59 +02:00
```python
@property
@enforce_types
def OCEAN_address(self) -> str:
return get_ocean_token_address(self.config)
```
2023-06-29 17:52:01 +02:00
[`get_ocean_token_address` ](https://github.com/oceanprotocol/ocean.py/blob/main/ocean_lib/ocean/util.py#LL31C1-L38C89 ) function is an utilitary function which gets the address from `address.json` file
2023-05-29 15:58:59 +02:00
{% code overflow="wrap" %}
```python
@enforce_types
def get_ocean_token_address(config_dict: dict) -> str:
2023-10-19 14:54:19 +02:00
"""Returns the OCEAN address for given network or web3 instance
2023-05-29 15:58:59 +02:00
Requires either network name or web3 instance.
"""
addresses = get_contracts_addresses(config_dict)
return Web3.toChecksumAddress(addresses.get("Ocean").lower()) if addresses else None
```
{% endcode %}
< / details >
2023-06-07 15:09:44 +02:00
### OCEAN Token Object
2023-05-29 15:58:59 +02:00
2023-06-29 17:52:01 +02:00
* **ocean_token**(`self`) -> `DatatokenBase`
2023-06-07 15:09:44 +02:00
* **OCEAN**(`self`) -> `DatatokenBase` as alias for the above option
2023-05-29 15:58:59 +02:00
It is a helper method for retrieving the OCEAN token object (Datatoken class).\
It can be called within Ocean class and returns the OCEAN Datatoken.
2023-06-07 15:09:44 +02:00
**Returns**
`DatatokenBase`
OCEAN token as `DatatokenBase` object.
**Defined in**
2023-06-29 17:52:01 +02:00
[ocean/ocean.py ](https://github.com/oceanprotocol/ocean.py/blob/main/ocean_lib/ocean/ocean.py#LL105C1-L113C32 )
2023-06-07 15:09:44 +02:00
< details >
< summary > Source code< / summary >
2023-05-29 15:58:59 +02:00
```python
@property
@enforce_types
def OCEAN_token(self) -> DatatokenBase:
return DatatokenBase.get_typed(self.config, self.OCEAN_address)
@property
@enforce_types
def OCEAN(self): # alias for OCEAN_token
return self.OCEAN_token
```
< / details >
2023-06-07 15:09:44 +02:00
### Data NFT Factory
2023-05-29 15:58:59 +02:00
2023-06-07 15:09:44 +02:00
* **data\_nft\_factory**(`self`) -> `DataNFTFactoryContract`
2023-05-29 17:04:27 +02:00
It is a property for getting `Data NFT Factory` object for the singleton smart contract.\
It can be called within Ocean class and returns the `DataNFTFactoryContract` instance.
2023-06-07 15:09:44 +02:00
**Returns**
`DataNFTFactoryContract`
Data NFT Factory contract object which access all the functionalities available from smart contracts in Python.
**Defined in**
2023-06-29 17:52:01 +02:00
[ocean/ocean.py ](https://github.com/oceanprotocol/ocean.py/blob/main/ocean_lib/ocean/ocean.py#LL117C1-L120C80 )
2023-06-07 15:09:44 +02:00
< details >
< summary > Source code< / summary >
2023-05-29 17:04:27 +02:00
{% code overflow="wrap" %}
```python
@property
@enforce_types
def data_nft_factory(self) -> DataNFTFactoryContract:
return DataNFTFactoryContract(self.config, self._addr("ERC721Factory"))
```
{% endcode %}
< / details >
2023-06-07 15:09:44 +02:00
### Dispenser
2023-05-29 17:04:27 +02:00
2023-06-07 15:09:44 +02:00
* **dispenser**(`self`) -> `Dispenser`
2023-05-29 17:04:27 +02:00
`Dispenser` is represented by a faucet for free data.\
It is a property for getting `Dispenser` object for the singleton smart contract.\
It can be called within Ocean class and returns the `Dispenser` instance.
2023-06-07 15:09:44 +02:00
**Returns**
`Dispenser`
Dispenser contract object which access all the functionalities available from smart contracts in Python.
**Defined in**
2023-06-29 17:52:01 +02:00
[ocean/ocean.py ](https://github.com/oceanprotocol/ocean.py/blob/main/ocean_lib/ocean/ocean.py#LL122C1-L125C63 )
2023-06-07 15:09:44 +02:00
< details >
< summary > Source code< / summary >
2023-05-29 17:04:27 +02:00
```python
@property
@enforce_types
def dispenser(self) -> Dispenser:
return Dispenser(self.config, self._addr("Dispenser"))
```
< / details >
2023-06-07 15:09:44 +02:00
### Fixed Rate Exchange
2023-05-29 17:04:27 +02:00
2023-06-07 15:09:44 +02:00
* **fixed\_rate\_exchange**(`self`) -> `FixedRateExchange`
2023-05-29 17:04:27 +02:00
Exchange is used for priced data.\
It is a property for getting `FixedRateExchange` object for the singleton smart contract.\
It can be called within Ocean class and returns the `FixedRateExchange` instance.
2023-06-07 15:09:44 +02:00
**Returns**
`FixedRateExchange`
Fixed Rate Exchange contract object which access all the functionalities available from smart contracts in Python.
**Defined in**
2023-06-29 17:52:01 +02:00
[ocean/ocean.py ](https://github.com/oceanprotocol/ocean.py/blob/main/ocean_lib/ocean/ocean.py#LL127C1-L130C72 )
2023-06-07 15:09:44 +02:00
< details >
< summary > Source code< / summary >
2023-05-29 17:04:27 +02:00
```python
@property
@enforce_types
def fixed_rate_exchange(self) -> FixedRateExchange:
return FixedRateExchange(self.config, self._addr("FixedPrice"))
```
< / details >
2023-05-29 15:58:59 +02:00
2023-06-07 15:09:44 +02:00
### NFT Token Getter
2023-05-29 17:04:27 +02:00
2023-06-07 15:09:44 +02:00
* **get\_nft\_token**(`self`, `token_adress: str` ) -> `DataNFT`
2023-05-29 17:04:27 +02:00
It is a getter for a specific data NFT object based on its checksumed address.\
2023-06-07 15:09:44 +02:00
It can be called within Ocean class which returns the `DataNFT` instance based on string `token_address` specified as parameter.
**Parameters**
* `token_address` - string checksumed address of the NFT token that you are searching for.
**Returns**
`DataNFT`
Data NFT object which access all the functionalities available for ERC721 template in Python.
**Defined in**
2023-06-29 17:52:01 +02:00
[ocean/ocean.py ](https://github.com/oceanprotocol/ocean.py/blob/main/ocean_lib/ocean/ocean.py#LL139C5-L145C51 )
2023-06-07 15:09:44 +02:00
< details >
< summary > Source code< / summary >
2023-05-29 17:04:27 +02:00
```python
@enforce_types
def get_nft_token(self, token_address: str) -> DataNFT:
"""
:param token_address: Token contract address, str
:return: `DataNFT` instance
"""
return DataNFT(self.config, token_address)
```
< / details >
2023-06-07 15:09:44 +02:00
### Datatoken Getter
2023-05-29 17:04:27 +02:00
2023-06-07 15:09:44 +02:00
* **get\_datatoken**(`self`, `token_address: str` ) -> `DatatokenBase`
2023-05-29 17:04:27 +02:00
It is a getter for a specific `datatoken` object based on its checksumed address.\
It can be called within Ocean class with a string `token_address` as parameter which returns the `DatatokenBase` instance depending on datatoken's template index.
2023-06-07 15:09:44 +02:00
**Parameters**
* `token_address` - string checksumed address of the datatoken that you are searching for.
**Returns**
`DatatokenBase`
Datatoken object which access all the functionalities available for ERC20 templates in Python.
**Defined in**
2023-06-29 17:52:01 +02:00
[ocean/ocean.py ](https://github.com/oceanprotocol/ocean.py/blob/main/ocean_lib/ocean/ocean.py#LL147C5-L153C67 )
2023-06-07 15:09:44 +02:00
< details >
< summary > Source code< / summary >
2023-05-29 17:04:27 +02:00
```python
@enforce_types
def get_datatoken(self, token_address: str) -> DatatokenBase:
"""
:param token_address: Token contract address, str
:return: `Datatoken1` or `Datatoken2` instance
"""
return DatatokenBase.get_typed(self.config, token_address)
```
< / details >
2023-06-07 15:09:44 +02:00
### User Orders Getter
2023-05-29 17:04:27 +02:00
2023-06-07 15:09:44 +02:00
* **get\_user\_orders**(`self`, `address: str` , `datatoken: str` ) -> `List[AttributeDict]`
2023-05-29 17:04:27 +02:00
Returns the list of orders that were made by a certain user on a specific datatoken.
2023-06-07 15:09:44 +02:00
It can be called within Ocean class.
2023-05-29 17:04:27 +02:00
2023-06-07 15:09:44 +02:00
**Parameters**
2023-05-29 17:04:27 +02:00
2023-06-07 15:09:44 +02:00
* `address` - wallet address of that user
* `datatoken` - datatoken address
**Returns**
`List[AttributeDict]`
List of all the orders on that `datatoken` done by the specified `user` .
**Defined in**
2023-06-29 17:52:01 +02:00
[ocean/ocean.py ](https://github.com/oceanprotocol/ocean.py/blob/main/ocean_lib/ocean/ocean.py#LL157C5-L173C23 )
2023-06-07 15:09:44 +02:00
< details >
< summary > Source code< / summary >
2023-05-29 17:04:27 +02:00
{% code overflow="wrap" %}
```python
@enforce_types
def get_user_orders(self, address: str, datatoken: str) -> List[AttributeDict]:
"""
:return: List of orders `[Order]`
"""
dt = DatatokenBase.get_typed(self.config_dict, datatoken)
_orders = []
for log in dt.get_start_order_logs(address):
a = dict(log.args.items())
a["amount"] = int(log.args.amount)
a["address"] = log.address
a["transactionHash"] = log.transactionHash
a = AttributeDict(a.items())
_orders.append(a)
return _orders
```
{% endcode %}
< / details >
2023-06-07 15:09:44 +02:00
### Provider Fees
2023-05-29 17:04:27 +02:00
2023-06-07 15:09:44 +02:00
* **retrieve\_provider\_fees**( `self` , `ddo: DDO` , `access_service: Service` , `publisher_wallet` ) -> `dict`
2023-05-29 17:04:27 +02:00
Calls Provider to compute provider fees as dictionary for access service.
2023-06-07 15:09:44 +02:00
**Parameters**
* `ddo` - the data asset which has the DDO object
* `access_service` - Service instance for the service that needs the provider fees
* `publisher_wallet` - Wallet instance of the user that wants to retrieve the provider fees
**Returns**
`dict`
A dictionary which contains the following keys (`providerFeeAddress`, `providerFeeToken` , `providerFeeAmount` , `providerData` , `v` , `r` , `s` , `validUntil` ).
**Defined in**
2023-06-29 17:52:01 +02:00
[ocean/ocean.py ](https://github.com/oceanprotocol/ocean.py/blob/main/ocean_lib/ocean/ocean.py#LL177C4-L189C1 )
2023-05-29 17:04:27 +02:00
2023-06-07 15:09:44 +02:00
< details >
< summary > Source code< / summary >
2023-05-29 17:04:27 +02:00
{% code overflow="wrap" %}
```python
@enforce_types
def retrieve_provider_fees(
self, ddo: DDO, access_service: Service, publisher_wallet
) -> dict:
initialize_response = DataServiceProvider.initialize(
ddo.did, access_service, consumer_address=publisher_wallet.address
)
initialize_data = initialize_response.json()
provider_fees = initialize_data["providerFee"]
return provider_fees
```
{% endcode %}
< / details >
2023-06-07 15:09:44 +02:00
### Compute Provider Fees
2023-05-29 17:04:27 +02:00
2023-06-07 15:09:44 +02:00
* **retrieve\_provider\_fees\_for\_compute**(`self`, `datasets: List[ComputeInput]` , `algorithm_data: Union[ComputeInput, AlgorithmMetadata]` , `consumer_address: str` , `compute_environment: str` , `valid_until: int` ) -> `dict`
2023-05-29 17:04:27 +02:00
Calls Provider to generate provider fees as dictionary for compute service.
2023-06-07 15:09:44 +02:00
**Parameters**
2023-05-29 17:04:27 +02:00
2023-06-07 15:09:44 +02:00
* `datasets` - list of `ComputeInput` which contains the data assets
* `algorithm_data` - necessary data for algorithm and it can be either a `ComputeInput` object, either just the algorithm metadata, `AlgorithmMetadata`
* `consumer_address` - address of the compute consumer wallet which is requesting the provider fees
* `compute_environment` - id provided from the compute environment as `string`
* `valid_until` - timestamp in UNIX miliseconds for the duration of provider fees for the compute service.
**Returns**
`dict`
A dictionary which contains the following keys (`providerFeeAddress`, `providerFeeToken` , `providerFeeAmount` , `providerData` , `v` , `r` , `s` , `validUntil` ).
**Defined in**
2023-06-29 17:52:01 +02:00
[ocean/ocean.py ](https://github.com/oceanprotocol/ocean.py/blob/main/ocean_lib/ocean/ocean.py#LL190C4-L210C1 )
2023-06-07 15:09:44 +02:00
< details >
< summary > Source code< / summary >
2023-05-29 17:04:27 +02:00
{% code overflow="wrap" %}
```python
@enforce_types
def retrieve_provider_fees_for_compute(
self,
datasets: List[ComputeInput],
algorithm_data: Union[ComputeInput, AlgorithmMetadata],
consumer_address: str,
compute_environment: str,
valid_until: int,
) -> dict:
initialize_compute_response = DataServiceProvider.initialize_compute(
[x.as_dictionary() for x in datasets],
algorithm_data.as_dictionary(),
datasets[0].service.service_endpoint,
consumer_address,
compute_environment,
valid_until,
)
return initialize_compute_response.json()
```
{% endcode %}
< / details >