ethereum.rb 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +2 -0
  4. data/.ruby-gemset +1 -0
  5. data/.travis.yml +26 -0
  6. data/CODE_OF_CONDUCT.md +13 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE +22 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +183 -0
  11. data/Rakefile +11 -0
  12. data/bin/console +14 -0
  13. data/bin/install_parity +29 -0
  14. data/bin/setup +7 -0
  15. data/contracts/AccountingLib.sol +112 -0
  16. data/contracts/AuditorInterface.sol +4 -0
  17. data/contracts/AuditorRegistry.sol +14 -0
  18. data/contracts/CustodianInterface.sol +27 -0
  19. data/contracts/CustodianRegistry.sol +40 -0
  20. data/contracts/DigixConfiguration.sol +68 -0
  21. data/contracts/Directory.sol +67 -0
  22. data/contracts/DoublyLinked.sol +54 -0
  23. data/contracts/GenericInterface.sol +56 -0
  24. data/contracts/GenericRegistry.sol +15 -0
  25. data/contracts/Gold.sol +105 -0
  26. data/contracts/GoldRegistry.sol +82 -0
  27. data/contracts/GoldTokenLedger.sol +3 -0
  28. data/contracts/Interface.sol +27 -0
  29. data/contracts/Minter.sol +3 -0
  30. data/contracts/Recaster.sol +3 -0
  31. data/contracts/Testing.sol +59 -0
  32. data/contracts/VendorInterface.sol +82 -0
  33. data/contracts/VendorRegistry.sol +39 -0
  34. data/contracts/classic/Digixbot.sol +106 -0
  35. data/contracts/classic/DigixbotConfiguration.sol +62 -0
  36. data/contracts/classic/DigixbotEthereum.sol +86 -0
  37. data/contracts/classic/DigixbotUsers.sol +103 -0
  38. data/contracts/classic/Gold.sol +497 -0
  39. data/contracts/classic/GoldRegistry.sol +503 -0
  40. data/contracts/classic/GoldTokenLedger.sol +560 -0
  41. data/contracts/classic/GoldTokenMinter.sol +607 -0
  42. data/contracts/classic/ParticipantRegistry.sol +94 -0
  43. data/contracts/classic/QueueSample.sol +54 -0
  44. data/ethereum.gemspec +35 -0
  45. data/lib/ethereum.rb +24 -0
  46. data/lib/ethereum/client.rb +97 -0
  47. data/lib/ethereum/contract.rb +266 -0
  48. data/lib/ethereum/contract_event.rb +25 -0
  49. data/lib/ethereum/contract_initializer.rb +54 -0
  50. data/lib/ethereum/deployment.rb +49 -0
  51. data/lib/ethereum/formatter.rb +172 -0
  52. data/lib/ethereum/function.rb +20 -0
  53. data/lib/ethereum/function_input.rb +13 -0
  54. data/lib/ethereum/function_output.rb +14 -0
  55. data/lib/ethereum/http_client.rb +38 -0
  56. data/lib/ethereum/initializer.rb +27 -0
  57. data/lib/ethereum/ipc_client.rb +46 -0
  58. data/lib/ethereum/project_initializer.rb +28 -0
  59. data/lib/ethereum/railtie.rb +10 -0
  60. data/lib/ethereum/singleton.rb +39 -0
  61. data/lib/ethereum/solidity.rb +47 -0
  62. data/lib/ethereum/transaction.rb +36 -0
  63. data/lib/ethereum/version.rb +3 -0
  64. data/lib/tasks/ethereum_contract.rake +27 -0
  65. data/lib/tasks/ethereum_node.rake +51 -0
  66. data/lib/tasks/ethereum_test.rake +32 -0
  67. data/lib/tasks/ethereum_transaction.rake +24 -0
  68. metadata +198 -0
