Class: Web3

Inherits:
Object
  • Object
show all
Includes:
EthCalls, NetCalls, PersonalCalls, ShhCalls, TxpoolCalls, Web3Calls
Defined in:
lib/web3.rb

Constant Summary collapse

@@jsonrpc =
"2.0"
@@debug =
ENV["ETH_DEBUG"] || false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint = ENV["ETH_ENDPOINT"] || "http://localhost:8545", id = ENV["ETH_DEFAULT_CLIENT_ID"] || 999) ⇒ Web3

the default endpoint is localhost:8545 and default client id is 999 these can be set as envioronment variables - ETH_ENDPOINT and ETH_DEFAULT_CLIENT_ID



14
15
16
17
18
# File 'lib/web3.rb', line 14

def initialize( endpoint = ENV["ETH_ENDPOINT"] || "http://localhost:8545",
                id = ENV["ETH_DEFAULT_CLIENT_ID"] || 999)
  @endpoint = endpoint
  @id = id
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



7
8
9
# File 'lib/web3.rb', line 7

def address
  @address
end

#debugObject

Returns the value of attribute debug.



7
8
9
# File 'lib/web3.rb', line 7

def debug
  @debug
end

#endpointObject

Returns the value of attribute endpoint.



7
8
9
# File 'lib/web3.rb', line 7

def endpoint
  @endpoint
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/web3.rb', line 7

def id
  @id
end

Instance Method Details

#do_request(method, params = [], id = @id) ⇒ Object

this is the method responsible for communicating with the endpoint it gets called by all the action-specific methods



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
# File 'lib/web3.rb', line 22

def do_request(method, params = [], id = @id)
  request_json = { :jsonrpc => @@jsonrpc,
             :method => method,
             :params => params,
             :id => @id
           }.to_json

  if @@debug
    puts "Request to " + @endpoint + ": " + request_json + "  ...."
  end

  response = HTTParty.post(@endpoint,
      :body => request_json,
      :headers => { 'Content-Type' => 'application/json' } )

  if @@debug
    puts "Response: " + response.to_s()
  end

  if response.bad_gateway?
    raise "Unable to connect to JSON-RPC endpont" + @endpoint
  end

  if !response.success?
    raise "JSON-RPC endpoint " + @endpoint + " returned http code " + response.code.to_s()
  end

  if response["error"]
    code = response["error"]["code"]
    message = response["error"]["message"]
    raise "In response to " + method + " request, JSON-RPC endpoint returned error #" + code.to_s() + ": " + message
  end
  response
end

#ether_to_0xwei(ether) ⇒ Object

Converts either to wei a hex-formatted string, including the 0x indicator



121
122
123
# File 'lib/web3.rb', line 121

def ether_to_0xwei(ether)
  to_0x(ether_to_wei(ether))
end

#ether_to_wei(ether) ⇒ Object

Converts either to wei



116
117
118
# File 'lib/web3.rb', line 116

def ether_to_wei(ether)
  (ether * 10**18).round()
end

#sendEther(from_address, to_address, ether, password) ⇒ Object

Convenience function to simply send ether from one account to another, using the default gas settings. This requires the personal api to be active. See github.com/ethereum/go-ethereum/wiki/Management-APIs



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/web3.rb', line 128

def sendEther(from_address, to_address, ether, password)
  trans = {}
  trans["from"] = from_address
  trans["to"] = to_address
  trans["value"] = ether_to_0xwei(ether)
#    if gas != nil

#        trans["gas"] = to_hex(gas) #should this to_hex or to_0x?

#    end

#    if gasPrice != nil

#      trans["gasPrice"] = to_hex(gasPrice)

#    end

  personal_signAndSendTransaction(trans, password)
end

#to_0x(decimal) ⇒ Object

Converts a decimal integer to a hex string that starts with a 0x marker



106
107
108
# File 'lib/web3.rb', line 106

def to_0x(decimal)
  "0x" + to_hex(decimal)
end

#to_decimal(hex) ⇒ Object

Converts a hex string or int to a decimal integer



83
84
85
86
87
88
89
90
91
# File 'lib/web3.rb', line 83

def to_decimal(hex)
  if hex == nil
    return nil
  end
  if hex.is_a?(Integer)
    return hex
  end
  hex.to_i(16)
end

#to_hex(decimal) ⇒ Object

Converts a decimal integer to a hex string



94
95
96
97
98
99
100
101
102
# File 'lib/web3.rb', line 94

def to_hex(decimal)
  if decimal == nil
    return nil
  end
  if decimal.is_a?(String)
    return decimal
  end
  decimal.to_s(16) #this will throw an error if a non-integer is used

end

#wei_to_ether(wei) ⇒ Object

Converts wei to ether



111
112
113
# File 'lib/web3.rb', line 111

def wei_to_ether(wei)
  1.0 * wei / 10**18
end