|
1 | 1 | require 'net/http' |
2 | 2 | module Ethereum |
3 | 3 | class HttpClient < Client |
4 | | - attr_accessor :command, :id, :host, :port, :batch, :converted_transactions, :uri, :ssl, :logger, :log |
| 4 | + attr_accessor :host, :port, :uri, :ssl |
5 | 5 |
|
6 | | - def initialize(host, port, ssl = false, log = true) |
| 6 | + def initialize(host, port, ssl = false, log = false) |
| 7 | + super(log) |
7 | 8 | @host = host |
8 | 9 | @port = port |
9 | | - @id = 1 |
10 | 10 | @ssl = ssl |
11 | | - @log = log |
12 | | - if @log == true |
13 | | - @logger = Logger.new("/tmp/ethereum_ruby_http.log") |
14 | | - end |
15 | 11 | if ssl |
16 | 12 | @uri = URI("https://#{@host}:#{@port}") |
17 | 13 | else |
18 | 14 | @uri = URI("http://#{@host}:#{@port}") |
19 | 15 | end |
20 | | - @batch = [] |
21 | 16 | end |
22 | 17 |
|
23 | | - RPC_COMMANDS.each do |rpc_command| |
24 | | - method_name = "#{rpc_command.split("_")[1].underscore}" |
25 | | - define_method method_name do |*args| |
26 | | - command = rpc_command |
27 | | - if command == "eth_call" |
28 | | - args << "latest" |
29 | | - end |
30 | | - payload = {jsonrpc: "2.0", method: command, params: args, id: get_id} |
31 | | - http = ::Net::HTTP.new(@host, @port) |
32 | | - if @ssl |
33 | | - http.use_ssl = true |
34 | | - end |
35 | | - header = {'Content-Type' => 'application/json'} |
36 | | - request = ::Net::HTTP::Post.new(uri, header) |
37 | | - if @log == true |
38 | | - @logger.info("Sending #{payload.to_json}") |
39 | | - end |
40 | | - request.body = payload.to_json |
41 | | - response = http.request(request) |
42 | | - return JSON.parse(response.body) |
43 | | - end |
44 | | - |
45 | | - define_method "#{method_name}_batch" do |*args| |
46 | | - command = rpc_command |
47 | | - payload = {jsonrpc: "2.0", method: command, params: args, id: get_id} |
48 | | - @batch << payload.to_json |
| 18 | + def send_single(payload) |
| 19 | + http = ::Net::HTTP.new(@host, @port) |
| 20 | + if @ssl |
| 21 | + http.use_ssl = true |
49 | 22 | end |
| 23 | + header = {'Content-Type' => 'application/json'} |
| 24 | + request = ::Net::HTTP::Post.new(uri, header) |
| 25 | + request.body = payload |
| 26 | + response = http.request(request) |
| 27 | + return response.body |
50 | 28 | end |
51 | 29 |
|
52 | | - def send_batch |
53 | | - |
| 30 | + def send_batch(batch) |
| 31 | + raise NotImplementedError |
54 | 32 | end |
55 | | - |
56 | 33 | end |
57 | 34 |
|
58 | 35 | end |
0 commit comments