I've refactored the Util class to improve readability and maintainability. The changes include:
Simplified the sum function using Array.reduce().
Refactored the lndRoutetoLdkRoute method to improve code clarity and reduce redundancy.
Provided explanations for the changes made.
Refactored Code:
function sum(arr: number[]): number {
// Function to calculate the sum of an array of numbers
return arr.reduce((acc, val) => acc + val, 0);
}
class Util {
static lndRoutetoLdkRoute(
lndRoute: any,
hopFees: any,
firstChanId: string,
minFinalCLTVExpiryFromTheInvoice: number
): any[] { // Adjust the return type accordingly
const craftedRoute: any[] = [];
const fees: number[] = [];
for (let c = lndRoute.routes[0].hops.length - 1; c >= 0; c--) {
const lndHop = lndRoute.routes[0].hops[c];
const hop: any = {};
// Setting up cltv_expiry_delta and fee_msat based on the hop
if (c === lndRoute.routes[0].hops.length - 1) {
hop.cltv_expiry_delta = minFinalCLTVExpiryFromTheInvoice;
hop.fee_msat = parseInt(lndHop.amt_to_forward_msat, 10);
} else {
hop.fee_msat = parseInt(lndHop.fee_msat, 10);
hop.cltv_expiry_delta = c > 0 ? lndRoute.routes[0].hops[c - 1].expiry - lndHop.expiry : 666;
if (c === 0) hop.cltv_expiry_delta = lndRoute.routes[0].total_time_lock - lndHop.expiry;
}
hop.short_channel_id = lndHop.chan_id;
hop.pubkey = lndHop.pub_key;
fees.push(hop.fee_msat);
craftedRoute.push(hop);
}
// Crafting the very first hop
const firstCraftedHop: any = {};
const nodePolicy = lndRoute.routes[0].hops[0].pub_key === hopFees.node2_pub ? hopFees.node1_policy : hopFees.node2_policy;
firstCraftedHop.pubkey = lndRoute.routes[0].hops[0].pub_key === hopFees.node2_pub ? hopFees.node1_pub : hopFees.node2_pub;
firstCraftedHop.short_channel_id = firstChanId;
firstCraftedHop.fee_msat = Math.floor((sum(fees) * parseInt(nodePolicy.fee_rate_milli_msat, 10)) / 1000000) + parseInt(nodePolicy.fee_base_msat, 10);
firstCraftedHop.cltv_expiry_delta = nodePolicy.time_lock_delta;
craftedRoute.push(firstCraftedHop);
return craftedRoute.reverse();
}
}
export default Util;
Explanation:
sum Function:
-- Refactored the sum function to use Array.reduce() method to calculate the sum of an array of numbers.
lndRoutetoLdkRoute Method:
-- Renamed the ret variable to craftedRoute for better clarity.
-- Simplified the conditionals for setting cltv_expiry_delta and fee_msat within the loop for each hop.
-- Extracted the logic for crafting the first hop into its own block for better readability.
-- Utilized ternary operator for conditionals to make the code more concise.
-- Utilized Array.reverse() method at the end to reverse the order of the craftedRoute array.
Additional Notes:
Feel free to review the changes and provide feedback or suggestions for further improvements.
Description:
I've refactored the Util class to improve readability and maintainability. The changes include:
sum
function usingArray.reduce()
.lndRoutetoLdkRoute
method to improve code clarity and reduce redundancy.Refactored Code:
Explanation:
sum Function: -- Refactored the sum function to use
Array.reduce()
method to calculate the sum of an array of numbers.lndRoutetoLdkRoute Method: -- Renamed the
ret
variable tocraftedRoute
for better clarity. -- Simplified the conditionals for settingcltv_expiry_delta
andfee_msat
within the loop for each hop. -- Extracted the logic for crafting the first hop into its own block for better readability. -- Utilized ternary operator for conditionals to make the code more concise. -- UtilizedArray.reverse()
method at the end to reverse the order of the craftedRoute array.Additional Notes:
Feel free to review the changes and provide feedback or suggestions for further improvements.