4
0
Fork 0

indexed event data field working

indexed
hswick 6 years ago
parent abc634865a
commit ecba23c693
  1. 23
      lib/exw3.ex
  2. 62
      test/exw3_test.exs

@ -429,20 +429,15 @@ defmodule ExW3 do
end end
listen(callback) listen(callback)
end end
defp decode_topics(topics) do
end
defp loop(state) do defp loop(state) do
receive do receive do
{:filter, filter_id, event_signature, event_fields, pid} -> {:filter, filter_id, event_signature, event_fields, pid} ->
IO.inspect(event_signature)
loop(Map.put(state, filter_id, %{pid: pid, signature: event_signature, names: event_fields})) loop(Map.put(state, filter_id, %{pid: pid, signature: event_signature, names: event_fields}))
{:event, filter_id, logs} -> {:event, filter_id, logs} ->
filter_attributes = Map.get(state, filter_id) filter_attributes = Map.get(state, filter_id)
unless logs == [] do unless logs == [] do
Enum.each(logs, fn log -> Enum.each(logs, fn log ->
IO.inspect log
data = Map.get(log, "data") data = Map.get(log, "data")
new_data = Enum.zip(filter_attributes[:names], ExW3.decode_event(data, filter_attributes[:signature])) |> Enum.into(%{}) new_data = Enum.zip(filter_attributes[:names], ExW3.decode_event(data, filter_attributes[:signature])) |> Enum.into(%{})
new_log = Map.put(log, "data", new_data) new_log = Map.put(log, "data", new_data)
@ -518,6 +513,11 @@ defmodule ExW3 do
{:ok, state} {:ok, state}
end end
defp data_signature_helper(name, fields) do
non_indexed_types = Enum.map(fields, &Map.get(&1, "type"))
Enum.join([name, "(", Enum.join(non_indexed_types, ","), ")"])
end
defp init_events(abi) do defp init_events(abi) do
events = events =
Enum.filter(abi, fn {_, v} -> Enum.filter(abi, fn {_, v} ->
@ -532,7 +532,17 @@ defmodule ExW3 do
encoded_event_signature = "0x#{ExW3.encode_event(signature)}" encoded_event_signature = "0x#{ExW3.encode_event(signature)}"
{{encoded_event_signature, %{signature: signature, names: names}}, {name, encoded_event_signature}} indexed_fields = Enum.filter(v["inputs"], fn input ->
input["indexed"]
end)
non_indexed_fields = Enum.filter(v["inputs"], fn input ->
!input["indexed"]
end)
data_signature = data_signature_helper(name, non_indexed_fields)
{{encoded_event_signature, %{signature: data_signature, names: names}}, {name, encoded_event_signature}}
end) end)
signature_types_map = signature_types_map =
@ -713,6 +723,7 @@ defmodule ExW3 do
contract_info = state[contract_name] contract_info = state[contract_name]
receipt = ExW3.tx_receipt(tx_hash) receipt = ExW3.tx_receipt(tx_hash)
events = contract_info[:events] events = contract_info[:events]
logs = receipt["logs"] logs = receipt["logs"]

@ -152,6 +152,18 @@ defmodule EXW3Test do
|> ExW3.bytes_to_string() |> ExW3.bytes_to_string()
assert data == "Hello, World!" assert data == "Hello, World!"
{:ok, tx_hash2} =
ExW3.Contract.send(:EventTester, :simpleIndex, ["Hello, World!"], %{
from: Enum.at(context[:accounts], 0),
gas: 30_000
})
{:ok, {receipt, logs}} = ExW3.Contract.tx_receipt(:EventTester, tx_hash2)
IO.inspect receipt
end end
test "starts a Contract GenServer and uses the event listener", context do test "starts a Contract GenServer and uses the event listener", context do
@ -203,37 +215,37 @@ defmodule EXW3Test do
ExW3.uninstall_filter(filter_id) ExW3.uninstall_filter(filter_id)
# Test indexed events # # Test indexed events
{:ok, agent2} = Agent.start_link(fn -> [] end) # {:ok, agent2} = Agent.start_link(fn -> [] end)
indexed_filter_id = ExW3.Contract.filter(:EventTester, "SimpleIndex", self()) # indexed_filter_id = ExW3.Contract.filter(:EventTester, "SimpleIndex", self())
{:ok, _tx_hash} = # {:ok, _tx_hash} =
ExW3.Contract.send( # ExW3.Contract.send(
:EventTester, # :EventTester,
:simpleIndex, # :simpleIndex,
["Hello, World!"], # ["Hello, World!"],
%{from: Enum.at(context[:accounts], 0), gas: 30_000} # %{from: Enum.at(context[:accounts], 0), gas: 30_000}
) # )
receive do # receive do
{:event, {_filter_id, data}} -> # {:event, {_filter_id, data}} ->
Agent.update(agent2, fn list -> [data | list] end) # Agent.update(agent2, fn list -> [data | list] end)
after 3_000 -> # after 3_000 ->
raise "Never received event" # raise "Never received event"
end # end
state = Agent.get(agent, fn list -> list end) # state = Agent.get(agent, fn list -> list end)
event_log = Enum.at(state, 0) # event_log = Enum.at(state, 0)
assert event_log |> is_map # assert event_log |> is_map
log_data = Map.get(event_log, "data") # log_data = Map.get(event_log, "data")
assert log_data |> is_map # assert log_data |> is_map
assert Map.get(log_data, "num") == 46 # assert Map.get(log_data, "num") == 46
assert ExW3.bytes_to_string(Map.get(log_data, "data")) == "Hello, World!" # assert ExW3.bytes_to_string(Map.get(log_data, "data")) == "Hello, World!"
assert Map.get(log_data, "otherNum") == 42 # assert Map.get(log_data, "otherNum") == 42
ExW3.uninstall_filter(indexed_filter_id) # ExW3.uninstall_filter(indexed_filter_id)
end end
test "starts a Contract GenServer for Complex contract", context do test "starts a Contract GenServer for Complex contract", context do

Loading…
Cancel
Save