@@ -32,53 +32,18 @@ import {
3232 BoltzInfoType ,
3333 BoltzSwap ,
3434 CreateBoltzReverseSwapType ,
35+ isBoltzError ,
3536} from './boltz.types' ;
3637import { getPreimageAndHash } from 'src/server/utils/crypto' ;
3738import { CurrentUser } from '../../security/security.decorators' ;
3839import { UserId } from '../../security/security.types' ;
3940import { toWithError } from 'src/server/utils/async' ;
4041import { ECPairAPI , ECPairFactory } from 'ecpair' ;
4142import * as ecc from 'tiny-secp256k1' ;
43+ import { mapSeries } from 'async' ;
4244
4345const ECPair : ECPairAPI = ECPairFactory ( ecc ) ;
4446
45- @Resolver ( BoltzSwap )
46- export class BoltzSwapResolver {
47- constructor (
48- private boltzService : BoltzService ,
49- @Inject ( WINSTON_MODULE_PROVIDER ) private readonly logger : Logger
50- ) { }
51-
52- @ResolveField ( )
53- async id ( @Parent ( ) parent : string ) {
54- return parent ;
55- }
56-
57- @ResolveField ( )
58- async boltz ( @Parent ( ) parent : string ) {
59- const [ info , error ] = await toWithError (
60- this . boltzService . getSwapStatus ( parent )
61- ) ;
62-
63- if ( error || info ?. error ) {
64- this . logger . error ( `Error getting status for swap with id: ${ parent } ` , {
65- error : error || info . error ,
66- } ) ;
67- return null ;
68- }
69-
70- if ( ! info ?. status ) {
71- this . logger . debug (
72- `No status in Boltz response for swap with id: ${ parent } ` ,
73- { info }
74- ) ;
75- return null ;
76- }
77-
78- return info ;
79- }
80- }
81-
8247@Resolver ( CreateBoltzReverseSwapType )
8348export class CreateBoltzReverseSwapTypeResolver {
8449 constructor ( private nodeService : NodeService ) { }
@@ -110,13 +75,14 @@ export class BoltzResolver {
11075
11176 @Query ( ( ) => BoltzInfoType )
11277 async getBoltzInfo ( ) {
113- const info = await this . boltzService . getPairs ( ) ;
78+ const [ info , error ] = await toWithError ( this . boltzService . getPairs ( ) ) ;
11479
115- if ( info ?. error ) {
80+ if ( error || isBoltzError ( info ) ) {
11681 this . logger . error ( 'Error getting swap information from Boltz' , {
117- error : info . error ,
82+ error,
83+ boltzError : info ,
11884 } ) ;
119- throw new Error ( info . error ) ;
85+ throw new GraphQLError ( 'Error getting swap informaion from Boltz' ) ;
12086 }
12187
12288 const btcPair = info ?. BTC ?. BTC ;
@@ -137,7 +103,29 @@ export class BoltzResolver {
137103 async getBoltzSwapStatus (
138104 @Args ( 'ids' , { type : ( ) => [ String ] } ) ids : string [ ]
139105 ) {
140- return ids ;
106+ return mapSeries ( ids , async ( id : string ) => {
107+ const [ info , error ] = await toWithError (
108+ this . boltzService . getSwapStatus ( id )
109+ ) ;
110+
111+ if ( error || isBoltzError ( info ) ) {
112+ this . logger . error ( `Error getting status for swap with id: ${ id } ` , {
113+ error,
114+ boltzError : info ,
115+ } ) ;
116+ return { id } ;
117+ }
118+
119+ if ( ! info . status ) {
120+ this . logger . debug (
121+ `No status in Boltz response for swap with id: ${ id } ` ,
122+ { info }
123+ ) ;
124+ return { id } ;
125+ }
126+
127+ return { id, boltz : info } ;
128+ } ) ;
141129 }
142130
143131 @Mutation ( ( ) => String )
@@ -289,15 +277,16 @@ export class BoltzResolver {
289277 publicKey,
290278 } ) ;
291279
292- const info = await this . boltzService . createReverseSwap (
293- amount ,
294- hash ,
295- publicKey
280+ const [ info , error ] = await toWithError (
281+ this . boltzService . createReverseSwap ( amount , hash , publicKey )
296282 ) ;
297283
298- if ( info ?. error ) {
299- this . logger . error ( 'Error creating reverse swap with Boltz' , info . error ) ;
300- throw new Error ( info . error ) ;
284+ if ( error || isBoltzError ( info ) ) {
285+ this . logger . error ( 'Error creating reverse swap with Boltz' , {
286+ error,
287+ boltzError : info ,
288+ } ) ;
289+ throw new GraphQLError ( 'Error creating reverse swap with Boltz' ) ;
301290 }
302291
303292 const finalInfo = {
@@ -327,17 +316,23 @@ export class BoltzResolver {
327316 private broadcastTransaction = async ( finalTransaction : Transaction ) => {
328317 this . logger . debug ( 'Final transaction' , { finalTransaction } ) ;
329318
330- const response = await this . boltzService . broadcastTransaction (
331- finalTransaction . toHex ( )
319+ const [ info , error ] = await toWithError (
320+ this . boltzService . broadcastTransaction ( finalTransaction . toHex ( ) )
332321 ) ;
333322
334- this . logger . debug ( 'Response from Boltz' , { response } ) ;
323+ if ( error || isBoltzError ( info ) ) {
324+ this . logger . error ( 'Error broadcasting transaction through Boltz' , {
325+ error,
326+ boltzError : info ,
327+ } ) ;
328+ throw new GraphQLError ( 'Error broadcasting transaction through Boltz' ) ;
329+ }
335330
336- if ( ! response ? .id ) {
331+ if ( ! info . id ) {
337332 this . logger . error ( 'Did not receive a transaction id from Boltz' ) ;
338333 throw new Error ( 'NoTransactionIdFromBoltz' ) ;
339334 }
340335
341- return response . id ;
336+ return info . id ;
342337 } ;
343338}
0 commit comments