ethereum.rb 2.0.10 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b37350a4448636d876aaeb83825c01b2903b4e8
4
- data.tar.gz: ec8e0b741ce6d4f3a7b4ed1125d5e34ebb0f522a
3
+ metadata.gz: 26d401a1321c2035fc258764f3806ccc20fe6eba
4
+ data.tar.gz: 99b9fe1ffbe16b5f4a93640979f1ecbf823c2dc5
5
5
  SHA512:
6
- metadata.gz: 7cfdc7e200941bf4876eb809f7929e9cd8a110f75eec933660814027891c3e56e2c024571587720be515e8549c90bfa6fb3d52e0bfd27c277c3bfb999b76b7f1
7
- data.tar.gz: 53b4188861d6f9f901bbcdcce2f7d9c7f22525633b42281ad4ff5ea047d2ac7e802892f8ce2a5af0f7834f20c0257a6fa75760b990fba632b1293901b5b636af
6
+ metadata.gz: ee49b13d4f24d61a4448e9b94ce3e2607781d554b345ad02c6b9a094e8f98a9d16f81079a1f73fc3488878c20389f5f7ef4b8e3bba10e29d7445db9e708790e4
7
+ data.tar.gz: 59eb35fc5add5ea192dcf0d632c6b7494fdad783364e117e5815ccefb9d2fd48a1843b461135173d798e7d391ce90acb92f27779768de37a5387cf9461eaad33
data/ethereum.gemspec CHANGED
@@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency "rake", "~> 12.0"
30
30
  spec.add_development_dependency "rspec", "~> 3.5"
31
31
  spec.add_development_dependency "pry", "~> 0.10"
32
+ spec.add_development_dependency "eth", "~> 0.4"
32
33
 
33
34
  spec.add_dependency "activesupport", "~> 5.0"
34
35
  spec.add_dependency "digest-sha3", "~> 1.1"
@@ -1,17 +1,25 @@
1
1
  module Ethereum
2
2
  class Client
3
+
4
+ DEFAULT_GAS_LIMIT = 4_000_000
5
+
6
+ DEFAULT_GAS_PRICE = 22_000_000_000
7
+
3
8
  # https://github.com/ethereum/wiki/wiki/JSON-RPC
4
9
  RPC_COMMANDS = %w(web3_clientVersion web3_sha3 net_version net_peerCount net_listening eth_protocolVersion eth_syncing eth_coinbase eth_mining eth_hashrate eth_gasPrice eth_accounts eth_blockNumber eth_getBalance eth_getStorageAt eth_getTransactionCount eth_getBlockTransactionCountByHash eth_getBlockTransactionCountByNumber eth_getUncleCountByBlockHash eth_getUncleCountByBlockNumber eth_getCode eth_sign eth_sendTransaction eth_sendRawTransaction eth_call eth_estimateGas eth_getBlockByHash eth_getBlockByNumber eth_getTransactionByHash eth_getTransactionByBlockHashAndIndex eth_getTransactionByBlockNumberAndIndex eth_getTransactionReceipt eth_getUncleByBlockHashAndIndex eth_getUncleByBlockNumberAndIndex eth_getCompilers eth_compileLLL eth_compileSolidity eth_compileSerpent eth_newFilter eth_newBlockFilter eth_newPendingTransactionFilter eth_uninstallFilter eth_getFilterChanges eth_getFilterLogs eth_getLogs eth_getWork eth_submitWork eth_submitHashrate db_putString db_getString db_putHex db_getHex shh_post shh_version shh_newIdentity shh_hasIdentity shh_newGroup shh_addToGroup shh_newFilter shh_uninstallFilter shh_getFilterChanges shh_getMessages)
10
+
5
11
  # https://github.com/ethereum/go-ethereum/wiki/Management-APIs
