-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbot-initialize.ts
More file actions
91 lines (80 loc) · 3.24 KB
/
bot-initialize.ts
File metadata and controls
91 lines (80 loc) · 3.24 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { envVar } from "@eng-automation/js";
import { sr25519CreateDerive } from "@polkadot-labs/hdkd";
import { entropyToMiniSecret, mnemonicToEntropy, parseSuri } from "@polkadot-labs/hdkd-helpers";
import { createClient } from "matrix-js-sdk";
import assert from "node:assert";
import * as process from "node:process";
import { PolkadotSigner } from "polkadot-api";
import { getPolkadotSigner } from "polkadot-api/signer";
import { ApplicationFunction, Context, Probot } from "probot";
import { updateAllBalances } from "./balance";
import { handleIssueCommentCreated } from "./bot-handle-comment";
import { addMetricsRoute } from "./metrics";
import { Polkassembly } from "./polkassembly/polkassembly";
import { State } from "./types";
type AsyncApplicationFunction = (
...params: Parameters<ApplicationFunction>
) => Promise<ReturnType<ApplicationFunction>>;
export const generateSigner = (accountSeed: string): PolkadotSigner => {
const suri = parseSuri(accountSeed);
assert(suri.phrase, "Invalid account seed");
assert(typeof suri.paths === "string", "Invalid account seed - paths should be a string");
const entropy = mnemonicToEntropy(suri.phrase);
const miniSecret = entropyToMiniSecret(entropy);
const hdkdKeyPair = sr25519CreateDerive(miniSecret)(suri.paths);
return getPolkadotSigner(hdkdKeyPair.publicKey, "Sr25519", (input) => hdkdKeyPair.sign(input));
};
export const botInitialize: AsyncApplicationFunction = async (bot: Probot, { getRouter }) => {
bot.log.info("Loading tip bot...");
const router = getRouter?.("/tip-bot");
if (router) {
addMetricsRoute(router);
} else {
bot.log.warn("No router received from the probot library, metrics were not added.");
}
const botTipAccount = generateSigner(envVar("ACCOUNT_SEED"));
const state: State = {
bot,
allowedGitHubOrg: envVar("APPROVERS_GH_ORG"),
allowedGitHubTeam: envVar("APPROVERS_GH_TEAM"),
botTipAccount,
polkassembly: (() => {
if (!process.env.POLKASSEMBLY_ENDPOINT) {
// convenient for local development, and tests
bot.log.warn("POLKASSEMBLY_ENDPOINT is not set; polkassembly integration is disabled");
return undefined;
}
return new Polkassembly(
envVar("POLKASSEMBLY_ENDPOINT"),
{ type: "polkadot", keyringPair: botTipAccount },
bot.log,
);
})(),
matrix: (() => {
if (!process.env.MATRIX_ACCESS_TOKEN) {
// convenient for local development, and tests
bot.log.warn("MATRIX_ACCESS_TOKEN is not set; matrix notifications are disabled");
return undefined;
}
return {
client: createClient({
accessToken: envVar("MATRIX_ACCESS_TOKEN"),
baseUrl: envVar("MATRIX_SERVER_URL"),
localTimeoutMs: 10000,
}),
roomId: envVar("MATRIX_ROOM_ID"),
};
})(),
};
bot.log.info("Tip bot was loaded!");
bot.on("issue_comment.created", async (context: Context<"issue_comment.created">) => {
await handleIssueCommentCreated(state, context.payload);
});
try {
bot.log.info("Loading bot balances across all networks...");
await updateAllBalances(botTipAccount.publicKey, bot.log);
bot.log.info("Updated bot balances across all networks!");
} catch (e) {
bot.log.error(e.message);
}
};