Skip to content

Commit

Permalink
feat: abort swaps after expiration (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored May 25, 2019
1 parent 321b3a9 commit 5879a3b
Show file tree
Hide file tree
Showing 12 changed files with 901 additions and 335 deletions.
5 changes: 2 additions & 3 deletions lib/grpc/GrpcServer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import assert from 'assert';
import { pki, md } from 'node-forge';
import grpc, { Server } from 'grpc';
import { pki, md } from 'node-forge';
import Errors from './Errors';
import Logger from '../Logger';
import GrpcService from './GrpcService';
Expand All @@ -28,10 +28,9 @@ class GrpcServer {
getFeeEstimation: grpcService.getFeeEstimation,
broadcastTransaction: grpcService.broadcastTransaction,
listenOnAddress: grpcService.listenOnAddress,
subscribeSwapEvents: grpcService.subscribeSwapEvents,
subscribeTransactions: grpcService.subscribeTransactions,
subscribeInvoices: grpcService.subscribeInvoices,
subscribeClaims: grpcService.subscribeClaims,
subscribeRefunds: grpcService.subscribeRefunds,
subscribeChannelBackups: grpcService.subscribeChannelBackups,
createSwap: grpcService.createSwap,
createReverseSwap: grpcService.createReverseSwap,
Expand Down
62 changes: 40 additions & 22 deletions lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import Service from '../service/Service';
import * as boltzrpc from '../proto/boltzrpc_pb';

class GrpcService {
private swapEventSubscriptions: ServerWriteableStream<boltzrpc.SubscribeSwapEventsRequest>[] = [];
private transactionSubscriptions: ServerWriteableStream<boltzrpc.SubscribeTransactionsRequest>[] = [];
private invoiceSubscriptions: ServerWriteableStream<boltzrpc.SubscribeInvoicesRequest>[] = [];
private claimSubscriptions: ServerWriteableStream<boltzrpc.SubscribeClaimsRequest>[] = [];
private refundSubscriptions: ServerWriteableStream<boltzrpc.SubscribeRefundsRequest>[] = [];
private channelBackupSubscriptions: ServerWriteableStream<boltzrpc.SubscribeChannelBackupsRequest>[] = [];

constructor(private service: Service) {
Expand Down Expand Up @@ -90,6 +89,10 @@ class GrpcService {
}
}

public subscribeSwapEvents: handleServerStreamingCall<boltzrpc.SubscribeSwapEventsRequest, boltzrpc.SubscribeSwapEventsResponse> = async (call) => {
this.registerSubscription(call, this.swapEventSubscriptions);
}

public subscribeTransactions: handleServerStreamingCall<boltzrpc.SubscribeTransactionsRequest, boltzrpc.SubscribeTransactionsResponse> = async (call) => {
this.registerSubscription(call, this.transactionSubscriptions);
}
Expand All @@ -98,14 +101,6 @@ class GrpcService {
this.registerSubscription(call, this.invoiceSubscriptions);
}

public subscribeClaims: handleServerStreamingCall<boltzrpc.SubscribeClaimsRequest, boltzrpc.SubscribeClaimsResponse> = async (call) => {
this.registerSubscription(call, this.claimSubscriptions);
}

public subscribeRefunds: handleServerStreamingCall<boltzrpc.SubscribeRefundsRequest, boltzrpc.SubscribeRefundsResponse> = async (call) => {
this.registerSubscription(call, this.refundSubscriptions);
}

public subscribeChannelBackups: handleServerStreamingCall<boltzrpc.SubscribeChannelBackupsRequest, boltzrpc.ChannelBackup> = async (call) => {
this.registerSubscription(call, this.channelBackupSubscriptions);
}
Expand Down Expand Up @@ -213,25 +208,48 @@ class GrpcService {
});
});

// Claim subscription
// Swap event subscriptions
this.service.on('claim', (lockupTransactionHash: string, lockupVout: number, minerFee: number) => {
this.claimSubscriptions.forEach((subscription) => {
const response = new boltzrpc.SubscribeClaimsResponse();
response.setLockupTransactionHash(lockupTransactionHash);
response.setLockupVout(lockupVout);
response.setMinerFee(minerFee);
this.swapEventSubscriptions.forEach((subscription) => {
const response = new boltzrpc.SubscribeSwapEventsResponse();
const claimDetails = new boltzrpc.ClaimDetails();

claimDetails.setLockupTransactionHash(lockupTransactionHash);
claimDetails.setLockupVout(lockupVout);
claimDetails.setMinerFee(minerFee);

response.setEvent(boltzrpc.SwapEvent.CLAIM);
response.setClaimDetails(claimDetails);

subscription.write(response);
});
});

this.service.on('abort', (invoice: string) => {
this.swapEventSubscriptions.forEach((subscription) => {
const response = new boltzrpc.SubscribeSwapEventsResponse();
const abortDetails = new boltzrpc.AbortDetails();

abortDetails.setInvoice(invoice);

response.setEvent(boltzrpc.SwapEvent.ABORT);
response.setAbortDetails(abortDetails);

subscription.write(response);
});
});

// Refund subscription
this.service.on('refund', (lockupTransactionHash: string, lockupVout: number, minerFee: number) => {
this.refundSubscriptions.forEach((subscription) => {
const response = new boltzrpc.SubscribeRefundsResponse();
response.setLockupTransactionHash(lockupTransactionHash);
response.setLockupVout(lockupVout);
response.setMinerFee(minerFee);
this.swapEventSubscriptions.forEach((subscription) => {
const response = new boltzrpc.SubscribeSwapEventsResponse();
const refundDetails = new boltzrpc.RefundDetails();

refundDetails.setLockupTransactionHash(lockupTransactionHash);
refundDetails.setLockupVout(lockupVout);
refundDetails.setMinerFee(minerFee);

response.setEvent(boltzrpc.SwapEvent.REFUND);
response.setRefundDetails(refundDetails);

subscription.write(response);
});
Expand Down
45 changes: 15 additions & 30 deletions lib/proto/boltzrpc_grpc_pb.d.ts

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

Loading

0 comments on commit 5879a3b

Please sign in to comment.