Skip to content

Commit

Permalink
feat: remove bulk-activate
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Dec 8, 2023
1 parent f926a9b commit 837067e
Showing 1 changed file with 26 additions and 90 deletions.
116 changes: 26 additions & 90 deletions cli/src/datafeed.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
import { command, option, string as cmdstring, subcommands } from 'cmd-ts'
import { command, option, subcommands } from 'cmd-ts'
import { insertHandler as adapterInsertHandler } from './adapter'
import { insertHandler as aggregatorInsertHandler } from './aggregator'
import { ReadFile } from './cli-types'
import {
contractConnectHandler,
contractInsertHandler,
functionInsertHandler,
organizationListHandler,
reporterInsertHandler as delegatorReporterInsertHandler
} from './delegator'
import {
activateHandler as activateListenerHandler,
insertHandler as listenerInsertHandler,
listHandler as listenerListHandler
} from './listener'
import {
activateHandler as activateReporterHandler,
insertHandler as reporterInsertHandler,
listHandler as reporterListHandler
} from './reporter'
import { insertHandler as listenerInsertHandler } from './listener'
import { insertHandler as reporterInsertHandler } from './reporter'
import { isValidUrl, loadJsonFromUrl } from './utils'

import { activateHandler as activateAggregatorHandler } from './aggregator'
import { startHandler as startFetcherHandler } from './fetcher'

interface InsertElement {
adapterSource: string
aggregatorSource: string
Expand All @@ -42,43 +32,33 @@ export function datafeedSub() {
data: option({
type: ReadFile,
long: 'source'
}),
chain: option({
type: cmdstring,
long: 'chain'
})
},
handler: bulkInsertHandler()
})

const activate = command({
name: 'bulk-activate',
args: {
data: option({
type: ReadFile,
long: 'source'
}),
chain: option({
type: cmdstring,
long: 'chain'
})
},
handler: bulkActivateHandler()
})

return subcommands({
name: 'adapter',
cmds: { insert, activate }
name: 'datafeed',
cmds: { insert }
})
}

export function bulkInsertHandler() {
async function wrapper({ data, chain }: { data; chain: string }) {
if (!checkBulkSource(data)) {
async function wrapper({ data }: { data }) {
const chain = data?.chain || 'baobab'
const service = data?.service || 'DATA_FEED'
const organization = data?.orgainzation || 'bisonai'
const functionName = data?.functionName || 'submit(uint256, int256)'
const eventName = data?.eventName || 'NewRound'
const organizationId = (await organizationListHandler()()).find(
(_organization) => _organization.name == organization
).id

if (!checkBulkSource(data?.bulk)) {
console.error('invalid json src format')
return
}
for (const insertElement of data) {
for (const insertElement of data.bulk) {
const adapterData = await loadJsonFromUrl(insertElement.adapterSource)
const aggregatorData = await loadJsonFromUrl(insertElement.aggregatorSource)

Expand All @@ -87,13 +67,13 @@ export function bulkInsertHandler() {

const reporterInsertResult = await delegatorReporterInsertHandler()({
address: insertElement.reporter.walletAddress,
organizationId: 1 // bisonai fixed to 1
organizationId
})
const contractInsertResult = await contractInsertHandler()({
address: aggregatorData.address
})
await functionInsertHandler()({
name: 'submit(uint256, int256)',
name: functionName,
contractId: contractInsertResult.id
})
await contractConnectHandler()({
Expand All @@ -103,72 +83,28 @@ export function bulkInsertHandler() {

await reporterInsertHandler()({
chain,
service: 'DATA_FEED',
service: service,
privateKey: insertElement.reporter.walletPrivateKey,
address: insertElement.reporter.walletAddress,
oracleAddress: aggregatorData.address
})

await listenerInsertHandler()({
chain,
service: 'DATA_FEED',
service: service,
address: aggregatorData.address,
eventName: 'NewRound'
})
}
}
return wrapper
}

export function bulkActivateHandler() {
const FETCHER_PORT = 4040
const WORKER_PORT = 5000
const LISTENER_PORT = 4000
const REPORTER_PORT = 6000
async function wrapper({ data, chain }: { data; chain: string }) {
const listeners = await listenerListHandler(false)({ chain, service: 'DATA_FEED' })
const reporters = await reporterListHandler(false)({ chain, service: 'DATA_FEED' })

for (const insertElement of data) {
const aggregatorData = await loadJsonFromUrl(insertElement.aggregatorSource)

const reporterId = reporters.find(
(item) => item.oracleAddress == aggregatorData.contractAddress
)
const listenerId = listeners.find((item) => item.address == aggregatorData.address)

await startFetcherHandler()({
id: aggregatorData.aggregatorHash,
chain,
host: 'http://fetcher.orakl.svc.cluster.local',
port: '4040'
})
await activateAggregatorHandler()({
aggregatorHash: aggregatorData.aggregatorHash,
host: 'http://worker.orakl.svc.cluster.local',
port: '5000'
})

await activateReporterHandler()({
id: reporterId,
host: 'http://reporter.orakl.svc.cluster.local',
port: REPORTER_PORT.toString()
})
await activateListenerHandler()({
id: listenerId,
host: 'http://listener.orakl.svc.cluster.local',
port: LISTENER_PORT.toString()
eventName
})
}
}
return wrapper
}

function checkBulkSource(data: InsertElement[]) {
if (!data || data.length == 0) {
function checkBulkSource(bulkData: InsertElement[]) {
if (!bulkData || bulkData.length == 0) {
return false
}
for (const insertElement of data) {
for (const insertElement of bulkData) {
if (!isValidUrl(insertElement.adapterSource) || !isValidUrl(insertElement.aggregatorSource)) {
return false
}
Expand Down

0 comments on commit 837067e

Please sign in to comment.