Skip to content

Commit

Permalink
feat: max unused wallet balance alert
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Oct 14, 2023
1 parent 7de4271 commit 11bfb6a
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 89 deletions.
33 changes: 17 additions & 16 deletions docker/regtest/data/lnd/certificates/tls.cert
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIC8TCCApigAwIBAgIQQVP4MQ4VpmS4YzRcehfYUzAKBggqhkjOPQQDAjAuMR8w
HQYDVQQKExZsbmQgYXV0b2dlbmVyYXRlZCBjZXJ0MQswCQYDVQQDEwJwYzAeFw0y
MjA4MTcyMDI0MTNaFw0yMzEwMTIyMDI0MTNaMC4xHzAdBgNVBAoTFmxuZCBhdXRv
Z2VuZXJhdGVkIGNlcnQxCzAJBgNVBAMTAnBjMFkwEwYHKoZIzj0CAQYIKoZIzj0D
AQcDQgAEjV+qpIuOm2/lKot5GYH1YBqSzZ/ZEvhb6MVuIlxzDDK8GEMz6U+BuaZx
9RXpUnxHy9oQvn10Jd57QZ8iw0wypqOCAZYwggGSMA4GA1UdDwEB/wQEAwICpDAT
BgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBTa
W0INu48g2wT+jxMbZxmQGxBdWzCCATkGA1UdEQSCATAwggEsggJwY4IJbG9jYWxo
b3N0ggR1bml4ggp1bml4cGFja2V0ggdidWZjb25uhwR/AAABhxAAAAAAAAAAAAAA
AAAAAAABhwQKAAEGhwTAqHoBhwSsEgABhwSsEQABhwTAqIoBhwSsEOsBhxD9AAAA
AAAAAEzWTkM0lLaThxD9AAAAAAAAAFV4AnjaCgH+hxD9AAAAAAAAAKLxwPzoX67z
hxD+gAAAAAAAAJu73GSvPP4/hxD+gAAAAAAAAABC/P/+Z1iThxD+gAAAAAAAAABC
Mf/+pQwzhxD+gAAAAAAAAAJQVv/+wAABhxD+gAAAAAAAAAJQVv/+w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Z///qYHCYcQ/oAAAAAAAABEjRT//rIV
6YcQ/oAAAAAAAAC8WHr//v4YvzAKBggqhkjOPQQDAgNIADBFAiEAke4GdfKlYbKm
HwIX4PLIRX4d+oJUw1W8mY1awE3Owo0CICukh5AIgdtTZ2hIPz2kzdvqI7/sSct1
0GGmeGB6KNbN
-----END CERTIFICATE-----
6 changes: 3 additions & 3 deletions docker/regtest/data/lnd/certificates/tls.key
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIIsPW/Yrm2GFyDYeb2IfbJyWplGSTaEts+nOoUzymlTDoAoGCCqGSM49
AwEHoUQDQgAEjV+qpIuOm2/lKot5GYH1YBqSzZ/ZEvhb6MVuIlxzDDK8GEMz6U+B
uaZx9RXpUnxHy9oQvn10Jd57QZ8iw0wypg==
MHcCAQEEIBf1oibpkO7itHOgFCjQjpKQBtX88uv08Y3YDR0QprKCoAoGCCqGSM49
AwEHoUQDQgAEUqBgYw3mP6vYCZ5dca3DFHj+7X8FussDWXjkUOZJAGqGFjWKLEJF
yjpTes1y5MQy7Fphj/xdUe7Wxd/0Gid3+g==
-----END EC PRIVATE KEY-----
9 changes: 7 additions & 2 deletions lib/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ type ChainConfig = {
mempoolSpace?: string;
};

type PreferredWallet = 'lnd' | 'core' | undefined;

