Skip to content

Commit

Permalink
refactor: resolve pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
blockchainguyy committed Nov 1, 2023
1 parent 3d6cf76 commit 6b56600
Showing 1 changed file with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
const axios = require('axios').default;
const { ethers } = require('ethers');

const WARNING_THRESHOLD = 10; // TODO: discuss for production
const CRITICAL_THRESHOLD = 20;
const TIME_SPLIT = 5 * 3600 * 1000; // 5 hours in milliseconds
const PAGER_DUTY_ALERT_URL = 'https://events.pagerduty.com/v2/enqueue';

const handleFailedTxFn = async (context, event) => {
const chainName = context.metadata.getNetwork();
const provider = new ethers.providers.JsonRpcProvider(await context.secrets.get(`RPC_${chainName.toUpperCase()}`));
const tx = await provider.getTransaction(event.hash);

const warningThreshold = await context.storage.getStr('WarningThreshold');
const criticalThreshold = await context.storage.getStr('CriticalThreshold');
const timeSplit = await context.storage.getStr('TimeSplit');

if (!tx.to || !tx.from || !tx.blockNumber || !tx.data) {
throw new Error('INVALID_TX_FORMAT');
}

const response = await provider.call(tx, tx.blockNumber);

if (response.length < 10) {
throw new Error('INVALID_RESPONSE_LENGTH');
}

const errorHash = response.slice(0, 10);
console.log('errorHash: ', errorHash);
let warningOptions = [];
Expand All @@ -21,13 +31,13 @@ const handleFailedTxFn = async (context, event) => {
warningOptions = ['FlowLimitExceeded', 'TokenManager'];
break;
case '0xbb6c1639':
warningOptions = ['MissingRole', '-'];
warningOptions = ['MissingRole', event.to];
break;
case '0x7fa6fbb4':
warningOptions = ['MissingAllRoles', '-'];
warningOptions = ['MissingAllRoles', event.to];
break;
case '0x218de251':
warningOptions = ['MissingAnyOfRoles', '-'];
warningOptions = ['MissingAnyOfRoles', event.to];
break;
case '0xb078d99c':
warningOptions = ['ReEntrancy', 'TokenManager'];
Expand All @@ -40,38 +50,38 @@ const handleFailedTxFn = async (context, event) => {
}

if (warningOptions.length !== 0) {
await sendWarning(event, context, chainName, ...warningOptions, 'info');
await sendWarning(event, context, chainName, ...warningOptions, Severity.INFO);
}

const failedTxStartTime = await context.storage.getNumber('FailedTxStartTimestamp');
let failedTxCount = await context.storage.getNumber('FailedTxCount');

const timeNow = Date.now();

if (timeNow - failedTxStartTime > TIME_SPLIT) {
if (timeNow - failedTxStartTime > timeSplit) {
console.log('Updating Time stamp');
failedTxCount = 1;
await context.storage.putNumber('FailedTxStartTimestamp', timeNow);
} else {
failedTxCount++;

if (failedTxCount >= CRITICAL_THRESHOLD) {
if (failedTxCount >= criticalThreshold) {
await sendWarning(
event,
context,
chainName,
`Threshold crossed for failed transactions: ${failedTxCount}`,
'ITS_PROJECT',
'critical',
Severity.CRITICAL,
);
} else if (failedTxCount >= WARNING_THRESHOLD) {
} else if (failedTxCount >= warningThreshold) {
await sendWarning(
event,
context,
chainName,
`Threshold crossed for failed transactions: ${failedTxCount}`,
'ITS_PROJECT',
'warning',
Severity.WARNING,
);
}
}
Expand Down Expand Up @@ -108,4 +118,11 @@ async function sendWarning(event, context, chainName, summary, source, severity)
}
}

const Severity = {
INFO: 'info',
CRITICAL: 'critical',
WARNING: 'warning',
};


module.exports = { handleFailedTxFn };

0 comments on commit 6b56600

Please sign in to comment.