LogoLogo
WebsitePredictoorData ChallengesData FarmingOcean.pyOcean.js
  • 👋Ocean docs
  • 🌊Discover Ocean
    • Why Ocean?
    • What is Ocean?
    • What can you do with Ocean?
    • OCEAN: The Ocean token
    • Networks
    • Network Bridges
    • FAQ
    • Glossary
  • 📚User Guides
    • Basic concepts
    • Using Wallets
      • Set Up MetaMask
    • Host Assets
      • Uploader
      • Arweave
      • AWS
      • Azure Cloud
      • Google Storage
      • Github
    • Liquidity Pools [deprecated]
  • 💻Developers
    • Architecture Overview
    • Ocean Nodes
      • Node Architecture
    • Contracts
      • Data NFTs
      • Datatokens
      • Data NFTs and Datatokens
      • Datatoken Templates
      • Roles
      • Pricing Schemas
      • Fees
    • Publish Flow Overview
    • Revenue
    • Fractional Ownership
    • Community Monetization
    • Metadata
    • Identifiers (DIDs)
    • New DDO Specification
    • Obsolete DDO Specification
    • Storage Specifications
    • Fine-Grained Permissions
    • Retrieve datatoken/data NFT addresses & Chain ID
    • Get API Keys for Blockchain Access
    • Barge
      • Local Setup
    • Ocean.js
      • Configuration
      • Creating a data NFT
      • Publish
      • Mint Datatokens
      • Update Metadata
      • Asset Visibility
      • Consume Asset
      • Run C2D Jobs
    • Ocean CLI
      • Install
      • Publish
      • Edit
      • Consume
      • Run C2D Jobs
    • DDO.js
      • Instantiate a DDO
      • DDO Fields interactions
      • Validate
      • Edit DDO Fields
    • Compute to data
    • Compute to data
    • Uploader
      • Uploader.js
      • Uploader UI
      • Uploader UI to Market
    • VSCode Extension
    • Old Infrastructure
      • Aquarius
        • Asset Requests
        • Chain Requests
        • Other Requests
      • Provider
        • General Endpoints
        • Encryption / Decryption
        • Compute Endpoints
        • Authentication Endpoints
      • Subgraph
        • Get data NFTs
        • Get data NFT information
        • Get datatokens
        • Get datatoken information
        • Get datatoken buyers
        • Get fixed-rate exchanges
        • Get veOCEAN stats
    • Developer FAQ
  • 📊Data Scientists
    • Ocean.py
      • Install
      • Local Setup
      • Remote Setup
      • Publish Flow
      • Consume Flow
      • Compute Flow
      • Ocean Instance Tech Details
      • Ocean Assets Tech Details
      • Ocean Compute Tech Details
      • Datatoken Interface Tech Details
    • Join a Data Challenge
    • Sponsor a Data Challenge
    • Data Value-Creation Loop
    • What data is valuable?
  • 👀Predictoor
  • 💰Data Farming
    • Predictoor DF
      • Guide to Predictoor DF
    • FAQ
  • 🔨Infrastructure
    • Set Up a Server
    • Deploy Aquarius
    • Deploy Provider
    • Deploy Ocean Subgraph
    • Deploy C2D
    • For C2D, Set Up Private Docker Registry
  • 🤝Contribute
    • Collaborators
    • Contributor Code of Conduct
    • Legal Requirements
Powered by GitBook
LogoLogo

Ocean Protocol

  • Website
  • Blog
  • Data Challenges

Community

  • Twitter
  • Discord
  • Telegram
  • Instagram

Resources

  • Whitepaper
  • GitHub
  • Docs

Copyright 2024 Ocean Protocol Foundation Ltd.

On this page

Was this helpful?

Edit on GitHub
Export as PDF
  1. Developers
  2. Ocean.js

Consume Asset

Last updated 1 year ago

Was this helpful?

Consuming an asset involves a two-step process: placing an order and then utilizing the order transaction to download and access the asset's files. Let's delve into each step in more detail.

To initiate the ordering process, there are two scenarios depending on the pricing schema of the asset. Firstly, if the asset has a fixed-rate pricing schema configured, you would need to acquire the corresponding datatoken by purchasing it. Once you have obtained the datatoken, you send it to the publisher to place the order for the asset.

The second scenario applies when the asset follows a free pricing schema. In this case, you can obtain a free datatoken from the dispenser service provided by Ocean Protocol. Using the acquired free datatoken, you can place the order for the desired asset.

