@@ -65,6 +65,68 @@ pub async fn trampolinepay(
6565) -> Result < cln_rpc:: model:: responses:: PayResponse > {
6666 let node_id = cln_rpc:: primitives:: PublicKey :: from_slice ( & req. trampoline_node_id [ ..] ) ?;
6767
68+ let mut rpc = ClnRpc :: new ( & rpc_path) . await ?;
69+
70+ // Extract the amount from the bolt11 or use the set amount field
71+ // Return an error if there is a mismatch.
72+ let decoded = rpc
73+ . call_typed ( & cln_rpc:: model:: requests:: DecodepayRequest {
74+ bolt11 : req. bolt11 . clone ( ) ,
75+ description : None ,
76+ } )
77+ . await ?;
78+
79+ let send_pays = rpc
80+ . call_typed ( & cln_rpc:: model:: requests:: ListsendpaysRequest {
81+ payment_hash : Some ( decoded. payment_hash . clone ( ) ) ,
82+ bolt11 : None ,
83+ index : None ,
84+ limit : None ,
85+ start : None ,
86+ status : None ,
87+ } )
88+ . await ?;
89+ if send_pays
90+ . payments
91+ . iter ( )
92+ . any ( |p| p. status != cln_rpc:: model:: responses:: ListsendpaysPaymentsStatus :: FAILED )
93+ {
94+ let resp = rpc
95+ . call_typed ( & cln_rpc:: model:: requests:: WaitsendpayRequest {
96+ payment_hash : decoded. payment_hash . clone ( ) ,
97+ groupid : None ,
98+ partid : None ,
99+ timeout : None ,
100+ } )
101+ . await ?;
102+
103+ let preimage = match resp. payment_preimage {
104+ Some ( preimage) => preimage,
105+ None => return Err ( anyhow ! ( "got completed payment part without preimage" ) ) ,
106+ } ;
107+ return Ok ( cln_rpc:: model:: responses:: PayResponse {
108+ amount_msat : resp. amount_msat . unwrap_or ( Amount :: from_msat ( 0 ) ) ,
109+ amount_sent_msat : resp. amount_sent_msat ,
110+ created_at : resp. created_at as f64 ,
111+ destination : resp. destination ,
112+ parts : resp. partid . unwrap_or ( 1 ) as u32 ,
113+ payment_hash : resp. payment_hash ,
114+ payment_preimage : preimage,
115+ status : match resp. status {
116+ cln_rpc:: model:: responses:: WaitsendpayStatus :: COMPLETE => {
117+ cln_rpc:: model:: responses:: PayStatus :: COMPLETE
118+ }
119+ } ,
120+ warning_partial_completion : None ,
121+ } ) ;
122+ }
123+
124+ let max_group_id = send_pays
125+ . payments
126+ . iter ( )
127+ . map ( |p| p. groupid )
128+ . max ( )
129+ . unwrap_or ( 0 ) ;
68130 log:: debug!(
69131 "New trampoline payment via {}: {} " ,
70132 node_id. to_hex( ) ,
@@ -78,7 +140,7 @@ pub async fn trampolinepay(
78140 . await ?;
79141
80142 // Check if peer has signaled that they support forward trampoline pays:
81- let mut rpc = ClnRpc :: new ( & rpc_path ) . await ? ;
143+
82144 let features = rpc
83145 . call_typed ( & cln_rpc:: model:: requests:: ListpeersRequest {
84146 id : Some ( node_id) ,
@@ -95,15 +157,6 @@ pub async fn trampolinepay(
95157
96158 feature_guard ( features, TRAMPOLINE_FEATURE_BIT ) ?;
97159
98- // Extract the amount from the bolt11 or use the set amount field
99- // Return an error if there is a mismatch.
100- let decoded = rpc
101- . call_typed ( & cln_rpc:: model:: requests:: DecodepayRequest {
102- bolt11 : req. bolt11 . clone ( ) ,
103- description : None ,
104- } )
105- . await ?;
106-
107160 let amount_msat = match ( as_option ( req. amount_msat ) , decoded. amount_msat ) {
108161 ( None , None ) => {
109162 return Err ( anyhow ! (
@@ -225,7 +278,7 @@ pub async fn trampolinepay(
225278 let payload_hex = hex:: encode ( SerializedTlvStream :: to_bytes ( payload) ) ;
226279
227280 let mut part_id = if choosen. len ( ) == 1 { 0 } else { 1 } ;
228- let group_id = 1 ;
281+ let group_id = max_group_id + 1 ;
229282 let mut handles: Vec <
230283 tokio:: task:: JoinHandle <
231284 std:: result:: Result < cln_rpc:: model:: responses:: WaitsendpayResponse , anyhow:: Error > ,
0 commit comments