Class: Etherlite::Account::PrivateKey

Inherits:
Base
  • Object
show all
Defined in:
lib/etherlite/account/private_key.rb

Instance Attribute Summary

Attributes inherited from Base

#connection, #normalized_address

Instance Method Summary collapse

Methods inherited from Base

#==, #call, #next_nonce, #transfer_to

Methods included from Etherlite::Api::Address

#address, #get_balance

Constructor Details

#initialize(_connection, _pk) ⇒ PrivateKey

Returns a new instance of PrivateKey.



6
7
8
9
# 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

Instance Method Details

#send_transaction(_options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/etherlite/account/private_key.rb', line 11

def send_transaction(_options = {})
  nonce_options = _options.slice(:replace, :nonce)
  
  nonce_manager.with_next_nonce_for(normalized_address, nonce_options) do |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
    )

    # Since eth gem does not allow configuration of chains for every tx, we need
    # to globally configure it before signing. This is not thread safe so a mutex is needed.
    sign_with_connection_chain tx

    Etherlite::Transaction.new @connection, @connection.eth_send_raw_transaction(tx.hex)
  end
end