Skip to content

Commit 3469df2

Browse files
committed
refactor: type checking for BalanceAlert event
1 parent 0f95df4 commit 3469df2

File tree

5 files changed

+38
-31
lines changed

5 files changed

+38
-31
lines changed

lib/alerts/Alerts.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { EventEmitter } from 'events';
2-
import { Alert, BalanceAlert } from './types';
3-
import SwapClientManager from '../swaps/SwapClientManager';
4-
import Logger from '../Logger';
5-
import { AlertType, ChannelSide } from '../constants/enums';
62
import { satsToCoinsStr } from '../cli/utils';
3+
import { AlertType, ChannelSide } from '../constants/enums';
4+
import Logger from '../Logger';
5+
import SwapClientManager from '../swaps/SwapClientManager';
6+
import { Alert, BalanceAlertEvent } from './types';
77

88
interface Alerts {
99
on(event: 'alert', listener: (alert: Alert) => void): this;
@@ -35,20 +35,25 @@ class Alerts extends EventEmitter {
3535
swapClientManager.connextClient?.on('lowTradingBalance', this.onLowTradingBalance);
3636
}
3737

38-
private onLowTradingBalance = (balanceAlert: BalanceAlert) => {
38+
private onLowTradingBalance = (balanceAlertEvent: BalanceAlertEvent) => {
3939
// TODO don't use JSON.stringify instead find a way to define unique ids per alert and keep in the map to avoid memory issues
40-
const stringRepresentation = JSON.stringify(balanceAlert);
40+
const stringRepresentation = JSON.stringify(balanceAlertEvent);
4141
this.logger.trace(`received low trading balance alert ${stringRepresentation}`);
4242
if (this.alerts.get(stringRepresentation) === undefined || this.checkAlertThreshold(stringRepresentation)) {
4343
this.logger.trace(`triggering low balance alert ${stringRepresentation}`);
4444

45-
balanceAlert.message = `${ChannelSide[balanceAlert.side || 0]} trading balance (${satsToCoinsStr(
46-
balanceAlert.sideBalance || 0,
47-
)} ${balanceAlert.currency}) is lower than 10% of trading capacity (${satsToCoinsStr(
48-
balanceAlert.totalBalance || 0,
49-
)} ${balanceAlert.currency})`;
50-
balanceAlert.type = AlertType.LowTradingBalance;
51-
balanceAlert.date = Date.now();
45+
const message = `${ChannelSide[balanceAlertEvent.side || 0]} trading balance (${satsToCoinsStr(
46+
balanceAlertEvent.sideBalance || 0,
47+
)} ${balanceAlertEvent.currency}) is lower than 10% of trading capacity (${satsToCoinsStr(
48+
balanceAlertEvent.totalBalance || 0,
49+
)} ${balanceAlertEvent.currency})`;
50+
51+
const balanceAlert = {
52+
...balanceAlertEvent,
53+
message,
54+
type: AlertType.LowTradingBalance,
55+
date: Date.now(),
56+
};
5257

5358
this.alerts.set(stringRepresentation, balanceAlert.date);
5459
this.emit('alert', balanceAlert);

lib/alerts/types.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { AlertType, ChannelSide } from '../constants/enums';
22

3-
export type Alert = BalanceAlert;
3+
type BaseAlert = {
4+
type: AlertType;
5+
message: string;
6+
date: number;
7+
};
48

5-
export type BalanceAlert = BaseAlert & {
9+
export type BalanceAlertEvent = {
610
/** The total balance of the channel when the alert is triggered. */
711
totalBalance: number;
812
/** The side of the balance either local or remote. */
@@ -14,9 +18,6 @@ export type BalanceAlert = BaseAlert & {
1418
/** The currency of the channel. */
1519
currency: string;
1620
};
21+
export type BalanceAlert = BaseAlert & BalanceAlertEvent;
1722

18-
type BaseAlert = {
19-
type: AlertType;
20-
message: string;
21-
date: number;
22-
};
23+
export type Alert = BalanceAlert;

lib/connextclient/ConnextClient.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import assert from 'assert';
33
import http from 'http';
44
import { combineLatest, defer, from, fromEvent, interval, Observable, of, Subscription, throwError, timer } from 'rxjs';
55
import { catchError, distinctUntilChanged, filter, mergeMap, mergeMapTo, pluck, take, timeout } from 'rxjs/operators';
6+
import { BalanceAlertEvent } from '../alerts/types';
67
import { SwapClientType, SwapRole, SwapState } from '../constants/enums';
78
import { CurrencyInstance } from '../db/types';
8-
import { Alert } from '../alerts/types';
99
import Logger from '../Logger';
1010
import swapErrors from '../swaps/errors';
1111
import SwapClient, {
@@ -51,15 +51,15 @@ interface ConnextClient {
5151
on(event: 'htlcAccepted', listener: (rHash: string, amount: number, currency: string) => void): this;
5252
on(event: 'connectionVerified', listener: (swapClientInfo: SwapClientInfo) => void): this;
5353
on(event: 'depositConfirmed', listener: (hash: string) => void): this;
54-
on(event: 'lowTradingBalance', listener: (alert: Alert) => void): this;
54+
on(event: 'lowTradingBalance', listener: (alert: BalanceAlertEvent) => void): this;
5555
once(event: 'initialized', listener: () => void): this;
5656
emit(event: 'htlcAccepted', rHash: string, amount: number, currency: string): boolean;
5757
emit(event: 'connectionVerified', swapClientInfo: SwapClientInfo): boolean;
5858
emit(event: 'initialized'): boolean;
5959
emit(event: 'preimage', preimageRequest: ProvidePreimageEvent): void;
6060
emit(event: 'transferReceived', transferReceivedRequest: TransferReceivedEvent): void;
6161
emit(event: 'depositConfirmed', hash: string): void;
62-
emit(event: 'lowTradingBalance', alert: Alert): boolean;
62+
emit(event: 'lowTradingBalance', alert: BalanceAlertEvent): boolean;
6363
}
6464

6565
const getRouterNodeIdentifier = (network: string): string => {
@@ -343,7 +343,7 @@ class ConnextClient extends SwapClient {
343343
const totalBalance = remoteBalance + localBalance;
344344
const alertThreshold = totalBalance * 0.1;
345345

346-
this.checkLowBalance(remoteBalance, localBalance, totalBalance, alertThreshold, currency, this.emit.bind(this));
346+
this.checkLowBalance(remoteBalance, localBalance, totalBalance, alertThreshold, currency);
347347
}
348348
} catch (e) {
349349
this.logger.error('failed to update total outbound capacity', e);

lib/lndclient/LndClient.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from 'assert';
22
import crypto from 'crypto';
33
import { promises as fs, watch } from 'fs';
44
import grpc, { ChannelCredentials, ClientReadableStream } from 'grpc';
5+
import { BalanceAlertEvent } from 'lib/alerts/types';
56
import path from 'path';
67
import { SwapClientType, SwapRole, SwapState } from '../constants/enums';
78
import Logger from '../Logger';
@@ -27,15 +28,14 @@ import { deriveChild } from '../utils/seedutil';
2728
import { base64ToHex, hexToUint8Array } from '../utils/utils';
2829
import errors from './errors';
2930
import { Chain, ChannelCount, ClientMethods, LndClientConfig, LndInfo } from './types';
30-
import { Alert } from '../alerts/types';
3131

3232
interface LndClient {
3333
on(event: 'connectionVerified', listener: (swapClientInfo: SwapClientInfo) => void): this;
3434
on(event: 'htlcAccepted', listener: (rHash: string, amount: number) => void): this;
3535
on(event: 'channelBackup', listener: (channelBackup: Uint8Array) => void): this;
3636
on(event: 'channelBackupEnd', listener: () => void): this;
3737
on(event: 'locked', listener: () => void): this;
38-
on(event: 'lowTradingBalance', listener: (alert: Alert) => void): this;
38+
on(event: 'lowTradingBalance', listener: (alert: BalanceAlertEvent) => void): this;
3939

4040
once(event: 'initialized', listener: () => void): this;
4141

@@ -45,7 +45,7 @@ interface LndClient {
4545
emit(event: 'channelBackupEnd'): boolean;
4646
emit(event: 'locked'): boolean;
4747
emit(event: 'initialized'): boolean;
48-
emit(event: 'lowTradingBalance', alert: Alert): boolean;
48+
emit(event: 'lowTradingBalance', alert: BalanceAlertEvent): boolean;
4949
}
5050

5151
const GRPC_CLIENT_OPTIONS = {
@@ -257,7 +257,6 @@ class LndClient extends SwapClient {
257257
totalBalance,
258258
alertThreshold,
259259
this.currency,
260-
this.emit.bind(this),
261260
);
262261
})
263262
.catch(async (err) => {

lib/swaps/SwapClient.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { EventEmitter } from 'events';
2+
import { BalanceAlertEvent } from 'lib/alerts/types';
23
import { ChannelSide, SwapClientType } from '../constants/enums';
34
import Logger from '../Logger';
45
import { setTimeoutPromise } from '../utils/utils';
@@ -71,8 +72,10 @@ export type WithdrawArguments = {
7172

7273
interface SwapClient {
7374
on(event: 'connectionVerified', listener: (swapClientInfo: SwapClientInfo) => void): this;
75+
on(event: 'lowTradingBalance', listener: (alert: BalanceAlertEvent) => void): this;
7476
once(event: 'initialized', listener: () => void): this;
7577
emit(event: 'connectionVerified', swapClientInfo: SwapClientInfo): boolean;
78+
emit(event: 'lowTradingBalance', alert: BalanceAlertEvent): boolean;
7679
emit(event: 'initialized'): boolean;
7780
}
7881

@@ -234,10 +237,9 @@ abstract class SwapClient extends EventEmitter {
234237
totalBalance: number,
235238
alertThreshold: number,
236239
currency: string,
237-
emit: Function,
238240
) => {
239241
if (localBalance < alertThreshold) {
240-
emit('lowTradingBalance', {
242+
this.emit('lowTradingBalance', {
241243
totalBalance,
242244
currency,
243245
side: ChannelSide.Local,
@@ -247,7 +249,7 @@ abstract class SwapClient extends EventEmitter {
247249
}
248250

249251
if (remoteBalance < alertThreshold) {
250-
emit('lowTradingBalance', {
252+
this.emit('lowTradingBalance', {
251253
totalBalance,
252254
currency,
253255
side: ChannelSide.Remote,

0 commit comments

Comments
 (0)