Skip to content

Commit 31af4ae

Browse files
committed
Added specs for fully indexsed Transfer event and fixed a decoding bug
1 parent 3b5d48b commit 31af4ae

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

lib/eth_contract.ex

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ defmodule EthContract do
231231
types_signature = Enum.join(["(", Enum.join(output_types_no_index, ","), ")"])
232232
output_signature = "#{method}(#{types_signature})"
233233

234-
# grab last two topics, first one is the event signature
235234
[ _ | topics ] = topics
236235

237236
decoded_data = trimmed_data
@@ -249,25 +248,31 @@ defmodule EthContract do
249248
|> Enum.map(fn x ->
250249
{ map, data } = x
251250
type = String.to_atom(map["type"])
252-
decoded = data
253-
|> trim_data
254-
|> ABI.TypeDecoder.decode_raw([type])
255-
|> List.first
256-
|> Base.encode16(case: :lower)
257-
258-
decoded = case type do
259-
:address -> "0x" <> decoded
260-
end
261251

262-
{map["name"], decoded}
252+
decoded = case type do
253+
:address ->
254+
result = data
255+
|> trim_data
256+
|> ABI.TypeDecoder.decode_raw([type])
257+
|> List.first
258+
|> Base.encode16(case: :lower)
259+
"0x" <> result
260+
:uint256 ->
261+
data
262+
|> trim_data
263+
|> :binary.decode_unsigned
264+
end
265+
266+
{map["name"], decoded}
263267
end)
264268

265-
indexed_result = res
266-
|> Enum.into(%{})
269+
indexed_result =
270+
res
271+
|> Enum.into(%{})
267272

268-
{:ok, Map.merge(indexed_result, non_indexed_result) }
273+
{:ok, Map.merge(indexed_result, non_indexed_result) }
269274
end
270-
275+
271276
@doc """
272277
Decodes events with no indexes.
273278

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule EthContract.MixProject do
44
def project do
55
[
66
app: :eth_contract,
7-
version: "0.2.8",
7+
version: "0.2.9",
88
elixir: "~> 1.6",
99
start_permanent: Mix.env() == :prod,
1010
name: "ETHContract",

test/eth_contract_test.exs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,38 @@ defmodule EthContractTest do
77
end
88

99
describe "decode_log/4" do
10+
test "it decodes fully indexed Transfer event" do
11+
abi = %{
12+
"Transfer" => %{
13+
"anonymous" => false,
14+
"inputs" => [
15+
%{
16+
"indexed" => true,
17+
"name" => "from",
18+
"type" => "address"
19+
},
20+
%{
21+
"indexed" => true,
22+
"name" => "to",
23+
"type" => "address"
24+
},
25+
%{
26+
"indexed" => true,
27+
"name" => "tokenId",
28+
"type" => "uint256"
29+
}
30+
],
31+
"name" => "Transfer",
32+
"type" => "event"
33+
}
34+
}
35+
36+
{:ok, log_data } = EthContract.decode_log("0x", ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x000000000000000000000000b7282d0d208f1fc533ba60a4a3dc03adc2aab8e7", "0x0000000000000000000000007192bb75777dab47ef6fbf6f6c0e4bcbb2294f38", "0x000000000000000000000000000000000000000000000000000000000000048f"], abi, "Transfer")
37+
38+
assert log_data === %{"tokenId" => 1167, "from" => "0xb7282d0d208f1fc533ba60a4a3dc03adc2aab8e7", "to" => "0x7192bb75777dab47ef6fbf6f6c0e4bcbb2294f38"}
39+
40+
end
41+
1042
test "it decodes coin Transfer event" do
1143
abi = %{
1244
"Transfer" => %{

0 commit comments

Comments
 (0)