Class: Web3

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

Defined Under Namespace

Modules: EthCalls, MinerCalls, NetCalls, PersonalCalls, ShhCalls, TxpoolCalls, Web3Calls

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MinerCalls

#miner_makeDAG, #miner_setExtra, #miner_setGasPrice, #miner_start, #miner_startAutoDAG, #miner_stop, #miner_stopAutoDAG

Methods included from TxpoolCalls

#txpool_status

Methods included from PersonalCalls

#personal_ecRecover, #personal_importRawKey, #personal_listAccounts, #personal_lockAccount, #personal_newAccount, #personal_sign, #personal_signAndSendTransaction, #personal_unlockAccount

Methods included from ShhCalls

#shh_addToGroup, #shh_getFilterChanges, #shh_getMessages, #shh_hasIdentity, #shh_newFilter, #shh_newGroup, #shh_newIdentity, #shh_post, #shh_uninstallFilter, #shh_version

Methods included from EthCalls

#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_pendingTransactions, #eth_protocolVersion, #eth_resend, #eth_sendRawTransaction, #eth_sendTransaction, #eth_sign, #eth_submitHashrate, #eth_submitWork, #eth_syncing, #eth_uninstallFilter

Methods included from NetCalls

#net_listening, #net_peerCount, #net_version

Methods included from Web3Calls

#web3_clientVersion, #web3_sha3

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

#createContract(from_address, bin_file, password) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/web3.rb', line 143

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) ⇒ 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



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

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

#ether_to_wei(ether) ⇒ Object

Converts either to wei



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

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

#getContractAddress(tx_number) ⇒ Object



152
153
154
155
# File 'lib/web3.rb', line 152

def getContractAddress(tx_number)
  receipt = eth_getTransactionReceipt tx_number
  receipt["contract"]
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



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

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



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

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

#to_decimal(hex) ⇒ Object

Converts a hex string or int to a decimal integer



84
85
86
87
88
89
90
91
92
# File 'lib/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) ⇒ Object

Converts a decimal integer to a hex string



95
96
97
98
99
100
101
102
103
# File 'lib/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) ⇒ Object

Converts wei to ether



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

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