From fcb87178373a4718aec7ae7a6575a991b2b146c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20=C5=BBelazko?= Date: Wed, 29 Aug 2018 17:01:41 +0200 Subject: [PATCH] Add error messages in case user does not provide either gas or transaction sender --- lib/exw3.ex | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/exw3.ex b/lib/exw3.ex index 3bc1470..9a1a8fc 100644 --- a/lib/exw3.ex +++ b/lib/exw3.ex @@ -541,6 +541,7 @@ defmodule ExW3 do ) end + # Casts def handle_cast({:at, address}, state) do @@ -562,10 +563,15 @@ defmodule ExW3 do # Calls def handle_call({:deploy, args}, _from, state) do - case {args[:bin], state[:bin]} do - {nil, nil} -> {:reply, {:error, "contract binary was never provided"}, state} - {bin, nil} -> {:reply, {:ok, deploy_helper(bin, state[:abi], args)}, state} - {nil, bin} -> {:reply, {:ok, deploy_helper(bin, state[:abi], args)}, state} + case {args[:options][:from], args[:options][:gas]} do + {nil, _} -> {:reply, {:error, "transaction sender not provided"}, state} + {_, nil} -> {:reply, {:error, "gas amount not provided"}, state} + {_, _} -> + case {args[:bin], state[:bin]} do + {nil, nil} -> {:reply, {:error, "contract binary was never provided"}, 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 end @@ -585,13 +591,19 @@ defmodule ExW3 do end def handle_call({:send, {method_name, args, options}}, _from, state) do - address = state[:address] - - if address do - result = eth_send_helper(address, state[:abi], Atom.to_string(method_name), args, options) - {:reply, result, state} - else - {:reply, {:error, "contract address not available"}, state} + case {state[:address], options[:from], options[:gas]} do + {nil, _, _} -> + {:reply, {:error, "contract address not available"}, state} + {_, nil, _} -> + {:reply, {:error, "transaction sender not provided"}, state} + {_, _, nil} -> + {:reply, {:error, "gas amount not provided"}, state} + {address, _, _} -> + { + :reply, + eth_send_helper(address, state[:abi], Atom.to_string(method_name), args, options), + state + } end end