lighstorm 0.0.6 → 0.0.7

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.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/README.md +17 -55
  4. data/adapters/connections/channel_node/fee.rb +1 -1
  5. data/adapters/connections/channel_node/policy.rb +6 -6
  6. data/adapters/connections/channel_node.rb +1 -1
  7. data/adapters/connections/payment_channel.rb +2 -2
  8. data/adapters/edges/channel.rb +7 -7
  9. data/adapters/edges/forward.rb +3 -3
  10. data/adapters/edges/payment.rb +1 -1
  11. data/adapters/invoice.rb +15 -0
  12. data/adapters/payment_request.rb +14 -3
  13. data/components/cache.rb +8 -5
  14. data/controllers/action.rb +24 -0
  15. data/controllers/channel/actions/apply_gossip.rb +18 -18
  16. data/controllers/channel/actions/update_fee.rb +33 -22
  17. data/controllers/forward/group_by_channel.rb +3 -3
  18. data/controllers/invoice/actions/create.rb +39 -12
  19. data/controllers/invoice/actions/pay_through_route.rb +1 -1
  20. data/controllers/invoice.rb +3 -3
  21. data/docs/README.md +390 -145
  22. data/docs/_coverpage.md +6 -1
  23. data/docs/index.html +1 -1
  24. data/models/connections/channel_node/accounting.rb +1 -1
  25. data/models/connections/channel_node/fee.rb +5 -4
  26. data/models/connections/channel_node/htlc.rb +4 -4
  27. data/models/connections/forward_channel.rb +1 -1
  28. data/models/connections/payment_channel.rb +4 -4
  29. data/models/edges/channel/accounting.rb +5 -5
  30. data/models/edges/forward.rb +3 -3
  31. data/models/edges/groups/channel_forwards/analysis.rb +8 -8
  32. data/models/edges/payment.rb +3 -3
  33. data/models/errors.rb +1 -1
  34. data/models/payment_request.rb +2 -2
  35. data/models/satoshis.rb +12 -12
  36. data/static/cache.rb +2 -0
  37. data/static/spec.rb +1 -1
  38. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bea078c4355c1f245ed7800a6d235763e325d3c4cf71e42f14589513eb69a429
4
- data.tar.gz: 026c1c57b2dc8a6d06cebc2155f32189b7d998574d1e802b8e64f90ae63938ae
3
+ metadata.gz: e3a0597ad2e13c24bcf64efd0e12e2b6c8ae0c7b6b012b6fab89caffd2ac06a9
4
+ data.tar.gz: 01fa72d76dcdb97c62a1edd5f8829d8038baba9639de245e75059bbf6309d199
5
5
  SHA512:
6
- metadata.gz: d69e4361ef42951fa5d316a476ce7f2ac63b9ee307198646a2b263601ec0e79d75792b99985767f61a38942401a4e3d24d8ff5b938b7e4fddcb8d427b566d2b4
7
- data.tar.gz: 85de98976d458ba9279291f37ae4cda03c8aa3965a53751932360a546b406004915d5035666bf6b45f624e1eaa8c0e2d2b8b135632cdee3b8c622b83e94b6631
6
+ metadata.gz: e7e7d996345446669475259095ad4843783ec21083094e3acbb883fbde421b57efec0d1ca5a495526ee6e1c536920003a3472db21eb6ee2394218bcf07fc235e
7
+ data.tar.gz: 8e4b80f814feff68293a6db0ecdc9346640d19e3f761d16fafa5c0f1dcfbea978208dca9c6e186834ef9b28bac0b296f37d5b3aeba5a0b05f4cac06e38bcbc4f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lighstorm (0.0.6)
4
+ lighstorm (0.0.7)
5
5
  dotenv (~> 2.8, >= 2.8.1)
6
6
  lnd-client (~> 0.0.5)
7
7
  zache (~> 0.12.0)
