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.to_s)
end

Instance Method Details

#build_raw_transaction(_options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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(
    chain_id: @connection.chain_id,
    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
  )

  tx.sign @key
  tx
end

#send_transaction(_options = {}) ⇒ Object



28
29
30
31
32
33
34
# 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.signer_nonce) do |_|
    Etherlite::Transaction.new @connection, @connection.eth_send_raw_transaction("0x#{tx.hex}")
  end
end