ln-accounting 5.0.6 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,12 @@
1
1
  # Versions
2
2
 
3
- ## Version 5.0.6
3
+ ## Version 6.0.0
4
+
5
+ ### Breaking Changes
6
+
7
+ - Node.js 14 or higher is now required
8
+
9
+ ## Version 5.0.7
4
10
 
5
11
  - `getChainTransactions`: Add mempool space tx data lookup method
6
12
 
@@ -4,6 +4,7 @@ const {returnResult} = require('asyncjs-util');
4
4
  const asCoingeckoDate = yyyymmdd => yyyymmdd.split('-').reverse().join('-');
5
5
  const centsPerDollar = 100;
6
6
  const dateComponents = date => date.substring(0, 'yyyy-mm-dd'.length);
7
+ const {keys} = Object;
7
8
  const remoteServiceTimeoutMs = 30 * 1000;
8
9
  const url = 'https://api.coingecko.com/api/v3/coins/bitcoin/history';
9
10
 
@@ -13,6 +14,7 @@ const url = 'https://api.coingecko.com/api/v3/coins/bitcoin/history';
13
14
  currency: <Currency Type String>
14
15
  date: <ISO 8601 Date String>
15
16
  fiat: <Fiat Type String>
17
+ rates: <Known Rates Object>
16
18
  request: <Request Function>
17
19
  }
18
20
 
@@ -21,7 +23,7 @@ const url = 'https://api.coingecko.com/api/v3/coins/bitcoin/history';
21
23
  cents: <Cents Per Token Number>
22
24
  }
23
25
  */
