Class: EvmTxInput::Encoder

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

Overview

A class containing a set of helpers to encode human-readable values to binary transactions input data

Class Method Summary collapse

Class Method Details

.encode_function_signature(function_name, types = []) ⇒ String

Encodes function into binary representation (first 8 symbols at input data)

Parameters:

  • function_name (String)

    the name of calling smart contract function, e.g. ‘transfer`

  • types (Array<String>) (defaults to: [])

    list of argument types, e.g. ‘[’address’, ‘uint256’]‘

Returns:

  • (String)

    binary representation of function



26
27
28
29
# File 'lib/evm_tx_input/encoder.rb', line 26

def encode_function_signature(function_name, types = [])
  function_signature = "#{function_name}(#{types.join(",")})"
  Eth::Util.bin_to_hex(Eth::Util.keccak256(function_signature)[0...4])
end

.encode_input(function_name, types = [], args = []) ⇒ String

Encodes function, types and arguments into binary input data

Parameters:

  • function_name (String)

    the name of calling smart contract function, e.g. ‘transfer`

  • types (Array<String>) (defaults to: [])

    list of argument types, e.g. ‘[’address’, ‘uint256’]‘

  • types (Array<String>) (defaults to: [])

    list of arguments, e.g. ‘[’0x21a31ee1afc51d94c2efccaa2092ad1028285549’, 100000]‘

Returns:

  • (String)

    binary representation of input data



14
15
16
17
18
19
# File 'lib/evm_tx_input/encoder.rb', line 14

def encode_input(function_name, types = [], args = [])
  encoded_function_signature = encode_function_signature(function_name, types)
  encoded_params = encode_parameters(types, args)

  "0x#{encoded_function_signature}#{encoded_params}"
end

.encode_parameters(types = [], args = []) ⇒ String

Encodes arguments into binary input data

Parameters:

  • types (Array<String>) (defaults to: [])

    list of argument types, e.g. ‘[’address’, ‘uint256’]‘

  • types (Array<String>) (defaults to: [])

    list of arguments, e.g. ‘[’0x21a31ee1afc51d94c2efccaa2092ad1028285549’, 100000]‘

Returns:

  • (String)

    binary representation of arguments



36
37
38
# File 'lib/evm_tx_input/encoder.rb', line 36

def encode_parameters(types = [], args = [])
  Eth::Abi.encode(types, args).unpack1('H*')
end