diff --git a/CodeExamples.md b/CodeExamples.md index bcaef145..2193a0cb 100644 --- a/CodeExamples.md +++ b/CodeExamples.md @@ -1,89 +1,89 @@ -/// # Ocean.js Code Examples +# Ocean.js Code Examples -/// ## Introduction +## Introduction -/// The following guide runs you through the process of using ocean.js to publish and then consume a dataset. The code examples below are all working and you can learn how to publish by following along. -/// The process involves creating a Data NFT (which represents the base-IP on-chain) and a datatoken (which will be used to purchase the dataset). This guide provides all the code you need and no prior knowledge is required. It is helpful if you have some experience with javascript but it is not necessary. +The following guide runs you through the process of using ocean.js to publish and then consume a dataset. The code examples below are all working and you can learn how to publish by following along. +The process involves creating a Data NFT (which represents the base-IP on-chain) and a datatoken (which will be used to purchase the dataset). This guide provides all the code you need and no prior knowledge is required. It is helpful if you have some experience with javascript but it is not necessary. -/// Selling your data over the blockchain puts you in charge of how it is used and can be a great source of passive income. There are many AI startups that have deep expertise in machine learning but need more data to improve their models. Selling your data via the blockchain gives you a level of security that you would be unable to achieve if you were selling via a centralised marketplace. +Selling your data over the blockchain puts you in charge of how it is used and can be a great source of passive income. There are many AI startups that have deep expertise in machine learning but need more data to improve their models. Selling your data via the blockchain gives you a level of security that you would be unable to achieve if you were selling via a centralised marketplace. -/// In this guide we'll be making use of the Ocean.js library. Ocean Protocol provides you with everything you need to quickly get setup and start selling data over the blockchain. +In this guide we'll be making use of the Ocean.js library. Ocean Protocol provides you with everything you need to quickly get setup and start selling data over the blockchain. -/// These examples take you through a typical user journey that focuses on Alice's experience as a publisher, and Bob's experience as a buyer & consumer. The rest are services used by Alice and Bob. +These examples take you through a typical user journey that focuses on Alice's experience as a publisher, and Bob's experience as a buyer & consumer. The rest are services used by Alice and Bob. -/// If you have any questions or issues at any point while following along to this article please reach out to us on [discord](https://discord.gg/TnXjkR5). +If you have any questions or issues at any point while following along to this article please reach out to us on [discord](https://discord.gg/TnXjkR5). -/// Here are the steps we will be following throughout the article: +Here are the steps we will be following throughout the article: -/// Here are the steps: +Here are the steps: -/// 1. Prerequisites -/// 2. Initialize services -/// 3. Create a new node.js project -/// 4. Install dependancies -/// 5. Import dependencies and add variables and constants -/// 6. Publish Data NFT and a Datatoken with a liquidity pool -/// 7. Publish Data NFT and a Datatoken with a fixed rate exchange -/// 8. Publish Data NFT and a Datatoken with a dispenser +1. Prerequisites +2. Initialize services +3. Create a new node.js project +4. Install dependancies +5. Import dependencies and add variables and constants +6. Publish Data NFT and a Datatoken with a liquidity pool +7. Publish Data NFT and a Datatoken with a fixed rate exchange +8. Publish Data NFT and a Datatoken with a dispenser -/// ## 1. Prerequisites -/// Before we start it is important that you have all of the necessary prerequisites installed on your computer. -/// - **A Unix based operating system (Linux or Mac)**. If you are a Windows user you can try to run linux inside a virtual machine but this is outside of the scope of this article. -/// - **Git**. Instructions for installing Git can be found here: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git -/// - **Node.js** can be downloaded from here: https://nodejs.org/en/download/ -/// - **Docker** can be installed from here: https://docs.docker.com/get-docker/. Please note that Docker must run as a non-root user, you can set this up by following these instructions: https://docs.docker.com/engine/install/linux-postinstall/ +## 1. Prerequisites +Before we start it is important that you have all of the necessary prerequisites installed on your computer. +- **A Unix based operating system (Linux or Mac)**. If you are a Windows user you can try to run linux inside a virtual machine but this is outside of the scope of this article. +- **Git**. Instructions for installing Git can be found here: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git +- **Node.js** can be downloaded from here: https://nodejs.org/en/download/ +- **Docker** can be installed from here: https://docs.docker.com/get-docker/. Please note that Docker must run as a non-root user, you can set this up by following these instructions: https://docs.docker.com/engine/install/linux-postinstall/ -/// ## 1. Initialize services +## 1. Initialize services -/// Ocean.js uses off-chain services for metadata (Aquarius) and consuming datasets (Provider). +Ocean.js uses off-chain services for metadata (Aquarius) and consuming datasets (Provider). -/// We start by initializing the services. To do this, we clone the Barge repository and run it. This will run the current default versions of [Aquarius](https://github.com/oceanprotocol/aquarius), [Provider](https://github.com/oceanprotocol/provider), and [Ganache](https://github.com/trufflesuite/ganache) with [our contracts](https://github.com/oceanprotocol/contracts) deployed to it. +We start by initializing the services. To do this, we clone the Barge repository and run it. This will run the current default versions of [Aquarius](https://github.com/oceanprotocol/aquarius), [Provider](https://github.com/oceanprotocol/provider), and [Ganache](https://github.com/trufflesuite/ganache) with [our contracts](https://github.com/oceanprotocol/contracts) deployed to it. -/// ```bash -/// git clone https://github.com/oceanprotocol/barge.git -/// cd barge/ -/// ./start_ocean.sh --with-provider2 --no-dashboard -/// ``` +```bash +git clone https://github.com/oceanprotocol/barge.git +cd barge/ +./start_ocean.sh --with-provider2 --no-dashboard +``` -/// ## 2. Create a new node.js project +## 2. Create a new node.js project -/// Start by creating a new Node.js project. Open a new terminal and enter the following commands: +Start by creating a new Node.js project. Open a new terminal and enter the following commands: -/// ```bash -/// mkdir marketplace-quickstart -/// cd marketplace-quickstart -/// npm init -/// # Answer the questions in the command line prompt -/// cat > marketplace.js -/// # On linux press CTRL + D to save -/// ``` +```bash +mkdir marketplace-quickstart +cd marketplace-quickstart +npm init +# Answer the questions in the command line prompt +cat > marketplace.js +# On linux press CTRL + D to save +``` -/// ## 3. Install dependancies +## 3. Install dependancies -/// Open the package.json file in a text editor and update the dependancies to include the following: +Open the package.json file in a text editor and update the dependancies to include the following: -/// ```JSON -/// "dependencies": { -/// "@oceanprotocol/contracts": "1.0.0-alpha.28", -/// "@oceanprotocol/lib": "1.0.0-next.37", -/// "crypto-js": "^4.1.1", -/// "web3": "^1.7.3" -/// } -/// ``` +```JSON + "dependencies": { + "@oceanprotocol/contracts": "1.0.0-alpha.28", + "@oceanprotocol/lib": "1.0.0-next.37", + "crypto-js": "^4.1.1", + "web3": "^1.7.3" + } +``` -/// Now in your terminal run the following command: +Now in your terminal run the following command: -/// ```bash -/// npm install -/// ``` +```bash +npm install +``` -/// ## 4. Import dependencies and add variables and constants +## 4. Import dependencies and add variables and constants -/// Now open the `marketplace.js` file in your text editor. +Now open the `marketplace.js` file in your text editor. -/// Start by importing all of the necessary dependencies +Start by importing all of the necessary dependencies -/// ```Typescript +```Typescript import { AbiItem } from 'web3-utils/types' import { SHA256 } from 'crypto-js' @@ -114,15 +114,15 @@ import { } from '../../src' import { getTestConfig, web3 } from '../config' import { Addresses, deployContracts } from '../TestContractHandler' -/// ``` +``` -/// + - /// Variables and constants needed for the test: +Variables and constants needed for the test: - /// ```Typescript +```Typescript let config: Config let aquarius: Aquarius let providerUrl: any @@ -147,10 +147,10 @@ describe('Marketplace flow tests', async () => { const FRE_NFT_SYMBOL = 'DT2' const DISP_NFT_NAME = 'Datatoken 3' const DISP_NFT_SYMBOL = 'DT3' - /// ``` +``` - /// We will need a file to publish, so here we define the file that we intend to publish. - /// ```Typescript + We will need a file to publish, so here we define the file that we intend to publish. +```Typescript const ASSET_URL = [ { type: 'url', @@ -158,10 +158,10 @@ describe('Marketplace flow tests', async () => { method: 'GET' } ] - /// ``` +``` - /// Next, we define the metadata that will describe our data asset. This is what we call the DDO - /// ```Typescript +Next, we define the metadata that will describe our data asset. This is what we call the DDO +```Typescript const DDO = { '@context': ['https://w3id.org/did/v1'], id: '', @@ -188,11 +188,11 @@ describe('Marketplace flow tests', async () => { } ] } - /// ``` +``` - /// We load the configuration: - /// ```Typescript - before(async () => { +We load the configuration: +```Typescript + config = await getTestConfig(web3) aquarius = new Aquarius(config.metadataCacheUri) providerUrl = config.providerUri @@ -200,10 +200,10 @@ describe('Marketplace flow tests', async () => { console.log(`Aquarius URL: ${config.metadataCacheUri}`) console.log(`Provider URL: ${providerUrl}`) }) - /// ``` +``` - it('Initialize accounts', async () => { - /// ```Typescript + ### Initialize accounts +```Typescript const accounts = await web3.eth.getAccounts() publisherAccount = accounts[0] consumerAccount = accounts[1] @@ -213,16 +213,16 @@ describe('Marketplace flow tests', async () => { console.log(`Consumer account address: ${consumerAccount}`) console.log(`Staker account address: ${stakerAccount}`) }) - /// ``` +``` - it('Deploy contracts', async () => { - /// ```Typescript + ### Deploy contracts +```Typescript contracts = await deployContracts(web3, publisherAccount) }) - /// ``` +``` - it('We send some OCEAN to consumer and staker accounts', async () => { - /// ```Typescript + ### We send some OCEAN to consumer and staker accounts +```Typescript const oceanContract = new web3.eth.Contract( MockERC20.abi as AbiItem[], contracts.oceanAddress @@ -236,14 +236,14 @@ describe('Marketplace flow tests', async () => { .transfer(stakerAccount, web3.utils.toWei('100')) .send({ from: publisherAccount }) }) - /// ``` +``` - /// ## 4. Publish Data NFT and a Datatoken with a liquidity pool +## 4. Publish Data NFT and a Datatoken with a liquidity pool - /// For pool creation, the OCEAN token is used as the base token. The base token can be changed into something else, such as USDC, DAI etc., but it will require an extra fee. +For pool creation, the OCEAN token is used as the base token. The base token can be changed into something else, such as USDC, DAI etc., but it will require an extra fee. - it('Publish a dataset (create NFT + Datatoken) with a liquidity pool', async () => { - /// ```Typescript + ### Publish a dataset (create NFT + Datatoken) with a liquidity pool +```Typescript const factory = new NftFactory(contracts.erc721FactoryAddress, web3) const nftParams: NftCreateData = { @@ -304,10 +304,10 @@ describe('Marketplace flow tests', async () => { console.log(`Pool Datatoken address: ${poolDatatokenAddress}`) console.log(`Pool address: ${poolAddress}`) }) - /// ``` +``` - it('Set metadata in the pool NFT', async () => { - /// ```Typescript + ### Set metadata in the pool NFT +```Typescript const nft = new Nft(web3) // update ddo and set the right did @@ -337,20 +337,20 @@ describe('Marketplace flow tests', async () => { '0x' + metadataHash ) }) - /// ``` +``` - it('User should add liquidity to the pool, receiving LP tokens', async () => { - /// ```Typescript + ### User should add liquidity to the pool, receiving LP tokens +```Typescript const pool = new Pool(web3) await approve(web3, stakerAccount, contracts.oceanAddress, poolAddress, '5', true) await pool.joinswapExternAmountIn(stakerAccount, poolAddress, '5', '0.1') }) - /// ``` +``` - it('Marketplace displays pool asset for sale', async () => { - /// ```Typescript + ### Marketplace displays pool asset for sale +```Typescript const pool = new Pool(web3) const prices = await pool.getAmountInExactOut( poolAddress, @@ -361,10 +361,10 @@ describe('Marketplace flow tests', async () => { ) console.log(`Price of 1 ${POOL_NFT_SYMBOL} is ${prices.tokenAmount} OCEAN`) }) - /// ``` +``` - it('Consumer buys a pool data asset, and downloads it', async () => { - /// ```Typescript + ### Consumer buys a pool data asset, and downloads it +```Typescript const datatoken = new Datatoken(web3) const consumerETHBalance = await web3.eth.getBalance(consumerAccount) @@ -458,12 +458,12 @@ describe('Marketplace flow tests', async () => { assert.fail('Download failed') } }) - /// ``` +``` - /// ## 5. Publish Data NFT and a Datatoken with a fixed rate exchange +## 5. Publish Data NFT and a Datatoken with a fixed rate exchange - it('Publish a dataset (create NFT + Datatoken) with a fixed rate exchange', async () => { - /// ```Typescript + ### Publish a dataset (create NFT + Datatoken) with a fixed rate exchange +```Typescript const factory = new NftFactory(contracts.erc721FactoryAddress, web3) const nftParams: NftCreateData = { @@ -515,10 +515,10 @@ describe('Marketplace flow tests', async () => { console.log(`Fixed rate exchange address: ${freAddress}`) console.log(`Fixed rate exchange Id: ${freId}`) }) - /// ``` +``` - it('Set metadata in the fixed rate exchange NFT', async () => { - /// ```Typescript + ### Set metadata in the fixed rate exchange NFT +```Typescript const nft = new Nft(web3) // update ddo and set the right did @@ -548,20 +548,20 @@ describe('Marketplace flow tests', async () => { '0x' + metadataHash ) }) - /// ``` +``` - it('Marketplace displays fixed rate asset for sale', async () => { - /// ```Typescript + ### Marketplace displays fixed rate asset for sale +```Typescript const fixedRate = new FixedRateExchange(web3, freAddress) const oceanAmount = await ( await fixedRate.calcBaseInGivenOutDT(freId, '1') ).baseTokenAmount console.log(`Price of 1 ${FRE_NFT_SYMBOL} is ${oceanAmount} OCEAN`) }) - /// ``` +``` - it('Consumer buys a fixed rate asset data asset, and downloads it', async () => { - /// ```Typescript + ### Consumer buys a fixed rate asset data asset, and downloads it +```Typescript const datatoken = new Datatoken(web3) const DATATOKEN_AMOUNT = '10000' @@ -650,12 +650,12 @@ describe('Marketplace flow tests', async () => { assert.fail('Download failed') } }) - /// ``` +``` - /// ## 6. Publish Data NFT and a Datatoken with a dispenser +## 6. Publish Data NFT and a Datatoken with a dispenser - it('Publish a dataset (create NFT + Datatoken) with a dipenser', async () => { - /// ```Typescript + ### Publish a dataset (create NFT + Datatoken) with a dipenser +```Typescript const factory = new NftFactory(contracts.erc721FactoryAddress, web3) const nftParams: NftCreateData = { @@ -700,10 +700,10 @@ describe('Marketplace flow tests', async () => { console.log(`Dispenser Datatoken address: ${dispenserDatatokenAddress}`) console.log(`Dispenser address: ${dispenserAddress}`) }) - /// ``` +``` - it('Set metadata in the dispenser NFT', async () => { - /// ```Typescript + ### Set metadata in the dispenser NFT +```Typescript const nft = new Nft(web3) // update ddo and set the right did @@ -733,10 +733,10 @@ describe('Marketplace flow tests', async () => { '0x' + metadataHash ) }) - /// ``` +``` - it('Consumer gets a dispenser data asset, and downloads it', async () => { - /// ```Typescript + ### Consumer gets a dispenser data asset, and downloads it +```Typescript const datatoken = new Datatoken(web3) const dispenser = new Dispenser(web3, contracts.dispenserAddress) @@ -814,5 +814,5 @@ describe('Marketplace flow tests', async () => { assert.fail('Download failed') } }) - /// ``` +``` }) diff --git a/package.json b/package.json index 176c172c..a4ebe4fa 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,8 @@ "test:provider": "npm run mocha -- 'test/integration/Provider.test.ts'", "test:compute": "npm run mocha -- 'test/integration/ComputeFlow.test.ts'", "test:integration:cover": "nyc --report-dir coverage/integration --no-clean npm run test:integration", - "create:guide": "chmod +x ./scripts/createCodeExamples.sh && ./scripts/createCodeExamples.sh" + "create:guide": "chmod +x ./scripts/createCodeExamples.sh && ./scripts/createCodeExamples.sh", + "create:guide:mac": "chmod +x ./scripts/createCodeExamples-mac.sh && ./scripts/createCodeExamples-mac.sh" }, "repository": { "type": "git", diff --git a/scripts/createCodeExamples-mac.sh b/scripts/createCodeExamples-mac.sh new file mode 100755 index 00000000..f69867ad --- /dev/null +++ b/scripts/createCodeExamples-mac.sh @@ -0,0 +1,20 @@ +#!/bin/bash +# Create markdown file +cp test/integration/CodeExamples.test.ts CodeExamples.md + +# Remove unneccessay imports +sed -i '' -e "s/import { assert } from 'chai'//" CodeExamples.md + + +# Replace comments +sed -i '' -e 's/}) \/\/\/ //' CodeExamples.md +sed -i '' -e 's/ \/\/\/ //' CodeExamples.md +sed -i '' -e 's/ \/\/\/ //' CodeExamples.md +sed -i '' -e 's/\/\/\/ //' CodeExamples.md + + +# Generate titles +sed -i '' -e "s/describe('Simple Publish & Consume Flow', async () => {//" CodeExamples.md +sed -i '' -e "s/it('/\#\#\# /" CodeExamples.md +sed -i '' -e "s/', async () => {//" CodeExamples.md +sed -i '' -e "s/before(async () => {//" CodeExamples.md \ No newline at end of file diff --git a/scripts/createCodeExamples.sh b/scripts/createCodeExamples.sh index f69867ad..856ff9bb 100755 --- a/scripts/createCodeExamples.sh +++ b/scripts/createCodeExamples.sh @@ -3,18 +3,18 @@ cp test/integration/CodeExamples.test.ts CodeExamples.md # Remove unneccessay imports -sed -i '' -e "s/import { assert } from 'chai'//" CodeExamples.md +sed -i "s/import { assert } from 'chai'//" CodeExamples.md # Replace comments -sed -i '' -e 's/}) \/\/\/ //' CodeExamples.md -sed -i '' -e 's/ \/\/\/ //' CodeExamples.md -sed -i '' -e 's/ \/\/\/ //' CodeExamples.md -sed -i '' -e 's/\/\/\/ //' CodeExamples.md +sed -i 's/}) \/\/\/ //' CodeExamples.md +sed -i 's/ \/\/\/ //' CodeExamples.md +sed -i 's/ \/\/\/ //' CodeExamples.md +sed -i 's/\/\/\/ //' CodeExamples.md # Generate titles -sed -i '' -e "s/describe('Simple Publish & Consume Flow', async () => {//" CodeExamples.md -sed -i '' -e "s/it('/\#\#\# /" CodeExamples.md -sed -i '' -e "s/', async () => {//" CodeExamples.md -sed -i '' -e "s/before(async () => {//" CodeExamples.md \ No newline at end of file +sed -i "s/describe('Simple Publish & Consume Flow', async () => {//" CodeExamples.md +sed -i "s/it('/\#\#\# /" CodeExamples.md +sed -i "s/', async () => {//" CodeExamples.md +sed -i "s/before(async () => {//" CodeExamples.md \ No newline at end of file