Skip to content

Commit

Permalink
feat: use satcomma to format notification amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Oct 10, 2023
1 parent 0c38daf commit 06c9f6d
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 205 deletions.
31 changes: 25 additions & 6 deletions lib/DenominationConverter.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
const decimals = 100_000_000;
const decimals = 8;
const satFactor = 100_000_000;

/**
* Round a number to a specific amount of decimals
*/
const roundToDecimals = (number: number, decimals: number): number => {
return Number(number.toFixed(decimals));
};

/**
* Convert whole coins to satoshis
*/
export const coinsToSatoshis = (coins: number): number => {
return coins * decimals;
return coins * satFactor;
};

/**
* Convert satoshis to whole coins and remove trailing zeros
*/
export const satoshisToCoins = (satoshis: number): number => {
return roundToDecimals(satoshis / decimals, 8);
return roundToDecimals(satoshis / satFactor, decimals);
};

/**
* Round a number to a specific amount of decimals
* Convert satoshis to whole coins with trailing zeros
*/
const roundToDecimals = (number: number, decimals: number): number => {
return Number(number.toFixed(decimals));
export const satoshisToPaddedCoins = (satoshis: number): string => {
return (satoshis / satFactor).toFixed(decimals);
};

export const satoshisToSatcomma = (satoshis: number): string => {
let coins = (satoshis / satFactor).toFixed(decimals);
for (const [num, index] of [3, 6].entries()) {
coins = `${coins.substring(
0,
coins.length - index - num,
)},${coins.substring(coins.length - index - num)}`;
}
return coins;
};
4 changes: 2 additions & 2 deletions lib/data/Stats.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getNestedObject } from './Utils';
import { satoshisToCoins } from '../DenominationConverter';
import { satoshisToPaddedCoins } from '../DenominationConverter';
import StatsRepository, { StatsDate } from '../db/repositories/StatsRepository';

