Skip to content

Commit

Permalink
feat: initial reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
moritzkirstein committed Feb 26, 2024
1 parent 0866ae5 commit 36f1aab
Show file tree
Hide file tree
Showing 14 changed files with 740 additions and 18 deletions.
29 changes: 12 additions & 17 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
{
"env": {
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
}
"env": {
"es2021": true
},
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {},
"ignorePatterns": ["dist/**", "node_modules/**"]
}
14 changes: 14 additions & 0 deletions config/genx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"chainId": 100,
"network": "genx",
"metadataCacheUri": "https://aquarius510.v4.delta-dao.com",
"nodeUri": "https://rpc.genx.minimal-gaia-x.eu",
"providerUri": "https://provider.v4.genx.delta-dao.com",
"subgraphUri": "https://subgraph.v4.genx.minimal-gaia-x.eu",
"oceanTokenAddress": "0x0995527d3473b3a98c471f1ed8787acd77fbf009",
"oceanTokenSymbol": "OCEAN",
"fixedRateExchangeAddress": "0xAD8E7d2aFf5F5ae7c2645a52110851914eE6664b",
"dispenserAddress": "0x94cb8FC8719Ed09bE3D9c696d2037EA95ef68d3e",
"nftFactoryAddress": "0x6cb85858183B82154921f68b434299EC4281da53",
"providerAddress": "0x68C24FA5b2319C81b34f248d1f928601D2E5246B"
}
31 changes: 31 additions & 0 deletions config/pontusx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"chainId": 32456,
"isDefault": false,
"isCustom": true,
"network": "pontusx",
"metadataCacheUri": "https://aquarius.dev.pontus-x.eu",
"nodeUri": "https://rpc.dev.pontus-x.eu",
"providerUri": "https://provider.dev.pontus-x.eu",
"subgraphUri": "https://subgraph.dev.pontus-x.eu",
"explorerUri": "https://explorer.dev.pontus-x.eu/testnet/pontusx",
"oceanTokenAddress": "0xdF171F74a8d3f4e2A789A566Dce9Fa4945196112",
"oceanTokenSymbol": "OCEAN",
"fixedRateExchangeAddress": "0x8372715D834d286c9aECE1AcD51Da5755B32D505",
"dispenserAddress": "0x5461b629E01f72E0A468931A36e039Eea394f9eA",
"startBlock": 57428,
"transactionBlockTimeout": 50,
"transactionConfirmationBlocks": 1,
"transactionPollingTimeout": 750,
"gasFeeMultiplier": 1.1,
"nftFactoryAddress": "0xFdC4a5DEaCDfc6D82F66e894539461a269900E13",
"opfCommunityFeeCollector": "0x1f84fB438292269219f9396D57431eA9257C23d4",
"veAllocate": "0x3fa1d5AC45ab1Ff9CFAe227c5583Ec0484b54Ef9",
"veOCEAN": "0x061955B6980A34fce74b235f90DBe20d76f087b1",
"veDelegation": "0x96E3aE4247a01C3d40a261df1F8ead70E32E7C0c",
"veFeeDistributor": "0x35F1e6765750E874EB9d0675393A1A394A4749b4",
"veDelegationProxy": "0x51B1b14b8bfb43a2fB0b49843787Ca440200F6b7",
"DFRewards": "0x6BB265D6c08b7E7432dF9B3D3499beEAA9856232",
"DFStrategyV1": "0x98FBBB6523441b960E4D1d9A98601332092F4aB6",
"veFeeEstimate": "0xCFeF55c6ae4d250586e293f29832967a04A9087d",
"providerAddress": "0x68C24FA5b2319C81b34f248d1f928601D2E5246B"
}
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"scripts": {
"start": "npx tsc && node dist/index.js",
"start": "npx tsc && node dist/src/index.js",
"test:audit:publish": "npx tsc && node dist/test/auditTrail.test.js",
"test:consumption:ctd": "npx tsc && node dist/test/consumptionReport.test.js",
"lint": "eslint",
"lint:format": "eslint && prettier src/**/*.ts --write"
},
"dependencies": {
"@deltadao/nautilus": "^1.0.0-beta.1",
"dotenv": "^16.4.5",
"ethers": "^5.7.2"
},
"devDependencies": {
Expand Down
143 changes: 143 additions & 0 deletions src/audit/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import {
AssetBuilder,
FileTypes,
LogLevel,
Nautilus,
PublishResponse,
ServiceBuilder,
ServiceTypes,
UrlFile
} from '@deltadao/nautilus'
import PONTUSX_CONFIG from '../../config/pontusx.json'
import { Wallet, ethers } from 'ethers'
import fs from 'fs'
import path from 'path'
import { Config } from '@oceanprotocol/lib'

const reports_dir = path.join(__dirname, `../reports`)
if (!fs.existsSync(reports_dir)) fs.mkdirSync(reports_dir)

const CONFIG: Config = PONTUSX_CONFIG

export async function createPublishAuditTrail() {
const { nodeUri } = CONFIG

const provider = new ethers.providers.JsonRpcProvider(nodeUri)
const wallet = new Wallet(process.env.PRIVATE_KEY, provider)

Nautilus.setLogLevel(LogLevel.Verbose)
const nautilus = await Nautilus.create(wallet, CONFIG)

const owner = await wallet.getAddress()

const serviceBuilder = new ServiceBuilder({
serviceType: ServiceTypes.ACCESS,
fileType: FileTypes.URL
}) // access type dataset with URL data source

const urlFile: UrlFile = {
type: 'url', // there are multiple supported data source types, see https://docs.oceanprotocol.com/developers/storage
url: 'https://raw.githubusercontent.com/deltaDAO/nautilus-examples/main/example_publish_assets/example-dataset.json', // link to your file or api
method: 'GET' // HTTP request method
// headers: {
// Authorization: 'Basic XXX' // optional headers field e.g. for basic access control
// }
}

const service = serviceBuilder
.setServiceEndpoint(CONFIG.providerUri)
.setTimeout(60)
.addFile(urlFile)
.setPricing({ type: 'free' })
.setDatatokenNameAndSymbol('Test Datatoken', 'TDT') // important for following access token transactions in the explorer
.build()

const assetBuilder = new AssetBuilder()
const asset = assetBuilder
.setType('dataset')
.setName('Nautilus-Report: Access Dataset')
.setDescription(
'# Nautilus-Example Description \n\nThis asset has been published using the [nautilus-examples](https://github.com/deltaDAO/nautilus-examples) repository.'
)
.setAuthor('Example')
.setLicense('MIT')
.addService(service)
.setOwner(owner)
.build()

const result = await nautilus.publish(asset)
console.log(`Publishing complete:`, result)

const auditTrailName = `${reports_dir}/publish-${result.nftAddress}-trail.md`

const auditTrail = transformPublishOutputToAuditTrail(result)

fs.writeFileSync(auditTrailName, auditTrail)
console.log(`Created a new publish audit trail: ${auditTrailName}`)
}

export function transformPublishOutputToAuditTrail(
result: PublishResponse
): string {
const { nftAddress, ddo, services, setMetadataTxReceipt } = result

const title = `Publish Report for NFT ${nftAddress}`
const metadata = `---\ntitle: ${title}\ndate: ${ddo.metadata.created}\n---`

let markdownString = ''
markdownString += metadata
markdownString += `\n`
markdownString += `\n`
markdownString += `# ${title}`
markdownString += `\n`
markdownString += `\n`
markdownString += `Successfully published a ${ddo.metadata.type} asset with ${services.length} service(s) on network ${ddo.chainId} at ${ddo.metadata.created}.`
markdownString += `\n`
markdownString += `\n`
markdownString += `## Service Overview`
services.forEach((service, index) => {
const serviceId = ddo.services.find(
(s) => s.datatokenAddress === service.datatokenAddress
)?.id
markdownString += `\n`
markdownString += `\n`
markdownString += `### Service ${serviceId || index}`
markdownString += `\n`
if (service.service.name)
markdownString += `- **Service Name**: ${service.service.name}\n`
if (service.service.description)
markdownString += `- **Service Description**: ${service.service.description}\n`
markdownString += `- **Service Datatoken**: ${service.datatokenAddress}`
markdownString += `\n`
markdownString += getTransactionMarkdown(service.tx)
})
markdownString += `\n`
markdownString += `\n`
markdownString += `## Asset Metadata`
markdownString += `\n`
markdownString += `- **DID**: ${ddo.id}`
markdownString += `\n`
markdownString += `- **Name**: ${ddo.metadata.name}`
markdownString += `\n`
markdownString += `- **Created**: ${ddo.metadata.created}`
markdownString += `\n`
markdownString += `- **Explore**: on [Pontus-X portal](https://pontus-x.eu/asset/${ddo.id})`
markdownString += `\n`
markdownString += getTransactionMarkdown(setMetadataTxReceipt)

return markdownString
}

export function getTransactionMarkdown(tx) {
let markdownString = '- **Transaction**:'
markdownString += `\n`
markdownString += ` - **Hash**: ${tx.transactionHash}`
markdownString += `\n`
markdownString += ` - **Block Number**: ${tx.blockNumber}`
if (CONFIG.explorerUri) {
markdownString += `\n`
markdownString += ` - **Explorer Link**: [${CONFIG.explorerUri}/tx/${tx.transactionHash.substring(0, 6)}...](${CONFIG.explorerUri}/tx/${tx.transactionHash})`
}

return markdownString
}
112 changes: 112 additions & 0 deletions src/consumption/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { LogLevel, Nautilus } from '@deltadao/nautilus'
import { Config } from '@oceanprotocol/lib'
import { Wallet, ethers } from 'ethers'
import fs from 'fs'
import path from 'path'
import PONTUSX_CONFIG from '../../config/pontusx.json'

const reports_dir = path.join(__dirname, `../reports`)
if (!fs.existsSync(reports_dir)) fs.mkdirSync(reports_dir)

const CONFIG: Config = PONTUSX_CONFIG

const DATASET_DID = `did:op:272c5fe3e0fdb1a1c06466e60fa229f2bbb7be8f54b5cb26c28952fc9d43d243`
const ALGO_DID = `did:op:0ae3069c26e968057a5a7666b07eb29928ce55744e6be3240eb7a5affa973445`

export async function createConsumptionReport() {
const { nodeUri } = CONFIG

const provider = new ethers.providers.JsonRpcProvider(nodeUri)
const wallet = new Wallet(process.env.PRIVATE_KEY, provider)

Nautilus.setLogLevel(LogLevel.Verbose)
const nautilus = await Nautilus.create(wallet, CONFIG)

const computeResult = await nautilus.compute({
dataset: {
did: DATASET_DID
},
algorithm: {
did: ALGO_DID
}
})

const computeJob = Array.isArray(computeResult)
? computeResult[0]
: computeResult

const datasetDDO = await nautilus.getAquariusAsset(DATASET_DID)

let computeStatus = { ...computeJob }
// 31 - DATA PROVISIONING FAILED
// 32 - ALGORITHM PROVISIONING FAILED
// 70 - JOB FINISHED
while (![31, 32, 70].includes(computeStatus.status)) {
console.log('Polling compute status update...')
computeStatus = await nautilus.getComputeStatus({
jobId: computeJob.jobId,
providerUri: datasetDDO.services.find(
(service) => service.type === 'compute'
).serviceEndpoint
})
console.log('New status:', { computeStatus })
await new Promise((resolve) => setTimeout(resolve, 5000))
}

const reportName = `${reports_dir}/consumption-report-${computeJob.jobId}.md`

const consumptiuonReport = transformComputeResponseToReport(
computeJob,
computeStatus
)

fs.writeFileSync(reportName, consumptiuonReport)
console.log(`Created a new consumption report: ${reportName}`)
}

export function transformComputeResponseToReport(job, finalStatus): string {
let markdownString = ``

const title = `Consumption Report for Compute Job ${job.jobId}`
const metadata = `---\ntitle: ${title}\ndate:${job.dateCreated}\n---`

markdownString += metadata
markdownString += `\n`
markdownString += `\n`
markdownString += `# ${title}`
markdownString += `\n`
markdownString += `\n`
markdownString += `## Overview`
markdownString += `\n`
markdownString += `- **Owner**: ${job.owner}`
markdownString += `\n`
markdownString += `- **Dataset**: [${job.inputDID[0]}](https://pontus-x.eu/asset/${job.inputDID[0]})`
markdownString += `\n`
markdownString += `- **Algorithm**: [${job.algoDID}](https://pontus-x.eu/asset/${job.algoDID})`
markdownString += `\n`
markdownString += `- **Date created**: ${job.dateCreated}`
markdownString += `\n`
markdownString += `- **Transaction**: [${job.agreementId}](${CONFIG.explorerUri}/tx/${job.agreementId})`
markdownString += `\n`
markdownString += `- **Explore**: on your [Pontus-X portal profile](https://pontus-x.eu/profile/${job.owner}?defaultTab=computeJobs)`
markdownString += `\n`
markdownString += `\n`
markdownString += `## Status Report`
markdownString += `\n`
markdownString += `- **Final status**: ${finalStatus.statusText} (${finalStatus.status})`
markdownString += `\n`
markdownString += `- **Date finished**: ${finalStatus.dateFinished}`
markdownString += `\n`
markdownString += `- **Results**:`
markdownString += `\n`
finalStatus.results.forEach((result) => {
markdownString += ` - **Filename**: ${result.filename}`
markdownString += `\n`
markdownString += ` - **Filesize (kb)**: ${result.filesize}`
markdownString += `\n`
markdownString += ` - **Type**: ${result.type}`
markdownString += `\n`
})

return markdownString
}
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as dotenv from 'dotenv'
import { createPublishAuditTrail } from './audit'
import { createConsumptionReport } from './consumption'
dotenv.config()

// createPublishAuditTrail()

createConsumptionReport()
Loading

0 comments on commit 36f1aab

Please sign in to comment.