Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit d1802ce

Browse files
Merge pull request #809 from lightninglabs/grpc-long-number
Grpc long number
2 parents ea91613 + e4a1e36 commit d1802ce

File tree

11 files changed

+62
-136
lines changed

11 files changed

+62
-136
lines changed

public/grpc-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ module.exports.init = async function({
6363
protoPath = path.join(__dirname, '..', 'assets', 'rpc.proto');
6464
const options = {
6565
keepCase: false,
66-
longs: String,
66+
longs: Number,
6767
enums: String,
6868
defaults: true,
6969
oneofs: true,

src/action/channel.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* call the corresponding GRPC apis for channel management.
44
*/
55

6-
import { toSatoshis, parseSat } from '../helper';
6+
import { toSatoshis } from '../helper';
77
import * as log from './log';
88

99
class ChannelAction {
@@ -102,9 +102,9 @@ class ChannelAction {
102102
this._store.channels = channels.map(channel => ({
103103
remotePubkey: channel.remotePubkey,
104104
id: channel.chanId,
105-
capacity: parseSat(channel.capacity),
106-
localBalance: parseSat(channel.localBalance),
107-
remoteBalance: parseSat(channel.remoteBalance),
105+
capacity: channel.capacity,
106+
localBalance: channel.localBalance,
107+
remoteBalance: channel.remoteBalance,
108108
channelPoint: channel.channelPoint,
109109
fundingTxId: this._parseChannelPoint(channel.channelPoint)
110110
.fundingTxidStr,
@@ -127,9 +127,9 @@ class ChannelAction {
127127
const response = await this._grpc.sendCommand('pendingChannels');
128128
const mapPendingAttributes = channel => ({
129129
remotePubkey: channel.remoteNodePub,
130-
capacity: parseSat(channel.capacity),
131-
localBalance: parseSat(channel.localBalance),
132-
remoteBalance: parseSat(channel.remoteBalance),
130+
capacity: channel.capacity,
131+
localBalance: channel.localBalance,
132+
remoteBalance: channel.remoteBalance,
133133
channelPoint: channel.channelPoint,
134134
fundingTxId: this._parseChannelPoint(channel.channelPoint)
135135
.fundingTxidStr,
@@ -177,13 +177,12 @@ class ChannelAction {
177177
const { channels } = await this._grpc.sendCommand('closedChannels');
178178
this._store.closedChannels = channels.map(channel => ({
179179
remotePubkey: channel.remotePubkey,
180-
capacity: parseSat(channel.capacity),
180+
capacity: channel.capacity,
181181
channelPoint: channel.channelPoint,
182182
fundingTxId: this._parseChannelPoint(channel.channelPoint)
183183
.fundingTxidStr,
184-
localBalance: parseSat(channel.settledBalance),
185-
remoteBalance:
186-
parseSat(channel.capacity) - parseSat(channel.settledBalance),
184+
localBalance: channel.settledBalance,
185+
remoteBalance: channel.capacity - channel.settledBalance,
187186
closingTxId: channel.closingTxHash,
188187
status: 'closed',
189188
}));

src/action/payment.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44
*/
55

66
import { PREFIX_URI, PAYMENT_TIMEOUT } from '../config';
7-
import {
8-
toSatoshis,
9-
toAmount,
10-
parseSat,
11-
isLnUri,
12-
isAddress,
13-
nap,
14-
} from '../helper';
7+
import { toSatoshis, toAmount, isLnUri, isAddress, nap } from '../helper';
158
import * as log from './log';
169

1710
class PaymentAction {
@@ -108,11 +101,11 @@ class PaymentAction {
108101
const request = await this._grpc.sendCommand('decodePayReq', {
109102
payReq: invoice.replace(PREFIX_URI, ''),
110103
});
111-
payment.amount = toAmount(parseSat(request.numSatoshis), settings);
104+
payment.amount = toAmount(request.numSatoshis, settings);
112105
payment.note = request.description;
113106
await this.estimateLightningFee({
114107
destination: request.destination,
115-
satAmt: parseSat(request.numSatoshis),
108+
satAmt: request.numSatoshis,
116109
});
117110
return true;
118111
} catch (err) {
@@ -136,7 +129,7 @@ class PaymentAction {
136129
amt: satAmt,
137130
numRoutes: 1,
138131
});
139-
payment.fee = toAmount(parseSat(routes[0].totalFees), settings);
132+
payment.fee = toAmount(routes[0].totalFees, settings);
140133
} catch (err) {
141134
log.error(`Estimating lightning fee failed!`, err);
142135
}

src/action/transaction.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import * as log from './log';
7-
import { parseDate, parseSat, toHex } from '../helper';
7+
import { parseDate, toHex } from '../helper';
88

99
class TransactionAction {
1010
constructor(store, grpc, nav, notification) {
@@ -60,8 +60,8 @@ class TransactionAction {
6060
this._store.transactions = transactions.map(transaction => ({
6161
id: transaction.txHash,
6262
type: 'bitcoin',
63-
amount: parseSat(transaction.amount),
64-
fee: parseSat(transaction.totalFees),
63+
amount: transaction.amount,
64+
fee: transaction.totalFees,
6565
confirmations: transaction.numConfirmations,
6666
status: transaction.numConfirmations < 1 ? 'unconfirmed' : 'confirmed',
6767
date: parseDate(transaction.timeStamp),
@@ -82,7 +82,7 @@ class TransactionAction {
8282
this._store.invoices = invoices.map(invoice => ({
8383
id: toHex(invoice.rHash),
8484
type: 'lightning',
85-
amount: parseSat(invoice.value),
85+
amount: invoice.value,
8686
status: invoice.settled ? 'complete' : 'in-progress',
8787
date: parseDate(invoice.creationDate),
8888
memo: invoice.memo,
@@ -103,8 +103,8 @@ class TransactionAction {
103103
this._store.payments = payments.map(payment => ({
104104
id: payment.paymentHash,
105105
type: 'lightning',
106-
amount: -1 * parseSat(payment.value),
107-
fee: parseSat(payment.fee),
106+
amount: -1 * payment.value,
107+
fee: payment.fee,
108108
status: 'complete',
109109
date: parseDate(payment.creationDate),
110110
}));

src/action/wallet.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* call the corresponding GRPC apis for updating wallet balances.
44
*/
55

6-
import { toBuffer, parseSat, checkHttpStatus, nap, poll } from '../helper';
6+
import { toBuffer, checkHttpStatus, nap, poll } from '../helper';
77
import {
88
MIN_PASSWORD_LENGTH,
99
NOTIFICATION_DELAY,
@@ -394,9 +394,9 @@ class WalletAction {
394394
async getBalance() {
395395
try {
396396
const r = await this._grpc.sendCommand('WalletBalance');
397-
this._store.balanceSatoshis = parseSat(r.totalBalance);
398-
this._store.confirmedBalanceSatoshis = parseSat(r.confirmedBalance);
399-
this._store.unconfirmedBalanceSatoshis = parseSat(r.unconfirmedBalance);
397+
this._store.balanceSatoshis = r.totalBalance;
398+
this._store.confirmedBalanceSatoshis = r.confirmedBalance;
399+
this._store.unconfirmedBalanceSatoshis = r.unconfirmedBalance;
400400
} catch (err) {
401401
log.error('Getting wallet balance failed', err);
402402
}
@@ -410,8 +410,8 @@ class WalletAction {
410410
async getChannelBalance() {
411411
try {
412412
const r = await this._grpc.sendCommand('ChannelBalance');
413-
this._store.channelBalanceSatoshis = parseSat(r.balance);
414-
this._store.pendingBalanceSatoshis = parseSat(r.pendingOpenBalance);
413+
this._store.channelBalanceSatoshis = r.balance;
414+
this._store.pendingBalanceSatoshis = r.pendingOpenBalance;
415415
} catch (err) {
416416
log.error('Getting channel balance failed', err);
417417
}

src/helper.js

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,14 @@ export const formatFiat = (val, currency) => {
3333

3434
/**
3535
* Parse a unix time stamp to a JavaScript date object
36-
* @param {string} timeStamp The unix time stamp in seconds
36+
* @param {number} timeStamp The unix time stamp in seconds
3737
* @return {Date} The date object
3838
*/
3939
export const parseDate = timeStamp => {
40-
if (typeof timeStamp !== 'string' || !/^[0-9]+$/.test(timeStamp)) {
40+
if (!Number.isInteger(timeStamp)) {
4141
throw new Error('Invalid input!');
4242
}
43-
return new Date(parseInt(timeStamp, 10) * 1000);
44-
};
45-
46-
/**
47-
* Parse satoshi values to an integer number
48-
* @param {string} satoshis The integer value as a string
49-
* @return {number} The satoshi integer as a number
50-
*/
51-
export const parseSat = satoshis => {
52-
if (typeof satoshis !== 'string' || !/^-*[0-9]+$/.test(satoshis)) {
53-
throw new Error('Invalid input!');
54-
}
55-
satoshis = parseInt(satoshis, 10);
56-
if (isNaN(satoshis)) {
57-
throw new Error('Invalid input!');
58-
}
59-
return satoshis;
43+
return new Date(timeStamp * 1000);
6044
};
6145

6246
/**
@@ -84,7 +68,7 @@ export const toSatoshis = (amount, settings) => {
8468

8569
/**
8670
* Convert satoshis to a BTC values than can set as a text input value
87-
* @param {number} satoshis The value as a string or number
71+
* @param {number} satoshis The value as a number
8872
* @param {Object} settings Contains the current exchange rate
8973
* @return {string} The amount formatted as '0.0001'
9074
*/

test/unit/action/channel.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ describe('Action Channels Unit Tests', () => {
9292
{
9393
chanId: 42,
9494
active: true,
95-
capacity: '100',
96-
localBalance: '10',
97-
remoteBalance: '90',
95+
capacity: 100,
96+
localBalance: 10,
97+
remoteBalance: 90,
9898
channelPoint: 'FFFF:1',
9999
},
100100
],
@@ -117,9 +117,9 @@ describe('Action Channels Unit Tests', () => {
117117
describe('getPendingChannels()', () => {
118118
const pendingChannel = {
119119
remoteNodePub: 'some-key',
120-
capacity: '100',
121-
localBalance: '10',
122-
remoteBalance: '90',
120+
capacity: 100,
121+
localBalance: 10,
122+
remoteBalance: 90,
123123
channelPoint: 'FFFF:1',
124124
};
125125

test/unit/action/payment.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describe('Action Payments Unit Tests', () => {
139139
describe('decodeInvoice()', () => {
140140
it('should decode successfully', async () => {
141141
grpc.sendCommand.withArgs('decodePayReq').resolves({
142-
numSatoshis: '1700',
142+
numSatoshis: 1700,
143143
description: 'foo',
144144
destination: 'bar',
145145
});
@@ -150,7 +150,7 @@ describe('Action Payments Unit Tests', () => {
150150
numRoutes: 1,
151151
})
152152
.resolves({
153-
routes: [{ totalFees: '100' }],
153+
routes: [{ totalFees: 100 }],
154154
});
155155
const isValid = await payment.decodeInvoice({ invoice: 'some-invoice' });
156156
expect(isValid, 'to be', true);
@@ -171,7 +171,7 @@ describe('Action Payments Unit Tests', () => {
171171

172172
it('should set no fee on query route error', async () => {
173173
grpc.sendCommand.withArgs('decodePayReq').resolves({
174-
numSatoshis: '1700',
174+
numSatoshis: 1700,
175175
description: 'foo',
176176
destination: 'bar',
177177
});

test/unit/action/transaction.spec.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ describe('Action Transactions Unit Tests', () => {
6060
transactions: [
6161
{
6262
txHash: 'some-hash',
63-
amount: '42',
64-
totalFees: '10',
63+
amount: 42,
64+
totalFees: 10,
6565
numConfirmations: 0,
66-
timeStamp: '1527070395',
66+
timeStamp: 1527070395,
6767
},
6868
],
6969
});
@@ -84,10 +84,10 @@ describe('Action Transactions Unit Tests', () => {
8484
transactions: [
8585
{
8686
txHash: 'some-hash',
87-
amount: '42',
88-
totalFees: '10',
87+
amount: 42,
88+
totalFees: 10,
8989
numConfirmations: 1,
90-
timeStamp: '1527070395',
90+
timeStamp: 1527070395,
9191
},
9292
],
9393
});
@@ -116,8 +116,8 @@ describe('Action Transactions Unit Tests', () => {
116116
invoices: [
117117
{
118118
rHash: Buffer.from('cdab', 'hex'),
119-
creationDate: '1527070395',
120-
value: '42',
119+
creationDate: 1527070395,
120+
value: 42,
121121
settled: true,
122122
memo: 'some-memo',
123123
r_preimage: 'some-preimage',
@@ -147,9 +147,9 @@ describe('Action Transactions Unit Tests', () => {
147147
grpc.sendCommand.withArgs('listPayments').resolves({
148148
payments: [
149149
{
150-
creationDate: '1527070395',
151-
value: '42',
152-
fee: '10',
150+
creationDate: 1527070395,
151+
value: 42,
152+
fee: 10,
153153
settled: true,
154154
paymentHash: 'some-hash',
155155
},

test/unit/action/wallet.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,9 @@ describe('Action Wallet Unit Tests', () => {
491491
describe('getBalance()', () => {
492492
it('should get wallet balance', async () => {
493493
grpc.sendCommand.withArgs('WalletBalance').resolves({
494-
totalBalance: '1',
495-
confirmedBalance: '2',
496-
unconfirmedBalance: '3',
494+
totalBalance: 1,
495+
confirmedBalance: 2,
496+
unconfirmedBalance: 3,
497497
});
498498
await wallet.getBalance();
499499
expect(store.balanceSatoshis, 'to equal', 1);
@@ -511,8 +511,8 @@ describe('Action Wallet Unit Tests', () => {
511511
describe('getChannelBalance()', () => {
512512
it('should get channel balance', async () => {
513513
grpc.sendCommand.withArgs('ChannelBalance').resolves({
514-
balance: '1',
515-
pendingOpenBalance: '2',
514+
balance: 1,
515+
pendingOpenBalance: 2,
516516
});
517517
await wallet.getChannelBalance();
518518
expect(store.channelBalanceSatoshis, 'to equal', 1);

0 commit comments

Comments
 (0)