ethereum 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 322a4404d64ad2d1d14328ec0440b6612aaf5867
4
+ data.tar.gz: 820552a955aeb11ccb156333758c94eb73eddaae
5
+ SHA512:
6
+ metadata.gz: b070b6eb2995dc8595a001e584e69fed37b855cbb03a88989fb5bd22fd4d85a57dea48e1d598c2cc990bab21b2162f94f06111acf9b767fa6d583d6d838e3f81
7
+ data.tar.gz: 84958a99e16f93a6de3f870398dc085c504582cd590c60570c1701e2802c52e845b22edcd220938c694e31c62321df08a4527c9ff5370a34634e6764d157707b
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.*.swp
11
+ /**/.*.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ rubythereum
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.2.3
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ethereum.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 DigixGlobal Pte Ltd
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 DigixGlobal Pte Ltd
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.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # Ethereum library for Ruby
2
+
3
+ A simple library for Ethereum.
4
+
5
+ ## Features
6
+
7
+ * IPC Client with batch calls support
8
+ * Compile and deploy Solidity contracts
9
+ * Expose deployed contracts as Ruby classes
10
+ * Test solidity contracts with a Ruby testing framework of your choice
11
+ * Call and wait for the result of Solidity function calls.
12
+
13
+ ## Requirements
14
+
15
+ We currently support UNIX/Linux environments and Windows IPC support on the roadmap.
16
+
17
+ You will need to have a properly working Ruby installation. We recommend [RVM](http://rvm.io/)
18
+
19
+ To use this library you will need to have a running Ethereum node with IPC support enabled (default). We currently support [Go-Ethereum client](https://github.com/ethereum/go-ethereum)
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile:
24
+
25
+ ```ruby
26
+ gem 'ethereum'
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ $ bundle
32
+
33
+ Or install it yourself as:
34
+
35
+ $ gem install ethereum
36
+
37
+ ## Basic Usage
38
+
39
+ ### IPC Client Connection
40
+
41
+ ```ruby
42
+ client = Ethereum::IpcClient.new("#{ENV['HOME']}/.ethereum_testnet/geth.ipc")
43
+ ```
44
+
45
+ ### Solidity contract compilation and deployment
46
+
47
+ ```ruby
48
+ init = Ethereum::Initializer.new("#{ENV['PWD']}/spec/fixtures/SimpleNameRegistry.sol", client)
49
+ init.build_all
50
+ simple_name_registry_instance = SimpleNameRegistry.new
51
+ simple_name_registry_instance.deploy_and_wait
52
+ ```
53
+
54
+ ### Transacting and Calling Solidity Functions
55
+
56
+ Solidity functions are exposed as transact_<underscore_function_name>(params) (or transact_and_wait_<underscore_function_name>(params)) and call_<underscore_function_name>(params) e.g.:
57
+
58
+ ```ruby
59
+ simple_name_registry_instance.transact_and_wait_register("0x5b6cb65d40b0e27fab87a2180abcab22174a2d45", "minter.contract.dgx")
60
+ simple_name_registry_instance.transact_register("0x385acafdb80b71ae001f1dbd0d65e62ec2fff055", "anthony@eufemio.dgx")
61
+ ```
62
+
63
+ ## Roadmap
64
+
65
+ * Add JSON RPC Client support
66
+ * Add Windows IPC Client (named pipes)
67
+ * Solidity constant function output should be properly formatted according to the ouput data type
68
+ * Unit testing and contract testing examples
69
+ * Add more examples
70
+ * API documentation
71
+
72
+ ## Development
73
+
74
+ 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.
75
+
76
+ 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).
77
+
78
+ ## Contributing
79
+
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/DigixGlobal/ethereum-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
85
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ethereum"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/ethereum.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ethereum/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ethereum"
8
+ spec.version = Ethereum::VERSION
9
+ spec.authors = ["Digix"]
10
+ spec.email = ["ace@dgx.io"]
11
+
12
+ spec.summary = %q{Ethereum libraries for Ruby}
13
+ spec.description = %q{Ethereum libraries for ruby programming language.}
14
+ spec.homepage = "https://github.com/DigixGlobal/ethereum-ruby"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib", "bin"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec"
33
+ spec.add_development_dependency "pry"
34
+ spec.add_dependency "activesupport"
35
+ spec.add_dependency "digest-sha3"
36
+ end
data/lib/ethereum.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "ethereum/version"
2
+ require 'active_support'
3
+ require 'active_support/core_ext'
4
+
5
+ module Ethereum
6
+ require 'ethereum/ipc_client'
7
+ require 'ethereum/initializer'
8
+ require 'ethereum/contract'
9
+ require 'ethereum/function'
10
+ require 'ethereum/function_input'
11
+ require 'ethereum/function_output'
12
+ require 'ethereum/formatter'
13
+ require 'ethereum/transaction'
14
+ require 'ethereum/deployment'
15
+ end
@@ -0,0 +1,115 @@
1
+ module Ethereum
2
+ class Contract
3
+
4
+ attr_accessor :code, :name, :functions, :abi
5
+
6
+ def initialize(name, code, abi)
7
+ @name = name
8
+ @code = code
9
+ @abi = abi
10
+ @functions = []
11
+ @abi.select {|x| x["type"] == "function" }.each do |abifun|
12
+ @functions << Ethereum::Function.new(abifun)
13
+ end
14
+ end
15
+
16
+ def build(connection)
17
+ class_name = @name
18
+ functions = @functions
19
+ code = @code
20
+ class_methods = Class.new do
21
+
22
+ define_method "connection".to_sym do
23
+ connection
24
+ end
25
+
26
+ define_method :deploy do
27
+ deploytx = connection.send_transaction({from: self.sender, gas: 2000000, gasPrice: 60000000000, data: code})["result"]
28
+ self.instance_variable_set("@deployment", Ethereum::Deployment.new(deploytx, connection))
29
+ end
30
+
31
+ define_method :deployment do
32
+ self.instance_variable_get("@deployment")
33
+ end
34
+
35
+ define_method :deploy_and_wait do |time = 60.seconds|
36
+ self.deploy
37
+ self.deployment.wait_for_deployment(time)
38
+ self.instance_variable_set("@address", self.deployment.contract_address)
39
+ end
40
+
41
+ define_method :at do |addr|
42
+ self.instance_variable_set("@address", addr)
43
+ end
44
+
45
+ define_method :address do
46
+ self.instance_variable_get("@address")
47
+ end
48
+
49
+ define_method :as do |addr|
50
+ self.instance_variable_set("@sender", addr)
51
+ end
52
+
53
+ define_method :sender do
54
+ self.instance_variable_get("@sender") || connection.coinbase["result"]
55
+ end
56
+
57
+ define_method :set_gas_price do |gp|
58
+ self.instance_variable_set("@gas_price", gp)
59
+ end
60
+
61
+ define_method :gas_price do
62
+ self.instance_variable_get("@gas_price") || 60000000000
63
+ end
64
+
65
+ define_method :set_gas do |gas|
66
+ self.instance_variable_set("@gas", gas)
67
+ end
68
+
69
+ define_method :gas do
70
+ self.instance_variable_get("@gas") || 2000000
71
+ end
72
+
73
+ functions.each do |fun|
74
+ if fun.constant
75
+ define_method "call_#{fun.name.underscore}".to_sym do |*args|
76
+ formatter = Ethereum::Formatter.new
77
+ arg_types = fun.inputs.collect(&:type)
78
+ #out_types = fun.outputs.collect(&:type)
79
+ connection = self.connection
80
+ return {result: :error, message: "missing parameters for #{fun.function_string}" } if arg_types.length != args.length
81
+ payload = []
82
+ payload << fun.signature
83
+ arg_types.zip(args).each do |arg|
84
+ payload << formatter.to_payload(arg)
85
+ end
86
+ return {result: connection.call({to: self.address, from: self.sender, data: payload.join()})["result"].gsub(/^0x/,'').scan(/.{64}/)}
87
+ end
88
+ else
89
+ define_method "transact_#{fun.name.underscore}".to_sym do |*args|
90
+ formatter = Ethereum::Formatter.new
91
+ arg_types = fun.inputs.collect(&:type)
92
+ connection = self.connection
93
+ return {result: :error, message: "missing parameters for #{fun.function_string}" } if arg_types.length != args.length
94
+ payload = []
95
+ payload << fun.signature
96
+ arg_types.zip(args).each do |arg|
97
+ payload << formatter.to_payload(arg)
98
+ end
99
+ txid = connection.send_transaction({to: self.address, from: self.sender, data: payload.join(), gas: self.gas, gasPrice: self.gas_price})["result"]
100
+ return Ethereum::Transaction.new(txid, self.connection)
101
+ end
102
+ define_method "transact_and_wait_#{fun.name.underscore}".to_sym do |*args|
103
+ function_name = "transact_#{fun.name.underscore}".to_sym
104
+ tx = self.send(function_name, *args)
105
+ tx.wait_for_miner
106
+ return tx
107
+ end
108
+ end
109
+ end
110
+ end
111
+ Object.const_set(class_name, class_methods)
112
+ end
113
+
114
+ end
115
+ end
@@ -0,0 +1,45 @@
1
+ module Ethereum
2
+
3
+ class Deployment
4
+
5
+ attr_accessor :id, :contract_address, :connection, :deployed, :mined, :valid_deployment
6
+
7
+ def initialize(txid, connection)
8
+ @id = txid
9
+ @connection = connection
10
+ @deployed = false
11
+ @contract_address = nil
12
+ @valid_deployment = false
13
+ end
14
+
15
+ def mined?
16
+ return true if @mined
17
+ @mined = @connection.get_transaction_by_hash(@id)["result"]["blockNumber"].present? rescue nil
18
+ @mined ||= false
19
+ end
20
+
21
+ def has_address?
22
+ return true if @contract_address.present?
23
+ return false unless self.mined?
24
+ @contract_address ||= @connection.get_transaction_receipt(@id)["result"]["contractAddress"]
25
+ return @contract_address.present?
26
+ end
27
+
28
+ def deployed?
29
+ return true if @valid_deployment
30
+ return false unless self.has_address?
31
+ @valid_deployment = @connection.get_code(@contract_address)["result"] != "0x"
32
+ end
33
+
34
+ def wait_for_deployment(timeout = 60.seconds)
35
+ start_time = Time.now
36
+ while self.deployed? == false
37
+ raise Timeout::Error if ((Time.now - start_time) > timeout)
38
+ sleep 5
39
+ return true if self.deployed?
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,100 @@
1
+ module Ethereum
2
+ class Formatter
3
+
4
+ def from_bool(boolval)
5
+ boolval ? "1" : "0"
6
+ end
7
+
8
+ def to_bool(hexstring)
9
+ (hexstring == "0000000000000000000000000000000000000000000000000000000000000001")
10
+ end
11
+
12
+ def to_ascii(hexstring)
13
+ hexstring.gsub(/^0x/,'').scan(/.{2}/).collect {|x| x.hex}.pack("c*")
14
+ end
15
+
16
+ def to_utf8(hexstring)
17
+ hexstring.gsub(/^0x/,'').scan(/.{2}/).collect {|x| x.hex}.pack("U*").delete("\u0000")
18
+ end
19
+
20
+ def from_ascii(ascii_string)
21
+ ascii_string.unpack('H*')[0]
22
+ end
23
+
24
+ def from_utf8(utf8_string)
25
+ utf8_string.force_encoding('UTF-8').split("").collect {|x| x.ord.to_s(16)}.join("")
26
+ end
27
+
28
+ def to_address(hexstring)
29
+ "0x" + hexstring[-40..-1]
30
+ end
31
+
32
+ def from_address(address)
33
+ address.gsub(/^0x/,'').rjust(64, "0")
34
+ end
35
+
36
+ def to_param(string)
37
+ string.ljust(64, '0')
38
+ end
39
+
40
+ def from_input(string)
41
+ string[10..-1].scan(/.{64}/)
42
+ end
43
+
44
+ def to_twos_complement(number)
45
+ (number & ((1 << 256) - 1)).to_s(16)
46
+ end
47
+
48
+ def to_int(hexstring)
49
+ (hexstring.gsub(/^0x/,'')[0..1] == "ff") ? (hexstring.hex - (2 ** 256)) : hexstring.hex
50
+ end
51
+
52
+ def address_to_payload(address)
53
+ from_address(address)
54
+ end
55
+
56
+ def uint_to_payload(uint)
57
+ self.to_twos_complement(uint)
58
+ end
59
+
60
+ def int_to_payload(int)
61
+ self.to_twos_complement(uint)
62
+ end
63
+
64
+ def bytes_to_payload(bytes)
65
+ self.from_utf8(bytes).ljust(64, '0')
66
+ end
67
+
68
+ def get_base_type(typename)
69
+ typename.gsub(/\d+/,'')
70
+ end
71
+
72
+ def to_payload(args)
73
+ converter = "#{self.get_base_type(args[0])}_to_payload".to_sym
74
+ self.send(converter, args[1])
75
+ end
76
+
77
+ def output_to_address(bytes)
78
+ self.to_address(bytes)
79
+ end
80
+
81
+ def output_to_bytes(bytes)
82
+ self.to_utf8(bytes)
83
+ end
84
+
85
+ def output_to_uint(bytes)
86
+ self.to_int(bytes)
87
+ end
88
+
89
+ def output_to_int(bytes)
90
+ self.to_int(bytes)
91
+ end
92
+
93
+ def to_output(args)
94
+ converter = "output_to_#{self.get_base_type(args[0])}".to_sym
95
+ self.send(converter, args[1])
96
+ end
97
+
98
+ end
99
+
100
+ end
@@ -0,0 +1,20 @@
1
+ module Ethereum
2
+ class Function
3
+
4
+ attr_accessor :name, :inputs, :outputs, :signature, :constant, :function_string
5
+
6
+ def initialize(data)
7
+ @name = data["name"]
8
+ @constant = data["constant"]
9
+ @inputs = data["inputs"].collect do |input|
10
+ Ethereum::FunctionInput.new(input)
11
+ end
12
+ @outputs = data["outputs"].collect do |output|
13
+ Ethereum::FunctionOutput.new(output)
14
+ end
15
+ @function_string = "#{@name}(#{@inputs.collect {|x| x.type }.join(",")})"
16
+ @signature = Digest::SHA3.hexdigest(@function_string, 256)[0..7]
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ module Ethereum
2
+ class FunctionInput
3
+
4
+ attr_accessor :type, :name
5
+
6
+ def initialize(data)
7
+ @type = data["type"]
8
+ @name = data["name"]
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,14 @@
1
+ module Ethereum
2
+
3
+ class FunctionOutput
4
+
5
+ attr_accessor :type, :name
6
+
7
+ def initialize(data)
8
+ @type = data["type"]
9
+ @name = data["name"]
10
+ end
11
+
12
+ end
13
+
14
+ end
@@ -0,0 +1,27 @@
1
+ module Ethereum
2
+
3
+ class Initializer
4
+ attr_accessor :contracts, :file, :client
5
+
6
+ def initialize(file, client = Ethereum::IpcClient.new)
7
+ @file = File.read(file)
8
+ @client = client
9
+ sol_output = @client.compile_solidity(@file)
10
+ contracts = sol_output["result"].keys
11
+ @contracts = []
12
+ contracts.each do |contract|
13
+ abi = sol_output["result"][contract]["info"]["abiDefinition"]
14
+ name = contract
15
+ code = sol_output["result"][contract]["code"]
16
+ @contracts << Ethereum::Contract.new(name, code, abi)
17
+ end
18
+ end
19
+
20
+ def build_all
21
+ @contracts.each do |contract|
22
+ contract.build(@client)
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,61 @@
1
+ require 'socket'
2
+ module Ethereum
3
+ class IpcClient
4
+ attr_accessor :command, :id, :ipcpath, :batch, :converted_transactions
5
+
6
+ def initialize(ipcpath = "#{ENV['HOME']}/.ethereum/geth.ipc")
7
+ @ipcpath = ipcpath
8
+ @id = 1
9
+ @batch = []
10
+ end
11
+
12
+ def get_id
13
+ @id = @id + 1
14
+ return @id
15
+ end
16
+
17
+ RPC_COMMANDS = %w(eth_accounts eth_blockNumber eth_getBalance eth_protocolVersion eth_coinbase eth_mining eth_gasPrice eth_getStorage eth_storageAt eth_getStorageAt eth_getTransactionCount eth_getBlockTransactionCountByHash eth_getBlockTransactionCountByNumber eth_getUncleCountByBlockHash eth_getUncleCountByBlockNumber eth_getData eth_getCode eth_sign eth_sendRawTransaction eth_sendTransaction eth_transact eth_estimateGas eth_call eth_flush eth_getBlockByHash eth_getBlockByNumber eth_getTransactionByHash eth_getTransactionByBlockNumberAndIndex eth_getTransactionByBlockHashAndIndex eth_getUncleByBlockHashAndIndex eth_getUncleByBlockNumberAndIndex eth_getCompilers eth_compileSolidity eth_newFilter eth_newBlockFilter eth_newPendingTransactionFilter eth_uninstallFilter eth_getFilterChanges eth_getFilterLogs eth_getLogs eth_hashrate eth_getWork eth_submitWork eth_resend eth_pendingTransactions eth_getTransactionReceipt)
18
+
19
+ RPC_COMMANDS.each do |rpc_command|
20
+ method_name = "#{rpc_command.split("_")[1].underscore}"
21
+ define_method method_name do |*args|
22
+ command = rpc_command
23
+ payload = {jsonrpc: "2.0", method: command, params: args, id: get_id}
24
+ socket = UNIXSocket.new(@ipcpath)
25
+ socket.write(payload.to_json)
26
+ socket.close_write
27
+ read = socket.read
28
+ socket.close_read
29
+ output = JSON.parse(read)
30
+ return output
31
+ end
32
+
33
+ define_method "#{method_name}_batch" do |*args|
34
+ command = rpc_command
35
+ payload = {jsonrpc: "2.0", method: command, params: args, id: get_id}
36
+ @batch << payload.to_json
37
+ end
38
+ end
39
+
40
+ def send_batch
41
+ socket = UNIXSocket.new(@ipcpath)
42
+ socket.write(@batch.join(" "))
43
+ socket.close_write
44
+ read = socket.read
45
+ collection = read.chop.split("}{").collect do |output|
46
+ if output[0] == "{"
47
+ JSON.parse("#{output}}")["result"]
48
+ else
49
+ JSON.parse("{#{output}}")["result"]
50
+ end
51
+ end
52
+ return collection
53
+ end
54
+
55
+ def clear_batch
56
+ @batch = []
57
+ end
58
+
59
+ end
60
+ end
61
+
@@ -0,0 +1,28 @@
1
+ module Ethereum
2
+
3
+ class Transaction
4
+ attr_accessor :id, :mined, :connection
5
+
6
+ def initialize(id, connection)
7
+ @mined = false
8
+ @connection = connection
9
+ @id = id
10
+ end
11
+
12
+ def mined?
13
+ return true if @mined
14
+ @mined = @connection.get_transaction_by_hash(@id)["result"]["blockNumber"].present?
15
+ end
16
+
17
+ def wait_for_miner(timeout = 60.seconds)
18
+ start_time = Time.now
19
+ while self.mined? == false
20
+ raise Timeout::Error if ((Time.now - start_time) > timeout)
21
+ sleep 5
22
+ return true if self.mined?
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,3 @@
1
+ module Ethereum
2
+ VERSION = "0.2.0"
3
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ethereum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Digix
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-08 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: digest-sha3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Ethereum libraries for ruby programming language.
98
+ email:
99
+ - ace@dgx.io
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".ruby-gemset"
107
+ - ".ruby-version"
108
+ - ".travis.yml"
109
+ - CODE_OF_CONDUCT.md
110
+ - Gemfile
111
+ - LICENSE
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - bin/console
116
+ - bin/setup
117
+ - ethereum.gemspec
118
+ - lib/ethereum.rb
119
+ - lib/ethereum/contract.rb
120
+ - lib/ethereum/deployment.rb
121
+ - lib/ethereum/formatter.rb
122
+ - lib/ethereum/function.rb
123
+ - lib/ethereum/function_input.rb
124
+ - lib/ethereum/function_output.rb
125
+ - lib/ethereum/initializer.rb
126
+ - lib/ethereum/ipc_client.rb
127
+ - lib/ethereum/transaction.rb
128
+ - lib/ethereum/version.rb
129
+ homepage: https://github.com/DigixGlobal/ethereum-ruby
130
+ licenses:
131
+ - MIT
132
+ metadata:
133
+ allowed_push_host: https://rubygems.org
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ - bin
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.4.5.1
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Ethereum libraries for Ruby
155
+ test_files: []