lisk 0.2.0 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5ae0ec3ce26db828b0aae312d3caaa44a0c4dce7
4
- data.tar.gz: ea7aab7e631ce1324b4be92fd364392c7dbc1b5c
3
+ metadata.gz: 915e70fda8a22aecbe253bfc7af695ce480f0411
4
+ data.tar.gz: 4936943b9255d984bc0669f6056c2294742643fb
5
5
  SHA512:
6
- metadata.gz: 1602e278569adf67a0219260721333fd93218c4d8249f5c076647bf6719353f6a1e1004708f40c7ffff64a1138efbc696e750d8b798716f7123c2de95a1f1469
7
- data.tar.gz: ce32d974e5623ca732af30f88813ca5734c6e01be2097cf9791386f9b67321c856d6e7a06220a248be6e544090858e7c731598aba7966d1097f5e8bc444ac221
6
+ metadata.gz: e1bbda2e1c0b9e08207ad2986483815884b5d7d947d613b3c94b74240b7da8ede1981b8e5a0c8301323ff62abea1ac4ae1a6b86ffb39e61939dffb282748266c
7
+ data.tar.gz: fc4bba4a3049851f79df439beeb9cab8852c08ad6b8929878248560af50dffef3e7b1748ad770825b86b548f7529ffc027cf470e89be94cb63659415f79d35b9
data/bin/debug CHANGED
@@ -6,4 +6,5 @@ gem uninstall lisk
6
6
  rm pkg/*.gem
7
7
  git add -A
8
8
  bundle exec rake install
9
- ruby example.rb
9
+ ruby examples/status.rb
10
+ ruby examples/payout.rb
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'lisk'
4
+
5
+ # Try to connect a local Lisk client on test network.
6
+ # Warning: Think twice and test thoroughly before enabling this on main network!
7
+ client = Lisk::Client.new "127.0.0.1", 7000
8
+
9
+ # The pre-1.0.0 legacy API connected to the client.
10
+ legacy_api = Lisk::Legacy.new client
11
+
12
+ # Only proceed if the client is connected, active, and fully synchronized.
13
+ if legacy_api.ping
14
+
15
+ # Get the desired delegate by name.
16
+ delegate = legacy_api.delegates_get_by_name "4fryn"
17
+ p "Delegate #{delegate["username"]} is rank \##{delegate["rank"]} with #{delegate["approval"]}\% approval and #{delegate["productivity"]}\% productivity."
18
+
19
+ # Get a list of voters for a delegate.
20
+ deleagte_public_key = delegate["publicKey"]
21
+ deleagte_address = delegate["address"]
22
+ deleagte_voters = legacy_api.delegates_voters deleagte_public_key
23
+
24
+ # Get the total vote weight of our delegate
25
+ deleagte_total_weight = delegate["vote"].to_f
26
+
27
+ # Get the forging rewards from balance
28
+ payout_balance = legacy_api.accounts_get_balance deleagte_address
29
+ payout_balance = payout_balance["balance"].to_f
30
+
31
+ # Our pool is sharing 80%, and keeping 20%
32
+ pool_share = 0.80
33
+
34
+ # Filter out voters with out any Lisk
35
+ voter_threshold = 0
36
+
37
+ _debug_payout_sum = 0
38
+
39
+ # Iterate all voters
40
+ deleagte_voters.each do | voter |
41
+ voter_balance = voter["balance"].to_f
42
+
43
+ # Only handle voters with balance above threshold
44
+ if voter_balance > voter_threshold # and not voter["address"].eql? deleagte_address
45
+ voter_share = voter["balance"].to_f / deleagte_total_weight
46
+ payout = voter_share * payout_balance * pool_share
47
+ # @TODO: do some handling of dust amounts
48
+ # @TODO: do sanity checks and create transactions here ...
49
+ p "Sending #{payout / 1e8} LSK to voter #{voter["address"]}..."
50
+ _debug_payout_sum += payout
51
+ end
52
+ end
53
+
54
+ # @TODO: sending 20% delegate share to her private address
55
+ p "Sending #{payout_balance * 0.20 / 1e8} LSK to delegate private funds..."
56
+
57
+ # Should not fail
58
+ _debug_payout_sum += payout_balance * 0.20
59
+ _debug_payout_diff = payout_balance - _debug_payout_sum
60
+ fail if not _debug_payout_diff === 0
61
+
62
+
63
+ else
64
+ p 'Lisk node disconnected, inactive, or not fully synchronized ...'
65
+ end
66
+
67
+ #account = Lisk::Account.new("14524922419337843943L")
68
+ #client.get_address(account)
69
+ #block = Lisk::Block.new("11145685198263496703")
70
+ #delegate = Lisk::Delegate.new("lightcurve")
71
+ #client.get_address(delegate)
72
+ #client.get_voters(delegate)
73
+ #transaction = Lisk::Transaction.new("10153999325502978458")
@@ -6,22 +6,31 @@ require 'lisk'
6
6
  client = Lisk::Client.new
7
7
 
8
8
  # Configure host and port of the Lisk client
9
- client.configure "127.0.0.1", 8000
9
+ client = client.configure "127.0.0.1", 8000
10
10
 
11
11
  # Same as above, just in one line
12
12
  client = Lisk::Client.new "127.0.0.1", 8000
13
13
 
14
- if client.ping
15
- version = client.version
14
+ # The pre-1.0.0 legacy API connected to the client
15
+ legacy_api = Lisk::Legacy.new client
16
+
17
+ # Only proceed if the client is connected, active, and fully synchronized
18
+ if legacy_api.ping
19
+
20
+ # Lisk version API example
21
+ version = legacy_api.version
16
22
  p "Lisk node version #{version["version"]} build #{version["build"]}..."
17
23
 
18
- status = client.status
24
+ # Lisk node status API example
25
+ status = legacy_api.status
19
26
  p "Lisk node is connected: #{status["success"]}... Blockchain loaded: #{status["loaded"]}..."
20
27
 
21
- syncing = client.sync
28
+ # Lisk node syncing API example
29
+ syncing = legacy_api.sync
22
30
  p "Lisk node is syncing: #{syncing["syncing"]}... #{syncing["blocks"]} remaining blocks to latest block #{syncing["height"]}..."
23
31
 
24
- peers = client.peers
32
+ # Lisk node peers API example
33
+ peers = legacy_api.peers
25
34
  cond = 0
26
35
  disd = 0
27
36
  band = 0
@@ -37,16 +46,10 @@ if client.ping
37
46
  end
38
47
  p "Lisk node saw #{peers.count} peers... #{cond} connected, #{disd} disconnected, #{band} banned..."
39
48
 
40
- chain = client.chain
49
+ # Lisk blockchain API example
50
+ chain = legacy_api.chain
41
51
  p "Lisk chain latest block: #{chain["height"]}... total supply: #{chain["supply"] / 1e8}... block reward: #{chain["reward"] / 1e8}"
52
+
42
53
  else
43
- p 'Lisk node disconnected or inactive ...'
54
+ p 'Lisk node disconnected, inactive, or not fully synchronized ...'
44
55
  end
45
-
46
- #account = Lisk::Account.new("14524922419337843943L")
47
- #client.get_address(account)
48
- #block = Lisk::Block.new("11145685198263496703")
49
- #delegate = Lisk::Delegate.new("lightcurve")
50
- #client.get_address(delegate)
51
- #client.get_voters(delegate)
52
- #transaction = Lisk::Transaction.new("10153999325502978458")
data/lib/lisk.rb CHANGED
@@ -1,10 +1,12 @@
1
1
  require "lisk/version"
2
2
  require "todonotes"
3
3
 
4
+ # The Lisk API Ruby wrapper gem.
4
5
  module Lisk
5
6
  require "lisk/client"
7
+ require "lisk/legacy"
6
8
 
7
- # Unimplemented methods
9
+ # Handles unimplemented methods
8
10
  def self.method_missing
9
11
  todo "#{self}::#{__method__} METHOD MISSING"
10
12
  end
data/lib/lisk/client.rb CHANGED
@@ -2,69 +2,116 @@ require "net/http"
2
2
  require "uri"
3
3
  require "json"
4
4
 
5
+ # The Lisk API Ruby wrapper gem.
5
6
  module Lisk
7
+
8
+ # A simple HTTP client connecting to a Lisk Core API node.
6
9
  class Client
7
10
 
8
- attr_accessor :host, :port
11
+ # Host and port of the API endpoint.
12
+ attr_accessor :host, :port, :ssl
9
13
 
14
+ # Initializes the Lisk HTTP client and defaults to localhost port 8000.
10
15
  def initialize host = "127.0.0.1", port = 8000
11
16
  @host = host
12
17
  @port = port
18
+ @ssl = false
19
+ if self.is_alive?
20
+ return self
21
+ else
22
+ return nil
23
+ end
13
24
  end
14
25
 
26
+ # Allows reconfiguring of the Lisk HTTP client's host and port.
15
27
  def configure host, port
16
28
  if not host.empty? or not port.empty?
17
29
  @host = host
18
30
  @port = port
31
+ @ssl = false
32
+ if self.is_alive?
33
+ return self
34
+ else
35
+ return nil
36
+ end
37
+ else
38
+ return nil
19
39
  end
20
40
  end
21
41
 
22
- def query endpoint
23
- node = ::Net::HTTP.new @host, @port
24
- uri = URI.parse "http://#{host}:#{port}/api/#{endpoint}"
25
- request = ::Net::HTTP::Get.new uri
26
- response = node.request request
27
- result = JSON::parse response.body
28
- end
29
-
30
42
  # Get the status of last received block.
31
43
  # Returns true if block was received in the past 120 seconds.
32
- def ping
33
- ping = self.query "loader/status/ping"
34
- ping["success"]
44
+ def is_alive?
45
+ connected = self.query_get "loader/status/ping"
46
+ connected["success"]
35
47
  end
36
48
 
37
- # Returns the sync status of the blockchain.
38
- def status
39
- status = self.query "loader/status"
40
- end
41
-
42
- # Get the synchronization status of the client.
43
- def sync
44
- sync = self.query "loader/status/sync"
45
- end
46
-
47
- # Gets list of peers.
48
- def peers
49
- peers = self.query "peers"
50
- if peers["success"]
51
- return peers["peers"]
52
- else
53
- return peers
49
+ # Handles GET requests to the given Lisk Core API endpoint
50
+ def query_get endpoint, params = nil
51
+ if not @ssl
52
+ # fixme "#{self}::#{__method__} Allow HTTPS requests"
53
+ begin
54
+ node = ::Net::HTTP.new @host, @port
55
+ uri = URI.parse "http://#{host}:#{port}/api/#{endpoint}"
56
+ if not params.nil?
57
+ uri.query = URI.encode_www_form params
58
+ end
59
+ request = ::Net::HTTP::Get.new uri
60
+ response = node.request request
61
+ result = JSON::parse response.body
62
+ rescue Timeout::Error => e
63
+ p "Can't connect to the Lisk node: Timeout!"
64
+ rescue Errno::EHOSTUNREACH => e
65
+ p "Can't connect to the Lisk node: Host Unreachable!"
66
+ rescue Errno::ECONNREFUSED => e
67
+ p "Can't connect to the Lisk node: Connection Refused!"
68
+ end
54
69
  end
55
70
  end
56
71
 
57
- # Gets version and build time.
58
- def version
59
- version = self.query "peers/version"
72
+ # Handles POST requests to the given Lisk Core API endpoint
73
+ def query_post endpoint, params
74
+ if not @ssl
75
+ # fixme "#{self}::#{__method__} Allow HTTPS requests"
76
+ begin
77
+ node = ::Net::HTTP.new @host, @port
78
+ uri = URI.parse "http://#{host}:#{port}/api/#{endpoint}"
79
+ uri.query = URI.encode_www_form params
80
+ request = ::Net::HTTP::POST.new uri
81
+ response = node.request request
82
+ result = JSON::parse response.body
83
+ rescue Timeout::Error => e
84
+ p "Can't connect to the Lisk node: Timeout!"
85
+ rescue Errno::EHOSTUNREACH => e
86
+ p "Can't connect to the Lisk node: Host Unreachable!"
87
+ rescue Errno::ECONNREFUSED => e
88
+ p "Can't connect to the Lisk node: Connection Refused!"
89
+ end
90
+ end
60
91
  end
61
92
 
62
- # Gets status of height, fee, milestone, blockreward and supply.
63
- def chain
64
- chain = self.query "blocks/getStatus"
93
+ # Handles PUT requests to the given Lisk Core API endpoint
94
+ def query_put endpoint, params
95
+ if not @ssl
96
+ # fixme "#{self}::#{__method__} Allow HTTPS requests"
97
+ begin
98
+ node = ::Net::HTTP.new @host, @port
99
+ uri = URI.parse "http://#{host}:#{port}/api/#{endpoint}"
100
+ uri.query = URI.encode_www_form params
101
+ request = ::Net::HTTP::Put.new uri
102
+ response = node.request request
103
+ result = JSON::parse response.body
104
+ rescue Timeout::Error => e
105
+ p "Can't connect to the Lisk node: Timeout!"
106
+ rescue Errno::EHOSTUNREACH => e
107
+ p "Can't connect to the Lisk node: Host Unreachable!"
108
+ rescue Errno::ECONNREFUSED => e
109
+ p "Can't connect to the Lisk node: Connection Refused!"
110
+ end
111
+ end
65
112
  end
66
113
 
67
- # Unimplemented methods
114
+ # Handles unimplemented methods
68
115
  def method_missing(name, *args, &block)
69
116
  todo "#{self}::#{name} METHOD MISSING"
70
117
  end
@@ -0,0 +1,102 @@
1
+ # The Lisk API Ruby wrapper gem.
2
+ module Lisk
3
+
4
+ # Implements legacy APIs of the Lisk Core pre-1.0.0 node
5
+ class Legacy
6
+
7
+ # A "lisk/client" connecting to a Lisk Core API node.
8
+ attr_accessor :client
9
+
10
+ # Initializing the legacy API with a legacy Lisk Core API client.
11
+ def initialize client
12
+ if not client.nil?
13
+ @client = client
14
+ return self
15
+ else
16
+ return nil
17
+ end
18
+ end
19
+
20
+ # Get the status of last received block.
21
+ # Returns true if block was received in the past 120 seconds.
22
+ def ping
23
+ ping = @client.query_get "loader/status/ping"
24
+ ping["success"]
25
+ end
26
+
27
+ # Returns the sync status of the blockchain.
28
+ def status
29
+ status = @client.query_get "loader/status"
30
+ end
31
+
32
+ # Get the synchronization status of the client.
33
+ def sync
34
+ sync = @client.query_get "loader/status/sync"
35
+ end
36
+
37
+ # Gets list of peers.
38
+ def peers
39
+ peers = @client.query_get "peers"
40
+ if peers["success"]
41
+ return peers["peers"]
42
+ else
43
+ return nil
44
+ end
45
+ end
46
+
47
+ # Gets version and build time.
48
+ def version
49
+ version = @client.query_get "peers/version"
50
+ end
51
+
52
+ # Gets status of height, fee, milestone, blockreward and supply.
53
+ def chain
54
+ chain = @client.query_get "blocks/getStatus"
55
+ end
56
+
57
+ # Gets delegate by username.
58
+ def delegates_get_by_name user_name
59
+ params = { :username => user_name }
60
+ delegate = @client.query_get "delegates/get", params
61
+ if delegate["success"]
62
+ return delegate["delegate"]
63
+ else
64
+ return nil
65
+ end
66
+ end
67
+
68
+ # Gets delegate by public key.
69
+ def delegates_get_by_key public_key
70
+ params = { :publicKey => public_key }
71
+ delegate = @client.query_get "delegates/get", params
72
+ if delegate["success"]
73
+ return delegate["delegate"]
74
+ else
75
+ return nil
76
+ end
77
+ end
78
+
79
+ # Gets voters of delegate.
80
+ def delegates_voters public_key
81
+ params = { :publicKey => public_key }
82
+ voters = @client.query_get "delegates/voters", params
83
+ if voters["success"]
84
+ return voters["accounts"]
85
+ else
86
+ return nil
87
+ end
88
+ end
89
+
90
+ # Request the balance of an account.
91
+ def accounts_get_balance address
92
+ params = { :address => address }
93
+ balance = @client.query_get "accounts/getBalance", params
94
+ end
95
+
96
+ # Handles unimplemented methods
97
+ def method_missing(name, *args, &block)
98
+ todo "#{self}::#{name} METHOD MISSING"
99
+ end
100
+
101
+ end
102
+ end
data/lib/lisk/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Lisk
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lisk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 4fryn Dings
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-26 00:00:00.000000000 Z
11
+ date: 2017-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -84,9 +84,11 @@ files:
84
84
  - bin/console
85
85
  - bin/debug
86
86
  - bin/setup
87
- - example.rb
87
+ - examples/payout.rb
88
+ - examples/status.rb
88
89
  - lib/lisk.rb
89
90
  - lib/lisk/client.rb
91
+ - lib/lisk/legacy.rb
90
92
  - lib/lisk/version.rb
91
93
  - lisk.gemspec
92
94
  homepage: https://github.com/4fryn/lisk.rb