Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: query swapinfo by more columns #399

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/db/models/ReverseSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ class ReverseSwap extends Model implements ReverseSwapType {
unique: true,
fields: ['id'],
},
{
unique: false,
fields: ['status'],
},
{
unique: true,
fields: ['preimageHash'],
Expand All @@ -172,6 +176,14 @@ class ReverseSwap extends Model implements ReverseSwapType {
unique: false,
fields: ['referral'],
},
{
unique: false,
fields: ['lockupAddress'],
},
{
unique: false,
fields: ['transactionId'],
},
],
},
);
Expand Down
12 changes: 12 additions & 0 deletions lib/db/models/Swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ class Swap extends Model implements SwapType {
unique: true,
fields: ['id'],
},
{
unique: false,
fields: ['status'],
},
{
unique: true,
fields: ['preimageHash'],
Expand All @@ -131,6 +135,14 @@ class Swap extends Model implements SwapType {
unique: false,
fields: ['referral'],
},
{
unique: false,
fields: ['lockupAddress'],
},
{
unique: false,
fields: ['lockupTransactionId'],
},
],
},
);
Expand Down
49 changes: 30 additions & 19 deletions lib/notifications/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/notifications/DiscordClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/notifications/Markup.ts
Original file line number Diff line number Diff line change
@@ -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:',
};
Expand Down
5 changes: 3 additions & 2 deletions lib/notifications/NotificationProvider.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -174,8 +175,8 @@ class NotificationProvider {
orderSide,
);

return `${receiving}${isReverse ? ' :zap:' : ''} -> ${sending}${
!isReverse ? ' :zap:' : ''
return `${receiving}${isReverse ? ` ${Emojis.Zap}` : ''} -> ${sending}${
!isReverse ? ` ${Emojis.Zap}` : ''
}`;
};

Expand Down
4 changes: 2 additions & 2 deletions test/unit/notifications/CommandHandler.spec.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down