From 765b019edbbda6c4f8302dffa07c86d464fb3904 Mon Sep 17 00:00:00 2001 From: hswick Date: Thu, 27 Sep 2018 15:27:12 -0500 Subject: [PATCH] Added call_async and send_async --- lib/exw3.ex | 16 ++++++++++++++++ test/exw3_test.exs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/lib/exw3.ex b/lib/exw3.ex index 90ccbb9..5e98c04 100644 --- a/lib/exw3.ex +++ b/lib/exw3.ex @@ -545,6 +545,22 @@ defmodule ExW3 do GenServer.call(ContractManager, {:send, {contract_name, method_name, args, options}}) end + @spec call_async(keyword(), keyword(), []) :: {:ok, any()} + @doc "Use a Contract's method with an eth_call. Returns a Task to be awaited." + def call_async(contract_name, method_name, args \\ []) do + Task.async(fn -> + GenServer.call(ContractManager, {:call, {contract_name, method_name, args}}) + end) + end + + @spec send_async(keyword(), keyword(), [], %{}) :: {:ok, binary()} + @doc "Use a Contract's method with an eth_sendTransaction. Returns a Task to be awaited." + def send_async(contract_name, method_name, args, options) do + Task.async(fn -> + GenServer.call(ContractManager, {:send, {contract_name, method_name, args, options}}) + end) + end + @spec tx_receipt(keyword(), binary()) :: %{} @doc "Returns a formatted transaction receipt for the given transaction hash(id)" def tx_receipt(contract_name, tx_hash) do diff --git a/test/exw3_test.exs b/test/exw3_test.exs index f010ec8..d4caf78 100644 --- a/test/exw3_test.exs +++ b/test/exw3_test.exs @@ -559,4 +559,42 @@ defmodule EXW3Test do assert ExW3.from_wei(1_000_000_000_000_000_000, :tether) == 0.000000000001 end + test "send and call sync with SimpleStorage", context do + + + ExW3.Contract.register(: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) + } + ) + + ExW3.Contract.at(:SimpleStorage, address) + + assert address == ExW3.Contract.address(:SimpleStorage) + + t = ExW3.Contract.call_async(:SimpleStorage, :get) + + {:ok, data} = Task.await(t) + + assert data == 0 + + t = ExW3.Contract.send_async(:SimpleStorage, :set, [1], %{from: Enum.at(context[:accounts], 0), gas: 50_000}) + + Task.await(t) + + t = ExW3.Contract.call_async(:SimpleStorage, :get) + + {:ok, data} = Task.await(t) + + assert data == 1 + end + + end