Skip to content

Commit

Permalink
refactor: decrease number of pagerduty API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
blockchainguyy committed Oct 26, 2023
1 parent be6f105 commit 390722e
Showing 1 changed file with 63 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ const handleTokenTransferFn = async (context, event) => {
throw new Error('NO_TOKEN_TRANSFER_DETECTED');
}

const amountsAboveThreshold = [];
const prices = [];
const symbols = [];
let severity = 0;

for (let index = 0; index < tokenIDs.length; index++) {
const id = tokenIDs[index];

Expand Down Expand Up @@ -80,46 +85,75 @@ const handleTokenTransferFn = async (context, event) => {
throw Error('ERROR_IN_FETCHING_PRICE');
}

let severity;
let tempSeverity = 0;

if (totalAmount > tokenThreshold[2]) {
severity = 'critical';
tempSeverity = 3;
} else if (totalAmount > tokenThreshold[1]) {
severity = 'warning';
tempSeverity = 2;
} else if (totalAmount > tokenThreshold[0]) {
severity = 'info';
tempSeverity = 1;
}

if (tempSeverity) {
if (tempSeverity > severity) {
severity = tempSeverity;
}

amountsAboveThreshold.push(totalAmount);
prices.push(tokenPrice);
symbols.push(symbol);
}
}

if (severity) {
try {
await axios.post(
PAGER_DUTY_ALERT_URL,
{
routing_key: await context.secrets.get('PD_ROUTING_KEY'),
event_action: 'trigger',
payload: {
summary: 'Token tranfer amount crossed threshold',
source: `${chainName}-ITS-${ITS_CONTRACT_ADDRESS}`,
severity,
custom_details: {
timestamp: Date.now(),
chain_name: chainName,
transaction: event.hash,
price: tokenPrice,
amount: `${tokenTransferAmount}`,
payload: event,
if (severity) {
try {
await axios.post(
PAGER_DUTY_ALERT_URL,
{
routing_key: await context.secrets.get('PD_ROUTING_KEY'),
event_action: 'trigger',
payload: {
summary: 'Token tranfer amount crossed threshold',
source: `${chainName}-ITS-${ITS_CONTRACT_ADDRESS}`,
severity: getSeverityString(severity),
custom_details: {
timestamp: Date.now(),
chain_name: chainName,
transaction: event.hash,
transfer_info: {
amounts: amountsAboveThreshold,
prices,
symbols,
},
payload: event,
},
},
{},
);
} catch (error) {
console.log('PD error status: ', error.response.status);
console.log('PD error response: ', error.response.data);
throw Error('TOKEN_TRANSFER_ALERT_FAILED');
}
},
{},
);
} catch (error) {
console.log('PD error status: ', error.response.status);
console.log('PD error response: ', error.response.data);
throw Error('TOKEN_TRANSFER_ALERT_FAILED');
}
}
};

function getSeverityString(severity) {
if (severity === 3) {
return 'critical';
}

if (severity === 2) {
return 'warning';
}

if (severity === 1) {
return 'info';
}

return '';
}

module.exports = { handleTokenTransferFn };

0 comments on commit 390722e

Please sign in to comment.