type BaseCurrencyConfig = {
symbol: string;
network: Network;

preferredWallet: PreferredWallet;

minWalletBalance: number;
maxWalletBalance?: number;

maxUnusedWalletBalance?: number;

maxZeroConfAmount: number;

chain: ChainConfig;
Expand All @@ -49,8 +55,6 @@ type RoutingOffsetException = {
};

type CurrencyConfig = BaseCurrencyConfig & {
preferredWallet: 'lnd' | 'core' | undefined;

lnd?: LndConfig;
cln?: ClnConfig;
routingOffsetExceptions?: RoutingOffsetException[];
Expand Down Expand Up @@ -466,6 +470,7 @@ export {
BackupConfig,
EthereumConfig,
CurrencyConfig,
PreferredWallet,
BaseCurrencyConfig,
NotificationConfig,
EthProviderServiceConfig,
Expand Down
70 changes: 55 additions & 15 deletions lib/notifications/BalanceChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import Service from '../service/Service';
import DiscordClient from './DiscordClient';
import { Balances } from '../proto/boltzrpc_pb';
import { liquidSymbol } from '../consts/LiquidTypes';
import { BaseCurrencyConfig, TokenConfig } from '../Config';
import { satoshisToSatcomma } from '../DenominationConverter';
import { BaseCurrencyConfig, PreferredWallet, TokenConfig } from '../Config';

enum BalanceType {
Wallet,
Expand All @@ -16,9 +16,13 @@ enum BalanceType {
type CurrenyThresholds = {
symbol: string;

preferredWallet?: PreferredWallet;

minWalletBalance: number;
maxWalletBalance?: number;

maxUnusedWalletBalance?: number;

minLocalBalance?: number;
minRemoteBalance?: number;
};
Expand Down Expand Up @@ -75,7 +79,8 @@ class BalanceChecker {
currency,
service,
BalanceType.Wallet,
balance.confirmed + balance.unconfirmed,
Number(BigInt(balance.confirmed) + BigInt(balance.unconfirmed)),
balances.walletsMap.length === 1,
);
}

Expand All @@ -101,18 +106,36 @@ class BalanceChecker {
service: string,
type: BalanceType,
balance: number,
isOnlyWallet?: boolean,
) => {
let isInBounds: boolean;
let isMainWallet = false;
let notificationSet: Set<string>;

if (type === BalanceType.Wallet) {
notificationSet = this.walletBalanceAlerts;

const { minWalletBalance, maxWalletBalance } = currency;

isInBounds =
minWalletBalance <= balance &&
balance <= (maxWalletBalance || Number.MAX_SAFE_INTEGER);
const {
preferredWallet,
minWalletBalance,
maxWalletBalance,
maxUnusedWalletBalance,
} = currency;

if (
isOnlyWallet ||
(preferredWallet || 'lnd') === service.toLowerCase()
) {
isMainWallet = true;
isInBounds =
minWalletBalance <= balance &&
balance <= (maxWalletBalance || Number.MAX_SAFE_INTEGER);
} else if (maxUnusedWalletBalance !== undefined) {
isInBounds = balance <= maxUnusedWalletBalance;
} else {
// Wallet does not need to be checked
return;
}
} else {
notificationSet =
type === BalanceType.ChannelLocal
Expand All @@ -131,10 +154,24 @@ class BalanceChecker {

if (!notificationSet.has(ident) && !isInBounds) {
notificationSet.add(ident);
await this.sendAlert(currency, type, service, isInBounds, balance);
await this.sendAlert(
currency,
type,
service,
isInBounds,
isMainWallet,
balance,
);
} else if (notificationSet.has(ident) && isInBounds) {
notificationSet.delete(ident);
await this.sendAlert(currency, type, service, isInBounds, balance);
await this.sendAlert(
currency,
type,
service,
isInBounds,
isMainWallet,
balance,
);
}
};

Expand All @@ -143,6 +180,7 @@ class BalanceChecker {
type: BalanceType,
service: string,
isInBounds: boolean,
isMainWallet: boolean,
balance: number,
) => {
const name = `${currency.symbol} ${service}`;
Expand Down Expand Up @@ -170,15 +208,17 @@ class BalanceChecker {
}
} else {
if (type === BalanceType.Wallet) {
const limits = isMainWallet
? `${
currency.maxWalletBalance
? ` Max: ${satoshisToSatcomma(currency.maxWalletBalance)}\n`
: ''
}` + ` Min: ${satoshisToSatcomma(currency.minWalletBalance)}`
: ` Max: ${satoshisToSatcomma(currency.maxUnusedWalletBalance!)}`;
message =
`${Emojis.RotatingLight} **${name} wallet balance is out of bounds** ${Emojis.RotatingLight}\n` +
` Balance: ${satoshisToSatcomma(balance)}\n` +
`${
currency.maxWalletBalance
? ` Max: ${satoshisToSatcomma(currency.maxWalletBalance)}\n`
: ''
}` +
` Min: ${satoshisToSatcomma(currency.minWalletBalance)}`;
limits;
} else {
message =
`${Emojis.RotatingLight} **${name} ${
Expand Down
82 changes: 37 additions & 45 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"dependencies": {
"@boltz/bolt11": "^1.2.7",
"@google-cloud/storage": "^7.2.0",
"@google-cloud/storage": "^7.3.0",
"@grpc/grpc-js": "^1.9.5",
"@iarna/toml": "^2.2.5",
"@vulpemventures/secp256k1-zkp": "^3.1.0",
Expand All @@ -77,7 +77,7 @@
"cross-os": "^1.5.0",
"discord.js": "^14.13.0",
"ecpair": "^2.1.0",
"ethers": "^6.7.1",
"ethers": "^6.8.0",
"express": "^4.18.2",
"google-protobuf": "^3.21.2",
"liquidjs-lib": "^6.0.2-liquid.30",
Expand All @@ -98,7 +98,7 @@
"@openzeppelin/contracts": "^4.9.3",
"@types/async-lock": "^1.4.0",
"@types/cors": "^2.8.14",
"@types/express": "^4.17.18",
"@types/express": "^4.17.19",
"@types/jest": "^29.5.5",
"@types/node-forge": "^1.3.7",
"@types/node-schedule": "^2.1.1",
Expand Down
Loading

0 comments on commit 11bfb6a

Please sign in to comment.