diff --git a/lib/exw3.ex b/lib/exw3.ex index a98364a..6847d6b 100644 --- a/lib/exw3.ex +++ b/lib/exw3.ex @@ -429,20 +429,15 @@ defmodule ExW3 do end listen(callback) end - - defp decode_topics(topics) do - end defp loop(state) do receive do {: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})) {:event, filter_id, logs} -> filter_attributes = Map.get(state, filter_id) unless logs == [] do Enum.each(logs, fn log -> - IO.inspect log data = Map.get(log, "data") new_data = Enum.zip(filter_attributes[:names], ExW3.decode_event(data, filter_attributes[:signature])) |> Enum.into(%{}) new_log = Map.put(log, "data", new_data) @@ -518,6 +513,11 @@ defmodule ExW3 do {:ok, state} 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 events = Enum.filter(abi, fn {_, v} -> @@ -532,7 +532,17 @@ defmodule ExW3 do 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) signature_types_map = @@ -713,6 +723,7 @@ defmodule ExW3 do contract_info = state[contract_name] receipt = ExW3.tx_receipt(tx_hash) + events = contract_info[:events] logs = receipt["logs"] diff --git a/test/exw3_test.exs b/test/exw3_test.exs index 0b3a631..4ba0536 100644 --- a/test/exw3_test.exs +++ b/test/exw3_test.exs @@ -152,6 +152,18 @@ defmodule EXW3Test do |> ExW3.bytes_to_string() 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 test "starts a Contract GenServer and uses the event listener", context do @@ -203,37 +215,37 @@ defmodule EXW3Test do 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} = - ExW3.Contract.send( - :EventTester, - :simpleIndex, - ["Hello, World!"], - %{from: Enum.at(context[:accounts], 0), gas: 30_000} - ) + # {:ok, _tx_hash} = + # ExW3.Contract.send( + # :EventTester, + # :simpleIndex, + # ["Hello, World!"], + # %{from: Enum.at(context[:accounts], 0), gas: 30_000} + # ) - receive do - {:event, {_filter_id, data}} -> - Agent.update(agent2, fn list -> [data | list] end) - after 3_000 -> - raise "Never received event" - end + # receive do + # {:event, {_filter_id, data}} -> + # Agent.update(agent2, fn list -> [data | list] end) + # after 3_000 -> + # raise "Never received event" + # end - state = Agent.get(agent, fn list -> list end) - event_log = Enum.at(state, 0) - assert event_log |> is_map + # state = Agent.get(agent, fn list -> list end) + # event_log = Enum.at(state, 0) + # assert event_log |> is_map - log_data = Map.get(event_log, "data") - assert log_data |> is_map - assert Map.get(log_data, "num") == 46 - assert ExW3.bytes_to_string(Map.get(log_data, "data")) == "Hello, World!" - assert Map.get(log_data, "otherNum") == 42 - ExW3.uninstall_filter(indexed_filter_id) + # log_data = Map.get(event_log, "data") + # assert log_data |> is_map + # assert Map.get(log_data, "num") == 46 + # assert ExW3.bytes_to_string(Map.get(log_data, "data")) == "Hello, World!" + # assert Map.get(log_data, "otherNum") == 42 + # ExW3.uninstall_filter(indexed_filter_id) end test "starts a Contract GenServer for Complex contract", context do