type MonthStats = {
Expand Down Expand Up @@ -32,7 +32,7 @@ class Stats {

volumes.forEach((volume) => {
const obj = getNestedObject(getMonthObj(volume), 'volume');
obj[volume.pair || Stats.totalString] = satoshisToCoins(volume.sum);
obj[volume.pair || Stats.totalString] = satoshisToPaddedCoins(volume.sum);
});

tradeCounts.forEach((counts) => {
Expand Down
18 changes: 9 additions & 9 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 { satoshisToCoins } from '../DenominationConverter';
import { BaseCurrencyConfig, TokenConfig } from '../Config';
import { satoshisToSatcomma } from '../DenominationConverter';

enum BalanceType {
Wallet,
Expand Down Expand Up @@ -152,16 +152,16 @@ class BalanceChecker {
if (type === BalanceType.Wallet) {
message = `${
Emojis.Checkmark
} ${name} wallet balance of ${satoshisToCoins(
} ${name} wallet balance of ${satoshisToSatcomma(
balance,
)} is in bounds again ${Emojis.Checkmark}`;
} else {
message =
`${Emojis.Checkmark} ${name} ${
type === BalanceType.ChannelLocal ? 'local' : 'remote'
} channel balance ` +
`of ${satoshisToCoins(balance)} is more than expected ` +
`${satoshisToCoins(
`of ${satoshisToSatcomma(balance)} is more than expected ` +
`${satoshisToSatcomma(
type === BalanceType.ChannelLocal
? currency.minLocalBalance!
: currency.minRemoteBalance!,
Expand All @@ -171,20 +171,20 @@ class BalanceChecker {
if (type === BalanceType.Wallet) {
message =
`${Emojis.RotatingLight} **${name} wallet balance is out of bounds** ${Emojis.RotatingLight}\n` +
` Balance: ${satoshisToCoins(balance)}\n` +
` Balance: ${satoshisToSatcomma(balance)}\n` +
`${
currency.maxWalletBalance
? ` Max: ${satoshisToCoins(currency.maxWalletBalance)}\n`
? ` Max: ${satoshisToSatcomma(currency.maxWalletBalance)}\n`
: ''
}` +
` Min: ${satoshisToCoins(currency.minWalletBalance)}`;
` Min: ${satoshisToSatcomma(currency.minWalletBalance)}`;
} else {
message =
`${Emojis.RotatingLight} **${name} ${
type === BalanceType.ChannelLocal ? 'local' : 'remote'
} channel balance ` +
`of ${satoshisToCoins(balance)} is less than expected ` +
`${satoshisToCoins(
`of ${satoshisToSatcomma(balance)} is less than expected ` +
`${satoshisToSatcomma(
type === BalanceType.ChannelLocal
? currency.minLocalBalance!
: currency.minRemoteBalance!,
Expand Down
14 changes: 7 additions & 7 deletions lib/notifications/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import BackupScheduler from '../backup/BackupScheduler';
import ChannelCreation from '../db/models/ChannelCreation';
import FeeRepository from '../db/repositories/FeeRepository';
import SwapRepository from '../db/repositories/SwapRepository';
import { coinsToSatoshis, satoshisToCoins } from '../DenominationConverter';
import ReverseSwapRepository from '../db/repositories/ReverseSwapRepository';
import { coinsToSatoshis, satoshisToSatcomma } from '../DenominationConverter';
import ChannelCreationRepository from '../db/repositories/ChannelCreationRepository';
import {
stringify,
Expand Down Expand Up @@ -257,7 +257,7 @@ class CommandHandler {
private getFees = async () => {
let message = 'Fees:\n';
(await FeeRepository.getFees()).forEach(({ asset, sum }) => {
message += `\n**${asset}**: ${satoshisToCoins(sum)} ${asset}`;
message += `\n**${asset}**: ${satoshisToSatcomma(sum)} ${asset}`;
});

await this.discord.sendMessage(message);
Expand Down Expand Up @@ -350,7 +350,7 @@ class CommandHandler {
message += `\n\n**${symbol}**\n`;

bals.walletsMap.forEach(([service, walletBals]) => {
message += `\n${service} Wallet: ${satoshisToCoins(
message += `\n${service} Wallet: ${satoshisToSatcomma(
walletBals.confirmed + walletBals.unconfirmed,
)} ${symbol}`;
});
Expand All @@ -362,8 +362,8 @@ class CommandHandler {
bals.lightningMap.forEach(([service, lightningBals]) => {
message +=
`\n${service}:\n` +
` Local: ${satoshisToCoins(lightningBals.local)} ${symbol}\n` +
` Remote: ${satoshisToCoins(lightningBals.remote)} ${symbol}`;
` Local: ${satoshisToSatcomma(lightningBals.local)} ${symbol}\n` +
` Remote: ${satoshisToSatcomma(lightningBals.remote)} ${symbol}`;
});
});

Expand Down Expand Up @@ -410,12 +410,12 @@ class CommandHandler {

for (const pendingReverseSwap of chainArray) {
symbolTotal += pendingReverseSwap.onchainAmount;
message += `\n - \`${pendingReverseSwap.id}\`: ${satoshisToCoins(
message += `\n - \`${pendingReverseSwap.id}\`: ${satoshisToSatcomma(
pendingReverseSwap.onchainAmount,
)}`;
}

message += `\n\nTotal: ${satoshisToCoins(symbolTotal)}\n`;
message += `\n\nTotal: ${satoshisToSatcomma(symbolTotal)}\n`;
}

await this.discord.sendMessage(message);
Expand Down
36 changes: 8 additions & 28 deletions lib/notifications/NotificationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import DiskUsageChecker from './DiskUsageChecker';
import ReverseSwap from '../db/models/ReverseSwap';
import BackupScheduler from '../backup/BackupScheduler';
import { CurrencyType, OrderSide } from '../consts/Enums';
import { satoshisToCoins } from '../DenominationConverter';
import { satoshisToSatcomma } from '../DenominationConverter';
import { ChainInfo, LightningInfo } from '../proto/boltzrpc_pb';
import { TokenConfig, BaseCurrencyConfig, NotificationConfig } from '../Config';
import {
Expand Down Expand Up @@ -192,12 +192,12 @@ class NotificationProvider {
message +=
`${
swap.onchainAmount
? `\nOnchain amount: ${satoshisToCoins(
? `\nOnchain amount: ${satoshisToSatcomma(
swap.onchainAmount,
)} ${onchainSymbol}`
: ''
}` +
`\nLightning amount: ${satoshisToCoins(
`\nLightning amount: ${satoshisToSatcomma(
lightningAmount,
)} ${lightningSymbol}`;
}
Expand Down Expand Up @@ -250,12 +250,10 @@ class NotificationProvider {
hasChannelCreation ? ' :construction_site:' : ''
}**\n` +
`${getBasicSwapInfo(swap, onchainSymbol, lightningSymbol)}\n` +
`Fees earned: ${this.numberToDecimal(
satoshisToCoins(swap.fee!),
)} ${onchainSymbol}\n` +
`Miner fees: ${satoshisToCoins(swap.minerFee!)} ${getMinerFeeSymbol(
onchainSymbol,
)}`;
`Fees earned: ${satoshisToSatcomma(swap.fee!)} ${onchainSymbol}\n` +
`Miner fees: ${satoshisToSatcomma(
swap.minerFee!,
)} ${getMinerFeeSymbol(onchainSymbol)}`;

if (!isReverse) {
// The routing fees are denominated in millisatoshi
Expand Down Expand Up @@ -300,7 +298,7 @@ class NotificationProvider {

if (isReverse) {
if (swap.minerFee) {
message += `\nMiner fees: ${satoshisToCoins(
message += `\nMiner fees: ${satoshisToSatcomma(
swap.minerFee,
)} ${onchainSymbol}`;
}
Expand Down Expand Up @@ -350,24 +348,6 @@ class NotificationProvider {
return 'satoshi';
}
};

private numberToDecimal = (toFormat: number) => {
// Numbers smaller 1e-6 are formatted in the scientific notation when converted to a string
if (toFormat < 1e-6) {
let format = toFormat.toFixed(8);

// Trim the trailing zeros if they exist
const lastZero = format.lastIndexOf('0');

if (lastZero === format.length - 1) {
format = format.slice(0, format.length - 1);
}

return format;
} else {
return toFormat.toString();
}
};
}

export default NotificationProvider;
Loading

0 comments on commit 06c9f6d

Please sign in to comment.