Skip to content

Commit

Permalink
Merge pull request #569 from lidofinance/refactor/adi-bsc
Browse files Browse the repository at this point in the history
add `handleBlock` to `bsc-adi-governance`
  • Loading branch information
katamarinaki authored Jun 20, 2024
2 parents 4b32edc + 3c7a0b7 commit d8de307
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
3 changes: 0 additions & 3 deletions bsc-adi-governance/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ LABEL "network.forta.settings.agent-logs.enable"="true"
ENV NODE_ENV=production
WORKDIR /app

RUN apk add --no-cache tini=0.19.0-r0

COPY package*.json yarn.lock ./
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/dist ./src
COPY version.json ./

ENTRYPOINT ["/sbin/tini", "--"]
CMD ["yarn", "run", "start:prod"]
31 changes: 29 additions & 2 deletions bsc-adi-governance/src/agent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Finding, FindingSeverity, FindingType, HandleTransaction, HealthCheck } from 'forta-agent'
import { BlockEvent, Finding, FindingSeverity, FindingType, HandleTransaction, HealthCheck } from 'forta-agent'
import * as process from 'process'
import { InitializeResponse } from 'forta-agent/dist/sdk/initialize.response'
import { Initialize } from 'forta-agent/dist/sdk/handlers'
import { HandleBlock, Initialize } from 'forta-agent/dist/sdk/handlers'
import * as E from 'fp-ts/Either'
import { App } from './app'
import { elapsedTime } from './utils/time'
Expand Down Expand Up @@ -55,6 +55,32 @@ export function initialize(): Initialize {
}
}

let isHandleBlockRunning: boolean = false
export const handleBlock = (): HandleBlock => {
return async function (blockEvent: BlockEvent): Promise<Finding[]> {
console.log(`#BSC block: ${blockEvent.block.number}`)
const startTime = new Date().getTime()
if (isHandleBlockRunning) {
return []
}

isHandleBlockRunning = true
const app = await App.getInstance()

const findings: Finding[] = []
const findingsAsync = await app.findingsRW.read()
if (findingsAsync.length > 0) {
findings.push(...findingsAsync)
}

app.healthChecker.check(findings)

console.log(elapsedTime('handleBlock', startTime) + '\n')
isHandleBlockRunning = false
return findings
}
}

export const handleTransaction = (): HandleTransaction => {
return async function (txEvent: TransactionEvent): Promise<Finding[]> {
const app = await App.getInstance()
Expand Down Expand Up @@ -85,6 +111,7 @@ export const healthCheck = (): HealthCheck => {

export default {
initialize: initialize(),
handleBlock: handleBlock(),
handleTransaction: handleTransaction(),
healthCheck: healthCheck(),
}

0 comments on commit d8de307

Please sign in to comment.