6
12
  RPC_MANAGEMENT_COMMANDS = %w(admin_addPeer admin_datadir admin_nodeInfo admin_peers admin_setSolc admin_startRPC admin_startWS admin_stopRPC admin_stopWS debug_backtraceAt debug_blockProfile debug_cpuProfile debug_dumpBlock debug_gcStats debug_getBlockRlp debug_goTrace debug_memStats debug_seedHash debug_setHead debug_setBlockProfileRate debug_stacks debug_startCPUProfile debug_startGoTrace debug_stopCPUProfile debug_stopGoTrace debug_traceBlock debug_traceBlockByNumber debug_traceBlockByHash debug_traceBlockFromFile debug_traceTransaction debug_verbosity debug_vmodule debug_writeBlockProfile debug_writeMemProfile miner_hashrate miner_makeDAG miner_setExtra miner_setGasPrice miner_start miner_startAutoDAG miner_stop miner_stopAutoDAG personal_importRawKey personal_listAccounts personal_lockAccount personal_newAccount personal_unlockAccount personal_sendTransaction txpool_content txpool_inspect txpool_status)
7
13
 
8
- attr_accessor :command, :id, :log, :logger, :default_account
14
+ attr_accessor :command, :id, :log, :logger, :default_account, :gas_price, :gas_limit
9
15
 
10
16
  def initialize(log = false)
11
17
  @id = 0
12
18
  @log = log
13
19
  @batch = nil
14
-
20
+ @formatter = Ethereum::Formatter.new
21
+ @gas_price = DEFAULT_GAS_PRICE
22
+ @gas_limit = DEFAULT_GAS_LIMIT
15
23
  if @log == true
16
24
  @logger = Logger.new("/tmp/ethereum_ruby_http.log")
17
25
  end
@@ -56,6 +64,41 @@ module Ethereum
56
64
  params.map(&method(:int_to_hex))
57
65
  end
58
66
 
67
+ def get_balance(address)
68
+ eth_get_balance(address)["result"].to_i(16)
69
+ end
70
+
71
+ def get_chain
72
+ net_version["result"].to_i(16)
73
+ end
74
+
75
+ def get_nonce(address)
76
+ eth_get_transaction_count(address)["result"].to_i(16)
77
+ end
78
+
79
+ def transfer(key, address, amount)
80
+ Eth.configure { |c| c.chain_id = get_chain }
81
+ args = {
82
+ from: key.address,
83
+ to: address,
84
+ value: amount,
85
+ data: "",
86
+ nonce: get_nonce(key.address),
87
+ gas_limit: gas_limit,
88
+ gas_price: gas_price
89
+ }
90
+ tx = Eth::Tx.new(args)
91
+ tx.sign key
92
+ eth_send_raw_transaction(tx.hex)["result"]
93
+ end
94
+
95
+ def transfer_and_wait(key, address, amount)
96
+ tx = transfer(key, address, amount)
97
+ transaction = Ethereum::Transaction.new(tx, self, "", [])
98
+ transaction.wait_for_miner
99
+ return transaction
100
+ end
101
+
59
102
  def send_command(command,args)
60
103
  if ["eth_getBalance", "eth_call"].include?(command)
61
104
  args << "latest"
@@ -3,7 +3,7 @@ module Ethereum
3
3
 
4
4
  attr_reader :address
5
5
  attr_accessor :key
6
- attr_accessor :gas, :gas_price
6
+ attr_accessor :gas_limit, :gas_price
7
7
  attr_accessor :code, :name, :abi, :class_object, :sender, :deployment, :client
8
8
  attr_accessor :events, :functions, :constructor_inputs
9
9
  attr_accessor :call_raw_proxy, :call_proxy, :transact_proxy, :transact_and_wait_proxy
@@ -19,6 +19,8 @@ module Ethereum
19
19
  @sender = client.default_account
20
20
  @encoder = Encoder.new
21
21
  @decoder = Decoder.new
22
+ @gas_limit = @client.gas_limit
23
+ @gas_price = @client.gas_price
22
24
  end
23
25
 
