Method: Klay::Tx::Legacy#initialize

Defined in:
lib/klay/tx/legacy.rb

#initialize(params, chain_id = Chain::CYPRESS) ⇒ Legacy

Create a legacy transaction object that can be prepared for signature and broadcast. Should not be used unless there is no EIP-1559 support.

Parameters:

  • params (Hash)

    all necessary transaction fields.

  • chain_id (Integer) (defaults to: Chain::CYPRESS)

    the EIP-155 Chain ID.

Options Hash (params):

  • :nonce (Integer)

    the signer nonce.

  • :gas_price (Integer)

    the gas price.

  • :gas_limit (Integer)

    the gas limit.

  • :from (Klay::Address)

    the sender address.

  • :to (Klay::Address)

    the reciever address.

  • :value (Integer)

    the transaction value.

  • :data (String)

    the transaction data payload.

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/klay/tx/legacy.rb', line 76

def initialize(params, chain_id = Chain::CYPRESS)
  fields = { v: chain_id, r: 0, s: 0 }.merge params

  # populate optional fields with serializable empty values
  fields[:value] = Tx.sanitize_amount fields[:value]
  fields[:from] = Tx.sanitize_address fields[:from]
  fields[:to] = Tx.sanitize_address fields[:to]
  fields[:data] = Tx.sanitize_data fields[:data]

  # ensure sane values for all mandatory fields
  fields = Tx.validate_legacy_params fields

  # ensure gas limit is not too low
  minimum_cost = Tx.estimate_intrinsic_gas fields[:data]
  raise ParameterError, "Transaction gas limit is too low, try #{minimum_cost}!" if fields[:gas_limit].to_i < minimum_cost

  # populate class attributes
  @signer_nonce = fields[:nonce].to_i
  @gas_price = fields[:gas_price].to_i
  @gas_limit = fields[:gas_limit].to_i
  @sender = fields[:from].to_s
  @destination = fields[:to].to_s
  @amount = fields[:value].to_i
  @payload = fields[:data]

  # the signature v is set to the chain id for unsigned transactions
  @signature_v = fields[:v]
  @chain_id = chain_id

  # the signature fields are empty for unsigned transactions.
  @signature_r = fields[:r]
  @signature_s = fields[:s]

  # last but not least, set the type.
  @type = TYPE_LEGACY
end