4
0
Fork 0

Removed send_async and call_async

optimize
hswick 6 years ago
parent 8581ffe090
commit 6071823075
  1. 42
      lib/exw3.ex
  2. 41
      test/exw3_test.exs

@ -182,7 +182,8 @@ defmodule ExW3 do
def tx_receipt(tx_hash) do
case call_client(:eth_get_transaction_receipt, [tx_hash]) do
{:ok, receipt} ->
{:ok, Map.merge(
{:ok,
Map.merge(
receipt,
keys_to_decimal(receipt, ["blockNumber", "cumulativeGasUsed", "gasUsed"])
)}
@ -462,22 +463,6 @@ 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
@ -634,10 +619,12 @@ defmodule ExW3 do
def eth_call_helper(address, abi, method_name, args) do
result =
ExW3.eth_call([%{
ExW3.eth_call([
%{
to: address,
data: "0x#{ExW3.encode_method_call(abi, method_name, args)}"
}])
}
])
case result do
{:ok, data} -> ([:ok] ++ ExW3.decode_output(abi, method_name, data)) |> List.to_tuple()
@ -655,8 +642,8 @@ defmodule ExW3 do
data: "0x#{ExW3.encode_method_call(abi, method_name, args)}"
},
Map.put(options, :gas, gas)
)]
)
])
end
defp register_helper(contract_info) do
@ -826,13 +813,22 @@ defmodule ExW3 do
filter_id = ExW3.new_filter(payload)
{:reply, {:ok, filter_id}, Map.put(state, :filters, Map.put(state[:filters], filter_id, %{contract_name: contract_name, event_name: event_name}))}
{:reply, {:ok, filter_id},
Map.put(
state,
:filters,
Map.put(state[:filters], filter_id, %{
contract_name: contract_name,
event_name: event_name
})
)}
end
def handle_call({:get_filter_changes, filter_id}, _from, state) do
filter_info = Map.get(state[:filters], filter_id)
event_attributes = get_event_attributes(state, filter_info[:contract_name], filter_info[:event_name])
event_attributes =
get_event_attributes(state, filter_info[:contract_name], filter_info[:event_name])
logs = ExW3.get_filter_changes(filter_id)

@ -186,7 +186,6 @@ defmodule EXW3Test do
end
test "Testing formatted get filter changes", context do
ExW3.Contract.register(:EventTester, abi: context[:event_tester_abi])
{:ok, address, _} =
@ -313,7 +312,6 @@ defmodule EXW3Test do
assert Map.get(log_data, "otherNum") == 42
ExW3.Contract.uninstall_filter(indexed_filter_id)
end
test "starts a Contract GenServer for Complex contract", context do
@ -528,43 +526,4 @@ defmodule EXW3Test do
assert ExW3.from_wei(1_000_000_000_000_000_000, :gether) == 0.000000001
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

Loading…
Cancel
Save