This repository was archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathisAlive.ts
More file actions
63 lines (62 loc) · 2.32 KB
/
isAlive.ts
File metadata and controls
63 lines (62 loc) · 2.32 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
import { IChannelSigner, IEngineStore, IMessagingService, IVectorChainService, Result } from "@connext/vector-types";
import { getParticipant } from "@connext/vector-utils";
import { BaseLogger } from "pino";
export async function sendIsAlive(
mySigner: IChannelSigner,
messaging: IMessagingService,
store: IEngineStore,
chainService: IVectorChainService,
logger: BaseLogger,
): Promise<void> {
const method = "sendIsAlive";
const channels = await store.getChannelStates();
const providers = chainService.getChainRpcProviders();
if (providers.isError) {
logger.error({ ...providers.getError(), method }, "Error getting chain providers");
return;
}
const supportedChains = Object.keys(providers.getValue()).map((chain) => parseInt(chain));
logger.info({ method, numChannels: channels.length }, "Sending check-in messages");
await Promise.all(
channels.map(async (channel) => {
if (!supportedChains.includes(channel.networkContext.chainId)) {
logger.debug(
{ chainId: channel.networkContext.chainId, supportedChains, method, channelAddress: channel.channelAddress },
"Channel chain not supported, skipping",
);
return;
}
const participant = getParticipant(channel, mySigner.publicIdentifier);
if (!participant) {
logger.error(
{ participant, alice: channel.aliceIdentifier, bob: channel.bobIdentifier, channel: channel.channelAddress },
"Signer not in channel",
);
return;
}
const counterpartyIdentifier = participant === "alice" ? channel.bobIdentifier : channel.aliceIdentifier;
const res = await messaging.sendIsAliveMessage(
Result.ok({ channelAddress: channel.channelAddress }),
counterpartyIdentifier,
mySigner.publicIdentifier,
);
if (res.isError) {
logger.error(
{
method,
counterpartyIdentifier,
channel: channel.channelAddress,
error: res.getError()?.message,
context: res.getError()?.context,
},
"Error sending checkIn message",
);
} else {
logger.info(
{ method, counterpartyIdentifier, channel: channel.channelAddress, result: res.getValue() },
"Successfully sent checkIn message",
);
}
}),
);
}