diff --git a/CHANGELOG.md b/CHANGELOG.md index ffebac8..5fb8e0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Enhancements + +- Enable raw use of `Ethers.call/2` (usage without function selector) + ## v0.5.1 (2024-08-02) ### Enhancements diff --git a/lib/ethers.ex b/lib/ethers.ex index ea2634c..a42ad20 100644 --- a/lib/ethers.ex +++ b/lib/ethers.ex @@ -667,10 +667,10 @@ defmodule Ethers do defp pre_process(data, [], _action, _opts), do: {:ok, data} - defp post_process({:ok, resp}, tx_data, :call) when valid_result(resp) do - tx_data.selector + defp post_process({:ok, resp}, %{selector: selector}, :call) when valid_result(resp) do + selector |> ABI.decode(Ethers.Utils.hex_decode!(resp), :output) - |> Enum.zip(tx_data.selector.returns) + |> Enum.zip(selector.returns) |> Enum.map(fn {return, type} -> Utils.human_arg(return, type) end) |> case do [element] -> {:ok, element} @@ -678,6 +678,11 @@ defmodule Ethers do end end + defp post_process({:ok, resp}, _tx_data, :call) when is_binary(resp) do + # Handle the case that call was used without a selector (raw call) + {:ok, resp} + end + defp post_process({:ok, tx_hash}, _tx_data, _action = :send) when valid_result(tx_hash), do: {:ok, tx_hash} diff --git a/test/ethers_test.exs b/test/ethers_test.exs index 9441356..54a8cea 100644 --- a/test/ethers_test.exs +++ b/test/ethers_test.exs @@ -599,4 +599,18 @@ defmodule EthersTest do end end end + + describe "call/2" do + test "works without selector (raw call)" do + address = deploy(HelloWorldContract, from: @from) + + tx_data = HelloWorldContract.say_hello() + + assert {:ok, + "0x000000000000000000000000000000000000000000000000000000000000002000000000000" <> + "0000000000000000000000000000000000000000000000000000c48656c6c6f20576f726c642100" <> + "00000000000000000000000000000000000000"} = + Ethers.call(%{data: tx_data.data}, to: address) + end + end end