From 012f7ccb241ab69b34042aa7225625462ef13108 Mon Sep 17 00:00:00 2001 From: Akshay Patel Date: Tue, 2 Aug 2022 12:52:51 +0000 Subject: [PATCH] GitBook: [#12] Add comments in code snippets --- SUMMARY.md | 4 +- .../using-ocean-libraries/configuration.md | 8 +-- .../create-datatoken-with-fixed-pricing.md | 50 ++++++++++++++++--- .../using-ocean-libraries/mint-datatoken.md | 9 ++++ .../using-ocean-libraries/update-metadata.md | 15 +++++- 5 files changed, 71 insertions(+), 15 deletions(-) diff --git a/SUMMARY.md b/SUMMARY.md index 3cbaf3e8..6227807c 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -24,8 +24,8 @@ * [Obtaining Ethereum node provider API key](building-with-ocean/obtaining-api-key.md) * [Using Ocean libraries](building-with-ocean/using-ocean-libraries/README.md) * [Configuration](building-with-ocean/using-ocean-libraries/configuration.md) - * [Creating a dataNFT](building-with-ocean/using-ocean-libraries/creating\_dataNFT.md) - * [Create Datatoken with fixed pricing](building-with-ocean/using-ocean-libraries/create-datatoken-with-fixed-pricing.md) + * [Creating a data NFT](building-with-ocean/using-ocean-libraries/creating\_dataNFT.md) + * [Create datatoken with fixed pricing](building-with-ocean/using-ocean-libraries/create-datatoken-with-fixed-pricing.md) * [Mint datatoken](building-with-ocean/using-ocean-libraries/mint-datatoken.md) * [Update metadata](building-with-ocean/using-ocean-libraries/update-metadata.md) * [Publish assets using hosting services](building-with-ocean/asset-hosting.md) diff --git a/building-with-ocean/using-ocean-libraries/configuration.md b/building-with-ocean/using-ocean-libraries/configuration.md index d4ee8ba9..f5ebc8fd 100644 --- a/building-with-ocean/using-ocean-libraries/configuration.md +++ b/building-with-ocean/using-ocean-libraries/configuration.md @@ -15,7 +15,7 @@ cd my-ocean-project ### Create a `.env` file -In the working directory create a \`.env\` file. The content of this file will store the values for following variables: +In the working directory create a `.env` file. The content of this file will store the values for following variables: | Variable name | Description | Required | | ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | @@ -29,7 +29,7 @@ In the working directory create a \`.env\` file. The content of this file will s Treat this file as a secret and do not commit this file to git or share the content publicly. {% endhint %} -The below tabs show partially filled \`.env\` file content for some of the supported networks. +The below tabs show partially filled `.env` file content for some of the supported networks. {% tabs %} {% tab title="Mainnet" %} @@ -110,9 +110,9 @@ pip3 install ocean-lib python-dotenv web3 ### Create a configuration file -A configuration file will read the content of the \`.env\` file and initialize the required configuration objects which will be used in the further tutorials. The below scripts creates a Web3 wallet instance and a Ocean's configuration object. +A configuration file will read the content of the `.env` file and initialize the required configuration objects which will be used in the further tutorials. The below scripts creates a Web3 wallet instance and a Ocean's configuration object. -Create the configuration file in the working directory i.e. at the same path where the \`.env\` is located. +Create the configuration file in the working directory i.e. at the same path where the `.env` is located. {% tabs %} {% tab title="ocean.js" %} diff --git a/building-with-ocean/using-ocean-libraries/create-datatoken-with-fixed-pricing.md b/building-with-ocean/using-ocean-libraries/create-datatoken-with-fixed-pricing.md index bc9c5245..58921b8f 100644 --- a/building-with-ocean/using-ocean-libraries/create-datatoken-with-fixed-pricing.md +++ b/building-with-ocean/using-ocean-libraries/create-datatoken-with-fixed-pricing.md @@ -1,4 +1,4 @@ -# Create Datatoken with fixed pricing +# Create datatoken with fixed pricing This tutorial will guide you for creating your own data NFT and a datatoken using Ocean libraries. To know more about data NFT and datatoken please refer [this page](../../core-concepts/datanft-and-datatoken.md). Ocean Protocol supports different pricing schemes which can be set while publishing an asset. Please refer [this page](../../core-concepts/asset-pricing.md) for more details on pricing schemes. @@ -6,7 +6,7 @@ This tutorial will guide you for creating your own data NFT and a datatoken usin See [this](configuration.md) guide on defining a `.env` file and a configuration file. -#### Create a script to deploy dataNFT and datatoken with fixed pricing. +#### Create a script to deploy data NFT and datatoken with fixed pricing. Create a new file in the same working directory where configuration file (`config.py`/`config.js`) and `.env` files are present, and copy the code as listed below. @@ -14,18 +14,23 @@ Create a new file in the same working directory where configuration file (`confi {% tab title="ocean.js" %} {% code title="create_datatoken_with_fre.js" %} ```javascript +// Import dependencies const { NftFactory, Datatoken } = require('@oceanprotocol/lib'); const Web3 = require('web3'); const { web3Provider, oceanConfig } = require('./config'); +// Create a web3 instance const web3 = new Web3(web3Provider); -const createDataNFT = async () => { +// Define a function createFRE() +const createFRE = async () => { const Factory = new NftFactory(oceanConfig.erc721FactoryAddress, web3); + // Get accounts from web3 instance const accounts = await web3.eth.getAccounts(); const publisherAccount = accounts[0]; + // data NFT parameters: name, symbol, templateIndex, etc. const nftParams = { name: '72120Bundle', symbol: '72Bundle', @@ -34,11 +39,15 @@ const createDataNFT = async () => { transferable: true, owner: publisherAccount }; - + + // datatoken parameters: name, symbol, templateIndex, etc. const erc20Params = { + name: "Sample datatoken", + symbol: "SDT", templateIndex: 1, cap: '100000', feeAmount: '0', + // paymentCollector is the address paymentCollector: '0x0000000000000000000000000000000000000000', feeToken: '0x0000000000000000000000000000000000000000', minter: publisherAccount, @@ -54,10 +63,12 @@ const createDataNFT = async () => { datatokenDecimals: 18, fixedRate: '100', marketFee: '0', - allowedConsumer: publisherAccount, - withMint: false + // Optional parameters + // allowedConsumer: publisherAccount, // only account that consume the exhchange + withMint: false // add FixedPriced contract as minter if withMint == true } + // Create data NFT and a datatoken with Fixed Rate exchange const result = await Factory.createNftErc20WithFixedRate( publisherAccount, nftParams, @@ -65,6 +76,7 @@ const createDataNFT = async () => { fixedPriceParams ); + // Get the data NFT address and datatoken address from the result const erc721Address = result.events.NFTCreated.returnValues[0]; const datatokenAddress = result.events.TokenCreated.returnValues[0]; @@ -74,7 +86,8 @@ const createDataNFT = async () => { }; }; -createDataNFT() +// Call the createFRE() function +createFRE() .then(({ erc721Address, datatokenAddress }) => { console.log(`DataNft address ${erc721Address}`); console.log(`Datatoken address ${datatokenAddress}`); @@ -113,8 +126,28 @@ data_nft = ocean.create_data_nft( ) print(f"Created dataNFT. Its address is {data_nft.address}") +# replace the addresses here +fee_manager = "0x0000000000000000000000000000000000000000" +publish_market_order_fee_address = "0x0000000000000000000000000000000000000000" +publish_market_order_fee_token = "0x0000000000000000000000000000000000000000" +minter = web3_wallet.address -datatoken = data_nft.create_datatoken("Datatoken 1", "DT1", from_wallet=web3_wallet) +# replace the fee amount +publish_market_order_fee_amount = 0 + +datatoken = data_nft.create_datatoken( + name="Datatoken 1", + symbol="DT1", + datatoken_cap="100000", + from_wallet=web3_wallet, + # Ootional parameters below + template_index=1, + fee_manager=fee_manager, + publish_market_order_fee_token=publish_market_order_fee_token, + publish_market_order_fee_amount=publish_market_order_fee_amount, + minter=minter, + publish_market_order_fee_address=publish_market_order_fee_address, +) print(f"Created datatoken. Its address is {datatoken.address}") @@ -126,6 +159,7 @@ exchange_id = ocean.create_fixed_rate( ) print(f"Created fixed rate exchange with ID {exchange_id.hex()}") + ``` {% endcode %} diff --git a/building-with-ocean/using-ocean-libraries/mint-datatoken.md b/building-with-ocean/using-ocean-libraries/mint-datatoken.md index bf357906..8220b047 100644 --- a/building-with-ocean/using-ocean-libraries/mint-datatoken.md +++ b/building-with-ocean/using-ocean-libraries/mint-datatoken.md @@ -14,28 +14,34 @@ Create a new file in the same working directory where configuration file (`confi {% tab title="ocean.js" %} {% code title="mint_datatoken.js" %} ```javascript +// Import dependencies const { NftFactory, Datatoken } = require('@oceanprotocol/lib'); const Web3 = require('web3'); const { web3Provider, oceanConfig } = require('./config'); +// Create a web3 instance const web3 = new Web3(web3Provider); // Change this const datatokenAddress = "0xD3542e5F56655fb818F9118CE219e1D10751BC82" const receiverAddress = "0xBE5449a6A97aD46c8558A3356267Ee5D2731ab5e" +// Create a function which will take `datatokenAddress` and `receiverAddress` as parameters const mintDatatoken = async (datatokenAddress, receiverAddress) => { const accounts = await web3.eth.getAccounts(); const publisherAccount = accounts[0]; + // Create datatoken instance const datatoken = new Datatoken(web3); + // Get current datatoken balance of receiver let receiverBalance = await datatoken.balance( datatokenAddress, receiverAddress ); console.log(`Receiver balance before mint: ${receiverBalance}`); + // Mint datatoken await datatoken.mint( datatokenAddress, publisherAccount, @@ -43,6 +49,7 @@ const mintDatatoken = async (datatokenAddress, receiverAddress) => { receiverAddress ); + // Get new datatoken balance of receiver receiverBalance = await datatoken.balance( datatokenAddress, receiverAddress @@ -50,6 +57,7 @@ const mintDatatoken = async (datatokenAddress, receiverAddress) => { console.log(`Receiver balance after mint: ${receiverBalance}`); }; +// Call mintDatatoken(...) function defined above mintDatatoken(datatokenAddress, receiverAddress) .then(() => { process.exit((err) => { @@ -85,6 +93,7 @@ datatoken = ocean.get_datatoken(datatoken_address) print(f"Balance before mint: {datatoken.balanceOf(receiver_address)}") +# Mint datatokens datatoken.mint( account_address=receiver_address, value=ocean.to_wei("1"), diff --git a/building-with-ocean/using-ocean-libraries/update-metadata.md b/building-with-ocean/using-ocean-libraries/update-metadata.md index 592dba7d..9d5d5e61 100644 --- a/building-with-ocean/using-ocean-libraries/update-metadata.md +++ b/building-with-ocean/using-ocean-libraries/update-metadata.md @@ -6,6 +6,10 @@ This tutorial will guide you to update an existing asset published on-chain usin See [this](configuration.md) guide on defining a `.env` file and a configuration file +{% hint style="info" %} +The variable **AQUARIUS\_URL** and **PROVIDER\_URL** should be set correctly in `.env` file +{% endhint %} + #### Create a script to update the metadata Create a new file in the same working directory where configuration file (`config.py`/`config.js`) and `.env` files are present, and copy the code as listed below. @@ -14,6 +18,7 @@ Create a new file in the same working directory where configuration file (`confi {% tab title="ocean.js" %} {% code title="updateMetadata.js" %} ```javascript +// Import dependencies const { Nft, ProviderInstance, @@ -24,7 +29,10 @@ const { SHA256 } = require('crypto-js'); const Web3 = require('web3'); const { web3Provider, oceanConfig } = require('./config'); +// Create a web3 instance const web3 = new Web3(web3Provider); + +// Create Aquarius instance const aquarius = new Aquarius(oceanConfig.metadataCacheUri); const nft = new Nft(web3); const providerUrl = oceanConfig.providerUri; @@ -32,10 +40,12 @@ const providerUrl = oceanConfig.providerUri; // replace the did here const did = "did:op:a419f07306d71f3357f8df74807d5d12bddd6bcd738eb0b461470c64859d6f0f"; +// This function takes did as a parameter and updates the data NFT information const setMetadata = async (did) => { const accounts = await web3.eth.getAccounts(); const publisherAccount = accounts[0]; - + + // Fetch ddo from Aquarius const ddo = await aquarius.resolve(did); // update the ddo here @@ -47,6 +57,7 @@ const setMetadata = async (did) => { const encryptedResponse = await providerResponse; const metadataHash = getHash(JSON.stringify(ddo)); + // Update the data NFT metadata await nft.setMetadata( ddo.nftAddress, publisherAccount, @@ -58,6 +69,7 @@ const setMetadata = async (did) => { `0x${metadataHash}` ); + // Check if ddo is correctly udpated in Aquarius await aquarius.waitForAqua(ddo.id); console.log(`Resolved asset did [${ddo.id}]from aquarius.`); @@ -67,6 +79,7 @@ const setMetadata = async (did) => { }; +// Call setMetadata(...) function defined above setMetadata(did).then(() => { process.exit(); }).catch((err) => {