Skip to content

Commit

Permalink
feat: automatically refund failed swaps
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Feb 2, 2019
1 parent 69c5d99 commit 7eda150
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 138 deletions.
6 changes: 4 additions & 2 deletions lib/chain/ChainClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ class ChainClient extends BaseClient implements ChainClientInterface {
switch (data.method) {
// Emits an event on mempool acceptance
case 'relevanttxaccepted':
data.params.forEach((transaction) => {
data.params.forEach((transaction: string) => {
this.emit('transaction.relevant.mempool', transaction);
});
break;

// Emits an event on block acceptance
// Emits an event when a blocks gets added
case 'filteredblockconnected':
const params: any[] = data.params;

this.emit('block.connected', params[0]);

if (params[2] !== null) {
const transactions = params[2] as string[];

Expand Down
9 changes: 7 additions & 2 deletions lib/chain/ChainClientInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ interface ChainClientInterface {
generate(blocks: number): Promise<string[]>;

on(event: 'error', listener: (error: string) => void): this;
on(event: 'transaction.relevant.mempool', listener: (transactionHex: string) => void): this;
on(event: 'transaction.relevant.block', listener: (transactionhex: string, blockHeigh: number) => void): this;
emit(event: 'error', error: string): boolean;

on(event: 'block.connected', listener: (height: number) => void): this;
emit(event: 'block.connected', height: number): boolean;

on(event: 'transaction.relevant.mempool', listener: (transactionHex: string) => void): this;
emit(event: 'transaction.relevant.mempool', transactionHex: string): boolean;

on(event: 'transaction.relevant.block', listener: (transactionhex: string, blockHeigh: number) => void): this;
emit(event: 'transaction.relevant.block', transactionhex: string, blockHeigh: number): boolean;
}

Expand Down
1 change: 1 addition & 0 deletions lib/cli/BuilderComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default {
},
timeoutBlockNumber: {
describe: 'after how my blocks the onchain script of the swap should time out',
default: '10',
type: 'number',
},
};
42 changes: 27 additions & 15 deletions lib/lightning/LndClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ import { ClientStatus } from '../consts/Enums';
import LightningClient from './LightningClient';
import { LightningClient as GrpcClient } from '../proto/lndrpc_grpc_pb';

/** The configurable options for the lnd client. */
/**
* The configurable options for the lnd client
*/
type LndConfig = {
host: string;
port: number;
certpath: string;
macaroonpath: string;
};

/** General information about the state of this lnd client. */
/**
* General information about the state of this lnd client
*/
type Info = {
version?: string;
syncedtochain?: boolean;
Expand All @@ -40,6 +44,7 @@ interface GrpcResponse {
interface LndClient {
on(event: 'invoice.paid', listener: (invoice: string) => void): this;
emit(event: 'invoice.paid', invoice: string): boolean;

on(event: 'invoice.failed', listener: (invoice: string) => void): this;
emit(event: 'invoice.failed', invoice: string): boolean;

Expand All @@ -51,7 +56,9 @@ interface LightningMethodIndex extends GrpcClient {
[methodName: string]: Function;
}

/** A class representing a client to interact with LND */
/**
* A class representing a client to interact with LND
*/
class LndClient extends BaseClient implements LightningClient {
public static readonly serviceName = 'LND';
private uri!: string;
Expand Down Expand Up @@ -132,7 +139,9 @@ class LndClient extends BaseClient implements LightningClient {
return true;
}

/** End all subscriptions and reconnection attempts. */
/**
* End all subscriptions and reconnection attempts
*/
public disconnect = () => {
this.clearReconnectTimer();

Expand Down Expand Up @@ -160,17 +169,21 @@ class LndClient extends BaseClient implements LightningClient {
let uris: string[] | undefined;
let version: string | undefined;
let syncedtochain: boolean | undefined;

try {
const lnd = await this.getInfo();

channels = {
active: lnd.numActiveChannels,
pending: lnd.numPendingChannels,
};

chainsList = lnd.chainsList,
blockheight = lnd.blockHeight,
uris = lnd.urisList,
version = lnd.version;
syncedtochain = lnd.syncedToChain;

return {
version,
syncedtochain,
Expand All @@ -181,6 +194,7 @@ class LndClient extends BaseClient implements LightningClient {
};
} catch (err) {
this.logger.error(`LND error: ${err}`);

return {
version,
syncedtochain,
Expand Down Expand Up @@ -220,18 +234,16 @@ class LndClient extends BaseClient implements LightningClient {
public payInvoice = async (invoice: string) => {
const request = new lndrpc.SendRequest();
request.setPaymentRequest(invoice);
try {
const response = await this.unaryCall<lndrpc.SendRequest, lndrpc.SendResponse.AsObject>('sendPaymentSync', request);
if (response.paymentError === '') {
this.emit('invoice.paid', invoice);
} else {
this.emit('invoice.failed', invoice);
}
return response;
} catch (error) {
this.logger.warn(`Failed to pay invoice ${invoice}: ${error}`);
return error;

const response = await this.unaryCall<lndrpc.SendRequest, lndrpc.SendResponse.AsObject>('sendPaymentSync', request);

if (response.paymentError === '') {
this.emit('invoice.paid', invoice);
} else {
this.emit('invoice.failed', invoice);
}

return response;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion lib/service/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ class Service extends EventEmitter {
* Gets the balance for either all wallets or just a single one if specified
*/
public getBalance = async (args: { currency: string }) => {
argChecks.VALID_CURRENCY(args);
if (args.currency !== '') {
argChecks.VALID_CURRENCY(args);
}

const { walletManager } = this.serviceComponents;

Expand Down
2 changes: 1 addition & 1 deletion lib/swap/Errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Error } from '../consts/Types';
import { ErrorCodePrefix } from '../consts/Enums';
import { concatErrorCode } from '../Utils';
import { ErrorCodePrefix } from '../consts/Enums';

export default {
CURRENCY_NOT_FOUND: (currency: string): Error => ({
Expand Down
Loading

0 comments on commit 7eda150

Please sign in to comment.