Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into taproot-swaps
  • Loading branch information
michael1011 committed Jan 7, 2024
2 parents 7e1ebed + 5ea0c82 commit b56bf57
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
15 changes: 14 additions & 1 deletion lib/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ export const minutesToMilliseconds = (minutes: number): number => {
return minutes * 60 * 1000;
};

const coalesceInvoiceAmount = (
decoded: bolt11.PaymentRequestObject,
): number => {
const decodedMsat = decoded.millisatoshis
? Math.ceil(Number(decoded.millisatoshis) / 1000)
: undefined;

return decoded.satoshis || decodedMsat || 0;
};

export const decodeInvoiceAmount = (invoice: string): number =>
coalesceInvoiceAmount(bolt11.decode(invoice));

export const decodeInvoice = (
invoice: string,
): bolt11.PaymentRequestObject & {
Expand Down Expand Up @@ -158,7 +171,7 @@ export const decodeInvoice = (
routingInfo,
paymentHash,
minFinalCltvExpiry,
satoshis: decoded.satoshis || 0,
satoshis: coalesceInvoiceAmount(decoded),
};
};

Expand Down
4 changes: 2 additions & 2 deletions lib/lightning/LightningClient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import bolt11 from 'bolt11';
import { IBaseClient } from '../BaseClient';
import { ClientStatus } from '../consts/Enums';
import * as lndrpc from '../proto/lnd/rpc_pb';
import { BalancerFetcher } from '../wallet/providers/WalletProviderInterface';
import { decodeInvoiceAmount } from '../Utils';

enum InvoiceState {
Open,
Expand Down Expand Up @@ -174,7 +174,7 @@ const calculatePaymentFee = (
maxRatio: number,
minFee: number,
): number => {
const invoiceAmt = bolt11.decode(invoice).satoshis || 0;
const invoiceAmt = decodeInvoiceAmount(invoice);
return Math.ceil(Math.max(invoiceAmt * maxRatio, minFee));
};

Expand Down
3 changes: 2 additions & 1 deletion lib/lightning/LndClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fs from 'fs';
import BaseClient from '../BaseClient';
import Logger from '../Logger';
import {
decodeInvoiceAmount,
formatError,
getHexBuffer,
getHexString,
Expand Down Expand Up @@ -579,9 +580,9 @@ class LndClient extends BaseClient implements LightningClient {

return {
features,
value: res.numSatoshis,
cltvExpiry: res.cltvExpiry,
destination: res.destination,
value: decodeInvoiceAmount(invoice),
routingHints: LndClient.routingHintsFromGrpc(res.routeHintsList),
};
};
Expand Down
14 changes: 7 additions & 7 deletions lib/service/Service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import bolt11 from 'bolt11';
import { OutputType, SwapTreeSerializer } from 'boltz-core';
import { Provider, getAddress } from 'ethers';
import Errors from './Errors';
import Logger from '../Logger';
import NodeInfo from './NodeInfo';
import { ConfigType } from '../Config';
import { parseTransaction } from '../Core';
import Logger from '../Logger';
import {
createApiCredential,
decodeInvoice,
decodeInvoiceAmount,
formatError,
getChainCurrency,
getHexBuffer,
Expand Down Expand Up @@ -64,14 +65,13 @@ import SwapOutputType from '../swap/SwapOutputType';
import WalletManager, { Currency } from '../wallet/WalletManager';
import EthereumManager from '../wallet/ethereum/EthereumManager';
import ElementsService from './ElementsService';
import Errors from './Errors';
import EventHandler from './EventHandler';
import MusigSigner from './MusigSigner';
import NodeInfo from './NodeInfo';
import PaymentRequestUtils from './PaymentRequestUtils';
import TimeoutDeltaProvider, {
PairTimeoutBlocksDelta,
} from './TimeoutDeltaProvider';
import { Provider, getAddress } from 'ethers';

type NetworkContracts = {
network: {
Expand Down Expand Up @@ -941,7 +941,7 @@ class Service {
false,
);

swap.invoiceAmount = bolt11.decode(invoice).satoshis || 0;
swap.invoiceAmount = decodeInvoiceAmount(invoice);
const lightningClient = this.nodeSwitch.getSwapNode(
this.currencies.get(lightningCurrency)!,
swap,
Expand Down Expand Up @@ -995,7 +995,7 @@ class Service {
false,
);

swap.invoiceAmount = decodeInvoice(invoice).satoshis || 0;
swap.invoiceAmount = decodeInvoice(invoice).satoshis;

const decodedInvoice = await this.nodeSwitch
.getSwapNode(this.getCurrency(lightningCurrency)!, swap)
Expand Down
12 changes: 12 additions & 0 deletions test/unit/Utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ describe('Utils', () => {
]);
});

test.each`
expectedSats | invoice
${120001} | ${'lnbcrt1200003230p1pje4yezsp5fcaw07mpwzqq4zxx349jfmp8rplpuznl3vl3jnz0gam4k97d3suqpp5rcrkszpgnkf7dqzrgmakmuvqyhe9df33fmavk3a8tpw3vzranm4qdq8v9ekgesxqyjw5qcqp2rzjqwdgldgs6nfufyx75x60nvjc0va7emuujkdqr7cuczfzrqyt3plvcqqqdcqqqqgqqqqqqqlgqqqqqqgq2q9qxpqysgqalxdy0fqurdu6z34uvcmz0hw5v6x5pf57yj7cwzdwdf4wt9m7y6jskkzeelpt9727ha7ktrl9s0x30hllc6twrnn7vlxka2dfaf9g9spgsjj0r'}
${130001} | ${'lnbcrt1300003230p1pje4yujsp54k30ymnuel6p7756nc00vq06stu8gdrn77z9frn6jxhpkf5fjekqpp57dz6tlrra5g2cs3k0relg55rt26hs4m9kjgt9nh8j5kuhuvuhrqsdqgv9ekgenxxqyjw5qcqp2rzjqtst8pw56xy8uydcjkd6t20rwux4uwkpsf7h7nwms4x2r6zgphyg6qqq0sqqqqgqqqqqqqlgqqqqqqgq2q9qxpqysgqed55scqulmlr5em9rww8v92rwmuefzpektrpmrvpyjprrjr23qckmh9cnst8crwspggm59366huqshtuwyawhj4tgmez0thfmfqrducqu8ez5v'}
`(
'should decode invoice with msats precision',
({ expectedSats, invoice }) => {
expect(utils.decodeInvoiceAmount(invoice)).toEqual(expectedSats);
expect(utils.decodeInvoice(invoice).satoshis).toEqual(expectedSats);
},
);

test('should get rate', () => {
const rate = 2;
const reverseRate = 1 / rate;
Expand Down

0 comments on commit b56bf57

Please sign in to comment.