Commit a7c89156 authored by Maurelian's avatar Maurelian

bmon: Report empty findings above a given threshold

bmon: Fix linter issues
parent d1f13662
export CHAINID=
export L1_RPC_URL= export L1_RPC_URL=
# Prepopulated with test values. # Prepopulated with test values.
...@@ -9,4 +8,3 @@ export SEQUENCER_DANGER_THRESHOLD=100 ...@@ -9,4 +8,3 @@ export SEQUENCER_DANGER_THRESHOLD=100
export PROPOSER_ADDRESS=0xacdc export PROPOSER_ADDRESS=0xacdc
export PROPOSER_WARNING_THRESHOLD=2000 export PROPOSER_WARNING_THRESHOLD=2000
export PROPOSER_DANGER_THRESHOLD=200 export PROPOSER_DANGER_THRESHOLD=200
...@@ -29,11 +29,12 @@ ...@@ -29,11 +29,12 @@
"forta-agent": "^0.1.1" "forta-agent": "^0.1.1"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^26.0.23", "@types/chai": "^4.2.18",
"@types/mocha": "^8.2.2",
"@types/nodemon": "^1.19.0", "@types/nodemon": "^1.19.0",
"jest": "^27.0.6", "chai": "^4.3.4",
"nodemon": "^2.0.8", "nodemon": "^2.0.8",
"ts-jest": "^27.0.3", "ts-mocha": "^10.0.0",
"typescript": "^4.3.4" "typescript": "^4.3.4"
} }
} }
import { HandleBlock, createBlockEvent } from 'forta-agent'
import { BigNumber, utils } from 'ethers'
import { expect } from 'chai'
import agent from './agent'
describe('minimum balance agent', async () => {
let handleBlock: HandleBlock
let mockEthersProvider
const blockEvent = createBlockEvent({
block: { hash: '0xa', number: 1 } as any,
})
// A function which returns a mock provider to give us values based on the case we want
// to test.
const mockEthersProviderByCase = (severity: string) => {
switch (severity) {
case 'safe':
return {
getBalance: async (addr: string): Promise<BigNumber> => {
if (addr === '0xabba') {
return utils.parseEther('1001')
}
if (addr === '0xacdc') {
return utils.parseEther('2001')
}
},
} as any
default:
break
}
}
before(() => {
handleBlock = agent.provideHandleBlock(mockEthersProvider)
})
describe('handleBlock', async () => {
it('returns empty findings if balance is above threshold', async () => {
mockEthersProvider = mockEthersProviderByCase('safe')
handleBlock = agent.provideHandleBlock(mockEthersProvider)
const findings = await handleBlock(blockEvent)
expect(findings).to.deep.equal([])
})
})
})
import { BlockEvent, Finding, HandleBlock } from 'forta-agent'
import { BigNumber, providers } from 'ethers'
type AccountAlert = {
name: string
address: string
thresholds: {
warning: BigNumber
danger: BigNumber
}
}
export const accounts: AccountAlert[] = [
{
name: 'Sequencer',
address: process.env.SEQUENCER_ADDRESS,
thresholds: {
warning: BigNumber.from(process.env.SEQUENCER_WARNING_THRESHOLD),
danger: BigNumber.from(process.env.SEQUENCER_DANGER_THRESHOLD),
},
},
{
name: 'Proposer',
address: process.env.PROPOSER_ADDRESS,
thresholds: {
warning: BigNumber.from(process.env.PROPOSER_WARNING_THRESHOLD),
danger: BigNumber.from(process.env.PROPOSER_DANGER_THRESHOLD),
},
},
]
const provideHandleBlock = (
provider: providers.JsonRpcProvider
): HandleBlock => {
return async (blockEvent: BlockEvent) => {
// report finding if specified account balance falls below threshold
const findings: Finding[] = []
// iterate over accounts with the index
for (const [idx, account] of accounts.entries()) {
const accountBalance = BigNumber.from(
(
await provider.getBalance(account.address, blockEvent.blockNumber)
).toString()
)
if (accountBalance.gte(account.thresholds.warning)) {
// return if this is the last account
if (idx === accounts.length - 1) {
return findings
} else {
continue
}
}
}
// todo: Add to the findings array when balances are below the threshold
return findings
}
}
const l1Provider = new providers.JsonRpcProvider(process.env.L1_RPC_URL)
export default {
provideHandleBlock,
handleBlock: provideHandleBlock(l1Provider),
}
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment