def buy_asset(): """ Requires all ocean services running. """ ConfigProvider.set_config(ExampleConfig.get_config()) config = ConfigProvider.get_config() # make ocean instance ocn = Ocean() acc = get_publisher_account(config) if not acc: acc = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] # Register ddo ddo = ocn.assets.create(Metadata.get_example(), acc, providers=[acc.address]) logging.info(f'registered ddo: {ddo.did}') # ocn here will be used only to publish the asset. Handling the asset by the publisher # will be performed by the Brizo server running locally keeper = Keeper.get_instance() if 'TEST_LOCAL_NILE' in os.environ and os.environ['TEST_LOCAL_NILE'] == '1': provider = keeper.did_registry.to_checksum_address( '0x413c9ba0a05b8a600899b41b0c62dd661e689354' ) keeper.did_registry.add_provider(ddo.asset_id, provider, acc) logging.debug(f'is did provider: ' f'{keeper.did_registry.is_did_provider(ddo.asset_id, provider)}') cons_ocn = Ocean() consumer_account = get_account_from_config(config, 'parity.address1', 'parity.password1') # sign agreement using the registered asset did above service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS) # This will send the purchase request to Brizo which in turn will execute the agreement on-chain cons_ocn.accounts.request_tokens(consumer_account, 100) sa = ServiceAgreement.from_service_dict(service.as_dictionary()) agreement_id = cons_ocn.assets.order( ddo.did, sa.service_definition_id, consumer_account) logging.info('placed order: %s, %s', ddo.did, agreement_id) i = 0 while ocn.agreements.is_access_granted( agreement_id, ddo.did, consumer_account.address) is not True and i < 30: time.sleep(1) i += 1 assert ocn.agreements.is_access_granted(agreement_id, ddo.did, consumer_account.address) ocn.assets.consume( agreement_id, ddo.did, sa.service_definition_id, consumer_account, config.downloads_path) logging.info('Success buying asset.')
def create_ocean_request(self, query) -> None: ConfigProvider.set_config(Config('../../ocean/config.ini')) # Make a new instance of Ocean ocean = Ocean() # or Ocean(Config('config.ini')) config = ocean.config # make account instance, assuming the ethereum account and password are set # in the config file `config.ini` account = ocean.accounts.list()[0] filename = '../../ocean/bitcoin_2017.csv' did = query['did'] my_data = genfromtxt(filename, delimiter=',') #print('data at', filename, my_data) return my_data #did = 'did:op:71fae96b1d9a4651ba0d3946603fb4c11deb724685f64780985ce32ea2dfe517' service_agreement_id = ocean.assets.order(did, 0, account) # after a short wait (seconds to minutes) the asset data files should be available in the `downloads.path` defined in config # wait a bit to let things happen print(service_agreement_id) time.sleep(20) # Asset files are saved in a folder named after the asset id dataset_dir = os.path.join(ocean.config.downloads_path, f'datafile.{asset_ddo.asset_id}.0') if os.path.exists(dataset_dir): print('asset files downloaded: {}'.format(os.listdir(dataset_dir)))
def search_assets(): ConfigProvider.set_config(ExampleConfig.get_config()) ocn = Ocean() account = get_account(0) ddo = ocn.assets.create( example_metadata.metadata, account, ) sleep(ASYNC_DELAY) logging.info(f'Registered asset: did={ddo.did}, ddo={ddo.as_text()}') resolved_ddo = ocn.assets.resolve(ddo.did) logging.info( f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}' ) ddo_list = ocn.assets.search('bonding curve') logging.info( f'found {len(ddo_list)} assets that contain `bonding curve` in their metadata.' ) ddo_list = ocn.assets.query( {"query": { "text": ['Ocean protocol white paper'] }}) logging.info( f'found {len(ddo_list)} assets with name that contains `Ocean protocol white paper`' )
def register_compute_asset(): # get ocean instance config = ExampleConfig.get_config() ConfigProvider.set_config(config) ocean = Ocean() keeper = Keeper.get_instance() consumer_account = get_account(0) publisher_account = get_account(1) # get descriptor for compute service compute_descriptor = build_compute_descriptor(ocean) # create an asset with compute service ddo = ocean.assets.create(example_metadata.metadata, publisher_account, providers=[config.provider_address], use_secret_store=False, service_descriptors=[compute_descriptor]) event = keeper.did_registry.subscribe_to_event( keeper.did_registry.DID_REGISTRY_EVENT_NAME, 15, event_filter={ '_did': Web3Provider.get_web3().toBytes(hexstr=ddo.asset_id), '_owner': publisher_account.address }, wait=True) assert event, 'There was a problem registering an asset' logging.info(f'Registered asset: did={ddo.did}') # buy an asset access agreement_id = ocean.compute.order(ddo.did, consumer_account) event = keeper.compute_execution_condition.subscribe_condition_fulfilled( agreement_id, 15, None, (), wait=True) assert event, 'There was a problem creating an agreement' logging.info(f'Created asset agreement: agreement_id={agreement_id}') # supply metadata describing the algorithm to run against the dataset # you can also use an algorithm published as an Ocean asset instead algo_meta = AlgorithmMetadata(example_metadata.algo_metadata) # whether to publish the algorithm results as an Ocean assets output_dict = { 'publishOutput': False, 'publishAlgorithmLog': False, } # start the compute job job_id = ocean.compute.start(agreement_id, consumer_account, algorithm_meta=algo_meta, output=output_dict) logging.info(f'Started compute job: job_id={job_id}') # query operator service for job status status = ocean.compute.status(agreement_id, job_id, consumer_account) logging.info(f'Job status: {status}')
def resolve_asset(): ConfigProvider.set_config(ExampleConfig.get_config()) ocn = Ocean() account = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] ddo = ocn.assets.create( Metadata.get_example(), account, ) sleep(ASYNC_DELAY) logging.info(f'Registered asset: did={ddo.did}, ddo={ddo.as_text()}') resolved_ddo = ocn.assets.resolve(ddo.did) logging.info(f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}')
def resolve_asset(): ConfigProvider.set_config(ExampleConfig.get_config()) ocn = Ocean() account = get_account(0) ddo = ocn.assets.create( example_metadata.metadata, account, ) sleep(ASYNC_DELAY) logging.info(f'Registered asset: did={ddo.did}, ddo={ddo.as_text()}') resolved_ddo = ocn.assets.resolve(ddo.did) logging.info(f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}')
def sign_service_agreement(): ConfigProvider.set_config(ExampleConfig.get_config()) # make ocean instance and register an asset ocn = Ocean() acc = get_account(0) ddo = ocn.assets.create(example_metadata.metadata, acc) consumer_account = get_account(1) agreement_id, signature = ocn.agreements.prepare(ddo.did, consumer_account) sleep(ASYNC_DELAY) logging.info(f'service agreement signed: ' f'\nservice agreement id: {agreement_id}, ' f'\nsignature: {signature}')
def register_asset(): # make ocean instance ConfigProvider.set_config(ExampleConfig.get_config()) ocn = Ocean() account = get_account(0) # account = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] ddo = ocn.assets.create( example_metadata.metadata, account, providers=['0xfEF2d5e1670342b9EF22eeeDcb287EC526B48095']) sleep(ASYNC_DELAY) logging.info( f'Registered asset: did={ddo.did}, ddo-services={ddo.services}') resolved_ddo = ocn.assets.resolve(ddo.did) logging.info( f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}' )
def sign_service_agreement(): ConfigProvider.set_config(ExampleConfig.get_config()) config = ConfigProvider.get_config() # make ocean instance and register an asset ocn = Ocean() acc = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] ddo = ocn.assets.create(Metadata.get_example(), acc) consumer_account = get_account_from_config(config, 'parity.address1', 'parity.password1') service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS) agreement_id, signature = ocn.agreements.prepare( ddo.did, service.service_definition_id, consumer_account) sleep(ASYNC_DELAY) logging.info(f'service agreement signed: ' f'\nservice agreement id: {agreement_id}, ' f'\nsignature: {signature}')
def __init__(self, name=None, store=None, options=None): self.name = name if name else conf['slack']['name'] self.ocean = Ocean(Config(filename=conf['ocean']['config'])) self.account = get_default_account(ConfigProvider.get_config()) balance = self.ocean.accounts.balance(self.account) print(balance) self.store = store if store else {} self.options = options if options else \ { 'slack': { 'token': conf['slack']['token.bot'], # bot's user ID in Slack: # value is assigned after the bot starts up 'user_id': None, # 1 second delay between reading from RTM 'rtm_read_delay': 1, }, } self.connections = { 'slack': SlackClient(self.options['slack']['token']), }
def buy_asset(): """ Requires all ocean services running. """ setup_logging(default_level=logging.INFO) ConfigProvider.set_config(ExampleConfig.get_config()) config = ConfigProvider.get_config() providers = { 'duero': '0xfEF2d5e1670342b9EF22eeeDcb287EC526B48095', 'nile': '0x4aaab179035dc57b35e2ce066919048686f82972' } # make ocean instance ocn = Ocean() acc = get_account(1) if not acc: acc = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] Diagnostics.verify_contracts() metadata = example_metadata.metadata.copy() metadata['main']['dateCreated'] = get_timestamp() keeper = Keeper.get_instance() # Register ddo did = '' if did: ddo = ocn.assets.resolve(did) logging.info(f'using ddo: {did}') else: ddo = ocn.assets.create(metadata, acc, providers=[], use_secret_store=True) assert ddo is not None, f'Registering asset on-chain failed.' did = ddo.did logging.info(f'registered ddo: {did}') # ocn here will be used only to publish the asset. Handling the asset by the publisher # will be performed by the Brizo server running locally test_net = os.environ.get('TEST_NET', '') if test_net.startswith('nile'): provider = keeper.did_registry.to_checksum_address(providers['nile']) elif test_net.startswith('duero'): provider = keeper.did_registry.to_checksum_address(providers['duero']) else: provider = '0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0' # Wait for did registry event event = keeper.did_registry.subscribe_to_event( keeper.did_registry.DID_REGISTRY_EVENT_NAME, 30, event_filter={ '_did': Web3Provider.get_web3().toBytes(hexstr=ddo.asset_id), '_owner': acc.address}, wait=True ) if not event: logging.warning(f'Failed to get the did registry event for asset with did {did}.') assert keeper.did_registry.get_block_number_updated(ddo.asset_id) > 0, \ f'There is an issue in registering asset {did} on-chain.' keeper.did_registry.add_provider(ddo.asset_id, provider, acc) logging.info(f'is {provider} set as did provider: ' f'{keeper.did_registry.is_did_provider(ddo.asset_id, provider)}') _providers = keeper.did_registry.get_did_providers(ddo.asset_id) access_template_name = keeper.template_manager.SERVICE_TO_TEMPLATE_NAME['access'] template_id = keeper.template_manager.create_template_id(access_template_name) approved = keeper.template_manager.is_template_approved(template_id) print(f'agreement template approved: {approved}') template_data = keeper.template_manager.get_template(template_id) print(f'access agreement template: {template_data}') cons_ocn = Ocean() consumer_account = get_account(0) # sign agreement using the registered asset did above service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS) # This will send the order request to Brizo which in turn will execute the agreement on-chain cons_ocn.accounts.request_tokens(consumer_account, 10) sa = ServiceAgreement.from_json(service.as_dictionary()) agreement_id = '' if not agreement_id: agreement_id = cons_ocn.assets.order( did, sa.index, consumer_account) logging.info('placed order: %s, %s', did, agreement_id) event = keeper.agreement_manager.subscribe_agreement_created( agreement_id, 60, None, (), wait=True ) assert event, "Agreement event is not found, check the keeper node's logs" logging.info(f'Got agreement event, next: lock reward condition') event = keeper.lock_reward_condition.subscribe_condition_fulfilled( agreement_id, 60, None, (), wait=True ) assert event, "Lock reward condition fulfilled event is not found, check the keeper node's logs" logging.info('Got lock reward event, next: wait for the access condition..') event = keeper.access_secret_store_condition.subscribe_condition_fulfilled( agreement_id, 15, None, (), wait=True ) logging.info(f'Got access event {event}') i = 0 while ocn.agreements.is_access_granted( agreement_id, did, consumer_account.address) is not True and i < 30: time.sleep(1) i += 1 assert ocn.agreements.is_access_granted(agreement_id, did, consumer_account.address) ocn.assets.consume( agreement_id, did, sa.index, consumer_account, config.downloads_path) logging.info('Success buying asset.') event = keeper.escrow_reward_condition.subscribe_condition_fulfilled( agreement_id, 30, None, (), wait=True ) assert event, 'no event for EscrowReward.Fulfilled' logging.info(f'got EscrowReward.FULFILLED event: {event}') logging.info('Done buy asset.')
import os import time from squid_py import ( Ocean, ConfigProvider, Config ) ConfigProvider.set_config(Config('config.ini')) # Make a new instance of Ocean ocean = Ocean() # or Ocean(Config('config.ini')) config = ocean.config # make account instance, assuming the ethereum account and password are set # in the config file `config.ini` account = ocean.accounts.list()[0] print('account', account) did = 'did:op:71fae96b1d9a4651ba0d3946603fb4c11deb724685f64780985ce32ea2dfe517' service_agreement_id = ocean.assets.order(did, 0, account) # after a short wait (seconds to minutes) the asset data files should be available in the `downloads.path` defined in config # wait a bit to let things happen print(service_agreement_id) time.sleep(20) # Asset files are saved in a folder named after the asset id dataset_dir = os.path.join(ocean.config.downloads_path, f'datafile.{asset_ddo.asset_id}.0') if os.path.exists(dataset_dir):
from src import default_config_dict from src.account import get_account from src.metadata import get_metadata FLASK_APPLICATION_NAME = 'ocean-protocol' TRANSACTION_RULE = '/transaction' config_dict = default_config_dict # ? use this dict? Do we want to input more configurations? If so, how and why? configuration = Config(config_dict=config_dict) squid_py.ConfigProvider.set_config(configuration) app = flask.Flask(FLASK_APPLICATION_NAME) CORS(app) ocn = Ocean(configuration) keeper = Keeper.get_instance() MARKET_PLACE_PROVIDER_ADDRESS = Web3.toChecksumAddress( configuration.provider_address) @app.route(TRANSACTION_RULE, methods=['POST']) def process_transaction(): """Route for making a transaction between seller and buyer Request JSON: { "publisher": { "address": <address point>, "private_key": <private key>
# Asset files are saved in a folder named after the asset id def find_csv(asset_id): path = os.path.join(ocean.config.downloads_path, f'datafile.{asset_id}.0') csv_files = [f for f in os.listdir(path) if f.endswith('.csv')] return os.path.join(path, csv_files[0]) if csv_files else None app = Flask(__name__) CORS(app) ConfigProvider.set_config(demo_config) # ocean = Ocean() ocean = Ocean( Config('config_local.ini') ) # use local or regular config for production pacific network usage. config = ocean.config accounts = ocean.accounts.list() if accounts: account = accounts[0] # take first account as primary source. @app.route('/', methods=['GET']) def hello(): return "Welcome to Oceanapi" # describe the data available in an existing dataset @app.route('/describe', methods=['GET']) def describe():
from squid_py import ( Ocean, ConfigProvider, Config, ) from ocean_keeper.account import Account from ocean_utils.ddo.metadata import Metadata with open('config.ini', 'r') as fp: configs = json.load(fp) ConfigProvider.set_config(Config(options_dict=configs)) # Make a new instance of Ocean ocean = Ocean(Config(options_dict=configs)) config = ocean.config # make account instance, assuming the ethereum account and password are set # in the config file `config.ini` # account = ocean.accounts.list()[0] # or account = Account(config.parity_address, config.parity_password) # PUBLISHER # Let's start by registering an asset in the Ocean network metadata = Metadata.get_example() # consume and service endpoints require `brizo.url` is set in the config file # or passed to Ocean instance in the config_dict. # define the services to include in the new asset DDO
def __init__(self, path_to_config): ConfigProvider.set_config(Config(path_to_config)) Ocean.__init__(self)