Class: Siwe::Rpc::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/siwe/rpc.rb

Overview

Minimal JSON-RPC client for eth_call. Default implementation used when Siwe.configure { |c| c.rpc_url = … } is set. Anything that responds to ‘eth_call(to:, data:, block:)` (and optionally `chain_id`) can be plugged in via `c.rpc = …` to use a different transport (web3.rb, eth-rpc, etc.).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, chain_id: nil, timeout: 10) ⇒ HttpClient

Returns a new instance of HttpClient.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
# File 'lib/siwe/rpc.rb', line 19

def initialize(url, chain_id: nil, timeout: 10)
  raise ArgumentError, "rpc url is required" if url.nil? || url.empty?

  @url = url
  @uri = URI(url)
  @chain_id = chain_id
  @timeout = timeout
end

Instance Attribute Details

#timeoutObject (readonly)

Returns the value of attribute timeout.



17
18
19
# File 'lib/siwe/rpc.rb', line 17

def timeout
  @timeout
end

#urlObject (readonly)

Returns the value of attribute url.



17
18
19
# File 'lib/siwe/rpc.rb', line 17

def url
  @url
end

Instance Method Details

#chain_idObject



28
29
30
# File 'lib/siwe/rpc.rb', line 28

def chain_id
  @chain_id ||= fetch_chain_id
end

#eth_call(to:, data:, block: "latest") ⇒ Object

Send eth_call. Returns the result as a hex string WITHOUT the 0x prefix. When to is nil, omits it from params (used by EIP-6492 deploy-and-call). Raises Siwe::Error :rpc_error on transport / HTTP / JSON-RPC failure.



35
36
37
38
39
# File 'lib/siwe/rpc.rb', line 35

def eth_call(to:, data:, block: "latest")
  call_params = { data: data }
  call_params[:to] = to if to
  rpc_request("eth_call", [call_params, block])
end