Skip to content

Commit

Permalink
feat: update bulk insert with test codes
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Dec 8, 2023
1 parent 20a52c8 commit d7e1416
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 137 deletions.
18 changes: 18 additions & 0 deletions cli/src/cli-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,21 @@ export interface IAggregator {
absoluteThreshold: number
adapterHash: string
}

export interface IDatafeedBulkInsertElement {
adapterSource: string
aggregatorSource: string
reporter: {
walletAddress: string
walletPrivateKey: string
}
}

export interface IDatafeedBulk {
chain?: string
service?: string
organization?: string
functionName?: string
eventName?: string
bulk: IDatafeedBulkInsertElement[]
}
28 changes: 17 additions & 11 deletions cli/src/datafeed.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 { IDatafeedBulk, ReadFile } from './cli-types'
import {
contractConnectHandler,
contractInsertHandler,
Expand All @@ -24,7 +24,11 @@ interface InsertElement {

export function datafeedSub() {
// datafeed bulk-insert --source ${source}

// TODOs
// datafeed bulk-remove --source ${source}
// datafeed bulk-activate --source ${source}
// datafeed bulk-deactivate --source ${source}

const insert = command({
name: 'bulk-insert',
Expand All @@ -45,11 +49,13 @@ export function datafeedSub() {

export function bulkInsertHandler() {
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 bulkData = data as IDatafeedBulk

const chain = bulkData?.chain || 'localhost'
const service = bulkData?.service || 'DATA_FEED'
const organization = bulkData?.organization || 'bisonai'
const functionName = bulkData?.functionName || 'submit(uint256, int256)'
const eventName = bulkData?.eventName || 'NewRound'
const organizationId = (await organizationListHandler()()).find(
(_organization) => _organization.name == organization
).id
Expand All @@ -58,7 +64,7 @@ export function bulkInsertHandler() {
console.error('invalid json src format')
return
}
for (const insertElement of data.bulk) {
for (const insertElement of bulkData.bulk) {
const adapterData = await loadJsonFromUrl(insertElement.adapterSource)
const aggregatorData = await loadJsonFromUrl(insertElement.aggregatorSource)

Expand All @@ -67,18 +73,18 @@ export function bulkInsertHandler() {

const reporterInsertResult = await delegatorReporterInsertHandler()({
address: insertElement.reporter.walletAddress,
organizationId
organizationId: Number(organizationId)
})
const contractInsertResult = await contractInsertHandler()({
address: aggregatorData.address
})
await functionInsertHandler()({
name: functionName,
contractId: contractInsertResult.id
contractId: Number(contractInsertResult.id)
})
await contractConnectHandler()({
contractId: contractInsertResult.id,
reporterId: reporterInsertResult.id
contractId: Number(contractInsertResult.id),
reporterId: Number(reporterInsertResult.id)
})

await reporterInsertHandler()({
Expand Down
45 changes: 1 addition & 44 deletions cli/test/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,8 @@
import { describe, expect, test } from '@jest/globals'
import { insertHandler, listHandler, removeHandler } from '../src/adapter'
import { ADAPTER_0, ADAPTER_1 } from './mockData'

describe('CLI Adapter', function () {
const ADAPTER_0 = {
active: true,
name: 'X-Y',
decimals: 8,
adapterHash: '0x020e150749af3bffaec9ae337da0b9b00c3cfe0b46b854a8e2f5922f6ba2c5db',
feeds: [
{
name: 'data-X-Y',
definition: {
url: 'https://data.com',
headers: { 'Content-Type': 'application/json' },
method: 'GET',
reducers: [
{ function: 'PARSE', args: ['PRICE'] },
{ function: 'POW10', args: '8' },
{ function: 'ROUND' }
]
}
}
]
}

const ADAPTER_1 = {
active: true,
name: 'Z-X',
decimals: 8,
adapterHash: '0x12da2f5119ba624ed025303b424d637349c0d120d02bd66a9cfff57e98463a81',
feeds: [
{
name: 'data-Z-X',
definition: {
url: 'https://data.com',
headers: { 'Content-Type': 'application/json' },
method: 'GET',
reducers: [
{ function: 'PARSE', args: ['PRICE'] },
{ function: 'POW10', args: '8' },
{ function: 'ROUND' }
]
}
}
]
}

let initalAdapterId
beforeAll(async () => {
// insert default adapter
Expand Down
65 changes: 1 addition & 64 deletions cli/test/aggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,72 +10,9 @@ import {
listHandler as chainListHandler,
removeHandler as chainRemoveHandler
} from '../src/chain'
import { ADAPTER_0, ADAPTER_1, AGGREGATOR_0, AGGREGATOR_1 } from './mockData'

describe('CLI Aggregator', function () {
const ADAPTER_0 = {
active: true,
name: 'X-Y',
decimals: 8,
adapterHash: '0x020e150749af3bffaec9ae337da0b9b00c3cfe0b46b854a8e2f5922f6ba2c5db',
feeds: [
{
name: 'data-X-Y',
definition: {
url: 'https://data.com',
headers: { 'Content-Type': 'application/json' },
method: 'GET',
reducers: [
{ function: 'PARSE', args: ['PRICE'] },
{ function: 'POW10', args: '8' },
{ function: 'ROUND' }
]
}
}
]
}

const ADAPTER_1 = {
active: true,
name: 'Z-X',
decimals: 8,
adapterHash: '0x12da2f5119ba624ed025303b424d637349c0d120d02bd66a9cfff57e98463a81',
feeds: [
{
name: 'data-Z-X',
definition: {
url: 'https://data.com',
headers: { 'Content-Type': 'application/json' },
method: 'GET',
reducers: [
{ function: 'PARSE', args: ['PRICE'] },
{ function: 'POW10', args: '8' },
{ function: 'ROUND' }
]
}
}
]
}

const AGGREGATOR_0 = {
name: 'X-Y',
aggregatorHash: '0x5bcc6c18d584dc54a666f9212229226f02f65b8dcda3ed72836b6c901f2d18e1',
address: '0x0000000000000000000000000000000000000000',
heartbeat: 15000,
threshold: 0.05,
absoluteThreshold: 0.1,
adapterHash: '0x020e150749af3bffaec9ae337da0b9b00c3cfe0b46b854a8e2f5922f6ba2c5db'
}

const AGGREGATOR_1 = {
name: 'Z-X',
aggregatorHash: '0x11ca65b539221125a64b38653f65dbbf961ed2ea16bcaf54408a5d2ebdc13a0b',
address: '0x0000000000000000000000000000000000000001',
heartbeat: 15000,
threshold: 0.05,
absoluteThreshold: 0.1,
adapterHash: '0x12da2f5119ba624ed025303b424d637349c0d120d02bd66a9cfff57e98463a81'
}

let initialAggregatorId
beforeAll(async () => {
await chainInsertHandler()({ name: 'localhost' })
Expand Down
156 changes: 156 additions & 0 deletions cli/test/datafeed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { describe } from '@jest/globals'
import {
listHandler as adapterListHandler,
removeHandler as adapterRemoveHandler
} from '../src/adapter'
import {
listHandler as aggregatorListHandler,
removeHandler as aggregatorRemoveHandler
} from '../src/aggregator'
import {
insertHandler as chainInsertHandler,
listHandler as chainListHandler,
removeHandler as chainRemoveHandler
} from '../src/chain'
import { bulkInsertHandler } from '../src/datafeed'
import {
contractListHandler,
contractRemoveHandler,
functionListHandler,
functionRemoveHandler,
organizationInsertHandler,
organizationListHandler,
organizationRemoveHandler,
reporterListHandler as delegatorReporterListHandler,
reporterRemoveHandler as delegatorReporterRemoveHandler
} from '../src/delegator'
import {
listHandler as listenerListHandler,
removeHandler as listenerRemoveHandler
} from '../src/listener'
import {
listHandler as reporterListHandler,
removeHandler as reporterRemoveHandler
} from '../src/reporter'
import {
insertHandler as serviceInsertHandler,
listHandler as serviceListHandler,
removeHandler as serviceRemoveHandler
} from '../src/service'
import { DATAFEED_BULK_0, DATAFEED_BULK_1 } from './mockData'

describe('CLI datafeed', function () {
beforeAll(async () => {
await chainInsertHandler()({ name: 'localhost' })
await chainInsertHandler()({ name: 'baobab' })
await serviceInsertHandler()({ name: 'DATA_FEED' })
await serviceInsertHandler()({ name: 'DATA_FEED_V2' })
await organizationInsertHandler()({ name: 'bisonai' })
await organizationInsertHandler()({ name: 'kf' })
})

afterAll(async () => {
const chains = await chainListHandler()()
for (const chain of chains) {
await chainRemoveHandler()({ id: chain.id })
}
const services = await serviceListHandler()()
for (const service of services) {
await serviceRemoveHandler()({ id: service.id })
}
const organizations = await organizationListHandler()()
for (const organization of organizations) {
await organizationRemoveHandler()({ id: organization.id })
}
})

afterEach(async () => {
const afterAdapterList = await adapterListHandler()()
const afterAggregatorList = await aggregatorListHandler()({})
const afterDelegatorReporterList = await delegatorReporterListHandler()()
const afterContractList = await contractListHandler()()
const afterListenerList = await listenerListHandler()({})
const afterReporterList = await reporterListHandler()({})
const afterFunctionList = await functionListHandler()()

for (const _function of afterFunctionList) {
await functionRemoveHandler()({ id: Number(_function.id) })
}
for (const aggregator of afterAggregatorList) {
await aggregatorRemoveHandler()({ id: aggregator.id })
}

for (const adapter of afterAdapterList) {
await adapterRemoveHandler()({ id: adapter.id })
}

for (const delegatorReporter of afterDelegatorReporterList) {
await delegatorReporterRemoveHandler()({ id: delegatorReporter.id })
}
for (const contract of afterContractList) {
await contractRemoveHandler()({ id: contract.id })
}
for (const listener of afterListenerList) {
await listenerRemoveHandler()({ id: listener.id })
}
for (const reporter of afterReporterList) {
await reporterRemoveHandler()({ id: reporter.id })
}
})

test('datafeed bulk insert with default values', async function () {
const beforeAdapterList = await adapterListHandler()()
const beforeAggregatorList = await aggregatorListHandler()({})
const beforeDelegatorReporterList = await delegatorReporterListHandler()()
const beforeContractList = await contractListHandler()()
const beforeListenerList = await listenerListHandler()({})
const beforeReporterList = await reporterListHandler()({})
const beforeFunctionList = await functionListHandler()()

await bulkInsertHandler()({ data: DATAFEED_BULK_0 })

const afterAdapterList = await adapterListHandler()()
const afterAggregatorList = await aggregatorListHandler()({})
const afterDelegatorReporterList = await delegatorReporterListHandler()()
const afterContractList = await contractListHandler()()
const afterListenerList = await listenerListHandler()({})
const afterReporterList = await reporterListHandler()({})
const afterFunctionList = await functionListHandler()()

expect(afterAdapterList.length).toEqual(beforeAdapterList.length + 3)
expect(afterAggregatorList.length).toEqual(beforeAggregatorList.length + 3)
expect(afterDelegatorReporterList.length).toEqual(beforeDelegatorReporterList.length + 3)
expect(afterContractList.length).toEqual(beforeContractList.length + 3)
expect(afterListenerList.length).toEqual(beforeListenerList.length + 3)
expect(afterReporterList.length).toEqual(beforeReporterList.length + 3)
expect(afterFunctionList.length).toEqual(beforeFunctionList.length + 3)
})

test('datafeed bulk insert', async function () {
const beforeAdapterList = await adapterListHandler()()
const beforeAggregatorList = await aggregatorListHandler()({})
const beforeDelegatorReporterList = await delegatorReporterListHandler()()
const beforeContractList = await contractListHandler()()
const beforeListenerList = await listenerListHandler()({})
const beforeReporterList = await reporterListHandler()({})
const beforeFunctionList = await functionListHandler()()

await bulkInsertHandler()({ data: DATAFEED_BULK_1 })

const afterAdapterList = await adapterListHandler()()
const afterAggregatorList = await aggregatorListHandler()({})
const afterDelegatorReporterList = await delegatorReporterListHandler()()
const afterContractList = await contractListHandler()()
const afterListenerList = await listenerListHandler()({})
const afterReporterList = await reporterListHandler()({})
const afterFunctionList = await functionListHandler()()

expect(afterAdapterList.length).toEqual(beforeAdapterList.length + 3)
expect(afterAggregatorList.length).toEqual(beforeAggregatorList.length + 3)
expect(afterDelegatorReporterList.length).toEqual(beforeDelegatorReporterList.length + 3)
expect(afterContractList.length).toEqual(beforeContractList.length + 3)
expect(afterListenerList.length).toEqual(beforeListenerList.length + 3)
expect(afterReporterList.length).toEqual(beforeReporterList.length + 3)
expect(afterFunctionList.length).toEqual(beforeFunctionList.length + 3)
})
})
Loading

0 comments on commit d7e1416

Please sign in to comment.