1
1
import 'dart:async' ;
2
+ import 'dart:collection' ;
2
3
import 'dart:convert' ;
3
4
import 'package:clightning_rpc/clightning_rpc.dart' ;
4
5
import 'package:cln_common/cln_common.dart' ;
@@ -16,20 +17,25 @@ class KrakenPlugin extends Plugin {
16
17
Plugin plugin, Map <String , Object > request) async {
17
18
log (level: "info" , message: "This is the kraken doctor output." );
18
19
var params = DoctorRequest .fromJson (request);
20
+ Map <String , dynamic > response = {};
19
21
try {
20
22
// TODO make sure that this will trow an exception
21
- Map <String , dynamic > listPays = await rpc!
22
- .call (method: "paystatus" , params: params.toPaysStatusRequest ());
23
+ Map <String , dynamic >? listPays;
24
+ if (request.containsKey ("bolt11" )) {
25
+ listPays = await rpc!
26
+ .call (method: "paystatus" , params: params.toPaysStatusRequest ());
27
+ response["listpays" ] = listPays;
28
+ }
23
29
Map <String , dynamic > listFunds = await rpc!
24
30
.call (method: "listfunds" , params: params.toListFundsRequest ());
25
31
26
- /// Make a call to listfunds like the prev one, and take only the channels
27
- /// The raw reason is the error message that core lightning return
28
- return Future .value ({
29
- "listpays" : listPays,
32
+ response.addAll ({
30
33
"channels" : listFunds["channels" ],
31
- "raw_reason" : request["raw_reason" ] ?? "unknown"
34
+ "raw_reason" : request["raw_reason" ] ?? "unknown" ,
35
+ "raw_error_code" : request["raw_error_code" ] ?? "unknown" ,
36
+ "raw_data" : request["raw_data" ] ?? "unknown"
32
37
});
38
+ return Future .value (response.cast <String , Object >());
33
39
} on LNClientException catch (ex, stacktrace) {
34
40
plugin.log (level: "broken" , message: "error received: ${ex .message }" );
35
41
plugin.log (level: "broken" , message: stacktrace.toString ());
@@ -39,9 +45,13 @@ class KrakenPlugin extends Plugin {
39
45
40
46
Future <String > handleBolt12 (Plugin plugin,
41
47
{required FetchInvoiceRequest offer}) async {
42
- var fetchInvoice = await rpc!
43
- .call <FetchInvoiceRequest , FetchInvoiceResponse >(
44
- method: "fetchinvoice" , params: offer);
48
+ var fetchInvoice = await rpc! .call< FetchInvoiceRequest ,
49
+ FetchInvoiceResponse > (
50
+ method: "fetchinvoice" ,
51
+ params: offer,
52
+ onDecode: (json) =>
53
+ FetchInvoiceResponse .fromJson (json as HashMap <String , dynamic >));
54
+ log (level: "info" , message: "fetch invoice: ${fetchInvoice .toJSON ()}" );
45
55
return fetchInvoice.invoice;
46
56
}
47
57
@@ -60,16 +70,22 @@ class KrakenPlugin extends Plugin {
60
70
String invoice = request["invoice" ]! as String ;
61
71
// The human-readable prefix for BOLT 12 offers is lno.
62
72
if (invoice.startsWith ("lno" )) {
73
+ request["bolt11" ] = invoice;
63
74
// Modify request with the correct bolt 12!
64
75
// FIXME: we should remove the msatoshi amount from the request? or the pay command
65
76
// us smart enough to handle it?
77
+ var amountMsat = request["amount_msat" ] == null
78
+ ? null
79
+ : int .parse (request["amount_msat" ].toString ());
80
+
66
81
request["bolt11" ] = await handleBolt12 (plugin,
67
82
offer: FetchInvoiceRequest (
68
- offer: invoice, msamsatoshi: request["msatoshi" ] as String ? ));
83
+ offer: invoice,
84
+ msatoshi: request["msatoshi" ]? .toString (),
85
+ amountMsat: amountMsat));
69
86
} else {
70
87
request["bolt11" ] = invoice;
71
88
}
72
- // TODO: rpc should return an exception if any error occurs, and if we have the error returned run the doctor command
73
89
PayResponse result = await rpc! .call <PayRequest , PayResponse >(
74
90
method: "pay" ,
75
91
params: PayRequest .fromJson (request),
@@ -78,7 +94,14 @@ class KrakenPlugin extends Plugin {
78
94
return result.toJSON () as Map <String , Object >;
79
95
} on LNClientException catch (ex, stacktrace) {
80
96
request["raw_reason" ] = ex.message;
81
- plugin.log (level: "broken" , message: "error received: ${ex .message }" );
97
+ request["raw_error_code" ] = ex.code;
98
+ if (ex.data != null ) {
99
+ request["raw_data" ] = ex.data! ;
100
+ }
101
+ plugin.log (
102
+ level: "broken" ,
103
+ message:
104
+ "error received: msg=${ex .message } code: ${ex .code } data: ${ex .data }" );
82
105
plugin.log (level: "broken" , message: stacktrace.toString ());
83
106
return krakenDoctor (plugin, request);
84
107
} catch (ex, stacktrace) {
@@ -112,7 +135,7 @@ class KrakenPlugin extends Plugin {
112
135
var rpcName = configuration["rpc-file" ]! .toString ();
113
136
rpc = RPCClient ().connect ("$rpcPath /$rpcName " ) as RPCClient ;
114
137
log (level: "info" , message: "kraken payment configured" );
115
-
138
+ log (level : "info" , message : "Configuration payload $ configuration " );
116
139
return response;
117
140
}
118
141
}
0 commit comments