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

update compute exammples

This commit is contained in:
Bogdan Fazakas 2023-03-23 18:47:44 +02:00
parent 6c248d0471
commit 0943cc9af1
3 changed files with 99 additions and 97 deletions

View File

@ -29,7 +29,7 @@
"mocha": "TS_NODE_PROJECT='./test/tsconfig.json' mocha --config=test/.mocharc.json --node-env=test --exit", "mocha": "TS_NODE_PROJECT='./test/tsconfig.json' mocha --config=test/.mocharc.json --node-env=test --exit",
"test": "npm run lint && npm run test:unit:cover && npm run test:integration:cover", "test": "npm run lint && npm run test:unit:cover && npm run test:integration:cover",
"test:unit": "npm run mocha -- 'test/unit/**/*.test.ts'", "test:unit": "npm run mocha -- 'test/unit/**/*.test.ts'",
"test:integration": "npm run mocha -- 'test/integration/ComputeFlow.test.ts'", "test:integration": "npm run mocha -- 'test/integration/ComputeExamples.test.ts'",
"test:unit:cover": "nyc --report-dir coverage/unit npm run test:unit", "test:unit:cover": "nyc --report-dir coverage/unit npm run test:unit",
"test:integration:cover": "nyc --report-dir coverage/integration --no-clean npm run test:integration", "test:integration:cover": "nyc --report-dir coverage/integration --no-clean npm run test:integration",
"create:guide": "./scripts/createCodeExamples.sh test/integration/CodeExamples.test.ts", "create:guide": "./scripts/createCodeExamples.sh test/integration/CodeExamples.test.ts",

View File

