1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

Updating CodeExamples.md

This commit is contained in:
GitHub Actions Bot 2023-03-27 09:57:03 +00:00
parent 65754a0b20
commit 815ee37c52

View File

@ -84,9 +84,11 @@ Now open the `marketplace.js` file in your text editor.
Start by importing all of the necessary dependencies
```Typescript
import fs from 'fs'
import { AbiItem } from 'web3-utils'
import { ethers, providers, Signer } from 'ethers'
import { SHA256 } from 'crypto-js'
import { homedir } from 'os'
import {
approve,
Aquarius,
@ -108,9 +110,14 @@ import {
ProviderInstance,
transfer,
ZERO_ADDRESS,
sendTx
sendTx,
ConfigHelper,
configHelperNetworks,
amountToUnits,
ValidateMetadata,
getEventFromTx,
DDO
} from '@oceanprotocol/lib'
import { getAddresses, getTestConfig, web3 } from '../config'
```
<!--
@ -120,12 +127,14 @@ describe('Marketplace flow tests
Now we define the variables which we will need later
```Typescript
let provider: ethers.providers.JsonRpcProvider
let config: Config
let aquarius: Aquarius
let datatoken: Datatoken
let providerUrl: any
let publisherAccount: string
let consumerAccount: string
let stakerAccount: string
let publisherAccount: Signer
let consumerAccount: Signer
let stakerAccount: Signer
let addresses: any
let freNftAddress: string
let freDatatokenAddress: string
@ -134,6 +143,7 @@ Now we define the variables which we will need later
let dispenserNftAddress: string
let dispenserDatatokenAddress: string
let dispenserAddress: string
let fixedDDO
```
We also define some constants that we will use:
@ -161,11 +171,11 @@ We also define some constants that we will use:
Next, we define the metadata that will describe our data asset. This is what we call the DDO
```Typescript
const DDO = {
const genericAsset: DDO = {
'@context': ['https://w3id.org/did/v1'],
id: '',
version: '4.1.0',
chainId: 5,
chainId: 4,
nftAddress: '0x0',
metadata: {
created: '2021-12-20T14:35:20Z',
@ -174,56 +184,64 @@ Next, we define the metadata that will describe our data asset. This is what we
name: 'dataset-name',
description: 'Ocean protocol test dataset description',
author: 'oceanprotocol-team',
license: 'MIT'
license: 'MIT',
tags: ['white-papers'],
additionalInformation: { 'test-key': 'test-value' },
links: ['http://data.ceda.ac.uk/badc/ukcp09/']
},
services: [
{
id: 'testFakeId',
type: 'access',
description: 'Download service',
files: '',
datatokenAddress: '0x0',
serviceEndpoint: 'https://v4.provider.goerli.oceanprotocol.com',
serviceEndpoint: 'http://172.15.0.4:8030',
timeout: 0
}
]
}
```
We load the configuration:
## 5. Load the configuration, initialize accounts and deploy contracts
```Typescript
config = await getTestConfig(web3)
aquarius = new Aquarius(config.metadataCacheUri)
providerUrl = config.providerUri
provider = new providers.JsonRpcProvider(
process.env.NODE_URI || configHelperNetworks[1].nodeUri
)
publisherAccount = (await provider.getSigner(0)) as Signer
consumerAccount = (await provider.getSigner(1)) as Signer
const config = new ConfigHelper().getConfig(
parseInt(String((await publisherAccount.provider.getNetwork()).chainId))
)
config.providerUri = process.env.PROVIDER_URL || config.providerUri
aquarius = new Aquarius(config?.metadataCacheUri)
providerUrl = config?.providerUri
addresses = JSON.parse(
// eslint-disable-next-line security/detect-non-literal-fs-filename
fs.readFileSync(
process.env.ADDRESS_FILE ||
`${homedir}/.ocean/ocean-contracts/artifacts/address.json`,
'utf8'
)
).development
```
As we go along it's a good idea to console log the values so that you check they are right
```Typescript
console.log(`Aquarius URL: ${config.metadataCacheUri}`)
console.log(`Provider URL: ${providerUrl}`)
```
## 5. Initialize accounts and deploy contracts
### 5.1 Next, lets get the address of the deployed contracts
```Typescript
addresses = getAddresses()
```
### 5.2 Initialize accounts
```Typescript
const accounts = await web3.eth.getAccounts()
publisherAccount = accounts[0]
consumerAccount = accounts[1]
stakerAccount = accounts[2]
```
Again, lets console log the values so that we can check that they have been saved properly
```Typescript
console.log(`Deployed contracts address: ${addresses}`)
console.log(`Publisher account address: ${publisherAccount}`)
console.log(`Consumer account address: ${consumerAccount}`)
console.log(`Staker account address: ${stakerAccount}`)
<!--
// mint ocean to publisherAccount
```
### 5.1 Mint OCEAN to publisher account
You can skip this step if you are running your script against a remote network,
you need to mint oceans to mentioned accounts only if you are using barge to test your script
```Typescript
const minAbi = [
{
constant: false,
@ -237,37 +255,40 @@ Again, lets console log the values so that we can check that they have been save
stateMutability: 'nonpayable',
type: 'function'
}
] as AbiItem[]
const tokenContract = new web3.eth.Contract(minAbi, addresses.Ocean)
const estGas = await calculateEstimatedGas(
publisherAccount,
tokenContract.methods.mint,
publisherAccount,
web3.utils.toWei('1000')
]
const tokenContract = new ethers.Contract(addresses.Ocean, minAbi, publisherAccount)
const estGasPublisher = await tokenContract.estimateGas.mint(
await publisherAccount.getAddress(),
amountToUnits(null, null, '1000', 18)
)
await sendTx(
estGasPublisher,
publisherAccount,
estGas,
web3,
1,
tokenContract.methods.mint,
publisherAccount,
web3.utils.toWei('1000')
tokenContract.mint,
await publisherAccount.getAddress(),
amountToUnits(null, null, '1000', 18)
)
-->
```
### 5.2 Next, lets get the address of the deployed contracts
```Typescript
addresses = getAddresses()
```
### 5.3 We send some OCEAN to consumer and staker accounts
```Typescript
transfer(web3, config, publisherAccount, addresses.Ocean, consumerAccount, '100')
transfer(web3, config, publisherAccount, addresses.Ocean, stakerAccount, '100')
transfer(
publisherAccount,
config,
addresses.Ocean,
await consumerAccount.getAddress(),
'100'
)
transfer(
publisherAccount,
config,
addresses.Ocean,
await stakerAccount.getAddress(),
'100'
)
```
@ -275,7 +296,7 @@ Again, lets console log the values so that we can check that they have been save
### 6.1 Publish a dataset (create NFT + Datatoken) with a fixed rate exchange
```Typescript
const factory = new NftFactory(addresses.ERC721Factory, web3)
const factory = new NftFactory(addresses.ERC721Factory, publisherAccount)
const nftParams: NftCreateData = {
name: FRE_NFT_NAME,
@ -283,7 +304,7 @@ Again, lets console log the values so that we can check that they have been save
templateIndex: 1,
tokenURI: '',
transferable: true,
owner: publisherAccount
owner: await publisherAccount.getAddress()
}
const datatokenParams: DatatokenCreateParams = {
@ -292,15 +313,15 @@ Again, lets console log the values so that we can check that they have been save
feeAmount: '0',
paymentCollector: ZERO_ADDRESS,
feeToken: ZERO_ADDRESS,
minter: publisherAccount,
minter: await publisherAccount.getAddress(),
mpFeeAddress: ZERO_ADDRESS
}
const freParams: FreCreationParams = {
fixedRateAddress: addresses.FixedPrice,
baseTokenAddress: addresses.Ocean,
owner: publisherAccount,
marketFeeCollector: publisherAccount,
owner: await publisherAccount.getAddress(),
marketFeeCollector: await publisherAccount.getAddress(),
baseTokenDecimals: 18,
datatokenDecimals: 18,
fixedRate: '1',
@ -309,17 +330,22 @@ Again, lets console log the values so that we can check that they have been save
withMint: false
}
const tx = await factory.createNftWithDatatokenWithFixedRate(
publisherAccount,
const bundleNFT = await factory.createNftWithDatatokenWithFixedRate(
nftParams,
datatokenParams,
freParams
)
freNftAddress = tx.events.NFTCreated.returnValues[0]
freDatatokenAddress = tx.events.TokenCreated.returnValues[0]
freAddress = tx.events.NewFixedRate.returnValues.exchangeContract
freId = tx.events.NewFixedRate.returnValues.exchangeId
const trxReceipt = await bundleNFT.wait()
// events have been emitted
const nftCreatedEvent = getEventFromTx(trxReceipt, 'NFTCreated')
const tokenCreatedEvent = getEventFromTx(trxReceipt, 'TokenCreated')
const newFreEvent = getEventFromTx(trxReceipt, 'NewFixedRate')
freNftAddress = nftCreatedEvent.args.newTokenAddress
freDatatokenAddress = tokenCreatedEvent.args.newTokenAddress
freAddress = newFreEvent.args.exchangeContract
freId = newFreEvent.args.exchangeId
```
Now let's console log each of those values to check everything is working
@ -333,54 +359,64 @@ Now let's console log each of those values to check everything is working
### 6.2 Set metadata in the fixed rate exchange NFT
```Typescript
const nft = new Nft(web3)
const nft = new Nft(
publisherAccount,
(await publisherAccount.provider.getNetwork()).chainId
)
fixedDDO = { ...genericAsset }
```
Now we are going to update the ddo and set the did
```Typescript
DDO.chainId = await web3.eth.getChainId()
DDO.id =
fixedDDO.chainId = (await publisherAccount.provider.getNetwork()).chainId
fixedDDO.id =
'did:op:' +
SHA256(web3.utils.toChecksumAddress(freNftAddress) + DDO.chainId.toString(10))
DDO.nftAddress = freNftAddress
SHA256(ethers.utils.getAddress(freNftAddress) + fixedDDO.chainId.toString(10))
fixedDDO.nftAddress = freNftAddress
```
Next, let's encrypt the file(s) using provider
```Typescript
ASSET_URL.datatokenAddress = freDatatokenAddress
ASSET_URL.nftAddress = freNftAddress
const encryptedFiles = await ProviderInstance.encrypt(
fixedDDO.services[0].files = await ProviderInstance.encrypt(
ASSET_URL,
DDO.chainId,
fixedDDO.chainId,
providerUrl
)
DDO.services[0].files = await encryptedFiles
DDO.services[0].datatokenAddress = freDatatokenAddress
fixedDDO.services[0].datatokenAddress = freDatatokenAddress
```
Now let's console log the DID to check everything is working
```Typescript
console.log(`DID: ${DDO.id}`)
console.log(`DID: ${fixedDDO.id}`)
const providerResponse = await ProviderInstance.encrypt(DDO, DDO.chainId, providerUrl)
const providerResponse = await ProviderInstance.encrypt(
fixedDDO,
fixedDDO.chainId,
providerUrl
)
const encryptedDDO = await providerResponse
const metadataHash = getHash(JSON.stringify(DDO))
const isAssetValid: ValidateMetadata = await aquarius.validate(fixedDDO)
assert(isAssetValid.valid === true, 'Published asset is not valid')
await nft.setMetadata(
freNftAddress,
publisherAccount,
await publisherAccount.getAddress(),
0,
providerUrl,
'',
'0x2',
encryptedDDO,
'0x' + metadataHash
isAssetValid.hash
)
})
```
### 6.3 Marketplace displays fixed rate asset for sale
```Typescript
const fixedRate = new FixedRateExchange(freAddress, web3)
const fixedRate = new FixedRateExchange(freAddress, publisherAccount)
const oceanAmount = await (
await fixedRate.calcBaseInGivenDatatokensOut(freId, '1')
).baseTokenAmount
@ -393,47 +429,75 @@ Now that the market has fetched those values it can display the asset on the fro
### 6.4 Consumer buys a fixed rate asset data asset, and downloads it
```Typescript
const datatoken = new Datatoken(web3)
datatoken = new Datatoken(publisherAccount)
const DATATOKEN_AMOUNT = '10000'
await datatoken.mint(freDatatokenAddress, publisherAccount, DATATOKEN_AMOUNT)
await datatoken.mint(
freDatatokenAddress,
await publisherAccount.getAddress(),
DATATOKEN_AMOUNT
)
const consumerETHBalance = await web3.eth.getBalance(consumerAccount)
const consumerBalance = await provider.getBalance(await consumerAccount.getAddress())
const consumerETHBalance = ethers.utils.formatEther(consumerBalance)
```
Let's do a quick check of the consumer ETH balance before the swap
```Typescript
console.log(`Consumer ETH balance: ${consumerETHBalance}`)
let consumerOCEANBalance = await balance(web3, addresses.Ocean, consumerAccount)
let consumerOCEANBalance = await balance(
consumerAccount,
addresses.Ocean,
await consumerAccount.getAddress()
)
console.log(`Consumer OCEAN balance before swap: ${consumerOCEANBalance}`)
let consumerDTBalance = await balance(web3, freDatatokenAddress, consumerAccount)
let consumerDTBalance = await balance(
consumerAccount,
freDatatokenAddress,
await consumerAccount.getAddress()
)
console.log(`Consumer ${FRE_NFT_SYMBOL} balance before swap: ${consumerDTBalance}`)
```
Before we call the contract we have to call `approve` so that the contract can move our tokens. This is standard when using any ERC20 Datatokens
```Typescript
await approve(web3, config, consumerAccount, addresses.Ocean, freAddress, '100')
await approve(
web3,
consumerAccount,
config,
await consumerAccount.getAddress(),
addresses.Ocean,
freAddress,
'100'
)
await approve(
publisherAccount,
config,
await publisherAccount.getAddress(),
freDatatokenAddress,
freAddress,
DATATOKEN_AMOUNT
)
const fixedRate = new FixedRateExchange(freAddress, web3)
const fixedRate = new FixedRateExchange(freAddress, consumerAccount)
```
Now we can make the contract call
```Typescript
await fixedRate.buyDatatokens(consumerAccount, freId, '1', '2')
await fixedRate.buyDatatokens(freId, '1', '2')
consumerOCEANBalance = await balance(web3, addresses.Ocean, consumerAccount)
consumerOCEANBalance = await balance(
consumerAccount,
addresses.Ocean,
await consumerAccount.getAddress()
)
console.log(`Consumer OCEAN balance after swap: ${consumerOCEANBalance}`)
consumerDTBalance = await balance(web3, freDatatokenAddress, consumerAccount)
consumerDTBalance = await balance(
consumerAccount,
freDatatokenAddress,
await consumerAccount.getAddress()
)
console.log(`Consumer ${FRE_NFT_SYMBOL} balance after swap: ${consumerDTBalance}`)
const resolvedDDO = await aquarius.waitForAqua(DDO.id)
const resolvedDDO = await aquarius.waitForAqua(fixedDDO.id)
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
```
@ -443,7 +507,7 @@ Next, we need to initialize the provider
resolvedDDO.id,
resolvedDDO.services[0].id,
0,
consumerAccount,
await consumerAccount.getAddress(),
providerUrl
)
@ -458,27 +522,29 @@ Next, we need to initialize the provider
validUntil: initializeData.providerFee.validUntil
}
datatoken = new Datatoken(consumerAccount)
```
Lets now make the payment
```Typescript
const tx = await datatoken.startOrder(
freDatatokenAddress,
consumerAccount,
consumerAccount,
await consumerAccount.getAddress(),
0,
providerFees
)
const orderTx = await tx.wait()
const orderStartedTx = getEventFromTx(orderTx, 'OrderStarted')
```
Now we can get the url
```Typescript
const downloadURL = await ProviderInstance.getDownloadUrl(
DDO.id,
consumerAccount,
DDO.services[0].id,
fixedDDO.id,
fixedDDO.services[0].id,
0,
tx.transactionHash,
orderStartedTx.transactionHash,
providerUrl,
web3
consumerAccount
)
```
@ -486,9 +552,17 @@ Lets check that the download URL was successfully received
```Typescript
console.log(`Download URL: ${downloadURL}`)
consumerOCEANBalance = await balance(web3, addresses.Ocean, consumerAccount)
consumerOCEANBalance = await balance(
consumerAccount,
addresses.Ocean,
await consumerAccount.getAddress()
)
console.log(`Consumer OCEAN balance after order: ${consumerOCEANBalance}`)
consumerDTBalance = await balance(web3, freDatatokenAddress, consumerAccount)
consumerDTBalance = await balance(
consumerAccount,
freDatatokenAddress,
await consumerAccount.getAddress()
)
console.log(`Consumer ${FRE_NFT_SYMBOL} balance after order: ${consumerDTBalance}`)
try {
@ -504,7 +578,7 @@ Lets check that the download URL was successfully received
### 7.1 Publish a dataset (create NFT + Datatoken) with a dispenser
```Typescript
const factory = new NftFactory(addresses.ERC721Factory, web3)
const factory = new NftFactory(addresses.ERC721Factory, publisherAccount)
const nftParams: NftCreateData = {
name: DISP_NFT_NAME,
@ -512,7 +586,7 @@ Lets check that the download URL was successfully received
templateIndex: 1,
tokenURI: '',
transferable: true,
owner: publisherAccount
owner: await publisherAccount.getAddress()
}
const datatokenParams: DatatokenCreateParams = {
@ -521,7 +595,7 @@ Lets check that the download URL was successfully received
feeAmount: '0',
paymentCollector: ZERO_ADDRESS,
feeToken: ZERO_ADDRESS,
minter: publisherAccount,
minter: await publisherAccount.getAddress(),
mpFeeAddress: ZERO_ADDRESS
}
@ -533,16 +607,19 @@ Lets check that the download URL was successfully received
allowedSwapper: ZERO_ADDRESS
}
const tx = await factory.createNftWithDatatokenWithDispenser(
publisherAccount,
const bundleNFT = await factory.createNftWithDatatokenWithDispenser(
nftParams,
datatokenParams,
dispenserParams
)
const trxReceipt = await bundleNFT.wait()
const nftCreatedEvent = getEventFromTx(trxReceipt, 'NFTCreated')
const tokenCreatedEvent = getEventFromTx(trxReceipt, 'TokenCreated')
const dispenserCreatedEvent = getEventFromTx(trxReceipt, 'DispenserCreated')
dispenserNftAddress = tx.events.NFTCreated.returnValues[0]
dispenserDatatokenAddress = tx.events.TokenCreated.returnValues[0]
dispenserAddress = tx.events.DispenserCreated.returnValues[0]
dispenserNftAddress = nftCreatedEvent.args.newTokenAddress
dispenserDatatokenAddress = tokenCreatedEvent.args.newTokenAddress
dispenserAddress = dispenserCreatedEvent.args.newTokenAddress // double check this
```
Lets check that we managed to received all of those values without any problems
```Typescript
@ -554,57 +631,63 @@ Lets check that we managed to received all of those values without any problems
### 7.2 Set metadata in the dispenser NFT
```Typescript
const nft = new Nft(web3)
const nft = new Nft(
publisherAccount,
(await publisherAccount.provider.getNetwork()).chainId
)
```
Lets start by updating the ddo and setting the did
```Typescript
DDO.chainId = await web3.eth.getChainId()
DDO.id =
'did:op:' +
SHA256(web3.utils.toChecksumAddress(dispenserNftAddress) + DDO.chainId.toString(10))
DDO.nftAddress = dispenserNftAddress
fixedDDO.chainId = (await publisherAccount.provider.getNetwork()).chainId
fixedDDO.id =
'did:op:' +
SHA256(ethers.utils.getAddress(dispenserNftAddress) + fixedDDO.chainId.toString(10))
fixedDDO.nftAddress = dispenserNftAddress
```
Now we need to encrypt file(s) using provider
```Typescript
ASSET_URL.datatokenAddress = dispenserDatatokenAddress
ASSET_URL.nftAddress = dispenserNftAddress
const encryptedFiles = await ProviderInstance.encrypt(
fixedDDO.services[0].files = await ProviderInstance.encrypt(
ASSET_URL,
DDO.chainId,
fixedDDO.chainId,
providerUrl
)
DDO.services[0].files = await encryptedFiles
DDO.services[0].datatokenAddress = dispenserDatatokenAddress
fixedDDO.services[0].datatokenAddress = dispenserDatatokenAddress
console.log(`DID: ${DDO.id}`)
console.log(`DID: ${fixedDDO.id}`)
const providerResponse = await ProviderInstance.encrypt(DDO, DDO.chainId, providerUrl)
const encryptedDDO = await providerResponse
const metadataHash = getHash(JSON.stringify(DDO))
const encryptedDDO = await ProviderInstance.encrypt(
fixedDDO,
fixedDDO.chainId,
providerUrl
)
const isAssetValid: ValidateMetadata = await aquarius.validate(fixedDDO)
assert(isAssetValid.valid === true, 'Published asset is not valid')
await nft.setMetadata(
dispenserNftAddress,
publisherAccount,
await publisherAccount.getAddress(),
0,
providerUrl,
'',
'0x2',
encryptedDDO,
'0x' + metadataHash
isAssetValid.hash
)
```
### 7.3 Consumer gets a dispenser data asset, and downloads it
```Typescript
const datatoken = new Datatoken(web3)
const dispenser = new Dispenser(addresses.Dispenser, web3)
datatoken = new Datatoken(publisherAccount)
const dispenser = new Dispenser(addresses.Dispenser, consumerAccount)
let consumerDTBalance = await balance(
web3,
consumerAccount,
dispenserDatatokenAddress,
consumerAccount
await consumerAccount.getAddress()
)
console.log(
`Consumer ${DISP_NFT_SYMBOL} balance before dispense: ${consumerDTBalance}`
@ -612,18 +695,24 @@ Now we need to encrypt file(s) using provider
await dispenser.dispense(
dispenserDatatokenAddress,
consumerAccount,
'1',
consumerAccount
await consumerAccount.getAddress()
)
consumerDTBalance = await balance(web3, dispenserDatatokenAddress, consumerAccount)
consumerDTBalance = await balance(
consumerAccount,
dispenserDatatokenAddress,
await consumerAccount.getAddress()
)
console.log(
`Consumer ${DISP_NFT_SYMBOL} balance after dispense: ${consumerDTBalance}`
)
const resolvedDDO = await aquarius.waitForAqua(DDO.id)
const resolvedDDO = await aquarius.waitForAqua(fixedDDO.id)
assert(resolvedDDO, 'Cannot fetch DDO from Aquarius')
datatoken = new Datatoken(publisherAccount)
```
At this point we need to encrypt file(s) using provider
```Typescript
@ -631,7 +720,7 @@ At this point we need to encrypt file(s) using provider
resolvedDDO.id,
resolvedDDO.services[0].id,
0,
consumerAccount,
await consumerAccount.getAddress(),
providerUrl
)
@ -650,29 +739,33 @@ Now we need to make the payment
```Typescript
const tx = await datatoken.startOrder(
dispenserDatatokenAddress,
consumerAccount,
consumerAccount,
await consumerAccount.getAddress(),
0,
providerFees
)
const orderTx = await tx.wait()
const orderStartedTx = getEventFromTx(orderTx, 'OrderStarted')
```
Now we can get the download URL
```Typescript
const downloadURL = await ProviderInstance.getDownloadUrl(
DDO.id,
consumerAccount,
DDO.services[0].id,
fixedDDO.id,
fixedDDO.services[0].id,
0,
tx.transactionHash,
orderStartedTx.transactionHash,
providerUrl,
web3
consumerAccount
)
```
Let's check we received the download URL ok
```Typescript
console.log(`Download URL: ${downloadURL}`)
consumerDTBalance = await balance(web3, dispenserDatatokenAddress, consumerAccount)
consumerDTBalance = await balance(
consumerAccount,
dispenserDatatokenAddress,
await consumerAccount.getAddress()
)
console.log(`Consumer ${DISP_NFT_SYMBOL} balance after order: ${consumerDTBalance}`)
try {
@ -700,10 +793,10 @@ Here are the steps:
### 8.1 Add key-value pair to data NFT
Let's start by using the `setData` method to update the nft key value store with some data
```Typescript
const nft = new Nft(web3)
const nft = new Nft(publisherAccount)
const data = 'SomeData'
try {
await nft.setData(freNftAddress, publisherAccount, '1', data)
await nft.setData(freNftAddress, await publisherAccount.getAddress(), '1', data)
} catch (e) {
assert.fail('Failed to set data in NFT ERC725 key value store', e)
}