class Web3
Attributes
address[RW]
debug[RW]
endpoint[RW]
id[RW]
Public Class Methods
new( endpoint = ENV["ETH_ENDPOINT"] || "http://localhost:8545", id = ENV["ETH_DEFAULT_CLIENT_ID"] || 999)
click to toggle source
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
# File 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
Public Instance Methods
createContract(from_address, bin_file, password)
click to toggle source
# File web3.rb, line 142 def createContract(from_address, bin_file, password) trans = {} trans["from"] = from_address trans["data"] = File.read(bin_file) gas = eth_estimateGas[trans] trans["gas"] = ether_to_0xwei(ether) personal_signAndSendTransaction(trans, password) end
do_request(method, params = [], id = @id)
click to toggle source
this is the method responsible for communicating with the endpoint it gets called by all the action-specific methods
# File 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)
click to toggle source
Converts either to wei a hex-formatted string, including the 0x indicator
# File web3.rb, line 121 def ether_to_0xwei(ether) to_0x(ether_to_wei(ether)) end
ether_to_wei(ether)
click to toggle source
Converts either to wei
# File web3.rb, line 116 def ether_to_wei(ether) (ether * 10**18).round() end
getContractAddress(tx_number)
click to toggle source
# File web3.rb, line 151 def getContractAddress(tx_number) receipt = eth_getTransactionReceipt tx_number receipt["contract"] end
sendEther(from_address, to_address, ether, password)
click to toggle source
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
# File 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)
click to toggle source
Converts a decimal integer to a hex string that starts with a 0x marker
# File web3.rb, line 106 def to_0x(decimal) "0x" + to_hex(decimal) end
to_decimal(hex)
click to toggle source
Utility methods
Converts a hex string or int to a decimal integer
# File web3.rb, line 84 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)
click to toggle source
Converts a decimal integer to a hex string
# File web3.rb, line 95 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)
click to toggle source
Converts wei to ether
# File web3.rb, line 111 def wei_to_ether(wei) 1.0 * wei / 10**18 end