-
Notifications
You must be signed in to change notification settings - Fork 10
/
contract-monitor.js
61 lines (57 loc) · 1.86 KB
/
contract-monitor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import Etherscan from '../models/Etherscan'
import { DAYS } from '@aragonone/court-backend-shared/helpers/times'
import getWalletFromPk from '@aragonone/court-backend-shared/helpers/get-wallet-from-pk'
import abi from 'web3-eth-abi'
const TRANSACTION_TYPES = [
{
type: 'reveal',
signature: 'reveal(uint256,address,uint8,bytes32)',
},
{
type: 'settleReward',
signature: 'settleReward(uint256,uint256,address)',
},
{
type: 'settlePenalties',
signature: 'settlePenalties(uint256,uint256,uint256)',
},
{
type: 'executeRuling',
signature: 'executeRuling(uint256)',
},
{
type: 'heartbeat',
signature: 'heartbeat(uint64)',
}
]
const COUNT_PERIOD = 1 * DAYS
const etherscan = new Etherscan()
const KEEPER_ADDRESS = getWalletFromPk(process.env.PRIVATE_KEY).getAddressString()
export default async function (ctx) {
const { logger, metrics } = ctx
const transactions = await etherscan.getTransactionsFrom(KEEPER_ADDRESS)
const transactionsRecent = transactions.filter(({ timeStamp }) => Number(timeStamp)*1000 > Date.now() - COUNT_PERIOD)
const errorCounts = getErrorCounts(transactionsRecent)
showMetrics(metrics, errorCounts)
logger.success(`Contract metrics updated.`)
}
function getErrorCounts(transactions) {
let errorCountsZero = {} // set initial metric to 0
TRANSACTION_TYPES.forEach(({ type }) => errorCountsZero[type] = 0)
return transactions.reduce((errorCounts, tx) => {
const { isError, input } = tx
if (Number(isError)) {
TRANSACTION_TYPES.forEach(({ type, signature }) => {
if (input.includes(abi.encodeFunctionSignature(signature))) {
errorCounts[type] ++
}
})
}
return errorCounts
}, errorCountsZero)
}
function showMetrics(metrics, errorCounts) {
Object.entries(errorCounts).forEach(([type, count]) => {
metrics.transactionErrors(type, count)
})
}