@@ -7,58 +7,12 @@ import * as lndrpc from '../proto/lndrpc_pb';
7
7
import LndClient from '../lndclient/LndClient' ;
8
8
import Pool from '../p2p/Pool' ;
9
9
import { EventEmitter } from 'events' ;
10
+ import SwapRepository from './SwapRepository' ;
10
11
import { StampedOwnOrder , StampedPeerOrder , SwapResult } from '../types/orders' ;
11
12
import assert from 'assert' ;
12
-
13
- type SwapDeal = {
14
- /** The role of the local node in the swap. */
15
- role : SwapRole ;
16
- /** The most updated deal phase */
17
- phase : SwapPhase ;
18
- /**
19
- * The most updated deal state. State works together with phase to indicate where the
20
- * deal is in its life cycle and if the deal is active, errored, or completed.
21
- */
22
- state : SwapState ;
23
- /** The reason for being in the current state. */
24
- errorReason ?: string ;
25
- /** The xud node pub key of the counterparty to this swap deal. */
26
- peerPubKey : string ;
27
- /** The global order id in the XU network for the order being executed. */
28
- orderId : string ;
29
- /** The local id for the order being executed. */
30
- localOrderId : string ;
31
- /** The quantity of the order to execute as proposed by the taker. */
32
- proposedQuantity : number ;
33
- /** The accepted quantity of the order to execute as accepted by the maker. */
34
- quantity ?: number ;
35
- /** The trading pair of the order. The pairId together with the orderId are needed to find the deal in orderBook. */
36
- pairId : string ;
37
- /** The number of satoshis (or equivalent) the taker is expecting to receive. */
38
- takerAmount : number ;
39
- /** The currency the taker is expecting to receive. */
40
- takerCurrency : string ;
41
- /** Taker's lnd pubkey on the taker currency's network. */
42
- takerPubKey ?: string ;
43
- /** The CLTV delta from the current height that should be used to set the timelock for the final hop when sending to taker. */
44
- takerCltvDelta : number ;
45
- /** The number of satoshis (or equivalent) the maker is expecting to receive. */
46
- makerAmount : number ;
47
- /** The currency the maker is expecting to receive. */
48
- makerCurrency : string ;
49
- /** The CLTV delta from the current height that should be used to set the timelock for the final hop when sending to maker. */
50
- makerCltvDelta ?: number ;
51
- /** The price of the order that's being executed. */
52
- price : number ;
53
- /** The hash of the preimage. */
54
- r_hash : string ;
55
- r_preimage ?: string ;
56
- /** The routes the maker should use to send to the taker. */
57
- makerToTakerRoutes ?: lndrpc . Route [ ] ;
58
- createTime : number ;
59
- executeTime ?: number ;
60
- completeTime ?: number
61
- } ;
13
+ import { Models } from '../db/DB' ;
14
+ import { SwapDealInstance } from 'lib/types/db' ;
15
+ import { SwapDeal } from './types' ;
62
16
63
17
type OrderToAccept = {
64
18
quantityToAccept : number ;
@@ -76,13 +30,15 @@ interface Swaps {
76
30
class Swaps extends EventEmitter {
77
31
/** A map between r_hash and swap deals. */
78
32
private deals = new Map < string , SwapDeal > ( ) ;
33
+ private usedHashes = new Set < string > ( ) ;
34
+ private repository : SwapRepository ;
79
35
80
36
/** The number of satoshis in a bitcoin. */
81
37
private static readonly SATOSHIS_PER_COIN = 100000000 ;
82
38
83
- constructor ( private logger : Logger , private pool : Pool , private lndBtcClient : LndClient , private lndLtcClient : LndClient ) {
39
+ constructor ( private logger : Logger , private models : Models , private pool : Pool , private lndBtcClient : LndClient , private lndLtcClient : LndClient ) {
84
40
super ( ) ;
85
-
41
+ this . repository = new SwapRepository ( this . models ) ;
86
42
this . bind ( ) ;
87
43
}
88
44
@@ -99,6 +55,14 @@ class Swaps extends EventEmitter {
99
55
return { baseCurrencyAmount, quoteCurrencyAmount } ;
100
56
}
101
57
58
+ public init = async ( ) => {
59
+ // Load Swaps from data base
60
+ const result = await this . repository . getSwapDeals ( ) ;
61
+ result . forEach ( ( deal : SwapDealInstance ) => {
62
+ this . usedHashes . add ( deal . r_hash ) ;
63
+ } ) ;
64
+ }
65
+
102
66
private bind ( ) {
103
67
this . pool . on ( 'packet.swapResponse' , this . handleSwapResponse ) ;
104
68
this . pool . on ( 'packet.swapComplete' , this . handleSwapComplete ) ;
@@ -153,6 +117,17 @@ class Swaps extends EventEmitter {
153
117
return ;
154
118
}
155
119
120
+ /**
121
+ * Saves deal to database and deletes from memory.
122
+ * @param deal The deal to persist.
123
+ */
124
+ private persistDeal = async ( deal : SwapDeal ) => {
125
+ if ( this . usedHashes . has ( deal . r_hash ) ) {
126
+ await this . repository . addSwapDeal ( deal ) ;
127
+ this . removeDeal ( deal ) ;
128
+ }
129
+ }
130
+
156
131
/**
157
132
* Gets a deal by its r_hash value.
158
133
* @param r_hash The r_hash value of the deal to get.
@@ -284,7 +259,7 @@ class Swaps extends EventEmitter {
284
259
const deal : SwapDeal = {
285
260
...swapRequestBody ,
286
261
peerPubKey : peer . nodePubKey ! ,
287
- localOrderId : taker . localId ,
262
+ localId : taker . localId ,
288
263
price : maker . price ,
289
264
phase : SwapPhase . SwapCreated ,
290
265
state : SwapState . Active ,
@@ -328,7 +303,7 @@ class Swaps extends EventEmitter {
328
303
takerPubKey,
329
304
peerPubKey : peer . nodePubKey ! ,
330
305
price : orderToAccept . price ,
331
- localOrderId : orderToAccept . localId ,
306
+ localId : orderToAccept . localId ,
332
307
quantity : orderToAccept . quantityToAccept ,
333
308
phase : SwapPhase . SwapCreated ,
334
309
state : SwapState . Active ,
@@ -581,7 +556,7 @@ class Swaps extends EventEmitter {
581
556
}
582
557
583
558
const request = new lndrpc . SendToRouteRequest ( ) ;
584
- request . setRoutesList ( deal . makerToTakerRoutes ! ) ;
559
+ request . setRoutesList ( deal . makerToTakerRoutes ? deal . makerToTakerRoutes : [ ] ) ;
585
560
request . setPaymentHashString ( deal . r_hash ) ;
586
561
587
562
try {
@@ -669,7 +644,7 @@ class Swaps extends EventEmitter {
669
644
if ( deal . phase === SwapPhase . AmountReceived ) {
670
645
const swapResult = {
671
646
orderId : deal . orderId ,
672
- localId : deal . localOrderId ,
647
+ localId : deal . localId ,
673
648
pairId : deal . pairId ,
674
649
quantity : deal . quantity ! ,
675
650
amountReceived : deal . role === SwapRole . Maker ? deal . makerAmount : deal . takerAmount ,
@@ -682,27 +657,28 @@ class Swaps extends EventEmitter {
682
657
}
683
658
}
684
659
685
- private handleSwapComplete = ( response : packets . SwapCompletePacket ) : void => {
660
+ private handleSwapComplete = ( response : packets . SwapCompletePacket ) => {
686
661
const { r_hash } = response . body ! ;
687
662
const deal = this . getDeal ( r_hash ) ;
688
663
if ( ! deal ) {
689
664
this . logger . error ( `received swap complete for unknown deal r_hash ${ r_hash } ` ) ;
690
665
return ;
691
666
}
692
667
this . setDealPhase ( deal , SwapPhase . SwapCompleted ) ;
668
+ return this . persistDeal ( deal ) ;
693
669
}
694
670
695
- private handleSwapError = ( error : packets . SwapErrorPacket ) : void => {
671
+ private handleSwapError = ( error : packets . SwapErrorPacket ) => {
696
672
const { r_hash, errorMessage } = error . body ! ;
697
673
const deal = this . getDeal ( r_hash ) ;
698
674
if ( ! deal ) {
699
675
this . logger . error ( `received swap error for unknown deal r_hash ${ r_hash } ` ) ;
700
676
return ;
701
677
}
702
678
this . setDealState ( deal , SwapState . Error , errorMessage ) ;
679
+ return this . persistDeal ( deal ) ;
703
680
}
704
681
705
682
}
706
683
707
684
export default Swaps ;
708
- export { SwapDeal } ;
0 commit comments