class Etherlite::Account::PrivateKey

Public Class Methods

new(_connection, _pk) click to toggle source
Calls superclass method Etherlite::Account::Base::new
# File lib/etherlite/account/private_key.rb, line 6
def initialize(_connection, _pk)
  @key = Eth::Key.new priv: _pk
  super _connection, Etherlite::Utils.normalize_address(@key.address)
end

Public Instance Methods

build_raw_transaction(_options = {}) click to toggle source
# File lib/etherlite/account/private_key.rb, line 11
def build_raw_transaction(_options = {})
  nonce = nonce_manager.next_nonce_for(normalized_address, _options.slice(:replace, :nonce))

  tx = Eth::Tx.new(
    value: _options.fetch(:value, 0),
    data: _options.fetch(:data, ''),
    gas_limit: _options.fetch(:gas, 90_000),
    gas_price: _options.fetch(:gas_price, gas_price),
    to: (Etherlite::Utils.encode_address_param(_options[:to]) if _options.key?(:to)),
    nonce: nonce
  )

  sign_with_connection_chain tx

  tx
end
send_transaction(_options = {}) click to toggle source
# File lib/etherlite/account/private_key.rb, line 28
def send_transaction(_options = {})
  tx = build_raw_transaction(_options)

  nonce_manager.with_next_nonce_for(normalized_address, nonce: tx.nonce) do |nonce|
    Etherlite::Transaction.new @connection, @connection.eth_send_raw_transaction(tx.hex)
  end
end

Private Instance Methods

gas_price() click to toggle source
# File lib/etherlite/account/private_key.rb, line 40
def gas_price
  # TODO: improve on this
  @gas_price ||= connection.eth_gas_price
end
nonce_manager() click to toggle source
# File lib/etherlite/account/private_key.rb, line 45
def nonce_manager
  Etherlite::NonceManager.new @connection
end
sign_with_connection_chain(_tx) click to toggle source
# File lib/etherlite/account/private_key.rb, line 49
def sign_with_connection_chain(_tx)
  @@eth_mutex.synchronize do
    Eth.configure { |c| c.chain_id = @connection.chain_id }
    _tx.sign @key
  end
end