From 871fcdcf576497d2258e9801e6f3eb5d16408e26 Mon Sep 17 00:00:00 2001 From: Alisina Bahadori Date: Fri, 27 Oct 2023 16:58:20 -0400 Subject: [PATCH] Add return names in docs and TxData inspection Closes #42 --- lib/ethers/contract_helpers.ex | 9 ++++----- lib/ethers/tx_data.ex | 13 ++++++++++++- test/ethers/contract_helpers_test.exs | 11 +++++++++++ test/ethers/counter_contract_test.exs | 11 +++++++---- test/support/contracts/counter.sol | 6 +++++- 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/lib/ethers/contract_helpers.ex b/lib/ethers/contract_helpers.ex index b75d331..9b9cbc3 100644 --- a/lib/ethers/contract_helpers.ex +++ b/lib/ethers/contract_helpers.ex @@ -150,11 +150,10 @@ defmodule Ethers.ContractHelpers do def document_returns(selectors) when is_list(selectors) do return_type_docs = selectors - |> Enum.map(& &1.returns) - |> Enum.uniq() - |> Enum.map_join("\n\n### OR\n", fn returns -> - if Enum.count(returns) > 0 do - document_types(returns) + |> Enum.uniq_by(& &1.returns) + |> Enum.map_join("\n\n### OR\n", fn selector -> + if Enum.count(selector.returns) > 0 do + document_types(selector.returns, selector.return_names) else "This function does not return any values!" end diff --git a/lib/ethers/tx_data.ex b/lib/ethers/tx_data.ex index 39552ef..a1414ef 100644 --- a/lib/ethers/tx_data.ex +++ b/lib/ethers/tx_data.ex @@ -72,7 +72,18 @@ defmodule Ethers.TxData do |> Enum.intersperse(concat(color(",", :operator, opts), break(" "))) returns = - Enum.map(selector.returns, &color(ABI.FunctionSelector.encode_type(&1), :atom, opts)) + Enum.zip(selector.returns, selector.return_names) + |> Enum.map(fn + {type, ""} -> + color(ABI.FunctionSelector.encode_type(type), :atom, opts) + + {type, name} -> + concat([ + color(ABI.FunctionSelector.encode_type(type), :atom, opts), + " ", + color(name, :variable, opts) + ]) + end) |> Enum.intersperse(concat(color(",", :operator, opts), break(" "))) returns_doc = diff --git a/test/ethers/contract_helpers_test.exs b/test/ethers/contract_helpers_test.exs index 6f6f28a..8865895 100644 --- a/test/ethers/contract_helpers_test.exs +++ b/test/ethers/contract_helpers_test.exs @@ -32,6 +32,17 @@ defmodule Ethers.ContractHelpersTest do end end + describe "document_types/2" do + test "returns correct type with name" do + assert " - amount: `{:uint, 256}`" == + ContractHelpers.document_types([{:uint, 256}], ["amount"]) + end + + test "returns correct type if names not provided" do + assert " - `{:uint, 256}`" == ContractHelpers.document_types([{:uint, 256}]) + end + end + describe "generate_arguments" do test "works with correct names" do assert [{:amount, [], _}, {:sender, [], _}] = diff --git a/test/ethers/counter_contract_test.exs b/test/ethers/counter_contract_test.exs index 1ae8d59..39bb86c 100644 --- a/test/ethers/counter_contract_test.exs +++ b/test/ethers/counter_contract_test.exs @@ -23,9 +23,12 @@ defmodule Ethers.CounterContractTest do describe "inspecting function calls" do test "renders the correct values when inspected" do - assert "#Ethers.TxData" == + assert "#Ethers.TxData" == inspect(CounterContract.get()) + assert "#Ethers.TxData" == + inspect(CounterContract.get_no_return_name()) + assert "#Ethers.TxData" == inspect(CounterContract.set(101)) end @@ -33,7 +36,7 @@ defmodule Ethers.CounterContractTest do test "shows unknown state mutability correctly" do tx_data = CounterContract.get() - assert "#Ethers.TxData" == + assert "#Ethers.TxData" == inspect(put_in(tx_data.selector.state_mutability, nil)) end @@ -49,7 +52,7 @@ defmodule Ethers.CounterContractTest do tx_data_with_default_address = %{tx_data | default_address: @from} - assert ~s'#Ethers.TxData' == + assert ~s'#Ethers.TxData' == inspect(tx_data_with_default_address) end end @@ -100,7 +103,7 @@ defmodule Ethers.CounterContractTest do input_names: [], types: [], returns: [uint: 256], - return_names: [""] + return_names: ["amount"] }, default_address: nil } == CounterContract.get() diff --git a/test/support/contracts/counter.sol b/test/support/contracts/counter.sol index 02e49d4..9150e8b 100644 --- a/test/support/contracts/counter.sol +++ b/test/support/contracts/counter.sol @@ -8,7 +8,11 @@ contract Counter { storeAmount = initialAmount; } - function get() public view returns (uint256) { + function get() public view returns (uint256 amount) { + return storeAmount; + } + + function getNoReturnName() public view returns (uint256) { return storeAmount; }