@ -104,7 +104,7 @@
/// Install dependencies running the following command in your terminal: /// Install dependencies running the following command in your terminal:
/// ```bash /// ```bash
/// npm install @oceanprotocol/lib crypto-js web3 web3-utils typescript @types/node ts-node /// npm install @oceanprotocol/lib crypto-js ethers typescript @types/node ts-node
/// ``` /// ```
/// ## 4. Import dependencies and add variables, constants and helper methods /// ## 4. Import dependencies and add variables, constants and helper methods
@ -120,8 +120,7 @@ import fs from 'fs'
import { homedir } from 'os' import { homedir } from 'os'
import { assert } from 'chai' import { assert } from 'chai'
import { SHA256 } from 'crypto-js' import { SHA256 } from 'crypto-js'
import Web3 from 'web3' import { ethers, providers, Signer } from 'ethers'
import { AbiItem } from 'web3-utils'
import { import {
ProviderInstance, ProviderInstance,
Aquarius, Aquarius,
@ -143,9 +142,10 @@ import {
DatatokenCreateParams, DatatokenCreateParams,
sendTx, sendTx,
configHelperNetworks, configHelperNetworks,
ConfigHelper ConfigHelper,
getEventFromTx,
amountToUnits
} from '../../src' } from '../../src'
/// ``` /// ```
/// ### 4.2. Constants and variables /// ### 4.2. Constants and variables
@ -259,13 +259,12 @@ const ALGORITHM_DDO: DDO = {
/// Now we define the variables which we will need later /// Now we define the variables which we will need later
/// ```Typescript /// ```Typescript
let web3: Web3
let config: Config let config: Config
let aquarius: Aquarius let aquariusInstance: Aquarius
let datatoken: Datatoken let datatoken: Datatoken
let providerUrl: string let providerUrl: string
let publisherAccount: string let publisherAccount: Signer
let consumerAccount: string let consumerAccount: Signer
let addresses let addresses
let computeEnvs let computeEnvs
@ -286,16 +285,17 @@ let computeJobId: string
async function createAsset( async function createAsset(
name: string, name: string,
symbol: string, symbol: string,
owner: string, owner: Signer,
assetUrl: Files, assetUrl: Files,
ddo: DDO, ddo: DDO,
providerUrl: string providerUrl: string
) { ) {
const nft = new Nft(web3) const nft = new Nft(owner, (await owner.provider.getNetwork()).chainId)
const Factory = new NftFactory(addresses.ERC721Factory, web3)
const nftFactory = new NftFactory(addresses.ERC721Factory, owner)
const chain = (await owner.provider.getNetwork()).chainId
// Now we update the DDO and set the right did
const chain = await web3.eth.getChainId()
ddo.chainId = parseInt(chain.toString(10)) ddo.chainId = parseInt(chain.toString(10))
const nftParamsAsset: NftCreateData = { const nftParamsAsset: NftCreateData = {
name, name,
@ -303,7 +303,7 @@ async function createAsset(
templateIndex: 1, templateIndex: 1,
tokenURI: 'aaa', tokenURI: 'aaa',
transferable: true, transferable: true,
owner owner: await owner.getAddress()
} }
const datatokenParams: DatatokenCreateParams = { const datatokenParams: DatatokenCreateParams = {
templateIndex: 1, templateIndex: 1,
@ -311,45 +311,41 @@ async function createAsset(
feeAmount: '0', feeAmount: '0',
paymentCollector: ZERO_ADDRESS, paymentCollector: ZERO_ADDRESS,
feeToken: ZERO_ADDRESS, feeToken: ZERO_ADDRESS,
minter: owner, minter: await owner.getAddress(),
mpFeeAddress: ZERO_ADDRESS mpFeeAddress: ZERO_ADDRESS
} }
// Now we can make the contract call createNftWithDatatoken
const result = await Factory.createNftWithDatatoken( const bundleNFT = await nftFactory.createNftWithDatatoken(
owner,
nftParamsAsset, nftParamsAsset,
datatokenParams datatokenParams
) )
const nftAddress = result.events.NFTCreated.returnValues[0] const trxReceipt = await bundleNFT.wait()
const datatokenAddressAsset = result.events.TokenCreated.returnValues[0] // events have been emitted
ddo.nftAddress = web3.utils.toChecksumAddress(nftAddress) const nftCreatedEvent = getEventFromTx(trxReceipt, 'NFTCreated')
const tokenCreatedEvent = getEventFromTx(trxReceipt, 'TokenCreated')
// Next we encrypt the file or files using Ocean Provider. The provider is an off chain proxy built specifically for this task const nftAddress = nftCreatedEvent.args.newTokenAddress
const datatokenAddressAsset = tokenCreatedEvent.args.newTokenAddress
// create the files encrypted string
assetUrl.datatokenAddress = datatokenAddressAsset assetUrl.datatokenAddress = datatokenAddressAsset
assetUrl.nftAddress = ddo.nftAddress assetUrl.nftAddress = nftAddress
let providerResponse = await ProviderInstance.encrypt(assetUrl, chain, providerUrl) ddo.services[0].files = await ProviderInstance.encrypt(assetUrl, chain, providerUrl)
ddo.services[0].files = await providerResponse
ddo.services[0].datatokenAddress = datatokenAddressAsset ddo.services[0].datatokenAddress = datatokenAddressAsset
ddo.services[0].serviceEndpoint = providerUrl ddo.services[0].serviceEndpoint = 'http://172.15.0.4:8030' // put back proviederUrl
// Next we update ddo and set the right did ddo.nftAddress = nftAddress
ddo.nftAddress = web3.utils.toChecksumAddress(nftAddress) ddo.id = 'did:op:' + SHA256(ethers.utils.getAddress(nftAddress) + chain.toString(10))
ddo.id =
'did:op:' + SHA256(web3.utils.toChecksumAddress(nftAddress) + chain.toString(10))
providerResponse = await ProviderInstance.encrypt(ddo, chain, providerUrl)
const encryptedResponse = await providerResponse
const validateResult = await aquarius.validate(ddo)
// Next you can check if if the ddo is valid by checking if validateResult.valid returned true
const encryptedResponse = await ProviderInstance.encrypt(ddo, chain, providerUrl)
const validateResult = await aquariusInstance.validate(ddo)
await nft.setMetadata( await nft.setMetadata(
nftAddress, nftAddress,
owner, await owner.getAddress(),
0, 0,
providerUrl, 'http://172.15.0.4:8030', // put back proviederUrl
'', '',
'0x2', ethers.utils.hexlify(2),
encryptedResponse, encryptedResponse,
validateResult.hash validateResult.hash
) )
@ -362,7 +358,7 @@ async function createAsset(
async function handleOrder( async function handleOrder(
order: ProviderComputeInitialize, order: ProviderComputeInitialize,
datatokenAddress: string, datatokenAddress: string,
payerAccount: string, payerAccount: Signer,
consumerAccount: string, consumerAccount: string,
serviceIndex: number, serviceIndex: number,
consumeMarkerFee?: ConsumeMarketFee consumeMarkerFee?: ConsumeMarketFee
@ -374,9 +370,9 @@ async function handleOrder(
*/ */
if (order.providerFee && order.providerFee.providerFeeAmount) { if (order.providerFee && order.providerFee.providerFeeAmount) {
await approveWei( await approveWei(
web3,
config,
payerAccount, payerAccount,
config,
await payerAccount.getAddress(),
order.providerFee.providerFeeToken, order.providerFee.providerFeeToken,
datatokenAddress, datatokenAddress,
order.providerFee.providerFeeAmount order.providerFee.providerFeeAmount
@ -386,21 +382,23 @@ async function handleOrder(
if (!order.providerFee) return order.validOrder if (!order.providerFee) return order.validOrder
const tx = await datatoken.reuseOrder( const tx = await datatoken.reuseOrder(
datatokenAddress, datatokenAddress,
payerAccount,
order.validOrder, order.validOrder,
order.providerFee order.providerFee
) )
return tx.transactionHash const reusedTx = await tx.wait()
const orderReusedTx = getEventFromTx(reusedTx, 'OrderReused')
return orderReusedTx.transactionHash
} }
const tx = await datatoken.startOrder( const tx = await datatoken.startOrder(
datatokenAddress, datatokenAddress,
payerAccount,
consumerAccount, consumerAccount,
serviceIndex, serviceIndex,
order.providerFee, order.providerFee,
consumeMarkerFee consumeMarkerFee
) )
return tx.transactionHash const orderTx = await tx.wait()
const orderStartedTx = getEventFromTx(orderTx, 'OrderStarted')
return orderStartedTx.transactionHash
} }
/// ``` /// ```
@ -412,9 +410,17 @@ describe('Compute-to-data example tests', async () => {
/// We need to load the configuration. Add the following code into your `run(){ }` function /// We need to load the configuration. Add the following code into your `run(){ }` function
/// ```Typescript /// ```Typescript
before(async () => { before(async () => {
web3 = new Web3(process.env.NODE_URI || configHelperNetworks[1].nodeUri) const provider = new providers.JsonRpcProvider(
config = new ConfigHelper().getConfig(await web3.eth.getChainId()) 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 config.providerUri = process.env.PROVIDER_URL || config.providerUri
aquariusInstance = new Aquarius(config?.metadataCacheUri)
providerUrl = config?.providerUri
addresses = JSON.parse( addresses = JSON.parse(
// eslint-disable-next-line security/detect-non-literal-fs-filename // eslint-disable-next-line security/detect-non-literal-fs-filename
fs.readFileSync( fs.readFileSync(
@ -423,37 +429,24 @@ describe('Compute-to-data example tests', async () => {
'utf8' 'utf8'
) )
).development ).development
aquarius = new Aquarius(config.metadataCacheUri)
providerUrl = config.providerUri
datatoken = new Datatoken(web3)
/// ``` /// ```
/// As we go along it's a good idea to console log the values so that you check they are right. At the end of your `run(){ ... }` function add the following logs: /// As we go along it's a good idea to console log the values so that you check they are right. At the end of your `run(){ ... }` function add the following logs:
/// ```Typescript /// ```Typescript
console.log(`Aquarius URL: ${config.metadataCacheUri}`) console.log(`Aquarius URL: ${config.metadataCacheUri}`)
console.log(`Provider URL: ${providerUrl}`) console.log(`Provider URL: ${providerUrl}`)
console.log(`Deployed contracts address: ${addresses}`) console.log(`Deployed contracts address: ${addresses}`)
}) ///
/// ```
/// Now at the end of your compute.ts file call you `run()` function. Next, let's compile the file with the `tsc` command in the console and run `node dist/compute.js`.
/// If everything is working you should see the logs in the console and no errors.
/// ## 5. Initialize accounts
it('5.1 Initialize accounts', async () => {
/// We will use all of the following code snippets in the same way. Add the code snippet and the logs to the end of your `run(){ ... }` function as well as the logs.
/// Then compile your file with the `tsc` command and run it with `node dist/compute.js`
/// ```Typescript
const accounts = await web3.eth.getAccounts()
publisherAccount = accounts[0]
consumerAccount = accounts[1]
/// ```
/// Again, lets console log the values so that we can check that they have been saved properly
/// ```Typescript
console.log(`Publisher account address: ${publisherAccount}`) console.log(`Publisher account address: ${publisherAccount}`)
console.log(`Consumer account address: ${consumerAccount}`) console.log(`Consumer account address: ${consumerAccount}`)
}) /// }) ///
/// ``` /// ```
it('5.2 Mint OCEAN to publisher account', async () => { /// Now at the end of your compute.ts file call you `run()` function. Next, let's compile the file with the `tsc` command in the console and run `node dist/compute.js`.
/// If everything is working you should see the logs in the console and no errors.
/// We will use all of the following code snippets in the same way. Add the code snippet and the logs to the end of your `run(){ ... }` function as well as the logs.
/// Then compile your file with the `tsc` command and run it with `node dist/compute.js`
it('5.1 Mint OCEAN to publisher account', async () => {
/// You can skip this step if you are running your script against a remote network, /// 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 /// you need to mint oceans to mentioned accounts only if you are using barge to test your script
@ -471,29 +464,33 @@ describe('Compute-to-data example tests', async () => {
stateMutability: 'nonpayable', stateMutability: 'nonpayable',
type: 'function' type: 'function'
} }
] as AbiItem[] ]
const tokenContract = new web3.eth.Contract(minAbi, addresses.Ocean)
const estGas = await calculateEstimatedGas( const tokenContract = new ethers.Contract(addresses.Ocean, minAbi, publisherAccount)
publisherAccount, const estGasPublisher = await tokenContract.estimateGas.mint(
tokenContract.methods.mint, await publisherAccount.getAddress(),
publisherAccount, amountToUnits(null, null, '1000', 18)
web3.utils.toWei('1000')
) )
await sendTx( await sendTx(
estGasPublisher,
publisherAccount, publisherAccount,
estGas,
web3,
1, 1,
tokenContract.methods.mint, tokenContract.mint,
publisherAccount, await publisherAccount.getAddress(),
web3.utils.toWei('1000') amountToUnits(null, null, '1000', 18)
) )
}) /// }) ///
/// ``` /// ```
it('5.3 Send some OCEAN to consumer account', async () => { it('5.2 Send some OCEAN to consumer account', async () => {
/// ```Typescript /// ```Typescript
transfer(web3, config, publisherAccount, addresses.Ocean, consumerAccount, '100') transfer(
publisherAccount,
config,
addresses.Ocean,
await consumerAccount.getAddress(),
'100'
)
}) /// }) ///
/// ``` /// ```
@ -537,8 +534,8 @@ describe('Compute-to-data example tests', async () => {
it('7.1 Resolve published datasets and algorithms', async () => { it('7.1 Resolve published datasets and algorithms', async () => {
/// ```Typescript /// ```Typescript
resolvedDatasetDdo = await aquarius.waitForAqua(datasetId) resolvedDatasetDdo = await aquariusInstance.waitForAqua(datasetId)
resolvedAlgorithmDdo = await aquarius.waitForAqua(algorithmId) resolvedAlgorithmDdo = await aquariusInstance.waitForAqua(algorithmId)
/// ``` /// ```
/// <!-- /// <!--
assert(resolvedDatasetDdo, 'Cannot fetch DDO from Aquarius') assert(resolvedDatasetDdo, 'Cannot fetch DDO from Aquarius')
@ -550,18 +547,22 @@ describe('Compute-to-data example tests', async () => {
it('8.1 Mint dataset and algorithm datatokens to publisher', async () => { it('8.1 Mint dataset and algorithm datatokens to publisher', async () => {
/// ```Typescript /// ```Typescript
const datatoken = new Datatoken(
publisherAccount,
(await publisherAccount.provider.getNetwork()).chainId
)
await datatoken.mint( await datatoken.mint(
resolvedDatasetDdo.services[0].datatokenAddress, resolvedDatasetDdo.services[0].datatokenAddress,
publisherAccount, await publisherAccount.getAddress(),
'10', '10',
consumerAccount await consumerAccount.getAddress()
) )
await datatoken.mint( await datatoken.mint(
resolvedAlgorithmDdo.services[0].datatokenAddress, resolvedAlgorithmDdo.services[0].datatokenAddress,
publisherAccount, await publisherAccount.getAddress(),
'10', '10',
consumerAccount await consumerAccount.getAddress()
) )
}) /// }) ///
/// ``` /// ```
@ -580,6 +581,11 @@ describe('Compute-to-data example tests', async () => {
/// ## 10. Consumer starts a compute job /// ## 10. Consumer starts a compute job
it('10.1 Start a compute job using a free C2D environment', async () => { it('10.1 Start a compute job using a free C2D environment', async () => {
datatoken = new Datatoken(
consumerAccount,
(await consumerAccount.provider.getNetwork()).chainId
)
/// let's check the free compute environment /// let's check the free compute environment
/// ```Typescript /// ```Typescript
const computeEnv = computeEnvs[resolvedDatasetDdo.chainId].find( const computeEnv = computeEnvs[resolvedDatasetDdo.chainId].find(
@ -616,7 +622,7 @@ describe('Compute-to-data example tests', async () => {
computeEnv.id, computeEnv.id,
computeValidUntil, computeValidUntil,
providerUrl, providerUrl,
consumerAccount await consumerAccount.getAddress()
) )
/// ``` /// ```
/// <!-- /// <!--
@ -639,14 +645,15 @@ describe('Compute-to-data example tests', async () => {
0 0
) )
} }
const computeJobs = await ProviderInstance.computeStart( const computeJobs = await ProviderInstance.computeStart(
providerUrl, providerUrl,
web3,
consumerAccount, consumerAccount,
computeEnv.id, computeEnv.id,
assets[0], assets[0],
algo algo
) )
/// ``` /// ```
/// <!-- /// <!--
assert(computeJobs, 'Cannot start compute job') assert(computeJobs, 'Cannot start compute job')
@ -663,7 +670,7 @@ describe('Compute-to-data example tests', async () => {
/// ```Typescript /// ```Typescript
const jobStatus = await ProviderInstance.computeStatus( const jobStatus = await ProviderInstance.computeStatus(
providerUrl, providerUrl,
consumerAccount, await consumerAccount.getAddress(),
computeJobId, computeJobId,
DATASET_DDO.id DATASET_DDO.id
) )
@ -682,7 +689,6 @@ describe('Compute-to-data example tests', async () => {
await sleep(10000) await sleep(10000)
const downloadURL = await ProviderInstance.getComputeResultUrl( const downloadURL = await ProviderInstance.getComputeResultUrl(
providerUrl, providerUrl,
web3,
consumerAccount, consumerAccount,
computeJobId, computeJobId,
0 0

View File

@ -133,7 +133,6 @@ export async function handleComputeOrder(
- no validOrder -> we need to call startOrder, to pay 1 DT & providerFees - no validOrder -> we need to call startOrder, to pay 1 DT & providerFees
*/ */
if (order.providerFee && order.providerFee.providerFeeAmount) { if (order.providerFee && order.providerFee.providerFeeAmount) {
console.log('approving provider fees')
await approveWei( await approveWei(
payerAccount, payerAccount,
config, config,
@ -144,7 +143,6 @@ export async function handleComputeOrder(
) )
} }
if (order.validOrder) { if (order.validOrder) {
console.log('return validOrder', order.validOrder)
if (!order.providerFee) return order.validOrder if (!order.providerFee) return order.validOrder
const tx = await datatoken.reuseOrder( const tx = await datatoken.reuseOrder(
datatokenAddress, datatokenAddress,
@ -153,7 +151,6 @@ export async function handleComputeOrder(
) )
const reusedTx = await tx.wait() const reusedTx = await tx.wait()
const orderReusedTx = getEventFromTx(reusedTx, 'OrderReused') const orderReusedTx = getEventFromTx(reusedTx, 'OrderReused')
console.log('return reused order', orderReusedTx.transactionHash)
return orderReusedTx.transactionHash return orderReusedTx.transactionHash
} }
const tx = await datatoken.startOrder( const tx = await datatoken.startOrder(
@ -165,7 +162,6 @@ export async function handleComputeOrder(
) )
const orderTx = await tx.wait() const orderTx = await tx.wait()
const orderStartedTx = getEventFromTx(orderTx, 'OrderStarted') const orderStartedTx = getEventFromTx(orderTx, 'OrderStarted')
console.log('create new order', orderStartedTx.transactionHash)
return orderStartedTx.transactionHash return orderStartedTx.transactionHash
} }