Skip to content

Commit

Permalink
Add return names in docs and TxData inspection
Browse files Browse the repository at this point in the history
Closes #42
  • Loading branch information
alisinabh committed Oct 27, 2023
1 parent 058a8d8 commit 871fcdc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
9 changes: 4 additions & 5 deletions lib/ethers/contract_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion lib/ethers/tx_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
11 changes: 11 additions & 0 deletions test/ethers/contract_helpers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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, [], _}] =
Expand Down
11 changes: 7 additions & 4 deletions test/ethers/counter_contract_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ defmodule Ethers.CounterContractTest do

describe "inspecting function calls" do
test "renders the correct values when inspected" do
assert "#Ethers.TxData<function get() view returns (uint256)>" ==
assert "#Ethers.TxData<function get() view returns (uint256 amount)>" ==
inspect(CounterContract.get())

assert "#Ethers.TxData<function getNoReturnName() view returns (uint256)>" ==
inspect(CounterContract.get_no_return_name())

assert "#Ethers.TxData<function set(uint256 newAmount 101) non_payable>" ==
inspect(CounterContract.set(101))
end

test "shows unknown state mutability correctly" do
tx_data = CounterContract.get()

assert "#Ethers.TxData<function get() unknown returns (uint256)>" ==
assert "#Ethers.TxData<function get() unknown returns (uint256 amount)>" ==
inspect(put_in(tx_data.selector.state_mutability, nil))
end

Expand All @@ -49,7 +52,7 @@ defmodule Ethers.CounterContractTest do

tx_data_with_default_address = %{tx_data | default_address: @from}

assert ~s'#Ethers.TxData<function get() view returns (uint256)\n default_address: "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1">' ==
assert ~s'#Ethers.TxData<function get() view returns (uint256 amount)\n default_address: "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1">' ==
inspect(tx_data_with_default_address)
end
end
Expand Down Expand Up @@ -100,7 +103,7 @@ defmodule Ethers.CounterContractTest do
input_names: [],
types: [],
returns: [uint: 256],
return_names: [""]
return_names: ["amount"]
},
default_address: nil
} == CounterContract.get()
Expand Down
6 changes: 5 additions & 1 deletion test/support/contracts/counter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 871fcdc

Please sign in to comment.