-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethereum.rb
99 lines (82 loc) · 2.98 KB
/
ethereum.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require 'socket'
require 'json'
require 'date'
require 'active_support/core_ext'
class Ethereum
DEFAULT_PORT = 8080
DEFAULT_HOST = 'localhost'
class Connection
attr_reader :socket
def initialize(host = DEFAULT_HOST, port = DEFAULT_PORT)
@socket = ::TCPSocket.new(host, port)
end
def send_command(request)
b = @socket.puts(request.to_json)
response = @socket.gets
response = ::JSON.parse(response)
return ::JSON.parse(response["result"]) unless response["error"]
end
def get_balance_at(address)
request = {id: 1, method: "EthereumApi.GetBalanceAt", params: [{address: address}]}
response = send_command(request)
return response["result"]["balance"] unless response["error"]
end
def get_block(hash)
request = {id: 1, method: "EthereumApi.GetBlock", params: [{hash: hash}]}
response = send_command(request)
return Block.new(response["result"]) unless response["error"]
end
def get_key
request = {id: 1, method: "EthereumApi.GetKey", params: [{}]}
response = send_command(request)
return response["result"] unless response["error"]
end
def get_storage_at(address, key)
request = {id: 1, method: "Ethereum.GetStorageAt", params: [{address: address, key: key}]}
response = send_command(request)
return response["result"] unless response["error"]
end
def transact(recipient, value, gas, gas_price)
request = {id: 1, method: "EthereumApi.Transact", params: [{recipient: recipient, value: value, gas: gas, gasprice: gas_price}]}
puts request.to_json
response = send_command(request)
return response["result"] unless response["error"]
end
def create(init, body, value, gas, gas_price)
init_content = ::File.read(init).gsub("\n", " ")
body_content = ::File.read(body).gsub("\n", " ")
request = {id: 1, method: "EthereumApi.Create", params: [{init: init_content, body: body_content, value: value, gas: gas, gasprice: gas_price}]}
puts request.to_json
response = send_command(request)
end
end
class Block
attr_reader :transactions, :hash, :number, :time
def initialize(rawdata)
@number, @hash, @time = rawdata["number"], rawdata["hash"], DateTime.strptime(rawdata["time"].to_s, '%s')
transactions = rawdata["transactions"] == "null" ? nil : []
if transactions != nil
@transactions = []
::JSON.parse(rawdata["transactions"]).each do |tx|
@transactions << Transaction.new(tx)
end
else
@transactions = nil
end
end
end
class Transaction
attr_reader :value, :gas, :gas_price, :hash, :address, :sender, :raw_data, :data, :is_contract, :creates_contract
def initialize(txhash)
txhash.each do |key, value|
self.instance_variable_set("@#{key.underscore}".to_sym, value)
end
end
def is_contract?
@is_contract
end
def creates_contract?
@creates_contract
end
end
end