data/README.md CHANGED
@@ -15,25 +15,26 @@ Lighstorm::Channel.mine.first.myself.node.alias
15
15
  - [About](#about)
16
16
  - [Usage](#usage)
17
17
  - [Documentation](https://icebaker.github.io/lighstorm)
18
- - [Development](#development)
19
- - [Testing](#testing)
20
- - [Generating Documentation](#generating-documentation)
21
- - [Publish to RubyGems](#publish-to-rubygems)
18
+ - [Development](https://icebaker.github.io/lighstorm/#/README?id=development)
22
19
 
23
20
  ## About
24
21
 
25
22
  _Lighstorm_ is an opinionated abstraction layer on top of the [lnd-client](https://github.com/icebaker/lnd-client).
26
23
 
27
- It brings an [object-oriented](https://en.wikipedia.org/wiki/Object-oriented_programming) approach for interacting with a [Lightning Node](https://github.com/lightningnetwork/lnd), influenced by the [Active Record Pattern](https://www.martinfowler.com/eaaCatalog/activeRecord.html) and [Active Record Models](https://guides.rubyonrails.org/active_record_basics.html) conventions.
24
+ It brings an [_object-oriented_](https://en.wikipedia.org/wiki/Object-oriented_programming) approach for interacting with a [Lightning Node](https://github.com/lightningnetwork/lnd), influenced by the [Active Record Pattern](https://www.martinfowler.com/eaaCatalog/activeRecord.html) and [Active Record Models](https://guides.rubyonrails.org/active_record_basics.html) conventions.
28
25
 
29
- Although it tries to stay close to [Lightning's terminologies](https://docs.lightning.engineering/lightning-network-tools/lnd), it brings its own vocabulary and [data modeling](https://icebaker.github.io/lighstorm/#/README?id=data-modeling), optimizing for [programmer happiness](https://rubyonrails.org/doctrine).
26
+ However, despite the fluidity of _Object Orientation_ being desired in its public interface, internally, most of its code is structured following the [_Hexagonal Architecture_](https://en.wikipedia.org/wiki/Hexagonal_architecture_(software)) and [_Functional Programming_](https://en.wikipedia.org/wiki/Functional_programming) principles.
27
+
28
+ It aims to be intuitive to use while being highly **reliable**, as it deals with people's money, and easily testable since its [tests](https://icebaker.github.io/lighstorm/#/README?id=testing) are the foundation for its reliability.
29
+
30
+ Although it tries to stay close to [Lightning's terminologies](https://docs.lightning.engineering/the-lightning-network/overview), it brings its own vocabulary and [data modeling](https://icebaker.github.io/lighstorm/#/README?id=data-modeling), optimizing for [programmer happiness](https://rubyonrails.org/doctrine#optimize-for-programmer-happiness).
30
31
 
31
32
  ## Usage
32
33
 
33
34
  Add to your `Gemfile`:
34
35
 
35
36
  ```ruby
36
- gem 'lighstorm', '~> 0.0.6'
37
+ gem 'lighstorm', '~> 0.0.7'
37
38
  ```
38
39
 
39
40
  ```ruby
@@ -45,60 +46,21 @@ Lighstorm.config!(
45
46
  macaroon_path: '/lnd/data/chain/bitcoin/mainnet/admin.macaroon',
46
47
  )
47
48
 
48
- puts Lighstorm.version # => 0.0.6
49
+ puts Lighstorm.version # => 0.0.7
50
+
51
+ Lighstorm::Node.myself.alias # => icebaker/old-stone
52
+
53
+ Lighstorm::Invoice.create(
54
+ description: 'Coffee', millisatoshis: 1_000
55
+ )
49
56
 
50
57
  Lighstorm::Satoshis.new(
51
- milisatoshis: 75_621_650
58
+ millisatoshis: 75_621_650
52
59
  ).satoshis # => 75_621
53
-
54
- Lighstorm::Node.myself.alias # => icebaker/old-stone
55
60
  ```
56
61
 
57
62
  Check the [full documentation](https://icebaker.github.io/lighstorm).
58
63
 
59
64
  ## Development
60
65
 
61
- Copy the `.env.example` file to `.env` and provide the required data.
62
-
63
- ```ruby
64
- # Gemfile
65
- gem 'lighstorm', path: '/home/user/lighstorm'
66
-
67
- # demo.rb
68
- require 'lighstorm'
69
-
70
- puts Lighstorm.version # => 0.0.6
71
- ```
72
-
73
- ```sh
74
- bundle
75
- rubocop -A
76
- ```
77
-
78
- ### Testing
79
-
80
- Copy the `.env.example` file to `.env` and provide the required data.
81
-
82
- ```
83
- bundle
84
-
85
- bundle exec rspec
86
- ```
87
-
88
- ### Generating Documentation
89
-
90
- ```sh
91
- npm i docsify-cli -g
92
-
93
- docsify serve ./docs
94
- ```
95
-
96
- ### Publish to RubyGems
97
-
98
- ```sh
99
- gem build lighstorm.gemspec
100
-
101
- gem signin
102
-
103
- gem push lighstorm-0.0.6.gem
104
- ```
66
+ Check the [development documentation](https://icebaker.github.io/lighstorm/#/README?id=development).
@@ -11,7 +11,7 @@ module Lighstorm
11
11
  policy: {
12
12
  fee: {
13
13
  base: {
14
- milisatoshis: grpc[:base_fee_msat]
14
+ millisatoshis: grpc[:base_fee_msat]
15
15
  },
16
16
  rate: {
17
17
  parts_per_million: grpc[:fee_per_mil]
@@ -6,12 +6,12 @@ module Lighstorm
6
6
  def self.get_chan_info(grpc)
7
7
  {
8
8
  fee: {
9
- base: { milisatoshis: grpc[:fee_base_msat] },
9
+ base: { millisatoshis: grpc[:fee_base_msat] },
10
10
  rate: { parts_per_million: grpc[:fee_rate_milli_msat] }
11
11
  },
12
12
  htlc: {
13
- minimum: { milisatoshis: grpc[:min_htlc] },
14
- maximum: { milisatoshis: grpc[:max_htlc_msat] },
13
+ minimum: { millisatoshis: grpc[:min_htlc] },
14
+ maximum: { millisatoshis: grpc[:max_htlc_msat] },
15
15
  # https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#cltv_expiry_delta-selection
16
16
  blocks: {
17
17
  delta: {
@@ -26,12 +26,12 @@ module Lighstorm
26
26
  result = {
27
27
  _source: :subscribe_channel_graph,
28
28
  fee: {
29
- base: { milisatoshis: json['routingPolicy']['feeBaseMsat'].to_i },
29
+ base: { millisatoshis: json['routingPolicy']['feeBaseMsat'].to_i },
30
30
  rate: { parts_per_million: json['routingPolicy']['feeRateMilliMsat'].to_i }
31
31
  },
32
32
  htlc: {
33
- minimum: { milisatoshis: json['routingPolicy']['minHtlc'].to_i },
34
- maximum: { milisatoshis: json['routingPolicy']['maxHtlcMsat'].to_i },
33
+ minimum: { millisatoshis: json['routingPolicy']['minHtlc'].to_i },
34
+ maximum: { millisatoshis: json['routingPolicy']['maxHtlcMsat'].to_i },
35
35
  # https://github.com/lightning/bolts/blob/master/02-peer-protocol.md#cltv_expiry_delta-selection
36
36
  blocks: {
37
37
  delta: {
@@ -10,7 +10,7 @@ module Lighstorm
10
10
  data = {
11
11
  _source: :list_channels,
12
12
  state: grpc[:active] ? 'active' : 'inactive',
13
- accounting: { balance: { milisatoshis: grpc[:"#{key}_balance"] * 1000 } },
13
+ accounting: { balance: { millisatoshis: grpc[:"#{key}_balance"] * 1000 } },
14
14
  node: Node.list_channels(grpc, key)
15
15
  }
16
16
 
@@ -9,8 +9,8 @@ module Lighstorm
9
9
  {
10
10
  _source: :list_payments,
11
11
  hop: index + 1,
12
- amount: { milisatoshis: grpc[:amt_to_forward_msat] },
13
- fee: { milisatoshis: grpc[:fee_msat] },
12
+ amount: { millisatoshis: grpc[:amt_to_forward_msat] },
13
+ fee: { millisatoshis: grpc[:fee_msat] },
14
14
  channel: {
15
15
  _key: Digest::SHA256.hexdigest(grpc[:chan_id].to_s),
16
16
  id: grpc[:chan_id].to_s,
@@ -30,10 +30,10 @@ module Lighstorm
30
30
  state: grpc[:active] ? 'active' : 'inactive',
31
31
  exposure: grpc[:private] ? 'private' : 'public',
32
32
  accounting: {
33
- capacity: { milisatoshis: grpc[:capacity] * 1000 },
34
- sent: { milisatoshis: grpc[:total_satoshis_sent] * 1000 },
35
- received: { milisatoshis: grpc[:total_satoshis_received] * 1000 },
36
- unsettled: { milisatoshis: grpc[:unsettled_balance] * 1000 }
33
+ capacity: { millisatoshis: grpc[:capacity] * 1000 },
34
+ sent: { millisatoshis: grpc[:total_satoshis_sent] * 1000 },
35
+ received: { millisatoshis: grpc[:total_satoshis_received] * 1000 },
36
+ unsettled: { millisatoshis: grpc[:unsettled_balance] * 1000 }
37
37
  },
38
38
  partners: [
39
39
  ChannelNode.list_channels(grpc, :local),
@@ -49,7 +49,7 @@ module Lighstorm
49
49
  # Standard JSON don't support BigInt, so, a String is safer.
50
50
  id: grpc[:channel_id].to_s,
51
51
  accounting: {
52
- capacity: { milisatoshis: grpc[:capacity] * 1000 }
52
+ capacity: { millisatoshis: grpc[:capacity] * 1000 }
53
53
  },
54
54
  partners: [
55
55
  ChannelNode.get_chan_info(grpc, 1),
@@ -66,7 +66,7 @@ module Lighstorm
66
66
  id: grpc[:channel_id].to_s,
67
67
  exposure: 'public',
68
68
  accounting: {
69
- capacity: { milisatoshis: grpc[:capacity] * 1000 }
69
+ capacity: { millisatoshis: grpc[:capacity] * 1000 }
70
70
  },
71
71
  partners: [
72
72
  ChannelNode.describe_graph(grpc, 1),
@@ -81,7 +81,7 @@ module Lighstorm
81
81
  _key: _key(json['chanId'], nil),
82
82
  id: json['chanId'],
83
83
  accounting: {
84
- capacity: { milisatoshis: json['capacity'].to_i * 1000 }
84
+ capacity: { millisatoshis: json['capacity'].to_i * 1000 }
85
85
  },
86
86
  partners: [
87
87
  ChannelNode.subscribe_channel_graph(json),
@@ -10,15 +10,15 @@ module Lighstorm
10
10
  _source: :forwarding_history,
11
11
  _key: _key(grpc),
12
12
  at: Time.at(grpc[:timestamp_ns] / 1e+9),
13
- fee: { milisatoshis: grpc[:fee_msat] },
13
+ fee: { millisatoshis: grpc[:fee_msat] },
14
14
  in: {
15
- amount: { milisatoshis: grpc[:amt_in_msat] },
15
+ amount: { millisatoshis: grpc[:amt_in_msat] },
16
16
  channel: {
17
17
  id: grpc[:chan_id_in].to_s
18
18
  }
19
19
  },
20
20
  out: {
21
- amount: { milisatoshis: grpc[:amt_out_msat] },
21
+ amount: { millisatoshis: grpc[:amt_out_msat] },
22
22
  channel: {
23
23
  id: grpc[:chan_id_out].to_s
24
24
  }
@@ -31,7 +31,7 @@ module Lighstorm
31
31
  _key: _key(grpc),
32
32
  created_at: Time.at(grpc[:creation_time_ns] / 1e+9),
33
33
  status: grpc[:status].to_s.downcase,
34
- fee: { milisatoshis: grpc[:fee_msat] },
34
+ fee: { millisatoshis: grpc[:fee_msat] },
35
35
  purpose: Purpose.list_payments(grpc, node_get_info),
36
36
  request: PaymentRequest.list_payments(grpc),
37
37
  hops: grpc[:htlcs].first[:route][:hops].map.with_index do |raw_hop, i|
data/adapters/invoice.rb CHANGED
@@ -7,6 +7,21 @@ require_relative 'payment_request'
7
7
  module Lighstorm
8
8
  module Adapter
9
9
  class Invoice
10
+ def self.add_invoice(grpc)
11
+ {
12
+ _source: :add_invoice,
13
+ _key: Digest::SHA256.hexdigest(
14
+ [
15
+ grpc[:r_hash],
16
+ grpc[:add_index],
17
+ grpc[:payment_request],
18
+ grpc[:payment_addr]
19
+ ].join('/')
20
+ ),
21
+ request: PaymentRequest.add_invoice(grpc)
22
+ }
23
+ end
24
+
10
25
  def self.lookup_invoice(grpc)
11
26
  adapted = list_or_lookup(grpc)
12
27
 
@@ -7,10 +7,21 @@ require_relative '../ports/dsl/lighstorm/errors'
7
7
  module Lighstorm
8
8
  module Adapter
9
9
  class PaymentRequest
10
+ def self.add_invoice(grpc)
11
+ {
12
+ _source: :add_invoice,
13
+ code: grpc[:payment_request],
14
+ address: grpc[:payment_addr].unpack1('H*'),
15
+ secret: {
16
+ hash: grpc[:r_hash].unpack1('H*')
17
+ }
18
+ }
19
+ end
20
+
10
21
  def self.decode_pay_req(grpc)
11
22
  {
12
23
  _source: :decode_pay_req,
13
- amount: { milisatoshis: grpc[:num_msat] },
24
+ amount: { millisatoshis: grpc[:num_msat] },
14
25
  description: {
15
26
  memo: grpc[:description],
16
27
  hash: grpc[:description_hash] == '' ? nil : grpc[:description_hash]
@@ -37,7 +48,7 @@ module Lighstorm
37
48
  def self.list_or_lookup_invoice(grpc)
38
49
  {
39
50
  code: grpc[:payment_request],
40
- amount: { milisatoshis: grpc[:value_msat] },
51
+ amount: { millisatoshis: grpc[:value_msat] },
41
52
  description: {
42
53
  memo: grpc[:memo],
43
54
  hash: grpc[:description_hash] == '' ? nil : grpc[:description_hash]
@@ -56,7 +67,7 @@ module Lighstorm
56
67
  data = {
57
68
  _source: :list_payments,
58
69
  code: grpc[:payment_request],
59
- amount: { milisatoshis: grpc[:value_msat] },
70
+ amount: { millisatoshis: grpc[:value_msat] },
60
71
  secret: {
61
72
  preimage: grpc[:payment_preimage],
62
73
  hash: grpc[:payment_hash]
data/components/cache.rb CHANGED
@@ -24,15 +24,18 @@ module Lighstorm
24
24
  def for(key, ttl: nil, params: {}, &block)
25
25
  if ttl.nil?
26
26
  ttl = Lighstorm::Static::CACHE[key.sub('lightning.', '').to_sym]
27
- raise MissingTTLError, "missing ttl for #{key} static/cache.rb" if ttl.nil?
27
+ raise MissingTTLError, "missing ttl for #{key.sub('lightning.', '')} static/cache.rb" if ttl.nil?
28
28
 
29
- ttl = ttl[:ttl]
29
+ ttl = ttl == false ? false : ttl[:ttl]
30
30
  end
31
31
 
32
- key = build_key_for(key, params)
33
-
34
- @client.get(key, lifetime: ttl) do
32
+ if ttl == false
35
33
  block.call
34
+ else
35
+ key = build_key_for(key, params)
36
+ @client.get(key, lifetime: ttl) do
37
+ block.call
38
+ end
36
39
  end
37
40
  end
38
41
 
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lighstorm
4
+ module Controllers
5
+ module Action
6
+ Output = Struct.new(:data) do
7
+ def response
8
+ data[:response]
9
+ end
10
+
11
+ def result
12
+ data[:result]
13
+ end
14
+
15
+ def to_h
16
+ {
17
+ response: response,
18
+ result: result.to_h
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -25,17 +25,17 @@ module Lighstorm
25
25
  ].freeze
26
26
 
27
27
  APPLICABLE = [
28
- 'accounting/capacity/milisatoshis',
29
- 'partners/0/policy/fee/base/milisatoshis',
28
+ 'accounting/capacity/millisatoshis',
29
+ 'partners/0/policy/fee/base/millisatoshis',
30
30
  'partners/0/state',
31
- 'partners/1/policy/fee/base/milisatoshis',
31
+ 'partners/1/policy/fee/base/millisatoshis',
32
32
  'partners/1/state',
33
33
  'partners/1/policy/fee/rate/parts_per_million',
34
34
  'partners/0/policy/fee/rate/parts_per_million',
35
- 'partners/0/policy/htlc/minimum/milisatoshis',
36
- 'partners/1/policy/htlc/minimum/milisatoshis',
37
- 'partners/0/policy/htlc/maximum/milisatoshis',
38
- 'partners/1/policy/htlc/maximum/milisatoshis',
35
+ 'partners/0/policy/htlc/minimum/millisatoshis',
36
+ 'partners/1/policy/htlc/minimum/millisatoshis',
37
+ 'partners/0/policy/htlc/maximum/millisatoshis',
38
+ 'partners/1/policy/htlc/maximum/millisatoshis',
39
39
  'partners/0/policy/htlc/blocks/delta/minimum',
40
40
  'partners/1/policy/htlc/blocks/delta/minimum'
41
41
  ].freeze
@@ -79,25 +79,25 @@ module Lighstorm
79
79
 
80
80
  def self.apply!(actual, key, change)
81
81
  case key
82
- when 'accounting/capacity/milisatoshis'
82
+ when 'accounting/capacity/millisatoshis'
83
83
  token = SecureRandom.hex
84
84
  actual.accounting.prepare_token!(token)
85
85
  actual.accounting.capacity = {
86
- value: Models::Satoshis.new(milisatoshis: change[:to]),
86
+ value: Models::Satoshis.new(millisatoshis: change[:to]),
87
87
  token: token
88
88
  }
89
- when 'partners/0/policy/htlc/maximum/milisatoshis',
90
- 'partners/1/policy/htlc/maximum/milisatoshis' then
89
+ when 'partners/0/policy/htlc/maximum/millisatoshis',
90
+ 'partners/1/policy/htlc/maximum/millisatoshis' then
91
91
  policy = actual.partners[change[:path][1]].policy
92
92
 
93
93
  token = SecureRandom.hex
94
94
  policy.htlc.prepare_token!(token)
95
95
  policy.htlc.maximum = {
96
- value: Models::Satoshis.new(milisatoshis: change[:to]),
96
+ value: Models::Satoshis.new(millisatoshis: change[:to]),
97
97
  token: token
98
98
  }
99
- when 'partners/0/policy/htlc/minimum/milisatoshis',
100
- 'partners/1/policy/htlc/minimum/milisatoshis' then
99
+ when 'partners/0/policy/htlc/minimum/millisatoshis',
100
+ 'partners/1/policy/htlc/minimum/millisatoshis' then
101
101
  if actual.partners[change[:path][1]].policy.nil?
102
102
  actual.partners[change[:path][1]].policy = Lighstorm::Models::Policy.new({})
103
103
  end
@@ -107,7 +107,7 @@ module Lighstorm
107
107
  token = SecureRandom.hex
108
108
  policy.htlc.prepare_token!(token)
109
109
  policy.htlc.minimum = {
110
- value: Models::Satoshis.new(milisatoshis: change[:to]),
110
+ value: Models::Satoshis.new(millisatoshis: change[:to]),
111
111
  token: token
112
112
  }
113
113
  when 'partners/0/policy/htlc/blocks/delta/minimum',
@@ -134,14 +134,14 @@ module Lighstorm
134
134
  value: Models::Rate.new(parts_per_million: change[:to]),
135
135
  token: token
136
136
  }
137
- when 'partners/0/policy/fee/base/milisatoshis',
138
- 'partners/1/policy/fee/base/milisatoshis' then
137
+ when 'partners/0/policy/fee/base/millisatoshis',
138
+ 'partners/1/policy/fee/base/millisatoshis' then
139
139
  policy = actual.partners[change[:path][1]].policy
140
140
 
141
141
  token = SecureRandom.hex
142
142
  policy.fee.prepare_token!(token)
143
143
  policy.fee.base = {
144
- value: Models::Satoshis.new(milisatoshis: change[:to]),
144
+ value: Models::Satoshis.new(millisatoshis: change[:to]),
145
145
  token: token
146
146
  }
147
147
  when 'partners/0/state',
@@ -4,25 +4,26 @@ require_relative '../../../ports/grpc'
4
4
  require_relative '../../../models/errors'
5
5
  require_relative '../../../models/satoshis'
6
6
  require_relative '../../../models/rate'
7
+ require_relative '../../action'
7
8
 
8
9
  module Lighstorm
9
10
  module Controllers
10
11
  module Channel
11
12
  module UpdateFee
12
- def self.perform(policy, params, preview: false, fake: false)
13
+ def self.prepare(policy, transaction, params)
13
14
  grpc_request = {
14
15
  service: :lightning,
15
16
  method: :update_channel_policy,
16
17
  params: {
17
18
  chan_point: {
18
- funding_txid_str: policy.transaction.funding.id,
19
- output_index: policy.transaction.funding.index
19
+ funding_txid_str: transaction[:funding][:id],
20
+ output_index: transaction[:funding][:index]
20
21
  },
21
- base_fee_msat: policy.fee.base.milisatoshis,
22
- fee_rate_ppm: policy.fee.rate.parts_per_million,
23
- time_lock_delta: policy.htlc.blocks.delta.minimum,
24
- max_htlc_msat: policy.htlc.maximum.milisatoshis,
25
- min_htlc_msat: policy.htlc.minimum.milisatoshis
22
+ base_fee_msat: policy[:fee][:base][:millisatoshis],
23
+ fee_rate_ppm: policy[:fee][:rate][:parts_per_million],
24
+ time_lock_delta: policy[:htlc][:blocks][:delta][:minimum],
25
+ max_htlc_msat: policy[:htlc][:maximum][:millisatoshis],
26
+ min_htlc_msat: policy[:htlc][:minimum][:millisatoshis]
26
27
  }
27
28
  }
28
29
 
@@ -34,30 +35,40 @@ module Lighstorm
34
35
  grpc_request[:params][:fee_rate_ppm] = params[:rate][:parts_per_million]
35
36
  end
36
37
 
37
- if params[:base] && params[:base][:milisatoshis]
38
- if (params[:base][:milisatoshis]).negative?
39
- raise Errors::NegativeNotAllowedError, "fee base can't be negative: #{params[:base][:milisatoshis]}"
38
+ if params[:base] && params[:base][:millisatoshis]
39
+ if (params[:base][:millisatoshis]).negative?
40
+ raise Errors::NegativeNotAllowedError, "fee base can't be negative: #{params[:base][:millisatoshis]}"
40
41
  end
41
42
 
42
- grpc_request[:params][:base_fee_msat] = params[:base][:milisatoshis]
43
+ grpc_request[:params][:base_fee_msat] = params[:base][:millisatoshis]
43
44
  end
44
45
 
46
+ grpc_request
47
+ end
48
+
49
+ def self.call(grpc_request)
50
+ Lighstorm::Ports::GRPC.send(grpc_request[:service]).send(
51
+ grpc_request[:method], grpc_request[:params]
52
+ ).to_h
53
+ end
54
+
55
+ def self.dispatch(grpc_request, &vcr)
56
+ vcr.nil? ? call(grpc_request) : vcr.call(-> { call(grpc_request) }, :dispatch)
57
+ end
58
+
59
+ def self.perform(policy, transaction, params, preview: false, &vcr)
60
+ grpc_request = prepare(policy.to_h, transaction.to_h, params)
61
+
45
62
  return grpc_request if preview
46
63
 
47
- response = if fake
48
- :fake
49
- else
50
- LND.instance.middleware("lightning.#{grpc_request[:method]}") do
51
- LND.instance.client.lightning.send(grpc_request[:method], grpc_request[:params])
52
- end
53
- end
64
+ response = dispatch(grpc_request, &vcr)
54
65
 
55
- raise UpdateChannelPolicyError.new(nil, response) unless fake || response.failed_updates.empty?
66
+ raise UpdateChannelPolicyError.new(nil, response) unless response[:failed_updates].empty?
56
67
 
57
68
  token = SecureRandom.hex
58
69
  policy.fee.prepare_token!(token)
59
70
  policy.fee.base = {
60
- value: Models::Satoshis.new(milisatoshis: grpc_request[:params][:base_fee_msat]),
71
+ value: Models::Satoshis.new(millisatoshis: grpc_request[:params][:base_fee_msat]),
61
72
  token: token
62
73
  }
63
74
 
@@ -68,7 +79,7 @@ module Lighstorm
68
79
  token: token
69
80
  }
70
81
 
71
- response
82
+ Action::Output.new({ response: response, result: policy })
72
83
  end
73
84
  end
74
85
  end
@@ -36,7 +36,7 @@ module Lighstorm
36
36
  last_at: nil,
37
37
  analysis: {
38
38
  count: 0,
39
- sums: { amount: { milisatoshis: 0 }, fee: { milisatoshis: 0 } }
39
+ sums: { amount: { millisatoshis: 0 }, fee: { millisatoshis: 0 } }
40
40
  },
41
41
  channel: nil
42
42
  }
@@ -47,8 +47,8 @@ module Lighstorm
47
47
  group[:channel] = forward[direction][:channel] if group[:channel].nil?
48
48
 
49
49
  group[:analysis][:count] += 1
50
- group[:analysis][:sums][:amount][:milisatoshis] += forward[:in][:amount][:milisatoshis]
51
- group[:analysis][:sums][:fee][:milisatoshis] += forward[:fee][:milisatoshis]
50
+ group[:analysis][:sums][:amount][:millisatoshis] += forward[:in][:amount][:millisatoshis]
51
+ group[:analysis][:sums][:fee][:millisatoshis] += forward[:fee][:millisatoshis]
52
52
  end
53
53
 
54
54
  group[:_key] = _key(group)
@@ -3,33 +3,60 @@
3
3
  require_relative '../../../ports/grpc'
4
4
  require_relative '../../../models/errors'
5
5
  require_relative '../../invoice'
6
+ require_relative '../../action'
6
7
 
7
8
  module Lighstorm
8
9
  module Controllers
9
10
  module Invoice
10
11
  module Create
11
- def self.perform(description: nil, milisatoshis: nil, preview: false, fake: nil)
12
- grpc_request = {
12
+ def self.call(grpc_request)
13
+ Lighstorm::Ports::GRPC.send(grpc_request[:service]).send(
14
+ grpc_request[:method], grpc_request[:params]
15
+ ).to_h
16
+ end
17
+
18
+ def self.prepare(description: nil, millisatoshis: nil)
19
+ {
13
20
  service: :lightning,
14
21
  method: :add_invoice,
15
22
  params: {
16
23
  memo: description,
17
- value_msat: milisatoshis
24
+ value_msat: millisatoshis
18
25
  }
19
26
  }
27
+ end
28
+
29
+ def self.dispatch(grpc_request, &vcr)
30
+ vcr.nil? ? call(grpc_request) : vcr.call(-> { call(grpc_request) }, :dispatch)
31
+ end
32
+
33
+ def self.adapt(response)
34
+ Lighstorm::Adapter::Invoice.add_invoice(response)
35
+ end
36
+
37
+ def self.fetch(adapted, &vcr)
38
+ FindBySecretHash.data(adapted[:request][:secret][:hash], &vcr)
39
+ end
40
+
41
+ def self.model(data)
42
+ FindBySecretHash.model(data)
43
+ end
44
+
45
+ def self.perform(description: nil, millisatoshis: nil, preview: false, &vcr)
46
+ grpc_request = prepare(
47
+ description: description, millisatoshis: millisatoshis
48
+ )
20
49
 
21
50
  return grpc_request if preview
22
51
 
23
- # expiry: Default is 86400 (24 hours).
24
- response = if fake.nil?
25
- LND.instance.middleware("lightning.#{grpc_request[:method]}") do
26
- LND.instance.client.lightning.send(grpc_request[:method], grpc_request[:params])
27
- end.to_h
28
- else
29
- fake
30
- end
52
+ response = dispatch(grpc_request, &vcr)
53
+
54
+ adapted = adapt(response)
55
+
56
+ data = fetch(adapted, &vcr)
57
+ model = self.model(data)
31
58
 
32
- Invoice.find_by_secret_hash(response[:r_hash].unpack1('H*'))
59
+ Action::Output.new({ response: response, result: model })
33
60
  end
34
61
  end
35
62
  end