Class: Ethereum::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/elchapo/ethereum/client.rb

Direct Known Subclasses

Connection, HttpConnection

Constant Summary collapse

DEFAULT_GAS_LIMIT =
4_000_000
DEFAULT_GAS_PRICE =
22_000_000_000
RPC_COMMANDS =
%w(web3_clientVersion web3_sha3 net_version net_peerCount net_listening eth_protocolVersion eth_syncing eth_coinbase eth_mining eth_hashrate eth_gasPrice eth_accounts eth_blockNumber eth_getBalance eth_getStorageAt eth_getTransactionCount eth_getBlockTransactionCountByHash eth_getBlockTransactionCountByNumber eth_getUncleCountByBlockHash eth_getUncleCountByBlockNumber eth_getCode eth_sign eth_sendTransaction eth_sendRawTransaction eth_call eth_estimateGas eth_getBlockByHash eth_getBlockByNumber eth_getTransactionByHash eth_getTransactionByBlockHashAndIndex eth_getTransactionByBlockNumberAndIndex eth_getTransactionReceipt eth_getUncleByBlockHashAndIndex eth_getUncleByBlockNumberAndIndex eth_getCompilers eth_compileLLL eth_compileSolidity eth_compileSerpent eth_newFilter eth_newBlockFilter eth_newPendingTransactionFilter eth_uninstallFilter eth_getFilterChanges eth_getFilterLogs eth_getLogs eth_getWork eth_submitWork eth_submitHashrate db_putString db_getString db_putHex db_getHex shh_post shh_version shh_newIdentity shh_hasIdentity shh_newGroup shh_addToGroup shh_newFilter shh_uninstallFilter shh_getFilterChanges shh_getMessages)
RPC_MANAGEMENT_COMMANDS =
%w(admin_addPeer admin_datadir admin_nodeInfo admin_peers admin_setSolc admin_startRPC admin_startWS admin_stopRPC admin_stopWS debug_backtraceAt debug_blockProfile debug_cpuProfile debug_dumpBlock debug_gcStats debug_getBlockRlp debug_goTrace debug_memStats debug_seedHash debug_setHead debug_setBlockProfileRate debug_stacks debug_startCPUProfile debug_startGoTrace debug_stopCPUProfile debug_stopGoTrace debug_traceBlock debug_traceBlockByNumber debug_traceBlockByHash debug_traceBlockFromFile debug_traceTransaction debug_verbosity debug_vmodule debug_writeBlockProfile debug_writeMemProfile miner_hashrate miner_makeDAG miner_setExtra miner_setGasPrice miner_start miner_startAutoDAG miner_stop miner_stopAutoDAG personal_importRawKey personal_listAccounts personal_lockAccount personal_newAccount personal_unlockAccount personal_sendTransaction txpool_content txpool_inspect txpool_status)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/elchapo/ethereum/client.rb', line 16

def initialize
  @id = 0
  @log = log
  @batch = nil
  # @formatter = Ethereum::Formatter.new
  @gas_price = DEFAULT_GAS_PRICE
  @gas_limit = DEFAULT_GAS_LIMIT
  if @log == true
    @logger = Logger.new("/tmp/ethereum_ruby_http.log")
  end
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



14
15
16
# File 'lib/elchapo/ethereum/client.rb', line 14

def command
  @command
end

#default_accountObject

Returns the value of attribute default_account.



14
15
16
# File 'lib/elchapo/ethereum/client.rb', line 14

def 
  @default_account
end

#gas_limitObject

Returns the value of attribute gas_limit.



14
15
16
# File 'lib/elchapo/ethereum/client.rb', line 14

def gas_limit
  @gas_limit
end

#gas_priceObject

Returns the value of attribute gas_price.



14
15
16
# File 'lib/elchapo/ethereum/client.rb', line 14

def gas_price
  @gas_price
end

#idObject

Returns the value of attribute id.



14
15
16
# File 'lib/elchapo/ethereum/client.rb', line 14

def id
  @id
end

#logObject

Returns the value of attribute log.



14
15
16
# File 'lib/elchapo/ethereum/client.rb', line 14

def log
  @log
end

#loggerObject

Returns the value of attribute logger.



14
15
16
# File 'lib/elchapo/ethereum/client.rb', line 14

def logger
  @logger
end

Instance Method Details

#encode_params(params) ⇒ Object



41
42
43
# File 'lib/elchapo/ethereum/client.rb', line 41

def encode_params(params)
  params.map(&method(:int_to_hex))
end

#get_idObject



28
29
30
31
# File 'lib/elchapo/ethereum/client.rb', line 28

def get_id
  @id += 1
  return @id
end

#int_to_hex(p) ⇒ Object



37
38
39
# File 'lib/elchapo/ethereum/client.rb', line 37

def int_to_hex(p)
  p.is_a?(Integer) ? "0x#{p.to_s(16)}" : p
end

#reset_idObject



33
34
35
# File 'lib/elchapo/ethereum/client.rb', line 33

def reset_id
  @id = 0
end

#send_command(command, args) ⇒ Object

Raises:

  • (IOError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/elchapo/ethereum/client.rb', line 45

def send_command(command,args)
  if ["eth_getBalance", "eth_call"].include?(command)
    args << "latest"
  end

  payload = {jsonrpc: "2.0", method: command, params: encode_params(args), id: get_id}
  @logger.info("Sending #{payload.to_json}") if @log

  output = JSON.parse(send_single(payload.to_json))
  @logger.info("Received #{output.to_json}") if @log
  reset_id
  raise IOError, output["error"]["message"] if output["error"]
  return output["result"]
end