Class: Eth::Contract::Error

Inherits:
Object
  • Object
show all
Defined in:
lib/eth/contract/error.rb

Overview

Provide classes for contract custom errors.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Error

Constructor of the Eth::Contract::Error class.

Parameters:

  • data (Hash)

    contract abi data for the error.



26
27
28
29
30
31
32
33
# File 'lib/eth/contract/error.rb', line 26

def initialize(data)
  @name = data["name"]
  @inputs = data.fetch("inputs", []).map do |input|
    Eth::Contract::FunctionInput.new(input)
  end
  @error_string = self.class.calc_signature(@name, @inputs)
  @signature = self.class.encoded_error_signature(@error_string)
end

Instance Attribute Details

#error_stringObject

Returns the value of attribute error_string.



21
22
23
# File 'lib/eth/contract/error.rb', line 21

def error_string
  @error_string
end

#inputsObject

Returns the value of attribute inputs.



21
22
23
# File 'lib/eth/contract/error.rb', line 21

def inputs
  @inputs
end

#nameObject

Returns the value of attribute name.



21
22
23
# File 'lib/eth/contract/error.rb', line 21

def name
  @name
end

#signatureObject

Returns the value of attribute signature.



21
22
23
# File 'lib/eth/contract/error.rb', line 21

def signature
  @signature
end

Class Method Details

.calc_signature(name, inputs) ⇒ String

Creates error strings.

Parameters:

Returns:

  • (String)

    error string.



40
41
42
# File 'lib/eth/contract/error.rb', line 40

def self.calc_signature(name, inputs)
  "#{name}(#{inputs.map { |x| x.parsed_type.to_s }.join(",")})"
end

.encoded_error_signature(signature) ⇒ String

Encodes an error signature.

Parameters:

  • signature (String)

    error signature.

Returns:

  • (String)

    encoded error signature string.



48
49
50
# File 'lib/eth/contract/error.rb', line 48

def self.encoded_error_signature(signature)
  Util.prefix_hex(Util.bin_to_hex(Util.keccak256(signature)[0..3]))
end

Instance Method Details

#decode(data) ⇒ Array

Decodes a revert error payload.

Parameters:

  • data (String)

    the hex-encoded revert data including selector.

Returns:

  • (Array)

    decoded error arguments.



56
57
58
59
60
# File 'lib/eth/contract/error.rb', line 56

def decode(data)
  types = inputs.map(&:type)
  payload = "0x" + data[10..]
  Eth::Abi.decode(types, payload)
end