24
- module.exports = ({currency, date, fiat, request}, cbk) => {
26
+ module.exports = ({currency, date, fiat, rates, request}, cbk) => {
25
27
  return new Promise((resolve, reject) => {
26
28
  return asyncAuto({
27
29
  // Check arguments
@@ -38,6 +40,10 @@ module.exports = ({currency, date, fiat, request}, cbk) => {
38
40
  return cbk([400, 'UnsupportedFiatTypeForCoingeckoFiatRateLookup']);
39
41
  }
40
42
 
43
+ if (!rates) {
44
+ return cbk([400, 'ExpectedKnownRatesForCoingeckoFiatRateLookup']);
45
+ }
46
+
41
47
  if (!request) {
42
48
  return cbk([400, 'ExpectedRequestMethodForCoingeckoFiatRateLookup']);
43
49
  }
@@ -47,6 +53,16 @@ module.exports = ({currency, date, fiat, request}, cbk) => {
47
53
 
48
54
  // Get rate
49
55
  getRate: ['validate', ({}, cbk) => {
56
+ // Look for an existing rate lookup
57
+ const matching = keys(rates).find(key => {
58
+ return dateComponents(key) === dateComponents(date);
59
+ });
60
+
61
+ // Exit early when there is a cached result
62
+ if (!!rates[matching]) {
63
+ return cbk(null, {cents: rates[matching]});
64
+ }
65
+
50
66
  return request({
51
67
  url,
52
68
  json: true,
@@ -73,6 +73,7 @@ module.exports = ({currency, dates, fiat, provider, rate, request}, cbk) => {
73
73
  date,
74
74
  fiat,
75
75
  provider,
76
+ rates,
76
77
  request,
77
78
  },
78
79
  (err, rate) => {
@@ -19,6 +19,7 @@ const times = 10;
19
19
  date: <ISO 8601 Date String>
20
20
  fiat: <Fiat Type String>
21
21
  [provider]: <Historic Rate Source Type String>
22
+ rates: <Known Rates Object>
22
23
  request: <Request Function>
23
24
  }
24
25
 
@@ -27,7 +28,7 @@ const times = 10;
27
28
  cents: <Cents Per Token Number>
28
29
  }
29
30
  */
30
- module.exports = ({currency, date, fiat, provider, request}, cbk) => {
31
+ module.exports = ({currency, date, fiat, provider, rates, request}, cbk) => {
31
32
  return new Promise((resolve, reject) => {
32
33
  return asyncAuto({
33
34
  // Check arguments
@@ -44,6 +45,10 @@ module.exports = ({currency, date, fiat, provider, request}, cbk) => {
44
45
  return cbk([400, 'ExpectedFiatToGetHistoricRate']);
45
46
  }
46
47
 
48
+ if (!rates) {
49
+ return cbk([400, 'ExpectedRatesToGetHistoricRate']);
50
+ }
51
+
47
52
  if (!request) {
48
53
  return cbk([400, 'ExpectedRequestFunctionToGetHistoricRate']);
49
54
  }
@@ -66,7 +71,7 @@ module.exports = ({currency, date, fiat, provider, request}, cbk) => {
66
71
  }
67
72
 
68
73
  return asyncRetry({interval, times}, cbk => {
69
- return source({currency, date, fiat, request}, cbk);
74
+ return source({currency, date, fiat, rates, request}, cbk);
70
75
  },
71
76
  cbk);
72
77
  }],
package/package.json CHANGED
@@ -7,19 +7,19 @@
7
7
  "url": "https://github.com/alexbosworth/ln-accounting/issues"
8
8
  },
9
9
  "dependencies": {
10
- "async": "3.2.3",
11
- "asyncjs-util": "1.2.9",
12
- "bitcoinjs-lib": "6.0.1",
13
- "goldengate": "11.1.0",
10
+ "async": "3.2.4",
11
+ "asyncjs-util": "1.2.10",
12
+ "bitcoinjs-lib": "6.0.2",
13
+ "goldengate": "11.4.0",
14
14
  "json2csv": "5.0.7",
15
- "ln-service": "53.10.0"
15
+ "ln-service": "54.2.0"
16
16
  },
17
17
  "description": "lnd accounting reports",
18
18
  "devDependencies": {
19
19
  "@alexbosworth/tap": "15.0.11"
20
20
  },
21
21
  "engines": {
22
- "node": ">=12.20"
22
+ "node": ">=14"
23
23
  },
24
24
  "keywords": [
25
25
  "accounting",
@@ -35,5 +35,5 @@
35
35
  "scripts": {
36
36
  "test": "tap --branches=1 --functions=1 --lines=1 --statements=1 test/blockstream/*.js test/fiat/*.js test/harmony/*.js test/records/*.js"
37
37
  },
38
- "version": "5.0.6"
38
+ "version": "6.0.0"
39
39
  }
@@ -2,71 +2,72 @@ const {test} = require('@alexbosworth/tap');
2
2
 
3
3
  const method = require('./../../fiat/get_coingecko_historic_rate');
4
4
 
5
+ const makeArgs = overrides => {
6
+ const args = {
7
+ date: new Date().toISOString(),
8
+ currency: 'BTC',
9
+ fiat: 'USD',
10
+ rates: {},
11
+ request: ({qs}, cbk) => api({qs}, cbk),
12
+ };
13
+
14
+ Object.keys(overrides).forEach(k => args[k] = overrides[k]);
15
+
16
+ return args;
17
+ };
18
+
5
19
  const api = ({qs}, cbk) => {
6
20
  return cbk(null, null, {market_data: {current_price: {usd: 12.34}}});
7
21
  };
8
22
 
9
23
  const tests = [
10
24
  {
11
- args: {},
25
+ args: makeArgs({currency: undefined}),
12
26
  description: 'A currency is required',
13
27
  error: [400, 'UnsupportedCurrencyForCoingeckoFiatRateLookup'],
14
28
  },
15
29
  {
16
- args: {currency: 'BTC'},
30
+ args: makeArgs({date: undefined}),
17
31
  description: 'A date is required',
18
32
  error: [400, 'ExpectedDateForCoingeckoRateLookup'],
19
33
  },
20
34
  {
21
- args: {currency: 'BTC', date: new Date().toISOString()},
22
- description: 'A currency type is required',
35
+ args: makeArgs({fiat: undefined}),
36
+ description: 'A fiat type is required',
23
37
  error: [400, 'UnsupportedFiatTypeForCoingeckoFiatRateLookup'],
24
38
  },
25
39
  {
26
- args: {currency: 'BTC', date: new Date().toISOString(), fiat: 'USD'},
40
+ args: makeArgs({rates: undefined}),
41
+ description: 'A rates object is required',
42
+ error: [400, 'ExpectedKnownRatesForCoingeckoFiatRateLookup'],
43
+ },
44
+ {
45
+ args: makeArgs({request: undefined}),
27
46
  description: 'A request method is required',
28
47
  error: [400, 'ExpectedRequestMethodForCoingeckoFiatRateLookup'],
29
48
  },
30
49
  {
31
- args: {
32
- date: new Date().toISOString(),
33
- currency: 'BTC',
34
- fiat: 'USD',
35
- request: ({}, cbk) => cbk('err'),
36
- },
50
+ args: makeArgs({request: ({}, cbk) => cbk('err')}),
37
51
  description: 'Errors returned from request',
38
52
  error: [503, 'UnexpectedErrGettingCoingeckoPastRate', {err: 'err'}],
39
53
  },
40
54
  {
41
- args: {
42
- date: new Date().toISOString(),
43
- currency: 'BTC',
44
- fiat: 'USD',
45
- request: ({}, cbk) => cbk(),
46
- },
55
+ args: makeArgs({request: ({}, cbk) => cbk()}),
47
56
  description: 'A body is expected in response',
48
57
  error: [503, 'UnexpectedResponseInCoingeckoPastRateResponse'],
49
58
  },
50
59
  {
51
- args: {
52
- date: new Date().toISOString(),
53
- currency: 'BTC',
54
- fiat: 'USD',
60
+ args: makeArgs({
55
61
  request: ({}, cbk) => cbk(null, null, {
56
62
  market_data: {current_price: {}},
57
63
  }),
58
- },
64
+ }),
59
65
  description: 'A price is expected in response',
60
66
  error: [503, 'ExpectedCoingeckoCurrentPriceForFiat'],
61
67
  },
62
68
  {
63
- args: {
64
- date: new Date().toISOString(),
65
- currency: 'BTC',
66
- fiat: 'USD',
67
- request: ({qs}, cbk) => api({qs}, cbk),
68
- },
69
- description: 'Get coindesk historic rate',
69
+ args: makeArgs({}),
70
+ description: 'Get coingecko historic rate',
70
71
  expected: {cents: 1234},
71
72
  },
72
73
  ];
@@ -8,45 +8,53 @@ const api = ({}, cbk) => {
8
8
  return cbk(null, null, {market_data: {current_price: {usd: 12.34}}});
9
9
  };
10
10
 
11
+ const makeArgs = overrides => {
12
+ const args = {
13
+ date: new Date().toISOString(),
14
+ currency: 'BTC',
15
+ fiat: 'USD',
16
+ rates: {},
17
+ request: ({qs}, cbk) => api({qs}, cbk),
18
+ };
19
+
20
+ Object.keys(overrides).forEach(k => args[k] = overrides[k]);
21
+
22
+ return args;
23
+ };
24
+
11
25
  const tests = [
12
26
  {
13
- args: {},
27
+ args: makeArgs({currency: undefined}),
14
28
  description: 'A currency code is required',
15
29
  error: [400, 'ExpectedCurrencyToGetHistoricRate'],
16
30
  },
17
31
  {
18
- args: {currency: 'BTC'},
32
+ args: makeArgs({date: undefined}),
19
33
  description: 'A date is required',
20
34
  error: [400, 'ExpectedDateToGetHistoricRate'],
21
35
  },
22
36
  {
23
- args: {date, currency: 'BTC'},
37
+ args: makeArgs({fiat: undefined}),
24
38
  description: 'A fiat type is required to get historic rate',
25
39
  error: [400, 'ExpectedFiatToGetHistoricRate'],
26
40
  },
27
41
  {
28
- args: {date, currency: 'BTC', fiat: 'USD'},
42
+ args: makeArgs({rates: undefined}),
43
+ description: 'Past rates are required to get historic rate',
44
+ error: [400, 'ExpectedRatesToGetHistoricRate'],
45
+ },
46
+ {
47
+ args: makeArgs({request: undefined}),
29
48
  description: 'A request function is required to get historic rate',
30
49
  error: [400, 'ExpectedRequestFunctionToGetHistoricRate'],
31
50
  },
32
51
  {
33
- args: {
34
- date,
35
- currency: 'BTC',
36
- fiat: 'USD',
37
- provider: 'provider',
38
- request: () => {},
39
- },
40
- description: 'A request function is required to get historic rate',
52
+ args: makeArgs({provider: 'provider'}),
53
+ description: 'A known rate provider is required to get historic rate',
41
54
  error: [400, 'ExpectedKnownRateProviderToGetHistoricRate'],
42
55
  },
43
56
  {
44
- args: {
45
- date,
46
- currency: 'BTC',
47
- fiat: 'USD',
48
- request: ({qs}, cbk) => api({qs}, cbk),
49
- },
57
+ args: makeArgs({}),
50
58
  description: 'Get historic fiat rates',
51
59
  expected: {cents: 1234},
52
60
  },
@@ -46,6 +46,7 @@ const tests = [
46
46
  r_preimage: Buffer.alloc(32),
47
47
  settle_date: 1,
48
48
  settled: true,
49
+ state: 'SETTLED',
49
50
  value: '1',
50
51
  value_msat: '1000',
51
52
  }],
@@ -58,19 +58,34 @@ const tests = [
58
58
  },
59
59
  resolve_time_ns: '1000000',
60
60
  route: {
61
- hops: [{
62
- amt_to_forward_msat: '1000',
63
- chan_id: '1',
64
- chan_capacity: 1,
65
- expiry: 1,
66
- fee_msat: '1000',
67
- mpp_record: {
68
- payment_addr: Buffer.alloc(32),
69
- total_amt_msat: '1000',
61
+ hops: [
62
+ {
63
+ amt_to_forward_msat: '1000',
64
+ chan_id: '1',
65
+ chan_capacity: 1,
66
+ expiry: 1,
67
+ fee_msat: '1000',
68
+ mpp_record: {
69
+ payment_addr: Buffer.alloc(32),
70
+ total_amt_msat: '1000',
71
+ },
72
+ pub_key: Buffer.alloc(33).toString('hex'),
73
+ tlv_payload: true,
70
74
  },
71
- pub_key: Buffer.alloc(33).toString('hex'),
72
- tlv_payload: true,
73
- }],
75
+ {
76
+ amt_to_forward_msat: '1000',
77
+ chan_id: '1',
78
+ chan_capacity: 1,
79
+ expiry: 1,
80
+ fee_msat: '1000',
81
+ mpp_record: {
82
+ payment_addr: Buffer.alloc(32),
83
+ total_amt_msat: '1000',
84
+ },
85
+ pub_key: Buffer.alloc(33).toString('hex'),
86
+ tlv_payload: true,
87
+ },
88
+ ],
74
89
  total_amt: '1',
75
90
  total_amt_msat: '1000',
76
91
  total_fees: '1',
@@ -99,23 +114,37 @@ const tests = [
99
114
  expected: {
100
115
  payments: [{
101
116
  attempts: [{
102
- confirmed_at: '1970-01-01T00:00:00.001Z',
117
+ confirmed_at: undefined,
118
+ created_at: '1970-01-01T00:00:00.001Z',
119
+ failed_at: '1970-01-01T00:00:00.001Z',
103
120
  is_confirmed: false,
104
121
  is_failed: true,
105
122
  is_pending: false,
106
123
  route: {
107
124
  fee: 1,
108
125
  fee_mtokens: '1000',
109
- hops: [{
110
- channel: '0x0x1',
111
- channel_capacity: 1,
112
- fee: 1,
113
- fee_mtokens: '1000',
114
- forward: 1,
115
- forward_mtokens: '1000',
116
- public_key: Buffer.alloc(33).toString('hex'),
117
- timeout: 1,
118
- }],
126
+ hops: [
127
+ {
128
+ channel: '0x0x1',
129
+ channel_capacity: 1,
130
+ fee: 1,
131
+ fee_mtokens: '1000',
132
+ forward: 1,
133
+ forward_mtokens: '1000',
134
+ public_key: Buffer.alloc(33).toString('hex'),
135
+ timeout: 1,
136
+ },
137
+ {
138
+ channel: '0x0x1',
139
+ channel_capacity: 1,
140
+ fee: 1,
141
+ fee_mtokens: '1000',
142
+ forward: 1,
143
+ forward_mtokens: '1000',
144
+ public_key: Buffer.alloc(33).toString('hex'),
145
+ timeout: 1,
146
+ },
147
+ ],
119
148
  mtokens: '1000',
120
149
  payment: Buffer.alloc(32).toString('hex'),
121
150
  timeout: 1,