Class: Web3
Constant Summary collapse
- @@jsonrpc =
"2.0"- @@debug =
ENV["ETH_DEBUG"] || false
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#id ⇒ Object
Returns the value of attribute id.
Instance Method Summary collapse
-
#do_request(method, params = [], id = @id) ⇒ Object
TODO: More error handling.
- #ether_to_0xwei(ether) ⇒ Object
- #ether_to_wei(ether) ⇒ Object
-
#initialize(endpoint = ENV["ETH_ENDPOINT"] || "http://localhost:8545", id = ENV["ETH_DEFAULT_CLIENT_ID"] || 999) ⇒ Web3
constructor
A new instance of Web3.
-
#sendEther(from_address, to_address, ether, password) ⇒ Object
Convenience function to simply send ether from one account to another, using the default gas settings.
- #to_0x(decimal) ⇒ Object
-
#to_decimal(hex) ⇒ Object
Utility methods.
- #to_hex(decimal) ⇒ Object
- #wei_to_ether(wei) ⇒ Object
Methods included from GeneratedWeb3Methods
#eth_accounts, #eth_blockNumber, #eth_call, #eth_coinbase, #eth_compileLLL, #eth_compileSerpent, #eth_compileSolidity, #eth_estimateGas, #eth_gasPrice, #eth_getBalance, #eth_getBlockByHash, #eth_getBlockByNumber, #eth_getBlockTransactionCountByHash, #eth_getBlockTransactionCountByNumber, #eth_getCode, #eth_getCompilers, #eth_getFilterChanges, #eth_getFilterLogs, #eth_getLogs, #eth_getStorageAt, #eth_getTransactionByBlockHashAndIndex, #eth_getTransactionByBlockNumberAndIndex, #eth_getTransactionByHash, #eth_getTransactionCount, #eth_getTransactionReceipt, #eth_getUncleByBlockHashAndIndex, #eth_getUncleByBlockNumberAndIndex, #eth_getUncleCountByBlockHash, #eth_getUncleCountByBlockNumber, #eth_getWork, #eth_hashrate, #eth_mining, #eth_newBlockFilter, #eth_newFilter, #eth_newPendingTransactionFilter, #eth_protocolVersion, #eth_sendRawTransaction, #eth_sendTransaction, #eth_sign, #eth_submitHashrate, #eth_submitWork, #eth_syncing, #eth_uninstallFilter, #net_listening, #net_peerCount, #net_version, #personal_importRawKey, #personal_listAccounts, #personal_lockAccount, #personal_newAccount, #personal_signAndSendTransaction, #personal_unlockAccount, #shh_addToGroup, #shh_getFilterChanges, #shh_getMessages, #shh_hasIdentity, #shh_newFilter, #shh_newGroup, #shh_newIdentity, #shh_post, #shh_uninstallFilter, #shh_version, #web3_clientVersion, #web3_sha3
Constructor Details
#initialize(endpoint = ENV["ETH_ENDPOINT"] || "http://localhost:8545", id = ENV["ETH_DEFAULT_CLIENT_ID"] || 999) ⇒ Web3
Returns a new instance of Web3.
12 13 14 15 16 |
# File 'lib/web3.rb', line 12 def initialize( endpoint = ENV["ETH_ENDPOINT"] || "http://localhost:8545", id = ENV["ETH_DEFAULT_CLIENT_ID"] || 999) @endpoint = endpoint @id = id end |
Instance Attribute Details
#address ⇒ Object
Returns the value of attribute address.
7 8 9 |
# File 'lib/web3.rb', line 7 def address @address end |
#debug ⇒ Object
Returns the value of attribute debug.
7 8 9 |
# File 'lib/web3.rb', line 7 def debug @debug end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
7 8 9 |
# File 'lib/web3.rb', line 7 def endpoint @endpoint end |
#id ⇒ Object
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
TODO: More error handling
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 |
# File 'lib/web3.rb', line 18 def do_request(method, params = [], id = @id) #TODO: More error handling 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"] = response["error"]["message"] raise "In response to " + method + " request, JSON-RPC endpoint returned error #" + code.to_s() + ": " + end response end |
#ether_to_0xwei(ether) ⇒ Object
91 92 93 |
# File 'lib/web3.rb', line 91 def ether_to_0xwei(ether) to_0x(ether_to_wei(ether)) end |
#ether_to_wei(ether) ⇒ Object
87 88 89 |
# File 'lib/web3.rb', line 87 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
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/web3.rb', line 98 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
79 80 81 |
# File 'lib/web3.rb', line 79 def to_0x(decimal) "0x" + to_hex(decimal) end |
#to_decimal(hex) ⇒ Object
Utility methods
59 60 61 62 63 64 65 66 67 |
# File 'lib/web3.rb', line 59 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
69 70 71 72 73 74 75 76 77 |
# File 'lib/web3.rb', line 69 def to_hex(decimal) if decimal == nil return nil end if decimal.is_a?(String) return decimal end decimal.to_s(16) end |
#wei_to_ether(wei) ⇒ Object
83 84 85 |
# File 'lib/web3.rb', line 83 def wei_to_ether(wei) 1.0 * wei / 10**18 end |