Class: EvmTxInput::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/evm_tx_input/decoder.rb

Overview

A class containing a set of helpers to decode transactions input data into human-readable values by provided contract’s ABI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(abi = []) ⇒ Decoder

Constructor of EvmTxInput::Decoder

Parameters:

  • abi (Array) (defaults to: [])

    parsed contract ABI



15
16
17
18
# File 'lib/evm_tx_input/decoder.rb', line 15

def initialize(abi = [])
  @abi = abi
  @method_definitions = method_definitions_by_id(abi)
end

Instance Attribute Details

#abiHash (readonly)

parsed contract ABI

Returns:

  • (Hash)

    the current value of abi



9
10
11
# File 'lib/evm_tx_input/decoder.rb', line 9

def abi
  @abi
end

#method_definitionHash (readonly)

parsed contract ABI

Returns:

  • (Hash)

    the current value of method_definition



9
10
11
# File 'lib/evm_tx_input/decoder.rb', line 9

def method_definition
  @method_definition
end

#method_definitionsObject (readonly)

Returns the value of attribute method_definitions.



10
11
12
# File 'lib/evm_tx_input/decoder.rb', line 10

def method_definitions
  @method_definitions
end

Instance Method Details

#decode_input(input_data) ⇒ EvmTxInput::Function

Decodes transaction input data

Parameters:

  • input_data (String)

    binary encoded input data from transaction

Returns:

Raises:



24
25
26
27
28
29
30
31
# File 'lib/evm_tx_input/decoder.rb', line 24

def decode_input(input_data)
  input_data = input_data[2..] if input_data.start_with?('0x')
  method_id = input_data[0...8]
  definition = method_definitions[method_id]
  raise Error, "ABI does not contain method with #{method_id} ID" unless definition

  Function.new(method_id, definition['name'], extract_arguments(input_data, definition))
end