4
0
Fork 0

Reformat code that handles returning proper error messages

pull/11/head
Piotr Żelazko 7 years ago
parent beade40d68
commit 84c41b5d37
No known key found for this signature in database
GPG Key ID: 27E11A19853A7913
  1. 61
      lib/exw3.ex
  2. 61
      test/exw3_test.exs

@ -542,6 +542,21 @@ defmodule ExW3 do
end end
# Options' checkers
defp check_option(nil, error_atom), do: {:error, error_atom}
defp check_option([], error_atom), do: {:error, error_atom}
defp check_option([head | tail], atom) do
if head do
{:ok, head}
else
check_option(tail, atom)
end
end
defp check_option(value, _atom), do: {:ok, value}
# Casts # Casts
def handle_cast({:at, address}, state) do def handle_cast({:at, address}, state) do
@ -563,15 +578,13 @@ defmodule ExW3 do
# Calls # Calls
def handle_call({:deploy, args}, _from, state) do def handle_call({:deploy, args}, _from, state) do
case {args[:options][:from], args[:options][:gas]} do with {:ok, _} <- check_option(args[:options][:from], :missing_sender),
{nil, _} -> {:reply, {:error, :missing_sender}, state} {:ok,_} <- check_option(args[:options][:gas], :missing_gas),
{_, nil} -> {:reply, {:error, :missing_gas}, state} {:ok, bin} <- check_option([state[:bin], args[:bin]], :missing_binary)
{_, _} -> do
case {args[:bin], state[:bin]} do {:reply, {:ok, deploy_helper(bin, state[:abi], args)}, state}
{nil, nil} -> {:reply, {:error, :missing_binary}, state} else
{bin, nil} -> {:reply, {:ok, deploy_helper(bin, state[:abi], args)}, state} err -> {:reply, err, state}
{nil, bin} -> {:reply, {:ok, deploy_helper(bin, state[:abi], args)}, state}
end
end end
end end
@ -580,30 +593,22 @@ defmodule ExW3 do
end end
def handle_call({:call, {method_name, args}}, _from, state) do def handle_call({:call, {method_name, args}}, _from, state) do
address = state[:address] with {:ok, address} <- check_option(state[:address], :missing_address)
do
if address do {:reply, eth_call_helper(address, state[:abi], Atom.to_string(method_name), args), state}
result = eth_call_helper(address, state[:abi], Atom.to_string(method_name), args)
{:reply, result, state}
else else
{:reply, {:error, :missing_address}, state} err -> {:reply, err, state}
end end
end end
def handle_call({:send, {method_name, args, options}}, _from, state) do def handle_call({:send, {method_name, args, options}}, _from, state) do
case {state[:address], options[:from], options[:gas]} do with {:ok, address} <- check_option(state[:address], :missing_address),
{nil, _, _} -> {:ok, _} <- check_option(options[:from], :missing_sender),
{:reply, {:error, :missing_address}, state} {:ok, _} <- check_option(options[:gas], :missing_gas)
{_, nil, _} -> do
{:reply, {:error, :missing_sender}, state} {:reply, eth_send_helper(address, state[:abi], Atom.to_string(method_name), args, options), state}
{_, _, nil} -> else
{:reply, {:error, :missing_gas}, state} err -> {:reply, err, state}
{address, _, _} ->
{
:reply,
eth_send_helper(address, state[:abi], Atom.to_string(method_name), args, options),
state
}
end end
end end

@ -280,4 +280,65 @@ defmodule EXW3Test do
test "returns invalid check for is_valid_checksum_address()" do test "returns invalid check for is_valid_checksum_address()" do
assert ExW3.is_valid_checksum_address("0x2f015c60e0be116b1f0cd534704db9c92118fb6a") == false assert ExW3.is_valid_checksum_address("0x2f015c60e0be116b1f0cd534704db9c92118fb6a") == false
end end
test "returns proper error messages at contract deployment", context do
ExW3.Contract.start_link(SimpleStorage, abi: context[:simple_storage_abi])
assert {:error, :missing_gas} ==
ExW3.Contract.deploy(
SimpleStorage,
bin: ExW3.load_bin("test/examples/build/SimpleStorage.bin"),
args: [],
options: %{
from: Enum.at(context[:accounts], 0)
}
)
assert {:error, :missing_sender} ==
ExW3.Contract.deploy(
SimpleStorage,
bin: ExW3.load_bin("test/examples/build/SimpleStorage.bin"),
args: [],
options: %{
gas: 300_000,
}
)
assert {:error, :missing_binary} ==
ExW3.Contract.deploy(
SimpleStorage,
args: [],
options: %{
gas: 300_000,
from: Enum.at(context[:accounts], 0)
}
)
end
test "return proper error messages at send and call", context do
ExW3.Contract.start_link(SimpleStorage, abi: context[:simple_storage_abi])
{:ok, address} =
ExW3.Contract.deploy(
SimpleStorage,
bin: ExW3.load_bin("test/examples/build/SimpleStorage.bin"),
args: [],
options: %{
gas: 300_000,
from: Enum.at(context[:accounts], 0)
}
)
assert {:error, :missing_address} == ExW3.Contract.call(SimpleStorage, :get)
assert {:error, :missing_address} == ExW3.Contract.send(SimpleStorage, :set, [1], %{from: Enum.at(context[:accounts], 0), gas: 50_000})
ExW3.Contract.at(SimpleStorage, address)
assert {:error, :missing_sender} == ExW3.Contract.send(SimpleStorage, :set, [1], %{gas: 50_000})
assert {:error, :missing_gas} == ExW3.Contract.send(SimpleStorage, :set, [1], %{from: Enum.at(context[:accounts], 0)})
end
end end

Loading…
Cancel
Save