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

Decode outputs #36

Merged
merged 3 commits into from
May 28, 2020
Merged

Conversation

ayrat555
Copy link
Collaborator

@ayrat555 ayrat555 commented May 25, 2020

This PR should be merged after #34
Resolves #25

Now to decode/encode output you should pass data type :output as the third parameter. By default, the third parameter is :input For example

      selector = %FunctionSelector{
        function: "getVersion",
        input_names: [],
        inputs_indexed: nil,
        method_id: <<13, 142, 110, 44>>,
        returns: [:string],
        type: :function,
        types: []
      }

      data =
        "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d312e302e342b6136396337363300000000000000000000000000000000000000"
        |> Base.decode16!(case: :lower)

      expected_result = ["1.0.4+a69c763"]

      assert expected_result == ABI.decode(selector, data, :output)
      assert data == ABI.encode(selector, expected_result, :output)

@ayrat555 ayrat555 mentioned this pull request May 25, 2020
@InoMurko
Copy link
Contributor

KUDOS!

@ayrat555 ayrat555 marked this pull request as ready for review May 28, 2020 04:23
@vbaranov vbaranov self-requested a review May 28, 2020 07:46
Copy link
Collaborator

@vbaranov vbaranov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ayrat555 shouldn't we provide backward compatibility with the previous versions of the library by allowing ABI.decode, ABI.encode with two input parameters?

@ayrat555
Copy link
Collaborator Author

@vbaranov the third parameter is optional. So it will work like before

@vbaranov
Copy link
Collaborator

@ayrat555 I've tried example, you provided, but tried to decode with 2 parameters. Here is the result:

iex(6)> ABI.decode(selector, data)
** (MatchError) no match of right hand side value: {:ok, <<0, 0, 0, 0>>, <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...>>}
    (ex_abi 0.4.0) lib/abi/type_decoder.ex:143: ABI.TypeDecoder.decode/3

@ayrat555
Copy link
Collaborator Author

@vbaranov I think you're trying to decode output with input types. Different data should be provided for input

@ayrat555
Copy link
Collaborator Author

@vbaranov the function doesn't accept any parameters

@vbaranov
Copy link
Collaborator

@ayrat555

I think you're trying to decode output with input types. Different data should be provided for input

Maybe, we should gracefully handle this error?

the function doesn't accept any parameters

what do you mean? which one?

@ayrat555
Copy link
Collaborator Author

ayrat555 commented May 28, 2020

Maybe, we should gracefully handle this error?

I can work on it as a separate issue. It's the same behaviour that was before my changes

what do you mean? which one?

getVersion - types: [] - so nothing to decode

@vbaranov vbaranov self-requested a review May 28, 2020 08:12
@vbaranov vbaranov merged commit 1a525e5 into poanetwork:master May 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

incorrect usage of types in function selectors
3 participants