Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

Commit

Permalink
feat: toggle availability of reverse swaps (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Apr 15, 2019
1 parent 8ebf2d3 commit 549a524
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
4 changes: 4 additions & 0 deletions lib/consts/Enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ export enum SwapUpdateEvent {
TransactionRefunded = 'transaction.refunded',
TransactionConfirmed = 'transaction.confirmed',
}

export enum ServiceWarning {
ReverseSwapsDisabled = 'reverse.swaps.disabled',
}
20 changes: 15 additions & 5 deletions lib/notifications/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -29,11 +31,13 @@ class CommandHandler {
private discord: DiscordClient) {

this.commands = new Map<string, CommandInfo>([
[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) => {
Expand Down Expand Up @@ -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';

Expand Down
4 changes: 4 additions & 0 deletions lib/service/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}),
};
21 changes: 18 additions & 3 deletions lib/service/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +31,8 @@ interface Service {
}

class Service extends EventEmitter {
public allowReverseSwaps = true;

public swapRepository: SwapRepository;
public reverseSwapRepository: ReverseSwapRepository;

Expand Down Expand Up @@ -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),
};
}

/**
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 549a524

Please sign in to comment.