diff --git a/cli/src/cli-types.ts b/cli/src/cli-types.ts index 5e892a12c..3f349f7d2 100644 --- a/cli/src/cli-types.ts +++ b/cli/src/cli-types.ts @@ -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[] +} diff --git a/cli/src/datafeed.ts b/cli/src/datafeed.ts index 3b8efecff..0ebfbbf9b 100644 --- a/cli/src/datafeed.ts +++ b/cli/src/datafeed.ts @@ -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, @@ -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', @@ -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 @@ -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) @@ -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()({ diff --git a/cli/test/adapter.ts b/cli/test/adapter.ts index a68bd8de8..853ac16ff 100644 --- a/cli/test/adapter.ts +++ b/cli/test/adapter.ts @@ -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 diff --git a/cli/test/aggregator.ts b/cli/test/aggregator.ts index 44a6cd0a4..3bd16caf4 100644 --- a/cli/test/aggregator.ts +++ b/cli/test/aggregator.ts @@ -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' }) diff --git a/cli/test/datafeed.test.ts b/cli/test/datafeed.test.ts new file mode 100644 index 000000000..c4f093a82 --- /dev/null +++ b/cli/test/datafeed.test.ts @@ -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) + }) +}) diff --git a/cli/test/mockData.ts b/cli/test/mockData.ts new file mode 100644 index 000000000..936ef7632 --- /dev/null +++ b/cli/test/mockData.ts @@ -0,0 +1,144 @@ +export 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' } + ] + } + } + ] +} + +export 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' } + ] + } + } + ] +} + +export const AGGREGATOR_0 = { + name: 'X-Y', + aggregatorHash: '0x5bcc6c18d584dc54a666f9212229226f02f65b8dcda3ed72836b6c901f2d18e1', + address: '0x0000000000000000000000000000000000000000', + heartbeat: 15000, + threshold: 0.05, + absoluteThreshold: 0.1, + adapterHash: '0x020e150749af3bffaec9ae337da0b9b00c3cfe0b46b854a8e2f5922f6ba2c5db' +} + +export const AGGREGATOR_1 = { + name: 'Z-X', + aggregatorHash: '0x11ca65b539221125a64b38653f65dbbf961ed2ea16bcaf54408a5d2ebdc13a0b', + address: '0x0000000000000000000000000000000000000001', + heartbeat: 15000, + threshold: 0.05, + absoluteThreshold: 0.1, + adapterHash: '0x12da2f5119ba624ed025303b424d637349c0d120d02bd66a9cfff57e98463a81' +} + +export const VRF_0 = { + chain: 'baobab', + sk: 'adcfaf9a860722a89472884a2aab4a62f06a42fd4bee55f2fc7f2f11b07f1d81', + pk: '041f058731839e8c2fb3a77a4be788520f1743f1298a84bd138871f31ffdee04e42b4f962995ba0135eed67f3ebd1739d4b09f1b84224c0d6765e5f426b25443a4', + pkX: '14031465612060486287063884409830887522455901523026705297854775800516553082084', + pkY: '19590069790275828365845547074408283587257770205538752975574862882950389973924', + keyHash: '0x956506aeada5568c80c984b908e9e1af01bd96709977b0b5cb1957736e80e883' +} + +export const VRF_1 = { + chain: 'localhost', + sk: '123', + pk: '456', + pkX: '789', + pkY: '101112', + keyHash: '0x' +} + +export const DATAFEED_BULK_0 = { + bulk: [ + { + adapterSource: 'https://config.orakl.network/adapter/ada-usdt.adapter.json', + aggregatorSource: 'https://config.orakl.network/aggregator/baobab/ada-usdt.aggregator.json', + reporter: { + walletAddress: '0xa', + walletPrivateKey: '0xb' + } + }, + { + adapterSource: 'https://config.orakl.network/adapter/atom-usdt.adapter.json', + aggregatorSource: 'https://config.orakl.network/aggregator/baobab/atom-usdt.aggregator.json', + reporter: { + walletAddress: '0xc', + walletPrivateKey: '0xd' + } + }, + { + adapterSource: 'https://config.orakl.network/adapter/avax-usdt.adapter.json', + aggregatorSource: 'https://config.orakl.network/aggregator/baobab/avax-usdt.aggregator.json', + reporter: { + walletAddress: '0xe', + walletPrivateKey: '0xf' + } + } + ] +} + +export const DATAFEED_BULK_1 = { + chain: 'baobab', + service: 'DATA_FEED_V2', + organization: 'kf', + functionName: 'submitV2(uint256, int256)', + eventName: 'NewRoundV2', + bulk: [ + { + adapterSource: 'https://config.orakl.network/adapter/bnb-usdt.adapter.json', + aggregatorSource: 'https://config.orakl.network/aggregator/baobab/bnb-usdt.aggregator.json', + reporter: { + walletAddress: '0x0', + walletPrivateKey: '0x1' + } + }, + { + adapterSource: 'https://config.orakl.network/adapter/bora-krw.adapter.json', + aggregatorSource: 'https://config.orakl.network/aggregator/baobab/bora-krw.aggregator.json', + reporter: { + walletAddress: '0x2', + walletPrivateKey: '0x3' + } + }, + { + adapterSource: 'https://config.orakl.network/adapter/eth-usdt.adapter.json', + aggregatorSource: 'https://config.orakl.network/aggregator/baobab/eth-usdt.aggregator.json', + reporter: { + walletAddress: '0x4', + walletPrivateKey: '0x5' + } + } + ] +} diff --git a/cli/test/vrf.test.ts b/cli/test/vrf.test.ts index 59cd44281..a64d102fc 100644 --- a/cli/test/vrf.test.ts +++ b/cli/test/vrf.test.ts @@ -5,26 +5,9 @@ import { removeHandler as chainRemoveHandler } from '../src/chain' import { insertHandler, listHandler, removeHandler } from '../src/vrf' +import { VRF_0, VRF_1 } from './mockData' describe('CLI Vrf', function () { - const VRF_0 = { - chain: 'baobab', - sk: 'adcfaf9a860722a89472884a2aab4a62f06a42fd4bee55f2fc7f2f11b07f1d81', - pk: '041f058731839e8c2fb3a77a4be788520f1743f1298a84bd138871f31ffdee04e42b4f962995ba0135eed67f3ebd1739d4b09f1b84224c0d6765e5f426b25443a4', - pkX: '14031465612060486287063884409830887522455901523026705297854775800516553082084', - pkY: '19590069790275828365845547074408283587257770205538752975574862882950389973924', - keyHash: '0x956506aeada5568c80c984b908e9e1af01bd96709977b0b5cb1957736e80e883' - } - - const VRF_1 = { - chain: 'localhost', - sk: '123', - pk: '456', - pkX: '789', - pkY: '101112', - keyHash: '0x' - } - let initialVrfId beforeAll(async () => { await chainInsertHandler()({ name: 'baobab' })