diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/encoder.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/encoder.ex index 034c12de67..221c082b02 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/encoder.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/encoder.ex @@ -12,7 +12,7 @@ defmodule EthereumJSONRPC.Encoder do This is what is expected on the Json RPC data parameter. """ @spec encode_function_call(ABI.FunctionSelector.t(), [term()]) :: String.t() - def encode_function_call(function_selector, args) do + def encode_function_call(function_selector, args) when is_list(args) do parsed_args = parse_args(args) encoded_args = @@ -23,6 +23,8 @@ defmodule EthereumJSONRPC.Encoder do "0x" <> encoded_args end + def encode_function_call(function_selector, args), do: encode_function_call(function_selector, [args]) + defp parse_args(args) when is_list(args) do args |> Enum.map(&parse_args/1) diff --git a/apps/explorer/test/explorer/token/instance_metadata_retriever_test.exs b/apps/explorer/test/explorer/token/instance_metadata_retriever_test.exs index 3afe5c937f..5330b5451e 100644 --- a/apps/explorer/test/explorer/token/instance_metadata_retriever_test.exs +++ b/apps/explorer/test/explorer/token/instance_metadata_retriever_test.exs @@ -1,6 +1,7 @@ defmodule Explorer.Token.InstanceMetadataRetrieverTest do use EthereumJSONRPC.Case + alias EthereumJSONRPC.Encoder alias Explorer.Token.InstanceMetadataRetriever alias Plug.Conn @@ -166,6 +167,104 @@ defmodule Explorer.Token.InstanceMetadataRetrieverTest do }) end + test "replace {id} with actual token_id", %{bypass: bypass} do + json = """ + { + "name": "Sérgio Mendonça {id}" + } + """ + + abi = + [ + %{ + "type" => "function", + "stateMutability" => "nonpayable", + "payable" => false, + "outputs" => [], + "name" => "tokenURI", + "inputs" => [ + %{"type" => "string", "name" => "name", "internalType" => "string"} + ] + } + ] + |> ABI.parse_specification() + |> Enum.at(0) + + encoded_url = + abi + |> Encoder.encode_function_call(["http://localhost:#{bypass.port}/api/card/{id}"]) + |> String.replace("4cf12d26", "") + + EthereumJSONRPC.Mox + |> expect(:json_rpc, fn [ + %{ + id: 0, + jsonrpc: "2.0", + method: "eth_call", + params: [ + %{ + data: + "0xc87b56dd0000000000000000000000000000000000000000000000000000000000000309", + to: "0x5caebd3b32e210e85ce3e9d51638b9c445481567" + }, + "latest" + ] + } + ], + _options -> + {:ok, + [ + %{ + id: 0, + jsonrpc: "2.0", + error: %{code: -32000, message: "execution reverted"} + } + ]} + end) + |> expect(:json_rpc, fn [ + %{ + id: 0, + jsonrpc: "2.0", + method: "eth_call", + params: [ + %{ + data: + "0x0e89341c0000000000000000000000000000000000000000000000000000000000000309", + to: "0x5caebd3b32e210e85ce3e9d51638b9c445481567" + }, + "latest" + ] + } + ], + _options -> + {:ok, + [ + %{ + id: 0, + jsonrpc: "2.0", + result: encoded_url + } + ]} + end) + + Bypass.expect( + bypass, + "GET", + "/api/card/0000000000000000000000000000000000000000000000000000000000000309", + fn conn -> + Conn.resp(conn, 200, json) + end + ) + + assert {:ok, + %{ + metadata: %{ + "name" => "Sérgio Mendonça 0000000000000000000000000000000000000000000000000000000000000309" + } + }} == + InstanceMetadataRetriever.fetch_metadata("0x5caebd3b32e210e85ce3e9d51638b9c445481567", 777) + end + test "decodes json file in tokenURI" do data = %{ "c87b56dd" =>