Skip to content

Commit 6eafa24

Browse files
committed
add startup notif
1 parent 719ffc6 commit 6eafa24

5 files changed

Lines changed: 102 additions & 57 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"accounts": [],
3+
"endpoints": [
4+
"wss://polkadot-collectives-rpc.polkadot.io",
5+
"wss://kusama-rpc.dwellir.com"
6+
],
7+
"api_subscription": "finalized",
8+
"startup_notification": false,
9+
"method_subscription": {
10+
"type": "only",
11+
"only": [
12+
{
13+
"pallet": "FellowshipReferenda",
14+
"method": "Confirmed"
15+
},
16+
{
17+
"pallet": "FellowshipReferenda",
18+
"method": "Submitted"
19+
},
20+
{
21+
"pallet": "FellowshipSalary",
22+
"method": "CycleStarted"
23+
}
24+
]
25+
},
26+
"reporters": {
27+
"console": {}
28+
}
29+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
],
1111
"type": "module",
1212
"scripts": {
13+
"run": "bun run src/index.ts",
1314
"dev": "bun run src/index.ts -c examples/config-dev-all.json",
1415
"devp": "bun run src/index.ts -c examples/config-p-dev-all.json",
1516

src/config-ti.ts

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,104 +5,105 @@ import * as t from "ts-interface-checker";
55
// tslint:disable:object-literal-key-quotes
66

77
export const Only = t.iface([], {
8-
"type": t.lit('only'),
9-
"only": t.array("ISubscriptionTarget"),
8+
"type": t.lit('only'),
9+
"only": t.array("ISubscriptionTarget"),
1010
});
1111

1212
export const Ignore = t.iface([], {
13-
"type": t.lit('ignore'),
14-
"ignore": t.array("ISubscriptionTarget"),
13+
"type": t.lit('ignore'),
14+
"ignore": t.array("ISubscriptionTarget"),
1515
});
1616

1717
export const All = t.iface([], {
18-
"type": t.lit('all'),
18+
"type": t.lit('all'),
1919
});
2020

2121
export const MethodSubscription = t.union("All", "Only", "Ignore");
2222

2323
export const ISubscriptionTarget = t.iface([], {
24-
"pallet": "string",
25-
"method": "string",
24+
"pallet": "string",
25+
"method": "string",
2626
});
2727

2828
export const RawAccount = t.iface([], {
29-
"address": "string",
30-
"nickname": "string",
29+
"address": "string",
30+
"nickname": "string",
3131
});
3232

3333
export const BatchConfig = t.iface([], {
34-
"interval": "number",
35-
"misc": t.opt("boolean"),
36-
"leftovers": t.opt("boolean"),
34+
"interval": "number",
35+
"misc": t.opt("boolean"),
36+
"leftovers": t.opt("boolean"),
3737
});
3838

3939
export const EmailConfig = t.iface([], {
40-
"from": "string",
41-
"to": t.array("string"),
42-
"gpgpubkey": t.opt("string"),
43-
"transporter": "any",
44-
"batch": t.opt("BatchConfig"),
40+
"from": "string",
41+
"to": t.array("string"),
42+
"gpgpubkey": t.opt("string"),
43+
"transporter": "any",
44+
"batch": t.opt("BatchConfig"),
4545
});
4646

4747
export const MatrixConfig = t.iface([], {
48-
"userId": t.opt("string"), // optional as it can be passed through env.MATRIX_USERID
49-
"accessToken": t.opt("string"), // optional as it can be passed through env.MATRIX_ACCESSTOKEN
50-
"roomId": "string",
51-
"server": "string",
52-
"batch": t.opt("BatchConfig"),
48+
"userId": t.opt("string"), // optional as it can be passed through env.MATRIX_USERID
49+
"accessToken": t.opt("string"), // optional as it can be passed through env.MATRIX_ACCESSTOKEN
50+
"roomId": "string",
51+
"server": "string",
52+
"batch": t.opt("BatchConfig"),
5353
});
5454

5555
export const FsConfig = t.iface([], {
56-
"path": "string",
57-
"batch": t.opt("BatchConfig"),
56+
"path": "string",
57+
"batch": t.opt("BatchConfig"),
5858
});
5959

6060
export const ConsoleConfig = t.iface([], {
61-
"batch": t.opt("BatchConfig"),
61+
"batch": t.opt("BatchConfig"),
6262
});
6363

6464
export const TelegramConfig = t.iface([], {
65-
"chatId": "string",
66-
"botToken": "string",
67-
"batch": t.opt("BatchConfig"),
65+
"chatId": "string",
66+
"botToken": "string",
67+
"batch": t.opt("BatchConfig"),
6868
});
6969

7070
export const ReportersConfig = t.iface([], {
71-
"email": t.opt("EmailConfig"),
72-
"matrix": t.opt("MatrixConfig"),
73-
"fs": t.opt("FsConfig"),
74-
"telegram": t.opt("TelegramConfig"),
75-
"console": t.opt("ConsoleConfig"),
71+
"email": t.opt("EmailConfig"),
72+
"matrix": t.opt("MatrixConfig"),
73+
"fs": t.opt("FsConfig"),
74+
"telegram": t.opt("TelegramConfig"),
75+
"console": t.opt("ConsoleConfig"),
7676
});
7777

7878
export const ApiSubscription = t.enumtype({
79-
"Head": "head",
80-
"Finalized": "finalized",
79+
"Head": "head",
80+
"Finalized": "finalized",
8181
});
8282

8383
export const AppConfig = t.iface([], {
84-
"accounts": t.array("RawAccount"),
85-
"endpoints": t.array("string"),
86-
"method_subscription": "MethodSubscription",
87-
"api_subscription": "ApiSubscription",
88-
"reporters": "ReportersConfig",
84+
"accounts": t.array("RawAccount"),
85+
"endpoints": t.array("string"),
86+
"startup_notification": "boolean",
87+
"method_subscription": "MethodSubscription",
88+
"api_subscription": "ApiSubscription",
89+
"reporters": "ReportersConfig",
8990
});
9091

9192
const exportedTypeSuite: t.ITypeSuite = {
92-
Only,
93-
Ignore,
94-
All,
95-
MethodSubscription,
96-
ISubscriptionTarget,
97-
RawAccount,
98-
BatchConfig,
99-
EmailConfig,
100-
MatrixConfig,
101-
FsConfig,
102-
ConsoleConfig,
103-
TelegramConfig,
104-
ReportersConfig,
105-
ApiSubscription,
106-
AppConfig,
93+
Only,
94+
Ignore,
95+
All,
96+
MethodSubscription,
97+
ISubscriptionTarget,
98+
RawAccount,
99+
BatchConfig,
100+
EmailConfig,
101+
MatrixConfig,
102+
FsConfig,
103+
ConsoleConfig,
104+
TelegramConfig,
105+
ReportersConfig,
106+
ApiSubscription,
107+
AppConfig,
107108
};
108109
export default exportedTypeSuite;

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export enum ApiSubscription {
106106
export interface AppConfig {
107107
accounts: RawAccount[];
108108
endpoints: string[];
109+
startup_notification: boolean;
109110
method_subscription: MethodSubscription;
110111
api_subscription: ApiSubscription;
111112
reporters: ReportersConfig;

src/index.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
Reporter,
1010
NotificationReportType,
1111
EventInner,
12-
ExtrinsicInner
12+
ExtrinsicInner,
13+
MiscReport
1314
} from './reporters';
1415
import { ApiSubscription, AppConfig, ConfigBuilder, MethodSubscription } from './config';
1516
import {
@@ -218,7 +219,7 @@ async function listAllChains(config: AppConfig, reporters: Reporter[]) {
218219
}
219220

220221
async function main() {
221-
const { config, reporters } = new ConfigBuilder();
222+
const { config, reporters, configName } = new ConfigBuilder();
222223

223224
const graceful = () => {
224225
// if they are batch reporters, clean them.
@@ -242,6 +243,18 @@ async function main() {
242243
const retry = true;
243244
while (retry) {
244245
try {
246+
if (config.startup_notification) {
247+
// send a startup notification
248+
const report: MiscReport = {
249+
time: new Date(),
250+
message: `program ${configName} restarted`,
251+
_type: 'misc'
252+
};
253+
reporters.forEach(async (r) => {
254+
await r.report(report);
255+
});
256+
}
257+
245258
await listAllChains(config, reporters);
246259
} catch (e) {
247260
// if they are batch reporters, clean them.

0 commit comments

Comments
 (0)