diff --git a/lib/consts/Enums.ts b/lib/consts/Enums.ts index efacf60..810f1ff 100644 --- a/lib/consts/Enums.ts +++ b/lib/consts/Enums.ts @@ -19,3 +19,7 @@ export enum SwapUpdateEvent { TransactionRefunded = 'transaction.refunded', TransactionConfirmed = 'transaction.confirmed', } + +export enum ServiceWarning { + ReverseSwapsDisabled = 'reverse.swaps.disabled', +} diff --git a/lib/notifications/CommandHandler.ts b/lib/notifications/CommandHandler.ts index 44e42c0..7788e1d 100644 --- a/lib/notifications/CommandHandler.ts +++ b/lib/notifications/CommandHandler.ts @@ -7,11 +7,13 @@ import { SwapInstance, ReverseSwapInstance, Swap } from '../consts/Database'; import { satoshisToCoins, parseBalances, getFeeSymbol, stringify, getSuccessfulTrades } from '../Utils'; enum Command { - GetBalance = 'getbalance', + Help = 'help', + GetFees = 'getfees', SwapInfo = 'swapinfo', + GetBalance = 'getbalance', NewAddress = 'newaddress', - Help = 'help', + ToggleReverseSwaps = 'togglereverse', } type CommandInfo = { @@ -29,11 +31,13 @@ class CommandHandler { private discord: DiscordClient) { this.commands = new Map([ - [Command.GetBalance, { description: 'gets the balance of the wallet and channels', executor: this.getBalance }], + [Command.Help, { description: 'gets a list of all available commands', executor: this.help }], + [Command.GetFees, { description: 'gets the accumulated fees', executor: this.getFees }], - [Command.SwapInfo, { description: 'gets all available information about a (reverse) swap', executor: this.swapInfo }], [Command.NewAddress, { description: 'generates a new address for a currency', executor: this.newAddress }], - [Command.Help, { description: 'gets a list of all available commands', executor: this.help }], + [Command.GetBalance, { description: 'gets the balance of the wallet and channels', executor: this.getBalance }], + [Command.SwapInfo, { description: 'gets all available information about a (reverse) swap', executor: this.swapInfo }], + [Command.ToggleReverseSwaps, { description: 'enables or disables reverse swaps', executor: this.toggleReverseSwaps }], ]); this.discord.on('message', async (message: string) => { @@ -147,6 +151,12 @@ class CommandHandler { } } + private toggleReverseSwaps = async () => { + this.service.allowReverseSwaps = !this.service.allowReverseSwaps; + + await this.discord.sendMessage(`${this.service.allowReverseSwaps ? 'Enabled' : 'Disabled'} reverse swaps`); + } + private help = async () => { let message = 'Commands:\n'; diff --git a/lib/service/Errors.ts b/lib/service/Errors.ts index ac1fb77..4089bc1 100644 --- a/lib/service/Errors.ts +++ b/lib/service/Errors.ts @@ -27,4 +27,8 @@ export default { message: 'a swap with this invoice exists already', code: concatErrorCode(ErrorCodePrefix.Service, 5), }), + REVERSE_SWAPS_DISABLED: (): Error => ({ + message: 'reverse swaps are disabled', + code: concatErrorCode(ErrorCodePrefix.Service, 6), + }), }; diff --git a/lib/service/Service.ts b/lib/service/Service.ts index 0918e8d..4de4305 100644 --- a/lib/service/Service.ts +++ b/lib/service/Service.ts @@ -8,13 +8,13 @@ import PairRepository from './PairRepository'; import BoltzClient from '../boltz/BoltzClient'; import FeeProvider from '../rates/FeeProvider'; import RateProvider from '../rates/RateProvider'; -import { SwapUpdateEvent } from '../consts/Enums'; +import { SwapUpdateEvent, ServiceWarning } from '../consts/Enums'; import { encodeBip21 } from './PaymentRequestUtils'; import ReverseSwapRepository from './ReverseSwapRepository'; import { SwapUpdate, CurrencyConfig, PairConfig } from '../consts/Types'; import { OrderSide, OutputType, CurrencyInfo } from '../proto/boltzrpc_pb'; -import { PairInstance, PairFactory, SwapInstance, ReverseSwapInstance } from '../consts/Database'; import { splitPairId, stringify, generateId, mapToObject, feeMapToObject } from '../Utils'; +import { PairInstance, PairFactory, SwapInstance, ReverseSwapInstance } from '../consts/Database'; type Pair = { id: string; @@ -31,6 +31,8 @@ interface Service { } class Service extends EventEmitter { + public allowReverseSwaps = true; + public swapRepository: SwapRepository; public reverseSwapRepository: ReverseSwapRepository; @@ -154,7 +156,16 @@ class Service extends EventEmitter { * Gets all supported pairs and their conversion rates */ public getPairs = () => { - return mapToObject(this.rateProvider.pairs); + const warnings: ServiceWarning[] = []; + + if (!this.allowReverseSwaps) { + warnings.push(ServiceWarning.ReverseSwapsDisabled); + } + + return { + warnings, + pairs: mapToObject(this.rateProvider.pairs), + }; } /** @@ -240,6 +251,10 @@ class Service extends EventEmitter { * Creates a new reverse Swap from Lightning to the chain */ public createReverseSwap = async (pairId: string, orderSide: string, claimPublicKey: string, amount: number) => { + if (!this.allowReverseSwaps) { + throw Errors.REVERSE_SWAPS_DISABLED(); + } + const { base, quote, rate } = this.getPair(pairId); const side = this.getOrderSide(orderSide);