Class: EvmClient::Client
- Inherits:
-
Object
- Object
- EvmClient::Client
- Defined in:
- lib/evm_client/client.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_GAS_LIMIT =
4_000_000.freeze
- DEFAULT_GAS_PRICE =
22_000_000_000.freeze
- DEFAULT_BLOCK_NUMBER =
'latest'.freeze
- 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
-
#block_number ⇒ Object
Returns the value of attribute block_number.
-
#command ⇒ Object
Returns the value of attribute command.
-
#default_account ⇒ Object
Returns the value of attribute default_account.
-
#gas_limit ⇒ Object
Returns the value of attribute gas_limit.
-
#gas_price ⇒ Object
Returns the value of attribute gas_price.
-
#id ⇒ Object
Returns the value of attribute id.
-
#log ⇒ Object
Returns the value of attribute log.
-
#logger ⇒ Object
Returns the value of attribute logger.
Class Method Summary collapse
Instance Method Summary collapse
- #batch ⇒ Object
- #encode_params(params) ⇒ Object
- #get_balance(address) ⇒ Object
- #get_chain ⇒ Object
- #get_id ⇒ Object
- #get_nonce(address) ⇒ Object
-
#initialize(log = false) ⇒ Client
constructor
A new instance of Client.
- #int_to_hex(p) ⇒ Object
- #reset_id ⇒ Object
- #send_command(command, args) ⇒ Object
- #transfer(key, address, amount) ⇒ Object
- #transfer_and_wait(key, address, amount) ⇒ Object
- #transfer_to(address, amount) ⇒ Object
- #transfer_to_and_wait(address, amount) ⇒ Object
- #wait_for(tx) ⇒ Object
Constructor Details
#initialize(log = false) ⇒ Client
Returns a new instance of Client.
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/evm_client/client.rb', line 16 def initialize(log = false) @id = 0 @log = log @batch = nil @formatter = ::EvmClient::Formatter.new @gas_price = DEFAULT_GAS_PRICE @gas_limit = DEFAULT_GAS_LIMIT @block_number = DEFAULT_BLOCK_NUMBER if @log == true @logger = Logger.new("/tmp/ethereum_ruby_http.log") end end |
Instance Attribute Details
#block_number ⇒ Object
Returns the value of attribute block_number.
14 15 16 |
# File 'lib/evm_client/client.rb', line 14 def block_number @block_number end |
#command ⇒ Object
Returns the value of attribute command.
14 15 16 |
# File 'lib/evm_client/client.rb', line 14 def command @command end |
#default_account ⇒ Object
Returns the value of attribute default_account.
14 15 16 |
# File 'lib/evm_client/client.rb', line 14 def default_account @default_account end |
#gas_limit ⇒ Object
Returns the value of attribute gas_limit.
14 15 16 |
# File 'lib/evm_client/client.rb', line 14 def gas_limit @gas_limit end |
#gas_price ⇒ Object
Returns the value of attribute gas_price.
14 15 16 |
# File 'lib/evm_client/client.rb', line 14 def gas_price @gas_price end |
#id ⇒ Object
Returns the value of attribute id.
14 15 16 |
# File 'lib/evm_client/client.rb', line 14 def id @id end |
#log ⇒ Object
Returns the value of attribute log.
14 15 16 |
# File 'lib/evm_client/client.rb', line 14 def log @log end |
#logger ⇒ Object
Returns the value of attribute logger.
14 15 16 |
# File 'lib/evm_client/client.rb', line 14 def logger @logger end |
Class Method Details
.create(host_or_ipcpath, log = false) ⇒ Object
30 31 32 33 34 |
# File 'lib/evm_client/client.rb', line 30 def self.create(host_or_ipcpath, log = false) return IpcClient.new(host_or_ipcpath, log) if host_or_ipcpath.end_with? '.ipc' return HttpClient.new(host_or_ipcpath, log) if host_or_ipcpath.start_with? 'http' raise ArgumentError.new('Unable to detect client type') end |
Instance Method Details
#batch ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/evm_client/client.rb', line 36 def batch @batch = [] yield result = send_batch(@batch) @batch = nil reset_id return result end |
#encode_params(params) ⇒ Object
65 66 67 |
# File 'lib/evm_client/client.rb', line 65 def encode_params(params) params.map(&method(:int_to_hex)) end |
#get_balance(address) ⇒ Object
69 70 71 |
# File 'lib/evm_client/client.rb', line 69 def get_balance(address) eth_get_balance(address)["result"].to_i(16) end |
#get_chain ⇒ Object
73 74 75 |
# File 'lib/evm_client/client.rb', line 73 def get_chain @net_version ||= net_version["result"].to_i end |
#get_id ⇒ Object
48 49 50 51 |
# File 'lib/evm_client/client.rb', line 48 def get_id @id += 1 return @id end |
#get_nonce(address) ⇒ Object
77 78 79 |
# File 'lib/evm_client/client.rb', line 77 def get_nonce(address) eth_get_transaction_count(address, "pending")["result"].to_i(16) end |
#int_to_hex(p) ⇒ Object
61 62 63 |
# File 'lib/evm_client/client.rb', line 61 def int_to_hex(p) p.is_a?(Integer) ? "0x#{p.to_s(16)}" : p end |
#reset_id ⇒ Object
53 54 55 |
# File 'lib/evm_client/client.rb', line 53 def reset_id @id = 0 end |
#send_command(command, args) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/evm_client/client.rb', line 117 def send_command(command,args) if ["eth_getBalance", "eth_call"].include?(command) args << block_number end payload = {jsonrpc: "2.0", method: command, params: encode_params(args), id: get_id} @logger.info("Sending #{payload.to_json}") if @log if @batch @batch << payload return true else 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 end end |
#transfer(key, address, amount) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/evm_client/client.rb', line 91 def transfer(key, address, amount) Eth.configure { |c| c.chain_id = net_version["result"].to_i } args = { from: key.address, to: address, value: amount, data: "", nonce: get_nonce(key.address), gas_limit: gas_limit, gas_price: gas_price } tx = Eth::Tx.new(args) tx.sign key eth_send_raw_transaction(tx.hex)["result"] end |
#transfer_and_wait(key, address, amount) ⇒ Object
107 108 109 |
# File 'lib/evm_client/client.rb', line 107 def transfer_and_wait(key, address, amount) return wait_for(transfer(key, address, amount)) end |
#transfer_to(address, amount) ⇒ Object
82 83 84 |
# File 'lib/evm_client/client.rb', line 82 def transfer_to(address, amount) eth_send_transaction({to: address, value: int_to_hex(amount)}) end |
#transfer_to_and_wait(address, amount) ⇒ Object
86 87 88 |
# File 'lib/evm_client/client.rb', line 86 def transfer_to_and_wait(address, amount) wait_for(transfer_to(address, amount)["result"]) end |
#wait_for(tx) ⇒ Object
111 112 113 114 115 |
# File 'lib/evm_client/client.rb', line 111 def wait_for(tx) transaction = EvmClient::Transaction.new(tx, self, "", []) transaction.wait_for_miner return transaction end |