Compare commits

...

2 Commits

Author SHA1 Message Date
Chidiebere Chukwuma
d5e48772f5 Add refactored Util class for improved readability 2024-03-23 18:17:16 +00:00
Overtorment
27a0868255
Update gradle.properties 2023-02-24 11:45:30 +00:00
2 changed files with 27 additions and 41 deletions

View File

@ -1,4 +1,4 @@
RnLdk_kotlinVersion=1.3.50
RnLdk_kotlinVersion=1.6.0
RnLdk_compileSdkVersion=29
RnLdk_buildToolsVersion=29.0.2
RnLdk_targetSdkVersion=29

View File

@ -1,65 +1,51 @@
function sum(arr: number[]) {
let ret = 0;
for (const num of arr) ret += num;
return ret;
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) {
const ret = [];
let firstIteration = true;
let fees: number[] = []; // aggregating fees of LND hops
static lndRoutetoLdkRoute(
lndRoute: any,
hopFees: any,
firstChanId: string,
minFinalCLTVExpiryFromTheInvoice: number
): any[] { // Adjust the return type accordingly
const craftedRoute: any[] = [];
const fees: number[] = [];
// iterating hops provided by LND's queryroutes
for (let c = lndRoute.routes[0].hops.length - 1; c >= 0; c--) {
let hop: any = {};
const lndHop = lndRoute.routes[0].hops[c];
const hop: any = {};
if (firstIteration) {
// last hop is a bit special
hop.cltv_expiry_delta = minFinalCLTVExpiryFromTheInvoice; //lndRoute.routes[0].total_time_lock - currentblockHeight;
hop.fee_msat = parseInt(lndRoute.routes[0].hops[c].amt_to_forward_msat, 10);
// 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(lndRoute.routes[0].hops[c].fee_msat, 10);
if (c > 0 && c < lndRoute.routes[0].hops.length - 1) {
// intermediate hops have expiry as a difference between neighbor hops
hop.cltv_expiry_delta = lndRoute.routes[0].hops[c - 1].expiry - lndRoute.routes[0].hops[c].expiry;
} else {
hop.cltv_expiry_delta = 666; // lndRoute.routes[0].total_time_lock - lndHop.expiry;
}
if (c === 0) {
hop.cltv_expiry_delta = lndRoute.routes[0].total_time_lock - lndRoute.routes[0].hops[0].expiry;
}
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);
firstIteration = false;
ret.push(hop);
craftedRoute.push(hop);
}
// now, crafting the very first hop out of provided chaninfo
// 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;
let nodePolicy;
let nodePubkey;
if (lndRoute.routes[0].hops[0].pub_key === hopFees.node2_pub) {
nodePolicy = hopFees.node1_policy;
nodePubkey = hopFees.node1_pub;
} else {
nodePolicy = hopFees.node2_policy;
nodePubkey = hopFees.node2_pub;
}
firstCraftedHop.pubkey = nodePubkey;
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;
ret.push(firstCraftedHop);
return ret.reverse();
craftedRoute.push(firstCraftedHop);
return craftedRoute.reverse();
}
}