Exception: BlockGiven::RpcError

Inherits:
Error
  • Object
show all
Defined in:
lib/block_given/errors.rb

Overview

JSON-RPC error object returned by the node.

Direct Known Subclasses

ContractRevertError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, code: nil, data: nil, rpc_method: nil) ⇒ RpcError

Returns a new instance of RpcError.



33
34
35
36
37
38
# File 'lib/block_given/errors.rb', line 33

def initialize(message, code: nil, data: nil, rpc_method: nil)
  super(message)
  @code = code
  @data = data
  @rpc_method = rpc_method
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



31
32
33
# File 'lib/block_given/errors.rb', line 31

def code
  @code
end

#dataObject (readonly)

Returns the value of attribute data.



31
32
33
# File 'lib/block_given/errors.rb', line 31

def data
  @data
end

#rpc_methodObject (readonly)

Returns the value of attribute rpc_method.



31
32
33
# File 'lib/block_given/errors.rb', line 31

def rpc_method
  @rpc_method
end

Class Method Details

.extract_revert_data(data) ⇒ Object

Providers disagree on where the revert bytes live: Alchemy/geth put them in data, Hardhat/Anvil nest them under data.data.



58
59
60
61
62
63
# File 'lib/block_given/errors.rb', line 58

def self.extract_revert_data(data)
  case data
  when String then data.start_with?("0x") ? data : nil
  when Hash then extract_revert_data(data["data"] || data[:data])
  end
end

.from_payload(error, rpc_method: nil) ⇒ Object

Builds the most specific error for a JSON-RPC error payload. Reverts carry ABI-encoded data that we surface as a ContractRevertError.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/block_given/errors.rb', line 42

def self.from_payload(error, rpc_method: nil)
  error = { "message" => error.to_s } unless error.is_a?(Hash)
  code = error["code"]
  message = error["message"].to_s
  data = error["data"]
  revert_data = extract_revert_data(data)

  if revert_data || message.match?(/revert/i)
    ContractRevertError.new(message, code: code, data: data, rpc_method: rpc_method, revert_data: revert_data)
  else
    new(message, code: code, data: data, rpc_method: rpc_method)
  end
end