However, it's crucial to note that even when utilizing free assets, network gas fees still apply. These fees cover the costs associated with executing transactions on the blockchain network.

Additionally, the specific type of datatoken associated with an asset influences the ordering process. There are two common datatoken templates: Template 1 (regular template) and Template 2 (enterprise template). The type of template determines the sequence of method calls required before placing an order.

For assets utilizing Template '1', prior to ordering, you need to perform two separate method calls. First, you need to call the approve method to grant permission for the fixedRateExchange contract to spend the required amount of datatokens. Then, you proceed to call the buyDatatokens method from the fixedRateExchange contract. This process ensures that you have the necessary datatokens in your possession to successfully place the order. Alternatively, if the asset follows a free pricing schema, you can employ the dispenser.dispense method to obtain the free datatoken before proceeding with the order.

On the other hand, assets utilizing Template '2' offer bundled methods for a more streamlined approach. For ordering such assets, you can use methods like buyFromFreeAndOrder or buyFromDispenserAndOrder. These bundled methods handle the acquisition of the necessary datatokens and the subsequent ordering process in a single step, simplifying the workflow for enterprise-template assets.

Later on, when working with the ocean.js library, you can use this order transaction identifier to call the getDownloadUrl method from the provider service class. This method allows you to retrieve the download URL for accessing the asset's files.

Prerequisites

The variables AQUARIUS_URL and PROVIDER_URL should be set correctly in .env file

Create a script to consume an asset

Create a new file in the same working directory where the configuration file (config.js) and .env files are present, and copy the code as listed below.

// Note: Make sure .env file and config.js are created and setup correctly
const { oceanConfig } = require("./config.js");
const {
	ZERO_ADDRESS,
	NftFactory,
	getHash,
	ProviderFees,
	Datatoken,
	ProviderInstance,
	Nft,
	FixedRateExchange,
  approve
} = require("@oceanprotocol/lib");

// replace the did here
const did = "did:op:a419f07306d71f3357f8df74807d5d12bddd6bcd738eb0b461470c64859d6f0f";

// This function takes did as a parameter and updates the data NFT information
const consumeAsset = async (did) => {
	const consumer = await oceanConfig.consumerAccount.getAddress();

	// Fetch ddo from Aquarius
	const asset = await await oceanConfig.aquarius.resolve(did);

	const nft = new Nft(consumer);

	await approve(
		Error,
		oceanConfig,
		await consumer.getAddress(),
		oceanConfig.Ocean,
		oceanConfig.fixedRateExchangeAddress,
		"1"
	);

	const fixedRate = new FixedRateExchange(
		oceanConfig.fixedRateExchangeAddress,
		consumer
	);

	const txBuyDt = await fixedRate.buyDatatokens(
		oceanConfig.fixedRateId,
		"1",
		"2"
	);

	const initializeData = await ProviderInstance.initialize(
		asset.id,
		asset.services[0].id,
		0,
		await consumer.getAddress(),
		oceanConfig.providerUri
	);

	const providerFees: ProviderFees = {
		providerFeeAddress: initializeData.providerFee.providerFeeAddress,
		providerFeeToken: initializeData.providerFee.providerFeeToken,
		providerFeeAmount: initializeData.providerFee.providerFeeAmount,
		v: initializeData.providerFee.v,
		r: initializeData.providerFee.r,
		s: initializeData.providerFee.s,
		providerData: initializeData.providerFee.providerData,
		validUntil: initializeData.providerFee.validUntil,
	};

	const datatoken = new Datatoken(consumer);

	const tx = await datatoken.startOrder(
		oceanConfig.fixedRateExchangeAddress,
		await consumer.getAddress(),
		0,
		providerFees
	);

	const orderTx = await tx.wait();
	const orderStartedTx = getEventFromTx(orderTx, "OrderStarted");

	const downloadURL = await ProviderInstance.getDownloadUrl(
		asset.id,
		asset.services[0].id,
		0,
		orderTx.transactionHash,
		oceanConfig.providerUri,
		consumer
	);
};

// Call setMetadata(...) function defined above
consumeAsset(did).then(() => {
  process.exit();
}).catch((err) => {
  console.error(err);
  process.exit(1);
});
💻
Obtain an API key
Set up the .env file
Install the dependencies
Create a configuration file