Skip to content

Commit dd0d2a4

Browse files
committed
Add a utility trait in router to get the fees along a given path
1 parent 59659d3 commit dd0d2a4

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lightning/src/routing/router.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,26 @@ pub struct Route {
7878
pub payee: Option<Payee>,
7979
}
8080

81+
pub(crate) trait RoutePath {
82+
/// Gets the fees for a given path, excluding any excess paid to the recipient.
83+
fn get_path_fees(&self) -> u64;
84+
}
85+
impl RoutePath for Vec<RouteHop> {
86+
fn get_path_fees(&self) -> u64 {
87+
self.split_last().map(|(_, path_prefix)| path_prefix).unwrap_or(&[])
88+
.iter().map(|hop| &hop.fee_msat)
89+
.sum()
90+
}
91+
}
92+
8193
impl Route {
8294
/// Returns the total amount of fees paid on this [`Route`].
8395
///
8496
/// This doesn't include any extra payment made to the recipient, which can happen in excess of
8597
/// the amount passed to [`find_route`]'s `params.final_value_msat`.
8698
pub fn get_total_fees(&self) -> u64 {
8799
// Do not count last hop of each path since that's the full value of the payment
88-
return self.paths.iter()
89-
.flat_map(|path| path.split_last().map(|(_, path_prefix)| path_prefix).unwrap_or(&[]))
90-
.map(|hop| &hop.fee_msat)
91-
.sum();
100+
self.paths.iter().map(|path| path.get_path_fees()).sum()
92101
}
93102

94103
/// Returns the total amount paid on this [`Route`], excluding the fees.

0 commit comments

Comments
 (0)