class EvmClient::Client
Constants
- DEFAULT_BLOCK_NUMBER
- DEFAULT_GAS_LIMIT
- DEFAULT_GAS_PRICE
- RPC_COMMANDS
- RPC_MANAGEMENT_COMMANDS
Attributes
block_number[RW]
command[RW]
default_account[RW]
gas_limit[RW]
gas_price[RW]
id[RW]
log[RW]
logger[RW]
Public Class Methods
create(host_or_ipcpath, log = false)
click to toggle source
# 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
new(log = false)
click to toggle source
# 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
Public Instance Methods
batch() { || ... }
click to toggle source
# 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)
click to toggle source
# File lib/evm_client/client.rb, line 65 def encode_params(params) params.map(&method(:int_to_hex)) end
get_balance(address)
click to toggle source
# File lib/evm_client/client.rb, line 69 def get_balance(address) eth_get_balance(address)["result"].to_i(16) end
get_chain()
click to toggle source
# File lib/evm_client/client.rb, line 73 def get_chain @net_version ||= net_version["result"].to_i end
get_id()
click to toggle source
# File lib/evm_client/client.rb, line 48 def get_id @id += 1 return @id end
get_nonce(address)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/evm_client/client.rb, line 53 def reset_id @id = 0 end
send_command(command,args)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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