From 16fa4a9bc848e95990e1ff4e7c68e88c14806bf8 Mon Sep 17 00:00:00 2001 From: michael1011 Date: Sun, 8 Oct 2023 22:56:09 +0200 Subject: [PATCH] feat: query swapinfo by more columns --- lib/notifications/CommandHandler.ts | 49 ++++++++++++------- lib/notifications/DiscordClient.ts | 2 +- lib/notifications/Markup.ts | 2 +- lib/notifications/NotificationProvider.ts | 5 +- .../unit/notifications/CommandHandler.spec.ts | 4 +- 5 files changed, 37 insertions(+), 25 deletions(-) diff --git a/lib/notifications/CommandHandler.ts b/lib/notifications/CommandHandler.ts index ab7695785..f180d619f 100644 --- a/lib/notifications/CommandHandler.ts +++ b/lib/notifications/CommandHandler.ts @@ -18,11 +18,11 @@ import { coinsToSatoshis, satoshisToCoins } from '../DenominationConverter'; import ReverseSwapRepository from '../db/repositories/ReverseSwapRepository'; import ChannelCreationRepository from '../db/repositories/ChannelCreationRepository'; import { + stringify, + splitPairId, formatError, - getChainCurrency, getHexString, - splitPairId, - stringify, + getChainCurrency, } from '../Utils'; enum Command { @@ -262,33 +262,44 @@ class CommandHandler { return; } - const id = args[0]; + const identifier = args[0]; - const swap = await SwapRepository.getSwap({ - id, + const swaps = await SwapRepository.getSwaps({ + [Op.or]: { + id: identifier, + invoice: identifier, + preimageHash: identifier, + lockupAddress: identifier, + lockupTransactionId: identifier, + }, }); - if (swap) { + for (const swap of swaps) { const channelCreation = await ChannelCreationRepository.getChannelCreation({ - swapId: id, + swapId: swap.id, }); await this.sendSwapInfo(swap, false, channelCreation); - return; - } else { - // Query for a reverse swap because there was no normal one found with the specified id - const reverseSwap = await ReverseSwapRepository.getReverseSwap({ - id, - }); + } - if (reverseSwap) { - await this.sendSwapInfo(reverseSwap, true); - return; - } + const reverseSwaps = await ReverseSwapRepository.getReverseSwaps({ + [Op.or]: { + id: identifier, + invoice: identifier, + preimageHash: identifier, + lockupAddress: identifier, + transactionId: identifier, + }, + }); + + for (const reverseSwap of reverseSwaps) { + await this.sendSwapInfo(reverseSwap, true); } - await this.sendCouldNotFindSwap(id); + if (swaps.length === 0 && reverseSwaps.length === 0) { + await this.sendCouldNotFindSwap(identifier); + } }; private getStats = async () => { diff --git a/lib/notifications/DiscordClient.ts b/lib/notifications/DiscordClient.ts index f5c0dc5c4..9be806c9a 100644 --- a/lib/notifications/DiscordClient.ts +++ b/lib/notifications/DiscordClient.ts @@ -64,7 +64,7 @@ class DiscordClient extends EventEmitter { public destroy = (): void => { this.channel = undefined; if (this.client.isReady()) { - this.client.destroy(); + this.client.destroy().then(); } }; diff --git a/lib/notifications/Markup.ts b/lib/notifications/Markup.ts index 17ee1f4b9..2708f47c7 100644 --- a/lib/notifications/Markup.ts +++ b/lib/notifications/Markup.ts @@ -1,5 +1,5 @@ -// TODO: add all other used emojis to this object export const Emojis = { + Zap: ':zap:', Checkmark: ':white_check_mark:', RotatingLight: ':rotating_light:', }; diff --git a/lib/notifications/NotificationProvider.ts b/lib/notifications/NotificationProvider.ts index de8be6008..8198b448c 100644 --- a/lib/notifications/NotificationProvider.ts +++ b/lib/notifications/NotificationProvider.ts @@ -1,4 +1,5 @@ import Logger from '../Logger'; +import { Emojis } from './Markup'; import Swap from '../db/models/Swap'; import Service from '../service/Service'; import DiscordClient from './DiscordClient'; @@ -174,8 +175,8 @@ class NotificationProvider { orderSide, ); - return `${receiving}${isReverse ? ' :zap:' : ''} -> ${sending}${ - !isReverse ? ' :zap:' : '' + return `${receiving}${isReverse ? ` ${Emojis.Zap}` : ''} -> ${sending}${ + !isReverse ? ` ${Emojis.Zap}` : '' }`; }; diff --git a/test/unit/notifications/CommandHandler.spec.ts b/test/unit/notifications/CommandHandler.spec.ts index 23ad71e9c..c675ae817 100644 --- a/test/unit/notifications/CommandHandler.spec.ts +++ b/test/unit/notifications/CommandHandler.spec.ts @@ -1,16 +1,16 @@ import { wait } from '../../Utils'; import Logger from '../../../lib/Logger'; -import { getHexBuffer, getHexString, stringify } from '../../../lib/Utils'; import Database from '../../../lib/db/Database'; import Service from '../../../lib/service/Service'; import { NotificationConfig } from '../../../lib/Config'; -import { Balances, GetBalanceResponse } from '../../../lib/proto/boltzrpc_pb'; import ReferralStats from '../../../lib/data/ReferralStats'; import BackupScheduler from '../../../lib/backup/BackupScheduler'; import DiscordClient from '../../../lib/notifications/DiscordClient'; import CommandHandler from '../../../lib/notifications/CommandHandler'; import PairRepository from '../../../lib/db/repositories/PairRepository'; import SwapRepository from '../../../lib/db/repositories/SwapRepository'; +import { getHexBuffer, getHexString, stringify } from '../../../lib/Utils'; +import { Balances, GetBalanceResponse } from '../../../lib/proto/boltzrpc_pb'; import ReverseSwapRepository from '../../../lib/db/repositories/ReverseSwapRepository'; import ChannelCreationRepository from '../../../lib/db/repositories/ChannelCreationRepository'; import {