-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.ts
64 lines (56 loc) · 1.62 KB
/
monitor.ts
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
62
63
64
import { Alchemy, Network, AlchemySubscription } from "alchemy-sdk";
import dotenv from "dotenv";
import axios from 'axios';
dotenv.config();
const networkEnv = process.env.NETWORK;
let network: Network;
switch (networkEnv) {
case 'zksync':
network = Network.ZKSYNC_MAINNET;
break;
case 'base':
network = Network.BASE_MAINNET;
break;
default:
console.error("Invalid NETWORK. Please check the .env file.");
process.exit(1);
}
const settings = {
apiKey: process.env.ALCHEMY_API_KEY || "", // Retrieve API key from .env
network: network,
};
const alchemy = new Alchemy(settings);
// Define the addresses to be monitored
const monitoredAddresses = process.env.MONITORED_ADDRESS;
if(!monitoredAddresses){
console.error("MONITORED_ADDRESS environment variable is not set");
process.exit(1);
}
async function sendSlackNotification(tx: any) {
const slackWebhookUrl = process.env.SLACK_WEBHOOK_URL;
if (!slackWebhookUrl) {
console.error("SLACK_WEBHOOK_URL environment variable is not set");
return;
}
try {
await axios.post(slackWebhookUrl, {
text: `New Transaction on ${networkEnv}: ${JSON.stringify(tx.hash)}`,
});
console.log("Slack notification sent successfully.");
} catch (error) {
console.error("Failed to send Slack notification:", error);
}
}
// Subscribe to mined transactions in Alchemy
alchemy.ws.on(
{
method: AlchemySubscription.MINED_TRANSACTIONS,
addresses: [{from: monitoredAddresses}],
includeRemoved: true,
hashesOnly: false,
},
async (tx) => {
console.log("New Transaction:", tx);
await sendSlackNotification(tx);
}
);