From 84c41b5d374741828a90a234e7a2a396dad72505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20=C5=BBelazko?= Date: Wed, 29 Aug 2018 18:50:20 +0200 Subject: [PATCH] Reformat code that handles returning proper error messages --- lib/exw3.ex | 65 +++++++++++++++++++++++++--------------------- test/exw3_test.exs | 61 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 30 deletions(-) diff --git a/lib/exw3.ex b/lib/exw3.ex index 574fe2c..3c851d2 100644 --- a/lib/exw3.ex +++ b/lib/exw3.ex @@ -542,6 +542,21 @@ defmodule ExW3 do 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 def handle_cast({:at, address}, state) do @@ -563,16 +578,14 @@ defmodule ExW3 do # Calls def handle_call({:deploy, args}, _from, state) do - case {args[:options][:from], args[:options][:gas]} do - {nil, _} -> {:reply, {:error, :missing_sender}, state} - {_, nil} -> {:reply, {:error, :missing_gas}, state} - {_, _} -> - case {args[:bin], state[:bin]} do - {nil, nil} -> {:reply, {:error, :missing_binary}, state} - {bin, nil} -> {:reply, {:ok, deploy_helper(bin, state[:abi], args)}, state} - {nil, bin} -> {:reply, {:ok, deploy_helper(bin, state[:abi], args)}, state} - end - end + with {:ok, _} <- check_option(args[:options][:from], :missing_sender), + {:ok,_} <- check_option(args[:options][:gas], :missing_gas), + {:ok, bin} <- check_option([state[:bin], args[:bin]], :missing_binary) + do + {:reply, {:ok, deploy_helper(bin, state[:abi], args)}, state} + else + err -> {:reply, err, state} + end end def handle_call(:address, _from, state) do @@ -580,30 +593,22 @@ defmodule ExW3 do end def handle_call({:call, {method_name, args}}, _from, state) do - address = state[:address] - - if address do - result = eth_call_helper(address, state[:abi], Atom.to_string(method_name), args) - {:reply, result, state} - else - {:reply, {:error, :missing_address}, state} + with {:ok, address} <- check_option(state[:address], :missing_address) + do + {:reply, eth_call_helper(address, state[:abi], Atom.to_string(method_name), args), state} + else + err -> {:reply, err, state} end end def handle_call({:send, {method_name, args, options}}, _from, state) do - case {state[:address], options[:from], options[:gas]} do - {nil, _, _} -> - {:reply, {:error, :missing_address}, state} - {_, nil, _} -> - {:reply, {:error, :missing_sender}, state} - {_, _, nil} -> - {:reply, {:error, :missing_gas}, state} - {address, _, _} -> - { - :reply, - eth_send_helper(address, state[:abi], Atom.to_string(method_name), args, options), - state - } + with {:ok, address} <- check_option(state[:address], :missing_address), + {:ok, _} <- check_option(options[:from], :missing_sender), + {:ok, _} <- check_option(options[:gas], :missing_gas) + do + {:reply, eth_send_helper(address, state[:abi], Atom.to_string(method_name), args, options), state} + else + err -> {:reply, err, state} end end diff --git a/test/exw3_test.exs b/test/exw3_test.exs index f9d8ad3..996500f 100644 --- a/test/exw3_test.exs +++ b/test/exw3_test.exs @@ -280,4 +280,65 @@ defmodule EXW3Test do test "returns invalid check for is_valid_checksum_address()" do assert ExW3.is_valid_checksum_address("0x2f015c60e0be116b1f0cd534704db9c92118fb6a") == false 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