ethmo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3586f58bafa8b9132b9455c1ba70c8fcf086eeb0
4
+ data.tar.gz: 15a1d3ae96e3a50e820cfaf1bf1ebde668bf187f
5
+ SHA512:
6
+ metadata.gz: e0c0cbe4c22cc4f177b376aedd6b85e3530291188fd84d3412b296d892e5afd44e40f74d85e5e7fa142b1264bc1dcc4d9129cce02f718c30dca14391c33343c1
7
+ data.tar.gz: d571fa0d33759142060b25e27a079c2aa673d5280768e7c66586d2c9ad2494b4311dbefea682d1dbf9d38edd1828be5458c2db5dbf502dc8b2aebc72ae2c0433
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,3 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+ DisplayCopNames: true
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.3
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in ethmo.gemspec
8
+ gemspec
9
+
10
+ gem 'pry'
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ethmo (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ coderay (1.1.2)
11
+ diff-lcs (1.3)
12
+ method_source (0.9.0)
13
+ parallel (1.12.1)
14
+ parser (2.4.0.2)
15
+ ast (~> 2.3)
16
+ powerpack (0.1.1)
17
+ pry (0.11.2)
18
+ coderay (~> 1.1.0)
19
+ method_source (~> 0.9.0)
20
+ rainbow (3.0.0)
21
+ rake (10.5.0)
22
+ rspec (3.7.0)
23
+ rspec-core (~> 3.7.0)
24
+ rspec-expectations (~> 3.7.0)
25
+ rspec-mocks (~> 3.7.0)
26
+ rspec-core (3.7.0)
27
+ rspec-support (~> 3.7.0)
28
+ rspec-expectations (3.7.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.7.0)
31
+ rspec-mocks (3.7.0)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.7.0)
34
+ rspec-support (3.7.0)
35
+ rubocop (0.52.1)
36
+ parallel (~> 1.10)
37
+ parser (>= 2.4.0.2, < 3.0)
38
+ powerpack (~> 0.1)
39
+ rainbow (>= 2.2.2, < 4.0)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (~> 1.0, >= 1.0.1)
42
+ ruby-progressbar (1.9.0)
43
+ unicode-display_width (1.3.0)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ bundler (~> 1.16)
50
+ ethmo!
51
+ pry
52
+ rake (~> 10.0)
53
+ rspec (~> 3.0)
54
+ rubocop (~> 0.52.1)
55
+
56
+ BUNDLED WITH
57
+ 1.16.1
@@ -0,0 +1,63 @@
1
+ # Ethmo
2
+
3
+ The Ethereum `geth` RCP tools for ruby developer
4
+
5
+ ## Requirement
6
+
7
+ * Ruby 2.4+
8
+ * Geth (or IPC capability client)
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'ethmo'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install ethmo
25
+
26
+ ## Usage
27
+
28
+ Current is under development, only support minimal features I need.
29
+
30
+ ### Balance of Account
31
+
32
+ ```ruby
33
+ account = Ethmo::Account.new('0x475AFd1a6F1D093263bE16948e1EcA6C4DC37Dcf')
34
+ p account.balance
35
+ ```
36
+
37
+ ### Monitor New Blocks
38
+
39
+ ```ruby
40
+ monitor = Ethmo::Monitor.new
41
+ monitor.start
42
+
43
+ # NOTE: It will blocking I/O until stop
44
+ monitor.each do |event|
45
+ block = Ethmo::Block.new(event.hash)
46
+ block.transactions.each do |tx|
47
+ next unless tx.transfer?
48
+ puts "From: #{tx.from}"
49
+ puts "To: #{tx.to}"
50
+ puts "Value: #{tx.value} ETH"
51
+ end
52
+ end
53
+ ```
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/elct9620/ethmo.
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'ethmo'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ require 'pry'
12
+ Pry.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'ethmo/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'ethmo'
10
+ spec.version = Ethmo::VERSION
11
+ spec.authors = ['蒼時弦也']
12
+ spec.email = ['elct9620@frost.tw']
13
+
14
+ spec.summary = 'The ethereum monitor tool'
15
+ spec.description = 'The ethereum monitor tool'
16
+ spec.homepage = 'https://github.com/elct9620/ethmo'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.16'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'rubocop', '~> 0.52.1'
29
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'socket'
4
+ require 'singleton'
5
+ require 'json'
6
+
7
+ require 'ethmo/version'
8
+
9
+ # :nodoc
10
+ module Ethmo
11
+ autoload :IPCSocket, 'ethmo/ipc_socket'
12
+ autoload :Config, 'ethmo/config'
13
+ autoload :Client, 'ethmo/client'
14
+ autoload :Account, 'ethmo/account'
15
+ autoload :Block, 'ethmo/block'
16
+ autoload :Transaction, 'ethmo/transaction'
17
+ autoload :Monitor, 'ethmo/monitor'
18
+
19
+ class << self
20
+ def config(&block)
21
+ Config.instance_exec(Config.instance, &block)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ethmo
4
+ # :nodoc:
5
+ class Account
6
+ attr_reader :address
7
+
8
+ def initialize(address)
9
+ @address = address
10
+ end
11
+
12
+ def balance
13
+ # TODO: Add ETH value to Float Helper
14
+ Client
15
+ .balance_of(address, 'latest')
16
+ .fetch('result', '0x0')
17
+ .to_i(16)
18
+ .to_f / Config::WEI
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ethmo
4
+ # :nodoc:
5
+ class Block < Hash
6
+ def initialize(hash)
7
+ merge! Client.block_from(hash, true).dig('result')
8
+ end
9
+
10
+ def transactions
11
+ @transactions ||=
12
+ self['transactions']
13
+ .map { |tx| Transaction.new(tx) }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ethmo
4
+ # :nodoc:
5
+ class Client
6
+ include Singleton
7
+
8
+ class << self
9
+ def respond_to_missing?(*args)
10
+ instance.respond_to_missing?(*args)
11
+ end
12
+
13
+ def method_missing(name, *args, &block)
14
+ return super unless instance.respond_to?(name)
15
+ instance.public_send(name, *args, &block)
16
+ end
17
+ end
18
+
19
+ AVAILABLE_METHOD = {
20
+ balance_of: 'eth_getBalance',
21
+ block_from: 'eth_getBlockByHash'
22
+ }.freeze
23
+
24
+ def initialize
25
+ # TODO: Add support for WebSocket and others
26
+ @conn = IPCSocket.new(Config.socket)
27
+ end
28
+
29
+ def respond_to_missing?(name, include_private = false)
30
+ AVAILABLE_METHOD.keys.include?(name) || super
31
+ end
32
+
33
+ def method_missing(name, *args, &block)
34
+ return super unless AVAILABLE_METHOD.keys.include?(name)
35
+ @conn.exec(AVAILABLE_METHOD[name], *args)
36
+ JSON.parse(@conn.read)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ethmo
4
+ # :nodoc:
5
+ class Config
6
+ include Singleton
7
+
8
+ WEI = 10**18 # 1 ETH = 10^18 Wei
9
+
10
+ class << self
11
+ def respond_to_missing?(*args)
12
+ instance.respond_to_missing?(*args)
13
+ end
14
+
15
+ def method_missing(name, *args, &block)
16
+ return super unless instance.respond_to?(name)
17
+ instance.public_send(name, *args, &block)
18
+ end
19
+ end
20
+
21
+ attr_accessor :socket
22
+ end
23
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ethmo
4
+ # :nodoc;
5
+ class IPCSocket < UNIXSocket
6
+ class << self
7
+ def next_id
8
+ @id ||= 1
9
+ @id += 1
10
+ end
11
+ end
12
+
13
+ VERSION = '2.0'
14
+
15
+ attr_reader :id
16
+
17
+ def initialize(path)
18
+ @buffer = ''
19
+ @id = self.class.next_id
20
+ super
21
+ end
22
+
23
+ def exec(method, *params)
24
+ send({
25
+ id: id,
26
+ jsonrpc: VERSION,
27
+ method: method,
28
+ params: params
29
+ }.to_json, 0)
30
+ end
31
+
32
+ def read
33
+ @buffer = @buffer.to_s + recv(1024) until @buffer.to_s.include?("\n")
34
+ (_, @buffer = @buffer.to_s.split("\n", 2)).first
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ethmo
4
+ # :nodoc:
5
+ class Monitor
6
+ autoload :NewHead, 'ethmo/monitor/new_head'
7
+
8
+ include Enumerable
9
+
10
+ def initialize(type = 'newHeads')
11
+ @conn = IPCSocket.new(Config.socket)
12
+ @type = type
13
+ @thread = nil
14
+ @stop = false
15
+ @queue = Queue.new
16
+ end
17
+
18
+ def each(&_block)
19
+ yield @queue.pop until stop?
20
+ end
21
+
22
+ def start
23
+ return if @thread
24
+ subscribe!
25
+ @thread = Thread.new { process until stop? }
26
+ end
27
+
28
+ def stop?
29
+ @stop == true
30
+ end
31
+
32
+ def stop
33
+ @stop = true
34
+ end
35
+
36
+ protected
37
+
38
+ def process
39
+ event = @conn.read
40
+ return sleep 1 if event.nil?
41
+ @queue << NewHead.new(JSON.parse(event))
42
+ end
43
+
44
+ def subscribe!
45
+ # TODO: Add options support
46
+ @conn.exec('eth_subscribe', @type, {})
47
+ JSON.parse(@conn.read)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ethmo
4
+ class Monitor
5
+ # :nodoc:
6
+ class NewHead < Hash
7
+ def initialize(event)
8
+ merge! event.dig('params', 'result')
9
+ end
10
+
11
+ # TODO: Add methods for others attribute
12
+
13
+ def hash
14
+ self['hash']
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ethmo
4
+ # :nodoc:
5
+ class Transaction < Hash
6
+ def initialize(hash)
7
+ # TODO: Add read transaction from hash support
8
+ # NOTE: Current only support direct merge transaction
9
+ merge! hash
10
+ end
11
+
12
+ def transfer?
13
+ value.positive?
14
+ end
15
+
16
+ def contract?
17
+ to.nil?
18
+ end
19
+
20
+ def from
21
+ self['from']
22
+ end
23
+
24
+ def to
25
+ self['to']
26
+ end
27
+
28
+ def value
29
+ @value ||=
30
+ fetch('value', '0x0')
31
+ .to_i(16)
32
+ .to_f / Config::WEI
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ethmo
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ethmo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - 蒼時弦也
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.52.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.52.1
69
+ description: The ethereum monitor tool
70
+ email:
71
+ - elct9620@frost.tw
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - ethmo.gemspec
87
+ - lib/ethmo.rb
88
+ - lib/ethmo/account.rb
89
+ - lib/ethmo/block.rb
90
+ - lib/ethmo/client.rb
91
+ - lib/ethmo/config.rb
92
+ - lib/ethmo/ipc_socket.rb
93
+ - lib/ethmo/monitor.rb
94
+ - lib/ethmo/monitor/new_head.rb
95
+ - lib/ethmo/transaction.rb
96
+ - lib/ethmo/version.rb
97
+ homepage: https://github.com/elct9620/ethmo
98
+ licenses: []
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.6.14
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: The ethereum monitor tool
120
+ test_files: []