@@ -0,0 +1,28 @@
1
+ module Ethereum
2
+
3
+ class ProjectInitializer
4
+
5
+ attr_accessor :contract_names, :combined_output, :contracts, :libraries
6
+
7
+ def initialize(location, optimize = false)
8
+ ENV['ETHEREUM_SOLIDITY_BINARY'] ||= "/usr/local/bin/solc"
9
+ solidity = ENV['ETHEREUM_SOLIDITY_BINARY']
10
+ contract_dir = location
11
+ if optimize
12
+ opt_flag = "--optimize"
13
+ else
14
+ opt_flag = ""
15
+ end
16
+ compile_command = "#{solidity} #{opt_flag} --combined-json abi,bin #{contract_dir}"
17
+ raw_data = `#{compile_command}`
18
+ data = JSON.parse(raw_data)
19
+ @contract_names = data["contracts"].keys
20
+ @libraries = {}
21
+ @contracts = @contract_names.collect do |contract_name|
22
+ ContractInitializer.new(contract_name, data["contracts"][contract_name], self)
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,10 @@
1
+ module Ethereum
2
+
3
+ class Railtie < Rails::Railtie
4
+ rake_tasks do
5
+ load('tasks/ethereum_test.rake')
6
+ load('tasks/ethereum_node.rake')
7
+ end
8
+ end
9
+
10
+ end
@@ -0,0 +1,39 @@
1
+ class Ethereum::Singleton
2
+
3
+ class << self
4
+
5
+ attr_accessor :client, :ipcpath, :host, :log, :instance, :default_account
6
+
7
+ def instance
8
+ @instance ||= configure_instance(create_instance)
9
+ end
10
+
11
+ def setup(&block)
12
+ yield(self)
13
+ end
14
+
15
+ def reset
16
+ @instance = nil
17
+ @client = nil
18
+ @host = nil
19
+ @log = nil
20
+ @ipcpath = nil
21
+ @default_account = nil
22
+ end
23
+
24
+ private
25
+ def create_instance
26
+ return Ethereum::IpcClient.new(@ipcpath) if @client == :ipc
27
+ return Ethereum::HttpClient.new(@host) if @client == :http
28
+ Ethereum::IpcClient.new
29
+ end
30
+
31
+ def configure_instance(instance)
32
+ instance.tap do |i|
33
+ i.default_account = @default_account if @default_account.present?
34
+ end
35
+ end
36
+ end
37
+
38
+
39
+ end
@@ -0,0 +1,47 @@
1
+ require 'tmpdir'
2
+ require 'open3'
3
+
4
+ module Ethereum
5
+ class CompilationError < StandardError;
6
+ def initialize(msg)
7
+ super
8
+ end
9
+ end
10
+
11
+ class Solidity
12
+
13
+ def initialize(bin_path = "solc")
14
+ @bin_path = bin_path
15
+ @args = "--bin --abi --userdoc --devdoc --add-std --optimize -o"
16
+ end
17
+
18
+ def compile(filename)
19
+ {}.tap do |result|
20
+ Dir.mktmpdir do |dir|
21
+ execute_solc(dir, filename)
22
+ Dir.foreach(dir) do |file|
23
+ process_file(dir, file, result)
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ private
30
+ def process_file(dir, file, result)
31
+ extension = File.extname(file)
32
+ path = "#{dir}/#{file}"
33
+ basename = File.basename(path, extension)
34
+ unless File.directory?(path)
35
+ result[basename] ||= {}
36
+ result[basename][extension[1..-1]] = File.read(path)
37
+ end
38
+ end
39
+
40
+ def execute_solc(dir, filename)
41
+ cmd = "#{@bin_path} #{@args} '#{dir}' '#{filename}'"
42
+ stdout, stderr, status = Open3.capture3(cmd)
43
+ raise SystemCallError, "Unanable to run solc compliers" if status.exitstatus == 127
44
+ raise CompilationError, stderr unless status.exitstatus == 0
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,36 @@
1
+ module Ethereum
2
+
3
+ class Transaction
4
+ attr_accessor :id, :mined, :connection, :input, :input_parameters
5
+
6
+ def initialize(id, connection, data, input_parameters = [])
7
+ @mined = false
8
+ @connection = connection
9
+ @id = id
10
+ @input = data
11
+ @input_parameters = input_parameters
12
+ end
13
+
14
+ def address
15
+ @id
16
+ end
17
+
18
+ def mined?
19
+ return true if @mined
20
+ @mined = @connection.eth_get_transaction_by_hash(@id)["result"]["blockNumber"].present?
21
+ end
22
+
23
+ def wait_for_miner(timeout = 1500.seconds)
24
+ start_time = Time.now
25
+ while self.mined? == false
26
+ raise Timeout::Error if ((Time.now - start_time) > timeout)
27
+ sleep 5
28
+ return true if self.mined?
29
+ end
30
+ end
31
+
32
+ def self.from_blockchain(address, connection = IpcClient.new)
33
+ Transaction.new(address, connection, nil, nil)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Ethereum
2
+ VERSION = "1.6.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ require File.expand_path("../../ethereum.rb", __FILE__)
2
+
3
+ namespace :ethereum do
4
+ namespace :contract do
5
+
6
+ desc "Compile a contract"
7
+ task :compile, [:path] do |t, args|
8
+ contract = Ethereum::Solidity.new.compile(args[:path])
9
+ puts "Contract abi:"
10
+ puts contract["Works"]["abi"]
11
+ puts
12
+ puts "Contract binary code:"
13
+ puts contract["Works"]["bin"]
14
+ puts
15
+ end
16
+
17
+ desc "Compile and deploy contract"
18
+ task :deploy, [:path] do |t, args|
19
+ puts "Deploing contract #{args[:path]}"
20
+ @works = Ethereum::Contract.from_file(args[:path])
21
+ @works.deploy_and_wait { puts "." }
22
+ address = @works.deployment.contract_address
23
+ puts "Contract deployed under address: #{address}"
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,51 @@
1
+ require 'open3'
2
+
3
+ namespace :ethereum do
4
+ namespace :node do
5
+
6
+ desc "Run testnet node "
7
+ task :test do
8
+ stdout, stdeerr, status = Open3.capture3("parity --chain ~/.parity/ropsten.json account list")
9
+ account = stdeerr.split(/[\[,\]]/)[1]
10
+ cmd = "parity --chain ~/.parity/ropsten.json --password ~/.parity/pass --unlock #{account} --author #{account}"
11
+ puts cmd
12
+ system cmd
13
+ end
14
+
15
+ desc "Run morden (production) node"
16
+ task :run do
17
+ stdout, stdeerr, status = Open3.capture3("parity account list")
18
+ account = stdeerr.split(/[\[,\]]/)[1]
19
+ system "parity --password ~/.parity/pass --unlock #{account} --author #{account} --no-jsonrpc"
20
+ end
21
+
22
+ desc "Mine ethereum testing environment for ethereum node"
23
+ task :mine do
24
+ cmd = "ethminer"
25
+ puts cmd
26
+ system cmd
27
+ end
28
+
29
+ desc "Check if node is syncing"
30
+ task :waitforsync do
31
+ formatter = Ethereum::Formatter.new
32
+ begin
33
+ while (1) do
34
+ result = Ethereum::Singleton.instance.eth_syncing["result"]
35
+ unless result
36
+ puts "Synced"
37
+ break
38
+ else
39
+ current = formatter.to_int(result["currentBlock"])
40
+ highest = formatter.to_int(result["highestBlock"])
41
+ puts "Syncing block: #{current}/#{highest}"
42
+ end
43
+ sleep 5
44
+ end
45
+ rescue
46
+ puts "Ethereum node not running?"
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,32 @@
1
+ namespace :ethereum do
2
+ namespace :test do
3
+
4
+ desc "Setup testing environment for ethereum node"
5
+ task :setup do
6
+ @client = Ethereum::Singleton.instance
7
+
8
+ network_id = @client.net_version["result"].to_i
9
+ raise "Error: Run your tests on ropsten testnet. Use rake ethereum:node:test to run node. Net id: #{network_id}" if network_id != 3
10
+
11
+ accounts = @client.eth_accounts["result"]
12
+ if accounts.size > 0
13
+ puts "Account already exist, skipping this step"
14
+ else
15
+ puts "Creating account..."
16
+ `parity --chain testnet account new`
17
+ end
18
+
19
+ balance = @client.eth_get_balance(@client.default_account)["result"]
20
+ formatter = Ethereum::Formatter.new
21
+ balance = formatter.to_int(balance)
22
+ balance = formatter.from_wei(balance).to_f
23
+
24
+ if balance.to_f > 0.02
25
+ puts "Done. You're ready to run tests.\nTests will use ether from account: #{@client.default_account} with #{balance} ether"
26
+ else
27
+ puts "Not enough ether to run tests. \nYou have: #{balance} ether. \nYou need at least 0.02 ether to run tests.\nTransfer ether to account: #{@client.default_account}.\nThe easiest way to get ether is to use Ethereum Testnet Faucet."
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,24 @@
1
+ require 'pp'
2
+ require File.expand_path("../../ethereum.rb", __FILE__)
3
+
4
+ namespace :ethereum do
5
+ namespace :transaction do
6
+
7
+ desc "Get info about transaction"
8
+ task :byhash, [:id] do |t, args|
9
+ @client = Ethereum::Singleton.instance
10
+ pp @client.eth_get_transaction_by_hash(args[:id])
11
+ end
12
+
13
+ desc "Send"
14
+ task :send, [:address, :amount] do |t, args|
15
+ @client = Ethereum::Singleton.instance
16
+ @formatter = Ethereum::Formatter.new
17
+ address = @formatter.to_address(args[:address])
18
+ value = @client.int_to_hex(@formatter.to_wei(args[:amount].to_f))
19
+ puts "Transfer from: #{@client.default_account} to: #{address}, amount: #{value}wei"
20
+ pp @client.eth_send_transaction({from: @client.default_account, to: address, value: value})
21
+ end
22
+
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,198 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ethereum.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.6.0
5
+ platform: ruby
6
+ authors:
7
+ - Marek Kirejczyk
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-18 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.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: 5.0.1
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 5.0.1
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: 1.1.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.1.0
97
+ description: Ethereum.rb is Ruby Ethereum client using the JSON-RPC interface. Provides
98
+ interface for sending transactions, creating and interacting with contracts as well
99
+ as usefull toolkit to work with Ethereum node.
100
+ email:
101
+ - marek.kirejczyk@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".ruby-gemset"
109
+ - ".travis.yml"
110
+ - CODE_OF_CONDUCT.md
111
+ - Gemfile
112
+ - LICENSE
113
+ - LICENSE.txt
114
+ - README.md
115
+ - Rakefile
116
+ - bin/console
117
+ - bin/install_parity
118
+ - bin/setup
119
+ - contracts/AccountingLib.sol
120
+ - contracts/AuditorInterface.sol
121
+ - contracts/AuditorRegistry.sol
122
+ - contracts/CustodianInterface.sol
123
+ - contracts/CustodianRegistry.sol
124
+ - contracts/DigixConfiguration.sol
125
+ - contracts/Directory.sol
126
+ - contracts/DoublyLinked.sol
127
+ - contracts/GenericInterface.sol
128
+ - contracts/GenericRegistry.sol
129
+ - contracts/Gold.sol
130
+ - contracts/GoldRegistry.sol
131
+ - contracts/GoldTokenLedger.sol
132
+ - contracts/Interface.sol
133
+ - contracts/Minter.sol
134
+ - contracts/Recaster.sol
135
+ - contracts/Testing.sol
136
+ - contracts/VendorInterface.sol
137
+ - contracts/VendorRegistry.sol
138
+ - contracts/classic/Digixbot.sol
139
+ - contracts/classic/DigixbotConfiguration.sol
140
+ - contracts/classic/DigixbotEthereum.sol
141
+ - contracts/classic/DigixbotUsers.sol
142
+ - contracts/classic/Gold.sol
143
+ - contracts/classic/GoldRegistry.sol
144
+ - contracts/classic/GoldTokenLedger.sol
145
+ - contracts/classic/GoldTokenMinter.sol
146
+ - contracts/classic/ParticipantRegistry.sol
147
+ - contracts/classic/QueueSample.sol
148
+ - ethereum.gemspec
149
+ - lib/ethereum.rb
150
+ - lib/ethereum/client.rb
151
+ - lib/ethereum/contract.rb
152
+ - lib/ethereum/contract_event.rb
153
+ - lib/ethereum/contract_initializer.rb
154
+ - lib/ethereum/deployment.rb
155
+ - lib/ethereum/formatter.rb
156
+ - lib/ethereum/function.rb
157
+ - lib/ethereum/function_input.rb
158
+ - lib/ethereum/function_output.rb
159
+ - lib/ethereum/http_client.rb
160
+ - lib/ethereum/initializer.rb
161
+ - lib/ethereum/ipc_client.rb
162
+ - lib/ethereum/project_initializer.rb
163
+ - lib/ethereum/railtie.rb
164
+ - lib/ethereum/singleton.rb
165
+ - lib/ethereum/solidity.rb
166
+ - lib/ethereum/transaction.rb
167
+ - lib/ethereum/version.rb
168
+ - lib/tasks/ethereum_contract.rake
169
+ - lib/tasks/ethereum_node.rake
170
+ - lib/tasks/ethereum_test.rake
171
+ - lib/tasks/ethereum_transaction.rake
172
+ homepage: https://github.com/marekkirejczyk/ethereum.rb
173
+ licenses:
174
+ - MIT
175
+ metadata:
176
+ allowed_push_host: https://rubygems.org
177
+ post_install_message:
178
+ rdoc_options: []
179
+ require_paths:
180
+ - lib
181
+ - bin
182
+ required_ruby_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ required_rubygems_version: !ruby/object:Gem::Requirement
188
+ requirements:
189
+ - - ">="
190
+ - !ruby/object:Gem::Version
191
+ version: '0'
192
+ requirements: []
193
+ rubyforge_project:
194
+ rubygems_version: 2.5.2
195
+ signing_key:
196
+ specification_version: 4
197
+ summary: Ruby Ethereum client using the JSON-RPC interface
198
+ test_files: []