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

Send a notification if a Reality Module with unsecured parameters is created #91

Merged
merged 13 commits into from
Dec 15, 2022
Merged
4 changes: 3 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ ETHERSCAN_API_KEY=
OZ_DEFENDER_API_KEY=
OZ_DEFENDER_API_SECRET=
# channel's url including key below: https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks
DISCORD_URL_WITH_KEY=
DISCORD_URL_WITH_KEY=
DISCORD_URL_WITH_KEY=
NODE_PROVIDER=
juliopavila marked this conversation as resolved.
Show resolved Hide resolved
26 changes: 26 additions & 0 deletions monitoring/defender/autotask/on_module_factory_events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { ethers } = require("ethers");

exports.handler = async function (event) {
console.log(
"\n\n/************************** EVENT *****************************/"
);
console.log(JSON.stringify(event));
console.log(
"/**********************************************************/\n\n"
);
if (event && event.request && event.request.body) {
// variables from autotask creation
const nodeProvider = "{{nodeProvider}}";
const { transaction } = event.request.body;
const txHash = transaction.transactionHash;
const provider = new ethers.providers.JsonRpcProvider(nodeProvider);
const tx = await provider.getTransaction(txHash);
console.log(
"\n\n/*********************** TRANSACTION **************************/"
);
console.log(JSON.stringify(tx));
console.log(
"/**********************************************************/\n\n"
);
}
};
52 changes: 47 additions & 5 deletions monitoring/defender/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
AutotaskClient,
CreateAutotaskRequest,
} from "defender-autotask-client";
import { Network } from "defender-base-client";
import {
CreateSentinelRequest,
Expand All @@ -6,17 +10,25 @@ import {
} from "defender-sentinel-client";
import { ContractAbis, ContractAddresses } from "../../src/factory/contracts";
import { KnownContracts } from "../../src/factory/types";
import { defenderNetworkToSupportedNetwork } from "./util";

import {
defenderNetworkToSupportedNetwork,
packageCode,
readFileAndReplace,
} from "./util";
export { NotificationType } from "defender-sentinel-client";

export const setupSentinelClient = ({
export const setupClients = ({
apiKey,
apiSecret,
}: {
apiKey: string;
apiSecret: string;
}) => new SentinelClient({ apiKey, apiSecret });
}) => {
return {
sentinel: new SentinelClient({ apiKey, apiSecret }),
autotask: new AutotaskClient({ apiKey, apiSecret }),
};
};

export const setupNewNotificationChannel = async (
client: SentinelClient,
Expand Down Expand Up @@ -51,6 +63,7 @@ export const createSentinelForModuleFactory = async (
notificationChannels: string[],
network: Network,
module: KnownContracts,

autotaskId?: string
) => {
const moduleMastercopyAddress =
Expand All @@ -62,7 +75,7 @@ export const createSentinelForModuleFactory = async (
const requestParameters: CreateSentinelRequest = {
type: "BLOCK",
network,
name: `New ${module} Module is set up via the Module Factory on ${network})`,
name: `New ${module} Module is set up via the Module Factory on (${network})`,
addresses: [moduleProxyFactoryAddress],
paused: false,
abi: ContractAbis[KnownContracts.FACTORY],
Expand All @@ -81,3 +94,32 @@ export const createSentinelForModuleFactory = async (

return sentinel.subscriberId;
};

/**
*
* @param client The AutotaskClient
* @param nodeProvider URL to generate Json Rpc Provider
* @returns
*/
export const createAutotaskForModuleFactory = async (
client: AutotaskClient,
nodeProvider: string
) => {
const code = readFileAndReplace(
"monitoring/defender/autotask/on_module_factory_events.js",
{
"{{nodeProvider}}": nodeProvider,
}
);
const params: CreateAutotaskRequest = {
name: "Reality Module Autotask",
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;
};
21 changes: 21 additions & 0 deletions monitoring/defender/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Network } from "defender-base-client";
import fs from "fs";
import JSZip from "jszip";
import { SupportedNetworks } from "../../src/factory/contracts";

export const defenderNetworkToSupportedNetwork = (networks: Network) => {
Expand All @@ -13,3 +15,22 @@ export const defenderNetworkToSupportedNetwork = (networks: Network) => {
throw new Error(`Unsupported network ${networks}`);
}
};

export const readFileAndReplace = (
filePath: string,
replaceMap: { [toReplace: string]: string }
) => {
const buffer = fs.readFileSync(filePath);
let fileContent = buffer.toString();
Object.keys(replaceMap).forEach((key) => {
fileContent = fileContent.replace(key, replaceMap[key]);
});
return fileContent;
};
asgeir-s marked this conversation as resolved.
Show resolved Hide resolved

export const packageCode = async (code: string) => {
const zip = new JSZip();
zip.file("index.js", code, { binary: false });
const zippedCode = await zip.generateAsync({ type: "nodebuffer" });
return zippedCode.toString("base64");
};
20 changes: 17 additions & 3 deletions monitoring/setup-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import dotenv from "dotenv";

import { KnownContracts } from "../src/factory/types";
import {
setupSentinelClient,
setupNewNotificationChannel,
createSentinelForModuleFactory,
setupClients,
createAutotaskForModuleFactory,
} from "./defender";

dotenv.config();
Expand All @@ -22,11 +23,18 @@ if (DISCORD_URL_WITH_KEY == null) {
throw new Error("DISCORD_URL_WITH_KEY is not defined");
}

const NODE_PROVIDER = process.env.NODE_PROVIDER;
if (NODE_PROVIDER == null) {
throw new Error("NODE_PROVIDER is not defined");
}

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

const notificationChannelId = await setupNewNotificationChannel(
Expand All @@ -37,11 +45,17 @@ const setup = async () => {
}
);

const autotaskId = await createAutotaskForModuleFactory(
autotaskClient,
NODE_PROVIDER
);

const sentinelCreationResponds = await createSentinelForModuleFactory(
sentinelClient,
[notificationChannelId],
"goerli",
KnownContracts.REALITY_ETH
KnownContracts.REALITY_ETH,
autotaskId
);
console.log("Sentinel creation responds", sentinelCreationResponds);
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"@openzeppelin/contracts": "^4.3.2",
"@openzeppelin/contracts-upgradeable": "^4.2.0",
"argv": "^0.0.2",
"defender-autotask-client": "^1.37.0",
"dotenv": "^16.0.3",
"ethers": "^5.7.1",
"solc": "^0.8.17",
Expand Down
47 changes: 46 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3347,6 +3347,19 @@ deep-is@^0.1.3, deep-is@~0.1.3:
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.4.tgz#a6f2dce612fadd2ef1f519b73551f17e85199831"
integrity sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==

defender-autotask-client@^1.37.0:
version "1.37.0"
resolved "https://registry.yarnpkg.com/defender-autotask-client/-/defender-autotask-client-1.37.0.tgz#3b59ac826e6d5df4fa3f706f36e09aad07e786d0"
integrity sha512-N66buqg7mgYiEMbN6R+/6tUB4gxp5Q9cu8QDFuDkviR32AxZ2jY7LqV+GkR/9dbRufQtb4ZRf+ihG/b01w5O5Q==
dependencies:
axios "^0.21.2"
defender-base-client "1.37.0"
dotenv "^10.0.0"
glob "^7.1.6"
jszip "^3.5.0"
lodash "^4.17.19"
node-fetch "^2.6.0"

[email protected], defender-base-client@^1.37.0:
version "1.37.0"
resolved "https://registry.yarnpkg.com/defender-base-client/-/defender-base-client-1.37.0.tgz#22f63357ac99c2c8f64eab6e52c99ef113c62d3a"
Expand Down Expand Up @@ -3524,6 +3537,11 @@ dom-walk@^0.1.0:
resolved "https://registry.yarnpkg.com/dom-walk/-/dom-walk-0.1.2.tgz#0c548bef048f4d1f2a97249002236060daa3fd84"
integrity sha512-6QvTW9mrGeIegrFXdtQi9pk7O/nSK6lSdXW2eqUspN5LWD7UTji2Fqw5V2YLjBpHEoU9Xl/eUWNpDeZvoyOv2w==

dotenv@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-10.0.0.tgz#3d4227b8fb95f81096cdd2b66653fb2c7085ba81"
integrity sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==

dotenv@^16.0.3:
version "16.0.3"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.3.tgz#115aec42bac5053db3c456db30cc243a5a836a07"
Expand Down Expand Up @@ -5153,7 +5171,7 @@ glob@^5.0.15:
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@^7.0.0, glob@^7.1.2, glob@^7.1.3, glob@~7.2.3:
glob@^7.0.0, glob@^7.1.2, glob@^7.1.3, glob@^7.1.6, glob@~7.2.3:
version "7.2.3"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.3.tgz#b8df0fb802bbfa8e89bd1d938b4e16578ed44f2b"
integrity sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==
Expand Down Expand Up @@ -5611,6 +5629,11 @@ immediate@^3.2.3:
resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.3.0.tgz#1aef225517836bcdf7f2a2de2600c79ff0269266"
integrity sha512-HR7EVodfFUdQCTIeySw+WDRFJlPcLOJbXfwwZ7Oom6tjsvZ3bOkCDJHehQC3nxJrv7+f9XecwazynjU8e4Vw3Q==

immediate@~3.0.5:
version "3.0.6"
resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b"
integrity sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==

immediate@~3.2.3:
version "3.2.3"
resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.2.3.tgz#d140fa8f614659bd6541233097ddaac25cdd991c"
Expand Down Expand Up @@ -6263,6 +6286,16 @@ jsprim@^1.2.2:
json-schema "0.4.0"
verror "1.10.0"

jszip@^3.5.0:
version "3.10.1"
resolved "https://registry.yarnpkg.com/jszip/-/jszip-3.10.1.tgz#34aee70eb18ea1faec2f589208a157d1feb091c2"
integrity sha512-xXDvecyTpGLrqFrvkrUSoxxfJI5AH7U8zxxtVclpsUtMCq4JQ290LY8AW5c7Ggnr/Y/oK+bQMbqK2qmtk3pN4g==
dependencies:
lie "~3.3.0"
pako "~1.0.2"
readable-stream "~2.3.6"
setimmediate "^1.0.5"

[email protected]:
version "3.0.1"
resolved "https://registry.yarnpkg.com/keccak/-/keccak-3.0.1.tgz#ae30a0e94dbe43414f741375cff6d64c8bea0bff"
Expand Down Expand Up @@ -6516,6 +6549,13 @@ levn@^0.4.1:
prelude-ls "^1.2.1"
type-check "~0.4.0"

lie@~3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/lie/-/lie-3.3.0.tgz#dcf82dee545f46074daf200c7c1c5a08e0f40f6a"
integrity sha512-UaiMJzeWRlEujzAuw5LokY1L5ecNQYZKfmyZ9L7wDHb/p5etKaxXhohBcrw0EYby+G/NA52vRSN4N39dxHAIwQ==
dependencies:
immediate "~3.0.5"

load-json-file@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/load-json-file/-/load-json-file-1.1.0.tgz#956905708d58b4bab4c2261b04f59f31c99374c0"
Expand Down Expand Up @@ -7491,6 +7531,11 @@ p-try@^2.0.0:
resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6"
integrity sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==

pako@~1.0.2:
version "1.0.11"
resolved "https://registry.yarnpkg.com/pako/-/pako-1.0.11.tgz#6c9599d340d54dfd3946380252a35705a6b992bf"
integrity sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==

parent-module@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/parent-module/-/parent-module-1.0.1.tgz#691d2709e78c79fae3a156622452d00762caaaa2"
Expand Down