Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow functions to have mutliple output types #8

Merged
merged 1 commit into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/abi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ defmodule ABI do
iex> File.read!("priv/dog.abi.json")
...> |> Poison.decode!
...> |> ABI.parse_specification
[%ABI.FunctionSelector{function: "bark", returns: nil, types: [:address, :bool]},
%ABI.FunctionSelector{function: "rollover", returns: :bool, types: []}]
[%ABI.FunctionSelector{function: "bark", returns: [], types: [:address, :bool]},
%ABI.FunctionSelector{function: "rollover", returns: [:bool], types: []}]

iex> [%{
...> "constant" => true,
Expand All @@ -103,7 +103,7 @@ defmodule ABI do
...> "type" => "function"
...> }]
...> |> ABI.parse_specification
[%ABI.FunctionSelector{function: "bark", returns: nil, types: [:address, :bool]}]
[%ABI.FunctionSelector{function: "bark", returns: [], types: [:address, :bool]}]

iex> [%{
...> "inputs" => [
Expand All @@ -122,7 +122,7 @@ defmodule ABI do
...> "type" => "fallback"
...> }]
...> |> ABI.parse_specification
[%ABI.FunctionSelector{function: nil, returns: nil, types: []}]
[%ABI.FunctionSelector{function: nil, returns: [], types: []}]
"""
def parse_specification(doc) do
doc
Expand Down
54 changes: 42 additions & 12 deletions lib/abi/function_selector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ defmodule ABI.FunctionSelector do
| {:array, type, non_neg_integer}
| {:tuple, [type]}

@typedoc """
Struct to represent a function and its input and output types.

* `:function` - Name of the function
* `:types` - Function's input types
* `:returns` - Function's return types
"""
@type t :: %__MODULE__{
function: String.t(),
types: [type],
returns: type
returns: [type]
}

defstruct [:function, :types, :returns]
defstruct [:function, types: [], returns: []]

@doc """
Decodes a function selector to a struct.
Expand All @@ -35,7 +42,8 @@ defmodule ABI.FunctionSelector do
types: [
{:uint, 256},
:bool
]
],
returns: []
}

iex> ABI.FunctionSelector.decode("growl(uint,address,string[])")
Expand All @@ -45,51 +53,58 @@ defmodule ABI.FunctionSelector do
{:uint, 256},
:address,
{:array, :string}
]
],
returns: []
}

iex> ABI.FunctionSelector.decode("rollover()")
%ABI.FunctionSelector{
function: "rollover",
types: []
types: [],
returns: []
}

iex> ABI.FunctionSelector.decode("do_playDead3()")
%ABI.FunctionSelector{
function: "do_playDead3",
types: []
types: [],
returns: []
}

iex> ABI.FunctionSelector.decode("pet(address[])")
%ABI.FunctionSelector{
function: "pet",
types: [
{:array, :address}
]
],
returns: []
}

iex> ABI.FunctionSelector.decode("paw(string[2])")
%ABI.FunctionSelector{
function: "paw",
types: [
{:array, :string, 2}
]
],
returns: []
}

iex> ABI.FunctionSelector.decode("scram(uint256[])")
%ABI.FunctionSelector{
function: "scram",
types: [
{:array, {:uint, 256}}
]
],
returns: []
}

iex> ABI.FunctionSelector.decode("shake((string))")
%ABI.FunctionSelector{
function: "shake",
types: [
{:tuple, [:string]}
]
],
returns: []
}
"""
def decode(signature) do
Expand Down Expand Up @@ -126,15 +141,15 @@ defmodule ABI.FunctionSelector do
%ABI.FunctionSelector{
function: function_name,
types: input_types,
returns: List.first(output_types)
returns: output_types
}
end

def parse_specification_item(%{"type" => "fallback"}) do
%ABI.FunctionSelector{
function: nil,
types: [],
returns: nil
returns: []
}
end

Expand Down Expand Up @@ -226,4 +241,19 @@ defmodule ABI.FunctionSelector do
def is_dynamic?({:array, type, len}) when len > 0, do: is_dynamic?(type)
def is_dynamic?({:tuple, types}), do: Enum.any?(types, &is_dynamic?/1)
def is_dynamic?(_), do: false

@doc false
def from_params(params) when is_map(params) do
formatted_params =
params
|> Map.take(~w(function types returns)a)
|> Enum.map(&sanitize_param/1)

struct!(ABI.FunctionSelector, formatted_params)
end

defp sanitize_param({key, nil}) when key in ~w(types returns)a do
{key, []}
end
defp sanitize_param(tuple), do: tuple
end
2 changes: 1 addition & 1 deletion lib/abi/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule ABI.Parser do

case ast do
{:type, type} -> type
{:selector, selector_parts} -> struct!(ABI.FunctionSelector, selector_parts)
{:selector, selector_parts} -> ABI.FunctionSelector.from_params(selector_parts)
end
end
end
4 changes: 2 additions & 2 deletions lib/abi/type_decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule ABI.TypeDecoder do
...> {:uint, 32},
...> :bool
...> ],
...> returns: :bool
...> returns: [:bool]
...> }
...> )
[69, true]
Expand All @@ -37,7 +37,7 @@ defmodule ABI.TypeDecoder do
...> types: [
...> {:int, 8}
...> ],
...> returns: :int
...> returns: [:int]
...> }
...> )
[-42]
Expand Down
81 changes: 81 additions & 0 deletions test/abi_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,85 @@
defmodule ABITest do
use ExUnit.Case
doctest ABI

import ABI

alias ABI.FunctionSelector

describe "parse_specification/1" do
test "parses an ABI" do
abi = [
%{
"constant" => true,
"inputs" => [
%{
"type" => "uint256",
"name" => ""
}
],
"name" => "fooBar",
"outputs" => [
%{
"name" => "",
"type" => "uint256[6]"
},
%{
"name" => "",
"type" => "bool"
},
%{
"name" => "",
"type" => "uint256[3]"
},
%{
"name" => "",
"type" => "string"
}
],
"payable" => false,
"type" => "function"
},
%{
"name" => "baz",
"type" => "function",
"outputs" => [
%{
"name" => "",
"type" => "tuple",
"components" => [
%{
"name" => "",
"type" => "uint256"
},
%{
"name" => "",
"type" => "uint256"
}
]
},
%{
"name" => "",
"type" => "string"
}
],
"inputs" => []
}
]

expected = [
%FunctionSelector{
function: "fooBar",
types: [{:uint, 256}],
returns: [{:array, {:uint, 256}, 6}, :bool, {:array, {:uint, 256}, 3}, :string]
},
%FunctionSelector{
function: "baz",
types: [],
returns: [{:tuple, [{:uint, 256}, {:uint, 256}]}, :string]
}
]

assert parse_specification(abi) == expected
end
end
end