Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added queue and worker for deviation #636

Merged
merged 4 commits into from
May 29, 2023

Conversation

KelvinThai
Copy link
Contributor

@KelvinThai KelvinThai commented May 25, 2023

Description

This PR implements deviation worker and queue.

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

Checklist before requesting a review

  • I have performed a self-review of my code.

Deployment

  • Should publish Docker image

@KelvinThai KelvinThai linked an issue May 25, 2023 that may be closed by this pull request
@KelvinThai KelvinThai marked this pull request as ready for review May 25, 2023 04:01
@KelvinThai KelvinThai requested a review from martinkersner as a code owner May 25, 2023 04:01
} from '../settings'
import { buildSubmissionRoundJobId, buildHeartbeatJobId } from '../utils'
import { buildSubmissionRoundJobId, buildHeartbeatJobId, hookConsoleError } from '../utils'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The imported hookConsoleError is not used anywhere in this file. Let's remove it.

connection: {
host: process.env.REDIS_HOST || 'localhost',
port: Number(process.env.REDIS_PORT) || 6379
BullModule.registerQueue(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using constants from settings?

import { FETCHER_QUEUE_NAME, DEVIATION_QUEUE_NAME } from '../settings'

@Module({
  imports: [
    BullModule.registerQueue(
    {
        name: FETCHER_QUEUE_NAME,
        connection: {
          host: process.env.REDIS_HOST || 'localhost',
          port: Number(process.env.REDIS_PORT) || 6379
        }
      },
      {
        name: DEVIATION_QUEUE_NAME,
        connection: {
          host: process.env.REDIS_HOST || 'localhost',
          port: Number(process.env.REDIS_PORT) || 6379
        }
      }
    )
  ],
...

logger
})

const { timestamp, value: submission } = await fetchDataFeed({ aggregatorHash, logger })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about passing the timestamp and submission through the job request rather than fetching back something what we already had access to during fetcher loop? We could remove the fetchDataFeed call and definition inside of Orakl Network Fetcher.

At line 504, we just just extract everything we need from job payload.

const {timestamp, submission, oracleAddress } = inData

const aggregatorHash = keys[0]
const aggregatorId = inData[aggregatorHash].aggregatorId
const feeds = inData[aggregatorHash].feeds
const adapterHash = keys[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for correcting this variable! 🙌

this.logger.debug(response)
if (abs(aggregate - Number(lastSubmission)) >= lastSubmission * threshold) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition is actually not correct because lastSubmission and submission operate with an extra decimals that are part of adapter definition (e.g. https://config.orakl.network/adapter/ada-usdt.adapter.json)

How about using this already implemented function instead?

/**
* Test whether the current submission deviates from the last
* submission more than given threshold or absolute threshold. If yes,
* return `true`, otherwise `false`.
*
* @param {number} latest submission value
* @param {number} current submission value
* @param {number} threshold configuration
* @param {number} absolute threshold configuration
* @return {boolean}
*/
export function shouldReport(
latestSubmission: number,
submission: number,
decimals: number,
threshold: number,
absoluteThreshold: number
): boolean {
if (latestSubmission && submission) {
const denominator = Math.pow(10, decimals)
const latestSubmissionReal = latestSubmission / denominator
const submissionReal = submission / denominator
const range = latestSubmissionReal * threshold
const l = latestSubmissionReal - range
const r = latestSubmissionReal + range
return submissionReal < l || submissionReal > r
} else if (!latestSubmission && submission) {
// latestSubmission hit zero
return submission > absoluteThreshold
} else {
// Something strange happened, don't report!
return false
}
}

…ON_QUEUE_NAME ' from setting, using 'shouldReport' function, added timestamp, submission variable in deviation queue
@KelvinThai
Copy link
Contributor Author

I fixed all the issues. Do you think we can merge this now @martinkersner

Copy link
Member

@martinkersner martinkersner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one thing left we should fix before merging. The rest looks great! Approving now, please fix the decimals, and then merge.

submission: aggregate,
oracleAddress
}
if (shouldReport(Number(lastSubmission), aggregate, 8, threshold, absoluteThreshold)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most likely the decimal value should be 8, but the actual settings that defines it is inside of adapter file. You should be able to get it the same way as threshold for example.

@KelvinThai KelvinThai merged commit ca639d3 into master May 29, 2023
@bayram98 bayram98 deleted the 630-implement-deviation-worker-for-datafeed branch July 3, 2023 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

implement deviation worker for datafeed
2 participants