Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve monitoring scripts #89

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 24 additions & 67 deletions monitoring/defender/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { Network } from "defender-base-client";
import {
CreateSentinelRequest,
NotificationType,
SentinelClient,
} from "defender-sentinel-client";
import { AutotaskClient } from "defender-autotask-client";
import { CreateAutotaskRequest } from "defender-autotask-client";
import { Network } from "defender-base-client";
import { packageCode, replaceInString } from "../util";
import autotaskJsCode from "./autotasks/on_new_question_from_module";
import { ContractAbis, ContractAddresses } from "../../src/factory/contracts";
import { KnownContracts } from "../../src/factory/types";
import { defenderNetworkToSupportedNetwork } from "./util";

export { NotificationType } from "defender-sentinel-client";

export const setupSentinelClient = ({ apiKey, apiSecret }) =>
new SentinelClient({ apiKey, apiSecret });

export const setupAutotaskClient = ({ apiKey, apiSecret }) =>
new AutotaskClient({ apiKey, apiSecret });
export const setupSentinelClient = ({
apiKey,
apiSecret,
}: {
apiKey: string;
apiSecret: string;
}) => new SentinelClient({ apiKey, apiSecret });

export const setupNewNotificationChannel = async (
client: SentinelClient,
Expand All @@ -41,47 +42,34 @@ export const setupNewNotificationChannel = async (
* @param client The SentinelClient
* @param notificationChannels Where to send notifications
* @param network What network to monitor
* @param moduleMastercopyAddress Address of mastercopy of the module
* @param moduleName Name of the module. For nice notification messages
* @param module The module from the `KnownContracts`
* @param autotaskId Optional: ID of autotask to run when the sentinel triggers
* @returns
*/
export const createSentinelForModuleFactory = async (
client: SentinelClient,
notificationChannels: string[],
network: Network,
moduleMastercopyAddress: string,
moduleName: string,
module: KnownContracts,
autotaskId?: string
) => {
const moduleMastercopyAddress =
ContractAddresses[defenderNetworkToSupportedNetwork(network)][module];
const moduleProxyFactoryAddress =
ContractAddresses[defenderNetworkToSupportedNetwork(network)][
KnownContracts.FACTORY
];
const requestParameters: CreateSentinelRequest = {
type: "BLOCK",
network,
name: `New ${moduleName} Module is set up (${realityModuleAddress} on ${network})`,
addresses: [realityModuleAddress],
name: `New ${module} Module is set up via the Module Factory on ${network})`,
addresses: [moduleProxyFactoryAddress],
paused: false,
abi: `[{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "bytes32",
"name": "questionId",
"type": "bytes32"
},
{
"indexed": true,
"internalType": "string",
"name": "proposalId",
"type": "string"
}
],
"name": "ProposalQuestionCreated",
"type": "event"
}]`,
abi: ContractAbis[KnownContracts.FACTORY],
eventConditions: [
{
eventSignature: "ProposalQuestionCreated(bytes32,string)",
eventSignature: "ModuleProxyCreation(address,address)", // ModuleProxyCreation(proxy, mastercopy)
expression: '$1 == "' + moduleMastercopyAddress + '"',
},
],
autotaskTrigger: autotaskId,
Expand All @@ -93,34 +81,3 @@ export const createSentinelForModuleFactory = async (

return sentinel.subscriberId;
};

export const createAutotask = async (
client: AutotaskClient,
oracleAddress: string,
notificationChannels: string[],
network: string,
apiKey: string,
apiSecret: string
) => {
const code = replaceInString(autotaskJsCode, {
"{{network}}": network,
"{{oracleAddress}}": oracleAddress,
'"{{notificationChannels}}"': JSON.stringify(notificationChannels),
"{{apiKey}}": apiKey,
"{{apiSecret}}": apiSecret,
});

const params: CreateAutotaskRequest = {
name: "Setup Sentinel for new Reality.eth question",
encodedZippedCode: await packageCode(code),
trigger: {
type: "webhook",
},
paused: false,
};

const createdAutotask = await client.create(params);
console.log("Created Autotask with ID: ", createdAutotask.autotaskId);

return createdAutotask.autotaskId;
};
13 changes: 13 additions & 0 deletions monitoring/defender/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Network } from "defender-base-client";
import { SupportedNetworks } from "../../src/factory/contracts";

export const defenderNetworkToSupportedNetwork = (networks: Network) => {
switch (networks) {
case "mainnet":
return SupportedNetworks.Mainnet;
case "goerli":
return SupportedNetworks.Goerli;
default:
throw new Error(`Unsupported network ${networks}`);
}
};
79 changes: 27 additions & 52 deletions monitoring/setup-script.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import dotenv from "dotenv";

import { KnownContracts } from "../src/factory/types";
import {
setupSentinelClient,
NotificationType,
setupNewNotificationChannel,
createSentinel,
createAutotask,
setupAutotaskClient,
createSentinelForModuleFactory,
} from "./defender";

import dotenv from "dotenv";

dotenv.config();

const API_KEY = process.env.OZ_DEFENDER_API_KEY;
Expand All @@ -25,50 +23,27 @@ if (DISCORD_URL_WITH_KEY == null) {
}

const setup = async () => {
try {
const sentinelClient = setupSentinelClient({
apiKey: API_KEY,
apiSecret: API_SECRET,
});
console.log("Client is ready");

const notificationChannel = {
channel: "discord",
config: {
url: DISCORD_URL_WITH_KEY,
},
};

const notificationChannelId = await setupNewNotificationChannel(
sentinelClient,
notificationChannel.channel,
notificationChannel.config
);

const sentinelCreationResponds = await createSentinel(
sentinelClient,
[notificationChannelId],
"mainnet",
realityModuleAddress,
autotaskId
);
console.log("Sentinel creation responds", sentinelCreationResponds);
return response
.status(200)
.setHeader("content-type", "application/json;charset=UTF-8")
.setHeader("Access-Control-Allow-Origin", "*")
.send({
success: true,
});
} catch (e) {
console.error(e);

const { name, message } = e;
// this is safe for we are requesting on behalf of the user (with their API key)
return response.status(500).send({
name,
message,
success: false,
});
}
const sentinelClient = setupSentinelClient({
apiKey: API_KEY,
apiSecret: API_SECRET,
});
console.log("Client is ready");

const notificationChannelId = await setupNewNotificationChannel(
sentinelClient,
"discord",
{
url: DISCORD_URL_WITH_KEY,
}
);

const sentinelCreationResponds = await createSentinelForModuleFactory(
sentinelClient,
[notificationChannelId],
"mainnet",
KnownContracts.REALITY_ETH
);
console.log("Sentinel creation responds", sentinelCreationResponds);
};

setup();
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
"@typescript-eslint/parser": "^5.40.0",
"chai": "^4.3.6",
"debug": "^4.3.4",
"defender-base-client": "^1.37.0",
"defender-sentinel-client": "^1.37.0",
"dotenv": "^16.0.3",
"eslint": "^8.25.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
Expand Down
Loading