Skip to content

Commit

Permalink
Make slack bot optional
Browse files Browse the repository at this point in the history
  • Loading branch information
moreal committed Dec 18, 2023
1 parent a7402b1 commit c01ea47
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 49 deletions.
13 changes: 2 additions & 11 deletions src/headless-graphql-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ import { Signer } from "./signer";
import { Sqlite3MonitorStateStore } from "./sqlite3-monitor-state-store";
import { HeadlessTxPool } from "./txpool/headless";

const FAKE_SLACK_MESSAGE_SENDER = {
sendMessage() {
return Promise.resolve({}) as Promise<ChatPostMessageResponse>;
},
};

const odinClient = new HeadlessGraphQLClient({
id: "0x100000000000",
rpcEndpoints: {
Expand Down Expand Up @@ -48,7 +42,7 @@ test(".getGarageUnloadEvents()", async () => {
await heimdallClient.getGenesisHash(),
);
const minter = new Minter(signer);
const observer = new GarageObserver(FAKE_SLACK_MESSAGE_SENDER, minter, {
const observer = new GarageObserver(null, minter, {
agentAddress: Address.fromHex(
"0x1c2ae97380CFB4F732049e454F6D9A25D4967c6f",
),
Expand Down Expand Up @@ -89,10 +83,7 @@ test("getAssetTransferredEvents()", async () => {
);
const minter = new Minter(signer);
const stateStore = await Sqlite3MonitorStateStore.open("test");
const observer = new AssetTransferredObserver(
FAKE_SLACK_MESSAGE_SENDER,
minter,
);
const observer = new AssetTransferredObserver(null, minter);
await observer.notify({
blockHash: "",
events: evs.map((ev) => {
Expand Down
31 changes: 18 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ import { processUpstreamEvents } from "./sync/upstream";
import { getTxpoolFromEnv } from "./txpool";
import { Planet } from "./types/registry";

const slackBot = new SlackBot(
getRequiredEnv("SLACK__BOT_USERNAME"),
new SlackChannel(
getRequiredEnv("SLACK__CHANNEL"),
new WebClient(getRequiredEnv("SLACK__BOT_TOKEN")),
),
);
const SLACK_BOT_USERNAME = getEnv("SLACK__BOT_USERNAME");
const SLACK_CHANNEL = getEnv("SLACK__CHANNEL");
const SLACK_BOT_TOKEN = getEnv("SLACK__BOT_TOKEN");
const slackBot: SlackBot | null =
SLACK_BOT_USERNAME !== undefined &&
SLACK_BOT_TOKEN !== undefined &&
SLACK_CHANNEL !== undefined
? new SlackBot(
SLACK_BOT_USERNAME,
new SlackChannel(SLACK_CHANNEL, new WebClient(SLACK_BOT_TOKEN)),
)
: null;

(async () => {
const [upstreamPlanet, downstreamPlanet]: Planet[] =
Expand All @@ -50,7 +55,7 @@ const slackBot = new SlackBot(
const upstreamAccount = getAccountFromEnv("NC_UPSTREAM");
const downstreamAccount = getAccountFromEnv("NC_DOWNSTREAM");

await slackBot.sendMessage(
await slackBot?.sendMessage(
new AppStartEvent(
await upstreamAccount.getAddress(),
await downstreamAccount.getAddress(),
Expand Down Expand Up @@ -86,7 +91,7 @@ const slackBot = new SlackBot(
}
})().catch(async (error) => {
console.error(error);
await slackBot.sendMessage(new AppStopEvent(error));
await slackBot?.sendMessage(new AppStopEvent(error));
process.exit(-1);
});

Expand All @@ -97,7 +102,7 @@ async function withMonitors(
downstreamAccount: Account,
agentAddress: Address,
avatarAddress: Address,
slackBot: SlackBot,
slackBot: SlackBot | null,
) {
const monitorStateStore: IMonitorStateStore =
await Sqlite3MonitorStateStore.open(
Expand Down Expand Up @@ -192,7 +197,7 @@ async function withMonitors(
downstreamTxpool.stop();
}

slackBot.sendMessage(new AppStopEvent());
slackBot?.sendMessage(new AppStopEvent());
};

process.on("SIGTERM", handleSignal);
Expand All @@ -218,7 +223,7 @@ async function withRDB(
downstreamAccount: Account,
agentAddress: Address,
avatarAddress: Address,
slackBot: SlackBot,
slackBot: SlackBot | null,
) {
const upstreamStartBlockIndex = BigInt(
getRequiredEnv("NC_UPSTREAM__RDB__START_BLOCK_INDEX"),
Expand Down Expand Up @@ -294,7 +299,7 @@ async function withRDB(
return async () => {
console.log(signal, "handler called.");
await processor.stop();
await slackBot.sendMessage(new AppStopEvent());
await slackBot?.sendMessage(new AppStopEvent());
};
}

Expand Down
10 changes: 5 additions & 5 deletions src/observers/asset-downstream-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export class AssetDownstreamObserver
events: (AssetTransferredEvent & TransactionLocation)[];
}>
{
private readonly _slackbot: ISlackMessageSender;
private readonly _slackbot: ISlackMessageSender | null;
private readonly _transfer: IAssetTransfer;
private readonly _burner: IAssetBurner;

constructor(
slackbot: ISlackMessageSender,
slackbot: ISlackMessageSender | null,
upstreamTransfer: IAssetTransfer,
downstreamBurner: IAssetBurner,
) {
Expand Down Expand Up @@ -60,7 +60,7 @@ export class AssetDownstreamObserver
const recipient = ev.memo.toString();
this.debug("Try to burn");
const burnTxId = await this._burner.burn(ev.amount, ev.txId);
await this._slackbot.sendMessage(
await this._slackbot?.sendMessage(
new BridgeEvent(
"BURN",
[ev.planetID, ev.txId],
Expand Down Expand Up @@ -95,7 +95,7 @@ export class AssetDownstreamObserver
amount,
null,
);
await this._slackbot.sendMessage(
await this._slackbot?.sendMessage(
new BridgeEvent(
"TRANSFER",
[ev.planetID, ev.txId],
Expand All @@ -107,7 +107,7 @@ export class AssetDownstreamObserver
this.debug("TransferAsset TxId is", transferTxId);
} catch (e) {
console.error(e);
await this._slackbot.sendMessage(
await this._slackbot?.sendMessage(
new BridgeErrorEvent([ev.planetID, ev.txId], e),
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/observers/asset-transferred-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export class AssetTransferredObserver
}>
{
private readonly _minter: IMinter;
private readonly _slackbot: ISlackMessageSender;
constructor(slackbot: ISlackMessageSender, minter: IMinter) {
private readonly _slackbot: ISlackMessageSender | null;
constructor(slackbot: ISlackMessageSender | null, minter: IMinter) {
this._slackbot = slackbot;

this._minter = minter;
Expand Down Expand Up @@ -55,7 +55,7 @@ export class AssetTransferredObserver
[{ recipient, amount: amountToMint }],
null,
);
await this._slackbot.sendMessage(
await this._slackbot?.sendMessage(
new BridgeEvent(
"MINT",
[planetID, txId],
Expand All @@ -66,7 +66,7 @@ export class AssetTransferredObserver
);
} catch (e) {
console.error(e);
await this._slackbot.sendMessage(
await this._slackbot?.sendMessage(
new BridgeErrorEvent([planetID, txId], e),
);
}
Expand Down
8 changes: 1 addition & 7 deletions src/observers/garage-observer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import { Sqlite3MonitorStateStore } from "../sqlite3-monitor-state-store";
import { HeadlessTxPool } from "../txpool/headless";
import { GarageObserver } from "./garage-observer";

const FAKE_SLACK_MESSAGE_SENDER = {
sendMessage() {
return Promise.resolve({}) as Promise<ChatPostMessageResponse>;
},
};

test("notify", async () => {
const monitorStateStore = await Sqlite3MonitorStateStore.open("test");
const account = RawPrivateKey.fromHex("");
Expand All @@ -32,7 +26,7 @@ test("notify", async () => {
await headlessClient.getGenesisHash(),
);
const minter = new Minter(signer);
const observer = new GarageObserver(FAKE_SLACK_MESSAGE_SENDER, minter, {
const observer = new GarageObserver(null, minter, {
agentAddress: Address.fromHex(
"0x1c2ae97380CFB4F732049e454F6D9A25D4967c6f",
),
Expand Down
8 changes: 4 additions & 4 deletions src/observers/garage-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ export class GarageObserver
events: (ValidatedGarageUnloadEvent & TransactionLocation)[];
}>
{
private readonly _slackbot: ISlackMessageSender;
private readonly _slackbot: ISlackMessageSender | null;
private readonly _minter: IMinter;
private readonly _vaultAddresses: {
agentAddress: Address;
avatarAddress: Address;
};

constructor(
slackbot: ISlackMessageSender,
slackbot: ISlackMessageSender | null,
minter: IMinter,
vaultAddresses: {
agentAddress: Address;
Expand Down Expand Up @@ -88,7 +88,7 @@ export class GarageObserver
requests,
memoForMinter,
);
await this._slackbot.sendMessage(
await this._slackbot?.sendMessage(
new BridgeEvent(
"MINT",
[planetID, txId],
Expand All @@ -100,7 +100,7 @@ export class GarageObserver
}
} catch (e) {
console.error(e);
await this._slackbot.sendMessage(
await this._slackbot?.sendMessage(
new BridgeErrorEvent([planetID, txId], e),
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/sync/downstream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function processDownstreamEvents(
downstreamGQLClient: IHeadlessGraphQLClient,
agentAddress: Address,
defaultStartBlockIndex: bigint,
slackBot: SlackBot,
slackBot: SlackBot | null,
) {
const upstreamNetworkId = upstreamGQLClient.getPlanetID();
const downstreamNetworkId = downstreamGQLClient.getPlanetID();
Expand Down Expand Up @@ -189,7 +189,7 @@ export async function processDownstreamEvents(
}

for (const responseTransaction of responseTransactions) {
await slackBot.sendMessage(
await slackBot?.sendMessage(
new BridgeEvent(
dbTypeToSlackType(responseTransaction.type),
[downstreamNetworkId, responseTransaction.requestTransactionId],
Expand Down
6 changes: 3 additions & 3 deletions src/sync/upstream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function processUpstreamEvents(
agentAddress: Address,
avatarAddress: Address,
defaultStartBlockIndex: bigint,
slackBot: SlackBot,
slackBot: SlackBot | null,
) {
const downstreamNetworkId = downstreamGQLClient.getPlanetID();
const upstreamNetworkId = upstreamGQLClient.getPlanetID();
Expand Down Expand Up @@ -220,7 +220,7 @@ export async function processUpstreamEvents(
}

for (const responseTransaction of responseTransactions) {
await slackBot.sendMessage(
await slackBot?.sendMessage(
new BridgeEvent(
dbTypeToSlackType(responseTransaction.type),
[upstreamNetworkId, responseTransaction.requestTransactionId],
Expand All @@ -233,7 +233,7 @@ export async function processUpstreamEvents(

for (const ev of unloadGarageEventsWithInvalidMemo) {
try {
await slackBot.sendMessage(
await slackBot?.sendMessage(
new BridgeErrorEvent(
[upstreamNetworkId, ev.txId],
new Error(`INVALID_MEMO: ${JSON.stringify(ev.parsedMemo)}`),
Expand Down

0 comments on commit c01ea47

Please sign in to comment.