Skip to content

Commit

Permalink
Support raw results in Ethers.call/2 (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisinabh authored Aug 7, 2024
1 parent 70854dd commit 6b35f04
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 8 additions & 3 deletions lib/ethers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -667,17 +667,22 @@ 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}
elements -> {:ok, elements}
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}

Expand Down
14 changes: 14 additions & 0 deletions test/ethers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6b35f04

Please sign in to comment.