24
26
  def self.create(file: nil, client: Ethereum::Singleton.instance, code: nil, abi: nil, address: nil, name: nil)
@@ -58,17 +60,31 @@ module Ethereum
58
60
  end
59
61
 
60
62
  def send_transaction(tx_args)
61
- if key
62
- Eth.configure { |c| c.chain_id = @client.net_version["result"].to_i }
63
- tx = Eth::Tx.new({ from: key.address, to: main_address, value: value, data: data, nonce: 0, gas_limit: 1_000_000, gas_price: gas_price})
64
- tx.sign key
65
- else
66
63
  @client.eth_send_transaction(tx_args)["result"]
67
- end
64
+ end
65
+
66
+ def send_raw_transaction(payload, to = nil)
67
+ Eth.configure { |c| c.chain_id = @client.get_chain }
68
+ args = {
69
+ from: key.address,
70
+ value: 0,
71
+ data: payload,
72
+ nonce: @client.get_nonce(key.address),
73
+ gas_limit: gas_limit,
74
+ gas_price: gas_price
75
+ }
76
+ args[:to] = to if to
77
+ tx = Eth::Tx.new(args)
78
+ tx.sign key
79
+ @client.eth_send_raw_transaction(tx.hex)["result"]
68
80
  end
69
81
 
70
82
  def deploy(*params)
71
- tx = send_transaction(deploy_args(params))
83
+ if key
84
+ tx = send_raw_transaction(deploy_payload(params))
85
+ else
86
+ tx = send_transaction(deploy_args(params))
87
+ end
72
88
  tx_failed = tx.nil? || tx == "0x0000000000000000000000000000000000000000000000000000000000000000"
73
89
  raise IOError, "Failed to deploy, did you unlock #{sender} account? Transaction hash: #{tx}" if tx_failed
74
90
  @deployment = Ethereum::Deployment.new(tx, @client)
@@ -113,7 +129,11 @@ module Ethereum
113
129
  end
114
130
 
115
131
  def transact(fun, *args)
116
- tx = send_transaction(call_args(fun, args))
132
+ if key
133
+ tx = send_raw_transaction(call_payload(fun, args), address)
134
+ else
135
+ tx = send_transaction(call_args(fun, args))
136
+ end
117
137
  return Ethereum::Transaction.new(tx, @client, call_payload(fun, args), args)
118
138
  end
119
139
 
@@ -170,7 +190,8 @@ module Ethereum
170
190
  class_methods = Class.new do
171
191
  extend Forwardable
172
192
  def_delegators :parent, :deploy_payload, :deploy_args, :call_payload, :call_args
173
- def_delegators :parent, :gas, :gas_price, :gas=, :gas_price=
193
+ def_delegators :parent, :signed_deploy, :key, :key=
194
+ def_delegators :parent, :gas_limit, :gas_price, :gas_limit=, :gas_price=
174
195
  def_delegators :parent, :abi, :deployment, :events
175
196
  def_delegators :parent, :estimate, :deploy, :deploy_and_wait
176
197
  def_delegators :parent, :address, :address=, :sender, :sender=
@@ -192,8 +213,8 @@ module Ethereum
192
213
 
193
214
  private
194
215
  def add_gas_options_args(args)
195
- args[:gas] = @client.int_to_hex(@gas) if @gas.present?
196
- args[:gasPrice] = @client.int_to_hex(@gas_price) if @gas_price.present?
216
+ args[:gas] = @client.int_to_hex(gas_limit) if gas_limit.present?
217
+ args[:gasPrice] = @client.int_to_hex(gas_price) if gas_price.present?
197
218
  args
198
219
  end
199
220
 
@@ -1,3 +1,3 @@
1
1
  module Ethereum
2
- VERSION = "2.0.10"
2
+ VERSION = "2.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ethereum.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.10
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marek Kirejczyk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-06 00:00:00.000000000 Z
11
+ date: 2017-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: eth
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.4'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: activesupport
71
85
  requirement: !ruby/object:Gem::Requirement