Class: PolarLoop::ContractCaller

Inherits:
Object
  • Object
show all
Defined in:
lib/polarloop/contract_caller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chain_config:, key:) ⇒ ContractCaller

Returns a new instance of ContractCaller.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/polarloop/contract_caller.rb', line 16

def initialize(chain_config:, key:)
  @chain_config = chain_config
  @key = key
  @eth_client = Eth::Client.create(chain_config.rpc_url)
  @contract = Eth::Contract.from_abi(
    name: "PolarLoop",
    address: chain_config.contract_address,
    abi: Abi.abi
  )
  @gas_strategy = GasStrategy.new(chain_config.chain_id, @eth_client)
end

Instance Attribute Details

#contractObject (readonly)

Returns the value of attribute contract.



14
15
16
# File 'lib/polarloop/contract_caller.rb', line 14

def contract
  @contract
end

#eth_clientObject (readonly)

Returns the value of attribute eth_client.



14
15
16
# File 'lib/polarloop/contract_caller.rb', line 14

def eth_client
  @eth_client
end

Instance Method Details

#call_view(function_name, *args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/polarloop/contract_caller.rb', line 28

def call_view(function_name, *args)
  result = @eth_client.call(@contract, function_name, *args)
  raise RpcError, "Empty response from node in call_view: #{function_name}" if result.nil?
  result
rescue Eth::Client::ContractExecutionError => e
  raise ContractRevertError, e.message
rescue Eth::Client::RpcError => e
  raise_from_rpc_error(e)
rescue Errno::ECONNREFUSED => e
  raise RpcError, "RPC connection failed: #{e.message}"
rescue JSON::ParserError
  raw = Thread.current[:polarloop_last_raw_response]
  raise RpcError, "RPC connection failed: #{raw || 'non-JSON response from node'}"
rescue NoMethodError => e
  raise RpcError, "Unexpected node response in call_view: #{e.message}"
end

#get_logs(topics:, from_block:, to_block:) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/polarloop/contract_caller.rb', line 110

def get_logs(topics:, from_block:, to_block:)
  filter = {
    address: @chain_config.contract_address,
    topics: topics,
    fromBlock: hex_block(from_block),
    toBlock: hex_block(to_block)
  }
  response = @eth_client.eth_get_logs(filter)
  response["result"] || []
rescue IOError, Errno::ECONNREFUSED => e
  raise RpcError, "RPC connection failed: #{e.message}"
rescue JSON::ParserError
  raw = Thread.current[:polarloop_last_raw_response]
  raise RpcError, "RPC connection failed: #{raw || 'non-JSON response from node'}"
end

#get_transaction_receipt(tx_hash) ⇒ Object



126
127
128
# File 'lib/polarloop/contract_caller.rb', line 126

def get_transaction_receipt(tx_hash)
  @eth_client.eth_get_transaction_receipt(tx_hash)
end

#latest_block_numberObject



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/polarloop/contract_caller.rb', line 98

def latest_block_number
  response = @eth_client.eth_block_number
  response["result"].to_i(16)
rescue IOError, Errno::ECONNREFUSED => e
  raise RpcError, "RPC connection failed: #{e.message}"
rescue JSON::ParserError
  raw = Thread.current[:polarloop_last_raw_response]
  raise RpcError, "RPC connection failed: #{raw || 'non-JSON response from node'}"
rescue NoMethodError => e
  raise RpcError, "Unexpected node response in latest_block_number: #{e.message}"
end

#send_batch_tx(function_name, *args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/polarloop/contract_caller.rb', line 68

def send_batch_tx(function_name, *args)
  simulation = @eth_client.call(@contract, function_name, *args)
  batch_results = Types::BatchResult.from_abi_array(
    simulation.is_a?(Array) ? simulation : [simulation]
  )

  gas = estimate_gas(function_name, *args)
  tx_hash, _success = @eth_client.transact_and_wait(
    @contract,
    function_name,
    *args,
    **tx_opts,
    gas_limit: gas
  )
  result = build_tx_result(tx_hash)
  result.batch_results = batch_results
  result
rescue Eth::Client::ContractExecutionError => e
  raise ContractRevertError, e.message
rescue Eth::Client::RpcError => e
  raise_from_rpc_error(e)
rescue Errno::ECONNREFUSED => e
  raise RpcError, "RPC connection failed: #{e.message}"
rescue JSON::ParserError
  raw = Thread.current[:polarloop_last_raw_response]
  raise RpcError, "RPC connection failed: #{raw || 'non-JSON response from node'}"
rescue NoMethodError => e
  raise RpcError, "Unexpected node response in send_batch_tx: #{e.message}"
end

#send_tx(function_name, *args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/polarloop/contract_caller.rb', line 45

def send_tx(function_name, *args)
  gas = estimate_gas(function_name, *args)
  tx_hash, _success = @eth_client.transact_and_wait(
    @contract,
    function_name,
    *args,
    **tx_opts,
    gas_limit: gas
  )
  build_tx_result(tx_hash)
rescue Eth::Client::ContractExecutionError => e
  raise ContractRevertError, e.message
rescue Eth::Client::RpcError => e
  raise_from_rpc_error(e)
rescue Errno::ECONNREFUSED => e
  raise RpcError, "RPC connection failed: #{e.message}"
rescue JSON::ParserError
  raw = Thread.current[:polarloop_last_raw_response]
  raise RpcError, "RPC connection failed: #{raw || 'non-JSON response from node'}"
rescue NoMethodError => e
  raise RpcError, "Unexpected node response in send_tx: #{e.message}"
end