From 782b2f4a2dc277fb9d0615c05bd73e4ce75c3e92 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Thu, 27 Aug 2020 19:50:48 +0300 Subject: [PATCH] Contract interaction: array input type --- CHANGELOG.md | 1 + .../lib/ethereum_jsonrpc/contract.ex | 35 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f3b409eed..08e53ea93e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features ### Fixes +- [#3259](https://github.com/poanetwork/blockscout/pull/3259) - Contract interaction: array input type parsing fix - [#3256](https://github.com/poanetwork/blockscout/pull/3256) - Fix for invisible validator address at block page and wrong alert text color at xDai ### Chore diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/contract.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/contract.ex index 7276ce395d..8cec3a6356 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/contract.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/contract.ex @@ -44,8 +44,10 @@ defmodule EthereumJSONRPC.Contract do func.function == function_name && Enum.count(func.input_names) == Enum.count(args) end) + formatted_args = format_args(function, args) + function - |> Encoder.encode_function_call(args) + |> Encoder.encode_function_call(formatted_args) |> eth_call_request(contract_address, index, Map.get(request, :block_number), Map.get(request, :from)) end) |> json_rpc(json_rpc_named_arguments) @@ -75,6 +77,37 @@ defmodule EthereumJSONRPC.Contract do Enum.map(requests, fn _ -> format_error(error) end) end + defp format_args(function, args) do + args + |> Enum.with_index() + |> Enum.map(fn {arg, index} -> + types = function.types + + case Enum.at(types, index) do + {:array, {:int, _size}} -> + convert_string_to_array(arg) + + {:array, {:uint, _size}} -> + convert_string_to_array(arg) + + {:array, _} -> + String.split(arg, ",") + + _ -> + arg + end + end) + end + + defp convert_string_to_array(arg) do + arg + |> String.split(",") + |> Enum.map(fn el -> + {int, _} = Integer.parse(el) + int + end) + end + def eth_call_request(data, contract_address, id, block_number, from) do block = case block_number do