Skip to content

Commit

Permalink
rename estimateRouteFee to getRoutingFeeEstimate
Browse files Browse the repository at this point in the history
  • Loading branch information
bnonni committed Jan 4, 2025
1 parent 99128f3 commit a5d3eba
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
4 changes: 2 additions & 2 deletions lnd_methods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const {disableChannel} = require('./offchain');
const {disconnectWatchtower} = require('./offchain');
const {enableChannel} = require('./offchain');
const {endGroupSigningSession} = require('./signer');
const {estimateRouteFee} = require('./offchain');
const {fundPendingChannels} = require('./onchain');
const {fundPsbt} = require('./onchain');
const {getAccessIds} = require('./macaroon');
Expand Down Expand Up @@ -78,6 +77,7 @@ const {getPublicKey} = require('./address');
const {getRouteConfidence} = require('./generic');
const {getRouteThroughHops} = require('./offchain');
const {getRouteToDestination} = require('./info');
const {getRoutingFeeEstimate} = require('./offchain');
const {getSettlementStatus} = require('./offchain');
const {getSweepTransactions} = require('./onchain');
const {getTowerServerInfo} = require('./info');
Expand Down Expand Up @@ -187,7 +187,6 @@ module.exports = {
disableChannel,
disconnectWatchtower,
enableChannel,
estimateRouteFee,
endGroupSigningSession,
fundPendingChannels,
fundPsbt,
Expand Down Expand Up @@ -239,6 +238,7 @@ module.exports = {
getRouteConfidence,
getRouteThroughHops,
getRouteToDestination,
getRoutingFeeEstimate,
getSettlementStatus,
getSweepTransactions,
getTowerServerInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import {
AuthenticatedLightningMethod
} from '../../typescript';

export type RouteFeeRequest = AuthenticatedLightningArgs<{
export type GetRoutingFeeEstimateRequest = AuthenticatedLightningArgs<{
lnd: AuthenticatedLnd;
/** BOLT 11 Encoded Payment Request */
request: string;
/** Optional Timeout in Milliseconds */
timeout: number;
}>;

export type RouteFeeResponse = {
/** Millitokens (Routing Fee Msat) */
mtoken: number;
export type GetRoutingFeeEstimateResponse = {
/** Sats (Routing Fee Sats) */
fee: number;
/** Timeout (Time Lock Delay) */
timeout: number;
timeout: string;
};

/**
Expand All @@ -26,7 +26,7 @@ export type RouteFeeResponse = {
*
* This method is not supported before LND 0.18.4
*/
export const estimateRouteFee: AuthenticatedLightningMethod<
RouteFeeRequest,
RouteFeeResponse
export const getRoutingFeeEstimate: AuthenticatedLightningMethod<
GetRoutingFeeEstimateRequest,
GetRoutingFeeEstimateResponse
>;
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ module.exports = ({lnd, request, timeout}, cbk) => {
// Check arguments
validate: cbk => {
if (!lnd || !lnd.router) {
return cbk([400, 'ExpectedAuthenticatedLndForEstimateRouteFee']);
return cbk([400, 'ExpectedAuthenticatedLndForGetRoutingFeeEstimate']);
}

if (!request) {
return cbk([400, 'ExpectedPaymentRequestStringForEstimateRouteFee']);
return cbk([400, 'ExpectedPaymentRequestStringForGetRoutingFeeEstimate']);
}

if (timeout > 86400000) {
return cbk([400, 'ExpectedTimeoutLessThanOneDayForEstimateRouteFee']);
return cbk([400, 'ExpectedTimeoutLessThanOneDayForGetRoutingFeeEstimate']);
}

if (timeout < 1) {
return cbk([400, 'ExpectedTimeoutGreaterThanZeroForEstimateRouteFee']);
return cbk([400, 'ExpectedTimeoutGreaterThanZeroForGetRoutingFeeEstimate']);
}

timeout = !timeout ? 60 : timeout / 1000;
Expand All @@ -48,43 +48,43 @@ module.exports = ({lnd, request, timeout}, cbk) => {
},

// Estimate route fee and return a successful routing fee and timeout or failure reason
estimateRouteFee: ['validate', ({}, cbk) => {
getEstimate: ['validate', ({}, cbk) => {
return lnd.router.estimateRouteFee({request, timeout},
(err, res) => {
if (err) {
return cbk([503, 'UnexpectedEstimateRouteFeeError', {err}]);
return cbk([503, 'UnexpectedGetRoutingFeeEstimateError', {err}]);
}

if (!res) {
return cbk([503, 'ExpectedRouteFeeResponse']);
return cbk([503, 'ExpectedGetRoutingFeeEstimateResponse']);
}

const {fee} = {fee: Number(res.fee)};

if (!fee) {
return cbk([503, 'ExpectedFeeInRouteFeeResponse']);
const mtokenFee = Number(res.fee);
if (!mtokenFee) {
return cbk([503, 'ExpectedFeeInGetRoutingFeeEstimateResponse']);
}

if (isNaN(fee)) {
return cbk([503, 'ExpectedFeeInRouteFeeResponseToBeNumber']);
if (isNaN(mtokenFee)) {
return cbk([503, 'ExpectedFeeInGetRoutingFeeEstimateResponseToBeNumber']);
}

if (!isFinite(fee)) {
return cbk([503, 'ExpectedFeeInRouteFeeResponseToBeFinite']);
if (!isFinite(mtokenFee)) {
return cbk([503, 'ExpectedFeeInGetRoutingFeeEstimateResponseToBeFinite']);
}

if (!res.timeout) {
return cbk([503, 'ExpectedTimeoutInRouteFeeResponse']);
return cbk([503, 'ExpectedTimeoutInGetRoutingFeeEstimateResponse']);
}

if (res.failure_reason !== 'FAILURE_REASON_NONE') {
return cbk([404, 'EstimateRouteFeeFailed', {failure: res.failure_reason}])
return cbk([404, 'GetRoutingFeeEstimateFailed', {failure: res.failure_reason}])
}

const fee = mtokenFee > 0 ? mtokenFee / 1000 : mtokenFee;
return cbk(null, {fee, timeout: res.timeout});
});
}],
},
returnResult({reject, resolve, of: 'estimateRouteFee'}, cbk));
returnResult({reject, resolve, of: 'getEstimate'}, cbk));
});
};
2 changes: 1 addition & 1 deletion lnd_methods/offchain/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export * from './delete_pending_channel';
export * from './disable_channel';
export * from './disconnect_watchtower';
export * from './enable_channel';
export * from './estimate_route_fee';
export * from './get_backup';
export * from './get_backups';
export * from './get_channel_balance';
Expand All @@ -28,6 +27,7 @@ export * from './get_payments';
export * from './get_pending_channels';
export * from './get_pending_payments';
export * from './get_route_through_hops';
export * from './get_routing_fee_estimate';
export * from './get_settlement_status';
export * from './is_destination_payable';
export * from './pay_via_payment_details';
Expand Down
2 changes: 2 additions & 0 deletions lnd_methods/offchain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const getPayments = require('./get_payments');
const getPendingChannels = require('./get_pending_channels');
const getPendingPayments = require('./get_pending_payments');
const getRouteThroughHops = require('./get_route_through_hops');
const getRoutingFeeEstimate = require('./get_routing_fee_estimate');
const getSettlementStatus = require('./get_settlement_status');
const isDestinationPayable = require('./is_destination_payable');
const pay = require('./pay');
Expand Down Expand Up @@ -86,6 +87,7 @@ module.exports = {
getPendingChannels,
getPendingPayments,
getRouteThroughHops,
getRoutingFeeEstimate,
getSettlementStatus,
isDestinationPayable,
pay,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {rejects, deepStrictEqual} = require('node:assert').strict;
const test = require('node:test');
const estimateRouteFee = require('./../../../lnd_methods/offchain/estimate_route_fee');
const getRoutingFeeEstimate = require('../../../lnd_methods/offchain/get_routing_fee_estimate');

const request = 'lnbcrt500u1pnh3r5dpp57cppte59jvmxnaunh03ecy6wchq8e0zh70n0nzsamaxztqxevcusdqqcqzzsxqyz5vqsp587ua488ttsts8cs97ekt9axdla3jmq4mj2h7xj7g6rw37fu65yqs9qxpqysgql27u5p9m2xv0r0pjykzvcgs88azzfkywzw2xw5q6u86qnwzrut94mks86zxelhltdtn6vnqgd8hay433wwq7uvple709gp7pmwmtzwcqakyevc';

Expand Down Expand Up @@ -28,41 +28,41 @@ const tests = [
{
args: makeArgs({lnd: undefined}),
description: 'LND is required',
error: [400, 'ExpectedAuthenticatedLndForEstimateRouteFee'],
error: [400, 'ExpectedAuthenticatedLndForGetRoutingFeeEstimate'],
},
{
args: makeArgs({request: undefined}),
description: 'Request is required',
error: [400, 'ExpectedPaymentRequestStringForEstimateRouteFee'],
error: [400, 'ExpectedPaymentRequestStringForGetRoutingFeeEstimate'],
},
{
args: makeArgs({timeout: 86400001}),
description: 'Timeout must be less than or equal to 86400000 milliseconds',
error: [400, 'ExpectedTimeoutLessThanOneDayForEstimateRouteFee'],
error: [400, 'ExpectedTimeoutLessThanOneDayForGetRoutingFeeEstimate'],
},
{
args: makeArgs({timeout: 0}),
description: 'Timeout must be greater than 0 milliseconds',
error: [400, 'ExpectedTimeoutGreaterThanZeroForEstimateRouteFee'],
error: [400, 'ExpectedTimeoutGreaterThanZeroForGetRoutingFeeEstimate'],
},
{
args: makeArgs({request, lnd: makeLnd({})}),
description: 'A route fee number in sats is expected for default timeout 60000 milliseconds',
expected: {fee: 1050, timeout: '3520'}
expected: {fee: 1.05, timeout: '3520'}
},
{
args: makeArgs({request, timeout: 86400000, lnd: makeLnd({})}),
description: 'A route fee number in sats is expected for timeout 86400000 milliseconds',
expected: {fee: 1050, timeout: '3520'}
expected: {fee: 1.05, timeout: '3520'}
}
];

tests.forEach(({args, description, error, expected}) => {
return test(description, async () => {
if (!!error) {
await rejects(estimateRouteFee(args), error, 'Got expected error');
await rejects(getRoutingFeeEstimate(args), error, 'Got expected error');
} else {
deepStrictEqual(await estimateRouteFee(args), expected, 'Got result');
deepStrictEqual(await getRoutingFeeEstimate(args), expected, 'Got result');
}

return;
Expand Down

0 comments on commit a5d3eba

Please sign in to comment.