web3-eth 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: 1f7893df18e36c16067fa42bddf10427099c9696
4
+ data.tar.gz: '0805e5a0a3ab5eaab92ab9407b72793f5373b1b8'
5
+ SHA512:
6
+ metadata.gz: c4d866889983b9bf6da84bfa32b0ef4a7faa6f1dd24735248151239e9ec5c0061ddf4dd9db5b119dd5ceb2dfaaf0f085109e8534419ce55d56b498437873f172
7
+ data.tar.gz: 047b409c3530c5bdcdf957184cf7960e65363020e2707b1baeb1ded9073bed09ab71b54a581d93178e849e113b9dde8e50233edf29495be5a18b422c59fa584b
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at aleksey@wondersoft.ru. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in web3-eth.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 astudnev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,79 @@
1
+ # Web3 RPC client for Ethereum node
2
+
3
+ This Ruby Gem is used to connect and communicate with Ethereum node ( geth, parity, etc),
4
+ having RPC interface.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'web3-eth'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install web3-eth
21
+
22
+ ## Usage
23
+
24
+ Connecting to local node ( or by SSH Tunnel )
25
+
26
+ ```ruby
27
+ web3 = Web3::Eth::Rpc.new
28
+ ```
29
+
30
+ To connect to remote Ethereum node, follow instructions: https://github.com/paritytech/parity/wiki/Wallet-Remote-Access
31
+
32
+ If you need to connect to remote host, you can specify host, port and HTTP connection options:
33
+
34
+ ```ruby
35
+ web3 = Web3::Eth::Rpc.new 'node.host.com', 8545, { use_ssl: true, read_timeout: 120 }
36
+ ```
37
+
38
+ Calling eth interface:
39
+
40
+ ```
41
+ >> web3.eth.blockNumber
42
+ 4376369
43
+
44
+ >> web3.eth.getBalance '0x829BD824B016326A401d083B33D092293333A830'
45
+ 3916.6597314456685
46
+
47
+ >> block = web3.eth.getBlockByNumber 4376369
48
+ #<Web3::Eth::Block:0x007f844d6f1138 @block_data={"author"=>"0x829bd824b016326a401d083b33d092293333a830", ...
49
+
50
+ >> block.timestamp_time
51
+ 2017-10-17 12:51:36 +0300
52
+
53
+ >> block.transactions.count
54
+ 129
55
+
56
+ >> block.transactions[0].from
57
+ "0xb2930b35844a230f00e51431acae96fe543a0347"
58
+
59
+ >> block.transactions[0].value_eth
60
+ 0.51896811
61
+
62
+ ```
63
+
64
+
65
+ ## Development
66
+
67
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
68
+
69
+ 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).
70
+
71
+ ## Contributing
72
+
73
+ Bug reports and pull requests are welcome on GitHub at https://github.com/izetex/web3-eth. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
74
+
75
+
76
+ ## License
77
+
78
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
79
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "web3/eth"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -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,7 @@
1
+ require "web3/eth/version"
2
+ require "web3/eth/utility"
3
+ require "web3/eth/block"
4
+ require "web3/eth/transaction"
5
+ require "web3/eth/transaction_receipt"
6
+ require "web3/eth/ethereum"
7
+ require "web3/eth/rpc"
@@ -0,0 +1,35 @@
1
+ module Web3
2
+ module Eth
3
+
4
+ class Block
5
+
6
+ include Web3::Eth::Utility
7
+
8
+ attr_reader :raw_data
9
+
10
+ def initialize block_data
11
+ @raw_data = block_data
12
+
13
+ block_data.each do |k, v|
14
+ self.instance_variable_set("@#{k}", v)
15
+ self.class.send(:define_method, k, proc {self.instance_variable_get("@#{k}")})
16
+ end
17
+
18
+ @transactions = block_data['transactions'].collect {|t|
19
+ Transaction.new t
20
+ }
21
+
22
+ end
23
+
24
+ def timestamp_time
25
+ Time.at from_hex timestamp
26
+ end
27
+
28
+ def block_number
29
+ from_hex number
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,43 @@
1
+ module Web3
2
+ module Eth
3
+
4
+ class Ethereum
5
+
6
+ include Web3::Eth::Utility
7
+
8
+ PREFIX = 'eth_'
9
+
10
+ def initialize web3_rpc
11
+ @web3_rpc = web3_rpc
12
+ end
13
+
14
+ def getBalance address, block = 'latest', convert_to_eth = true
15
+ wei = @web3_rpc.request("#{PREFIX}#{__method__}", [address, block]).to_i 16
16
+ convert_to_eth ? wei_to_ether(wei) : wei
17
+ end
18
+
19
+ def getBlockByNumber block, full = true, convert_to_object = true
20
+ resp = @web3_rpc.request("#{PREFIX}#{__method__}", [hex(block), full])
21
+ convert_to_object ? Block.new(resp) : resp
22
+ end
23
+
24
+ def blockNumber
25
+ from_hex @web3_rpc.request("#{PREFIX}#{__method__}")
26
+ end
27
+
28
+ def getTransactionByHash tx_hash
29
+ Transaction.new @web3_rpc.request("#{PREFIX}#{__method__}", [tx_hash])
30
+ end
31
+
32
+ def getTransactionReceipt tx_hash
33
+ TransactionReceipt.new @web3_rpc.request("#{PREFIX}#{__method__}", [tx_hash])
34
+ end
35
+
36
+ def method_missing m, *args
37
+ @web3_rpc.request "#{PREFIX}#{m}", args[0]
38
+ end
39
+
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,59 @@
1
+ module Web3
2
+ module Eth
3
+
4
+ class Rpc
5
+
6
+ require 'json'
7
+ require 'net/http'
8
+
9
+ JSON_RPC_VERSION = '2.0'
10
+
11
+ DEFAULT_CONNECT_OPTIONS = {
12
+ use_ssl: false,
13
+ open_timeout: 10,
14
+ read_timeout: 70
15
+ }
16
+
17
+ DEFAULT_HOST = 'localhost'
18
+ DEFAULT_PORT = 8545
19
+
20
+ def initialize host: DEFAULT_HOST, port: DEFAULT_PORT, connect_options: DEFAULT_CONNECT_OPTIONS
21
+
22
+ @client_id = Random.rand 10000000
23
+
24
+ @uri = URI((connect_options[:use_ssl] ? 'https' : 'http')+ "://#{host}:#{port}")
25
+ @connect_options = connect_options
26
+
27
+ @eth = Ethereum.new self
28
+ end
29
+
30
+
31
+ def eth
32
+ @eth
33
+ end
34
+
35
+
36
+ def request method, params = nil
37
+
38
+
39
+ Net::HTTP.start(@uri.host, @uri.port, @connect_options) do |http|
40
+
41
+ request = Net::HTTP::Post.new @uri, {"Content-Type" => "application/json"}
42
+ request.body = {:jsonrpc => JSON_RPC_VERSION, method: method, params: params, id: @client_id}.compact.to_json
43
+ response = http.request request
44
+
45
+ raise "Error code #{response.code} on request #{@uri.to_s} #{request.body}" unless response.kind_of? Net::HTTPOK
46
+
47
+ json = JSON.parse(response.body)['result']
48
+ raise "No response on request #{@uri.to_s} #{request.body}" unless json
49
+
50
+ json
51
+
52
+ end
53
+
54
+ end
55
+
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,39 @@
1
+ module Web3
2
+ module Eth
3
+
4
+ class Transaction
5
+
6
+ include Web3::Eth::Utility
7
+
8
+ attr_reader :raw_data
9
+
10
+ def initialize transaction_data
11
+ @raw_data = transaction_data
12
+
13
+ transaction_data.each do |k, v|
14
+ self.instance_variable_set("@#{k}", v)
15
+ self.class.send(:define_method, k, proc {self.instance_variable_get("@#{k}")})
16
+ end
17
+
18
+
19
+ end
20
+
21
+ def block_number
22
+ from_hex blockNumber
23
+ end
24
+
25
+ def value_eth
26
+ wei_to_ether from_hex value
27
+ end
28
+
29
+ def gas_eth
30
+ wei_to_ether from_hex gas
31
+ end
32
+
33
+ def gasPrice_eth
34
+ wei_to_ether from_hex gasPrice
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ module Web3
2
+ module Eth
3
+
4
+ class TransactionReceipt
5
+
6
+ include Web3::Eth::Utility
7
+
8
+ attr_reader :raw_data
9
+
10
+ def initialize transaction_data
11
+ @raw_data = transaction_data
12
+
13
+ transaction_data.each do |k, v|
14
+ self.instance_variable_set("@#{k}", v)
15
+ self.class.send(:define_method, k, proc {self.instance_variable_get("@#{k}")})
16
+ end
17
+
18
+ end
19
+
20
+ def block_number
21
+ from_hex blockNumber
22
+ end
23
+
24
+ def success?
25
+ status==1 || status.nil?
26
+ end
27
+
28
+ def gas_used_eth
29
+ wei_to_ether from_hex gasUsed
30
+ end
31
+
32
+
33
+ def cumulative_gas_used_eth
34
+ wei_to_ether from_hex cumulativeGasUsed
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,21 @@
1
+ module Web3
2
+ module Eth
3
+
4
+ module Utility
5
+
6
+ def hex num
7
+ '0x' + num.to_s(16)
8
+ end
9
+
10
+ def wei_to_ether(wei)
11
+ 1.0 * wei / 10**18
12
+ end
13
+
14
+ def from_hex h
15
+ h.to_i 16
16
+ end
17
+
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module Web3
2
+ module Eth
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'web3/eth/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "web3-eth"
8
+ spec.version = Web3::Eth::VERSION
9
+ spec.authors = ["studnev"]
10
+ spec.email = ["studnev@izx.io"]
11
+
12
+ spec.summary = %q{Web3 client to connect to Ethereum node by RPC.}
13
+ spec.description = %q{Calling RPC methods of Ethereum node with Ruby.}
14
+ spec.homepage = "https://github.com/izetex/web3-eth"
15
+ spec.license = "MIT"
16
+
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.14"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest", "~> 5.0"
29
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: web3-eth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - studnev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Calling RPC methods of Ethereum node with Ruby.
56
+ email:
57
+ - studnev@izx.io
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".idea/workspace.xml"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/web3/eth.rb
73
+ - lib/web3/eth/block.rb
74
+ - lib/web3/eth/ethereum.rb
75
+ - lib/web3/eth/rpc.rb
76
+ - lib/web3/eth/transaction.rb
77
+ - lib/web3/eth/transaction_receipt.rb
78
+ - lib/web3/eth/utility.rb
79
+ - lib/web3/eth/version.rb
80
+ - web3-eth.gemspec
81
+ homepage: https://github.com/izetex/web3-eth
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.6.10
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Web3 client to connect to Ethereum node by RPC.
105
+ test_files: []