diff --git a/src/headless-graphql-client.spec.ts b/src/headless-graphql-client.spec.ts index c4bfaea..555b5ce 100644 --- a/src/headless-graphql-client.spec.ts +++ b/src/headless-graphql-client.spec.ts @@ -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; - }, -}; - const odinClient = new HeadlessGraphQLClient({ id: "0x100000000000", rpcEndpoints: { @@ -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", ), @@ -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) => { diff --git a/src/index.ts b/src/index.ts index aef091b..ec7e6b4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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[] = @@ -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(), @@ -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); }); @@ -97,7 +102,7 @@ async function withMonitors( downstreamAccount: Account, agentAddress: Address, avatarAddress: Address, - slackBot: SlackBot, + slackBot: SlackBot | null, ) { const monitorStateStore: IMonitorStateStore = await Sqlite3MonitorStateStore.open( @@ -192,7 +197,7 @@ async function withMonitors( downstreamTxpool.stop(); } - slackBot.sendMessage(new AppStopEvent()); + slackBot?.sendMessage(new AppStopEvent()); }; process.on("SIGTERM", handleSignal); @@ -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"), @@ -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()); }; } diff --git a/src/observers/asset-downstream-observer.ts b/src/observers/asset-downstream-observer.ts index 213607a..a8454d9 100644 --- a/src/observers/asset-downstream-observer.ts +++ b/src/observers/asset-downstream-observer.ts @@ -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, ) { @@ -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], @@ -95,7 +95,7 @@ export class AssetDownstreamObserver amount, null, ); - await this._slackbot.sendMessage( + await this._slackbot?.sendMessage( new BridgeEvent( "TRANSFER", [ev.planetID, ev.txId], @@ -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), ); } diff --git a/src/observers/asset-transferred-observer.ts b/src/observers/asset-transferred-observer.ts index e1408db..5abda35 100644 --- a/src/observers/asset-transferred-observer.ts +++ b/src/observers/asset-transferred-observer.ts @@ -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; @@ -55,7 +55,7 @@ export class AssetTransferredObserver [{ recipient, amount: amountToMint }], null, ); - await this._slackbot.sendMessage( + await this._slackbot?.sendMessage( new BridgeEvent( "MINT", [planetID, txId], @@ -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), ); } diff --git a/src/observers/garage-observer.spec.ts b/src/observers/garage-observer.spec.ts index 649b764..308b81f 100644 --- a/src/observers/garage-observer.spec.ts +++ b/src/observers/garage-observer.spec.ts @@ -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; - }, -}; - test("notify", async () => { const monitorStateStore = await Sqlite3MonitorStateStore.open("test"); const account = RawPrivateKey.fromHex(""); @@ -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", ), diff --git a/src/observers/garage-observer.ts b/src/observers/garage-observer.ts index dc1d22f..9e8a3ef 100644 --- a/src/observers/garage-observer.ts +++ b/src/observers/garage-observer.ts @@ -21,7 +21,7 @@ export class GarageObserver events: (ValidatedGarageUnloadEvent & TransactionLocation)[]; }> { - private readonly _slackbot: ISlackMessageSender; + private readonly _slackbot: ISlackMessageSender | null; private readonly _minter: IMinter; private readonly _vaultAddresses: { agentAddress: Address; @@ -29,7 +29,7 @@ export class GarageObserver }; constructor( - slackbot: ISlackMessageSender, + slackbot: ISlackMessageSender | null, minter: IMinter, vaultAddresses: { agentAddress: Address; @@ -88,7 +88,7 @@ export class GarageObserver requests, memoForMinter, ); - await this._slackbot.sendMessage( + await this._slackbot?.sendMessage( new BridgeEvent( "MINT", [planetID, txId], @@ -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), ); } diff --git a/src/sync/downstream/index.ts b/src/sync/downstream/index.ts index f9576be..98a14d7 100644 --- a/src/sync/downstream/index.ts +++ b/src/sync/downstream/index.ts @@ -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(); @@ -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], diff --git a/src/sync/upstream/index.ts b/src/sync/upstream/index.ts index 94b450d..9288564 100644 --- a/src/sync/upstream/index.ts +++ b/src/sync/upstream/index.ts @@ -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(); @@ -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], @@ -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)}`),