Skip to content

Commit

Permalink
impl event processor
Browse files Browse the repository at this point in the history
  • Loading branch information
alfulinku committed Dec 21, 2023
1 parent d6dcfed commit 7b95203
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 17 deletions.
5 changes: 1 addition & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
"prettier"
],
"rules": {
"indent": [
"error",
2
],
"indent": ["error", 2, { "SwitchCase": 1 }],
"linebreak-style": [
"error",
"unix"
Expand Down
91 changes: 91 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"typescript": "^5.3.3"
},
"dependencies": {
"axios": "^1.6.2",
"dotenv": "^16.3.1",
"ethers": "^6.9.1"
}
Expand Down
158 changes: 149 additions & 9 deletions src/EventProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { EventData, EventQueue } from './EventQueue';
import { sleep } from './Utils';
import { SendTelegramMessage } from './TelegramHelper';

const TG_BOT_ID: string | undefined = process.env.TG_BOT_ID;
const TG_CHAT_ID: string | undefined = process.env.TG_CHAT_ID;

async function startEventProcessor() {
console.log('Started the event processor');

// eslint-disable-next-line no-constant-condition
while (true) {
if (EventQueue.length > 0) {
Expand All @@ -16,18 +22,152 @@ async function startEventProcessor() {
}

async function ProcessAsync(event: EventData) {
if (!TG_BOT_ID) {
throw new Error('No TG_BOT_ID found in env');
}
if (!TG_CHAT_ID) {
throw new Error('No TG_BOT_ID found in env');
}

console.log(`NEW EVENT DETECTED AT BLOCK ${event.block}: ${event.eventName}`, { args: event.eventArgs });
const msgToSend: string | undefined = buildMessageFromEvent(event);
if (!msgToSend) {
console.log('Nothing to send to TG');
} else {
await SendTelegramMessage(TG_CHAT_ID, TG_BOT_ID, msgToSend, false);
}
}

function buildMessageFromEvent(event: EventData): string | undefined {
switch (event.eventName.toLowerCase()) {
default:
return `${buildMsgHeader(event)}\n` + 'NO SPECIFIC IMPLEMENTATION';
case 'updatelasttotalassets':
case 'accrueinterest':
case 'createmetamorpho':
case 'transfer':
case 'deposit':
case 'withdraw':
case 'approval':
// user facing events, no need for an alert
return undefined;
case 'submittimelock':
// event SubmitTimelock(uint256 newTimelock);
return `${buildMsgHeader(event)}\n` + `newTimelock: ${event.eventArgs[0]}\n`;
case 'settimelock':
// event SetTimelock(address indexed caller, uint256 newTimelock);
return `${buildMsgHeader(event)}\n` + `caller: ${event.eventArgs[0]}\n` + `newTimelock: ${event.eventArgs[1]}\n`;
case 'setskimrecipient':
// event SetSkimRecipient(address indexed newSkimRecipient);
return `${buildMsgHeader(event)}\n` + `newSkimRecipient: ${event.eventArgs[0]}\n`;
case 'setfee':
// event SetFee(address indexed caller, uint256 newFee);
return `${buildMsgHeader(event)}\n` + `caller: ${event.eventArgs[0]}\n` + `newFee: ${event.eventArgs[1]}\n`;
case 'setfeerecipient':
// event SetFeeRecipient(address indexed newFeeRecipient);
return `${buildMsgHeader(event)}\n` + `newFeeRecipient: ${event.eventArgs[0]}\n`;
case 'submitguardian':
// event SubmitGuardian(address indexed newGuardian);
return `${buildMsgHeader(event)}\n` + `newGuardian: ${event.eventArgs[0]}\n`;
case 'setguardian':
// event SetGuardian(address indexed caller, address indexed guardian);
return `${buildMsgHeader(event)}\n` + `caller: ${event.eventArgs[0]}\n` + `guardian: ${event.eventArgs[1]}\n`;
case 'submitcap':
// event SubmitCap(address indexed caller, Id indexed id, uint256 cap);
return (
`${buildMsgHeader(event)}\n` +
`caller: ${event.eventArgs[0]}\n` +
`id: ${event.eventArgs[1]}\n` +
`cap: ${event.eventArgs[2]}\n`
);

case 'setcap':
// event SetCap(address indexed caller, Id indexed id, uint256 cap);
return (
`${buildMsgHeader(event)}\n` +
`caller: ${event.eventArgs[0]}\n` +
`id: ${event.eventArgs[1]}\n` +
`cap: ${event.eventArgs[2]}\n`
);

case 'submitmarketremoval':
// event SubmitMarketRemoval(address indexed caller, Id indexed id);
return `${buildMsgHeader(event)}\n` + `caller: ${event.eventArgs[0]}\n` + `id: ${event.eventArgs[1]}\n`;

case 'setcurator':
// event SetCurator(address indexed newCurator);
return `${buildMsgHeader(event)}\n` + `newCurator: ${event.eventArgs[0]}\n`;

case 'setisallocator':
// event SetIsAllocator(address indexed allocator, bool isAllocator);
return (
`${buildMsgHeader(event)}\n` + `allocator: ${event.eventArgs[0]}\n` + `isAllocator: ${event.eventArgs[1]}\n`
);

case 'revokependingtimelock':
// event RevokePendingTimelock(address indexed caller);
return `${buildMsgHeader(event)}\n` + `caller: ${event.eventArgs[0]}\n`;

case 'revokependingcap':
// event RevokePendingCap(address indexed caller, Id indexed id);
return `${buildMsgHeader(event)}\n` + `caller: ${event.eventArgs[0]}\n` + `id: ${event.eventArgs[1]}\n`;

case 'revokependingguardian':
// event RevokePendingGuardian(address indexed caller);
return `${buildMsgHeader(event)}\n` + `caller: ${event.eventArgs[0]}\n`;

case 'revokependingmarketremoval':
// event RevokePendingMarketRemoval(address indexed caller, Id indexed id);
return `${buildMsgHeader(event)}\n` + `caller: ${event.eventArgs[0]}\n` + `id: ${event.eventArgs[1]}\n`;

case 'setsupplyqueue':
// event SetSupplyQueue(address indexed caller, Id[] newSupplyQueue);
return (
`${buildMsgHeader(event)}\n` +
`caller: ${event.eventArgs[0]}\n` +
`newSupplyQueue: ${event.originArgs[1].map((_: any) => _.toString()).join(', ')}\n`
);

case 'setwithdrawqueue':
// event SetWithdrawQueue(address indexed caller, Id[] newWithdrawQueue);
return (
`${buildMsgHeader(event)}\n` +
`caller: ${event.eventArgs[0]}\n` +
`newWithdrawQueue:\n${event.originArgs[1].map((_: any) => '- ' + _.toString()).join('\n')}\n`
);
case 'reallocatesupply':
// event ReallocateSupply(address indexed caller, Id indexed id, uint256 suppliedAssets, uint256 suppliedShares);
return (
`${buildMsgHeader(event)}\n` +
`caller: ${event.eventArgs[0]}\n` +
`id: ${event.eventArgs[1]}\n` +
`suppliedAssets: ${event.eventArgs[2]}\n` +
`suppliedShares: ${event.eventArgs[3]}\n`
);

case 'reallocatewithdraw':
// event ReallocateWithdraw(address indexed caller, Id indexed id, uint256 withdrawnAssets, uint256 withdrawnShares);
return (
`${buildMsgHeader(event)}\n` +
`caller: ${event.eventArgs[0]}\n` +
`id: ${event.eventArgs[1]}\n` +
`withdrawnAssets: ${event.eventArgs[2]}\n` +
`withdrawnShares: ${event.eventArgs[3]}\n`
);

case 'skim':
// event Skim(address indexed caller, address indexed token, uint256 amount);
return (
`${buildMsgHeader(event)}\n` +
`caller: ${event.eventArgs[0]}\n` +
`token: ${event.eventArgs[1]}\n` +
`amount: ${event.eventArgs[2]}\n`
);
}
}

/**
*
* @param {number} ms milliseconds to sleep
* @returns async promise
*/
async function sleep(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
function buildMsgHeader(event: EventData): string {
return `New ${event.eventName} detected on block ${event.block}:`;
}

startEventProcessor();
3 changes: 3 additions & 0 deletions src/EventQueue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Result } from 'ethers';

export const EventQueue: EventData[] = [];

export interface EventData {
eventName: string;
eventArgs: string[];
block: number;
originArgs: Result;
}
5 changes: 3 additions & 2 deletions src/EventWatcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers, Contract, Interface, Log, ContractEventPayload } from 'ethers';
import { ethers, Contract, Interface } from 'ethers';
import WebSocket from 'ws';
import dotenv from 'dotenv';
import { metamorphoAbi } from './abis/MetaMorphoAbi';
Expand Down Expand Up @@ -54,7 +54,8 @@ function startListening() {
EventQueue.push({
eventName: parsed.name,
eventArgs: parsed.args.map((_) => _.toString()),
block: event.log.blockNumber
block: event.log.blockNumber,
originArgs: parsed.args
});
});
}
Expand Down
Loading

0 comments on commit 7b95203

Please sign in to comment.