Skip to content

Commit

Permalink
transaction queries complete with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Jan 28, 2018
1 parent 2a563f6 commit 78f4a8e
Show file tree
Hide file tree
Showing 9 changed files with 497 additions and 141 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Additionally this library wouldnt exist without the libraries below:
- libsecp256k1

### TODO:
contract creation via
Ethereum Smart Contracts, small enhancements

## Installation

Expand All @@ -84,6 +84,12 @@ def deps do
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/eth](https://hexdocs.pm/eth).
### Running the tests

Install a testrpc(now renamed to ganache-cli) to run ethereum locally:
```npm install -g ganache-cli```

Elixir tests automatically spawns and kills the ganache-cli process. All you need to do now is to run the tests in sequence thus --seed 0:
```mix test --seed 0```

The docs can be found at [https://hexdocs.pm/eth](https://hexdocs.pm/eth).
10 changes: 8 additions & 2 deletions lib/eth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ defmodule ETH do
defdelegate gas_price, to: ETH.Query
defdelegate gas_price!, to: ETH.Query
defdelegate call(call_params), to: ETH.Query
defdelegate call(call_params, state), to: ETH.Query
defdelegate call!(call_params), to: ETH.Query
defdelegate call!(call_params, state), to: ETH.Query
defdelegate get_block, to: ETH.Query
defdelegate get_block(identifier), to: ETH.Query
defdelegate get_block!, to: ETH.Query
defdelegate get_block!(identifier), to: ETH.Query
defdelegate get_balance(wallet_or_address), to: ETH.Query
defdelegate get_balance(wallet_or_address, denomination), to: ETH.Query
defdelegate get_balance(wallet_or_address, state), to: ETH.Query
defdelegate get_balance(wallet_or_address, denomination, state), to: ETH.Query
defdelegate get_balance!(wallet_or_address), to: ETH.Query
defdelegate get_balance!(wallet_or_address, denomination), to: ETH.Query
defdelegate get_balance!(wallet_or_address, state), to: ETH.Query
defdelegate get_balance!(wallet_or_address, denomination, state), to: ETH.Query
defdelegate estimate_gas(transaction), to: ETH.Query
defdelegate estimate_gas!(transaction), to: ETH.Query
defdelegate estimate_gas(transaction, denomination), to: ETH.Query
Expand All @@ -49,7 +53,9 @@ defmodule ETH do
defdelegate get_transaction_receipt(transaction_hash), to: ETH.TransactionQueries
defdelegate get_transaction_receipt!(transaction_hash), to: ETH.TransactionQueries
defdelegate get_transaction_count(wallet_or_address), to: ETH.TransactionQueries
defdelegate get_transaction_count(wallet_or_address, state), to: ETH.TransactionQueries
defdelegate get_transaction_count!(wallet_or_address), to: ETH.TransactionQueries
defdelegate get_transaction_count!(wallet_or_address, state), to: ETH.TransactionQueries

defdelegate parse(data), to: ETH.Transaction.Parser
defdelegate to_list(data), to: ETH.Transaction.Parser
Expand Down
29 changes: 13 additions & 16 deletions lib/eth/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ defmodule ETH.Query do
end

# TODO: test this one
def call(call_params), do: HttpClient.eth_call(call_params)
def call(call_params, state \\ "latest"), do: HttpClient.eth_call(call_params, state)

def call!(call_params) do
{:ok, result} = HttpClient.eth_call(call_params)
def call!(call_params, state \\ "latest") do
{:ok, result} = HttpClient.eth_call(call_params, state)

result
end
Expand Down Expand Up @@ -99,10 +99,10 @@ defmodule ETH.Query do
convert_block_details(raw_block_details)
end

def get_balance(param, denomination \\ :ether)
def get_balance(param, denomination \\ :ether, state \\ "latest")

def get_balance(wallet, denomination) when is_map(wallet) do
case HttpClient.eth_get_balance(wallet.eth_address) do
def get_balance(wallet, denomination, state) when is_map(wallet) do
case HttpClient.eth_get_balance(wallet.eth_address, state) do
{:ok, hex_balance} ->
balance =
hex_balance
Expand All @@ -116,8 +116,8 @@ defmodule ETH.Query do
end
end

def get_balance(eth_address, denomination) do
case HttpClient.eth_get_balance(eth_address) do
def get_balance(eth_address, denomination, state) do
case HttpClient.eth_get_balance(eth_address, state) do
{:ok, hex_balance} ->
balance =
hex_balance
Expand All @@ -131,18 +131,18 @@ defmodule ETH.Query do
end
end

def get_balance!(param, denomination \\ :ether)
def get_balance!(param, denomination \\ :ether, state \\ "latest")

def get_balance!(wallet, denomination) when is_map(wallet) do
{:ok, hex_balance} = HttpClient.eth_get_balance(wallet.eth_address)
def get_balance!(wallet, denomination, state) when is_map(wallet) do
{:ok, hex_balance} = HttpClient.eth_get_balance(wallet.eth_address, state)

hex_balance
|> convert_to_number
|> convert(denomination)
end

def get_balance!(eth_address, denomination) do
{:ok, hex_balance} = HttpClient.eth_get_balance(eth_address)
def get_balance!(eth_address, denomination, state) do
{:ok, hex_balance} = HttpClient.eth_get_balance(eth_address, state)

hex_balance
|> convert_to_number
Expand All @@ -169,9 +169,6 @@ defmodule ETH.Query do
hex_gas_estimate |> convert_to_number |> convert(denomination) |> round
end

defp get_result({:ok, eth_result}), do: Map.get(eth_result, "result")
defp get_result(error), do: raise(error)

def convert_transaction_log(log) do
Enum.reduce(log, %{}, fn tuple, acc ->
{key, value} = tuple
Expand Down
5 changes: 3 additions & 2 deletions lib/eth/transaction/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ defmodule ETH.Transaction.Builder do
value = Keyword.get(params, :value, 0)
gas_price = Keyword.get(params, :gas_price, ETH.gas_price!())
data = Keyword.get(params, :data, "")
nonce = Keyword.get(params, :nonce, ETH.get_transaction_count!(params[:from]))
# NOTE: this probably causes bug
nonce = Keyword.get(params, :nonce, ETH.get_transaction_count!(params[:from], "pending"))
chain_id = Keyword.get(params, :chain_id, 3)

gas_limit =
Expand All @@ -91,7 +92,7 @@ defmodule ETH.Transaction.Builder do
value = Map.get(params, :value, 0)
gas_price = Map.get(params, :gas_price, ETH.gas_price!())
data = Map.get(params, :data, "")
nonce = Map.get(params, :nonce, ETH.get_transaction_count!(params.from))
nonce = Map.get(params, :nonce, ETH.get_transaction_count!(params.from, "pending"))
chain_id = Map.get(params, :chain_id, 3)

gas_limit =
Expand Down
4 changes: 2 additions & 2 deletions lib/eth/transaction/signer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ defmodule ETH.Transaction.Signer do
value,
data,
v,
r,
s
_r,
_s
],
<<private_key::binary-size(32)>>
) do
Expand Down
41 changes: 19 additions & 22 deletions lib/eth/transaction_queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ defmodule ETH.TransactionQueries do
def get_block_transactions(identifier) do
case get_block(identifier) do
{:ok, block} ->
{:ok,
(block.transactions || [])
|> Enum.map(fn transaction ->
convert_transaction_details(transaction)
end)}
{:ok, Map.get(block, :transactions, [])}

error ->
error
Expand All @@ -19,9 +15,6 @@ defmodule ETH.TransactionQueries do

def get_block_transactions!(identifier) do
get_block!(identifier) |> Map.get(:transactions, [])
|> Enum.map(fn transaction ->
convert_transaction_details(transaction)
end)
end

def get_block_transaction_count(block_number) when is_number(block_number) do
Expand Down Expand Up @@ -52,14 +45,14 @@ defmodule ETH.TransactionQueries do

def get_transaction_from_block(block_number, index) when is_number(block_number) do
case HttpClient.eth_get_transaction_by_block_number_and_index(block_number, index) do
{:ok, transaction} -> {:ok, transaction}
{:ok, transaction} -> {:ok, convert_transaction_details(transaction)}
error -> error
end
end

def get_transaction_from_block(block_hash, index) do
case HttpClient.eth_get_transaction_by_block_hash_and_index(block_hash, index) do
{:ok, transaction} -> {:ok, transaction}
{:ok, transaction} -> {:ok, convert_transaction_details(transaction)}
error -> error
end
end
Expand All @@ -68,13 +61,13 @@ defmodule ETH.TransactionQueries do
{:ok, transaction} =
HttpClient.eth_get_transaction_by_block_number_and_index(block_number, index)

transaction
convert_transaction_details(transaction)
end

def get_transaction_from_block!(block_hash, index) do
{:ok, transaction} = HttpClient.eth_get_transaction_by_block_hash_and_index(block_hash, index)

transaction
convert_transaction_details(transaction)
end

def get_transaction(transaction_hash) do
Expand Down Expand Up @@ -105,39 +98,43 @@ defmodule ETH.TransactionQueries do
convert_transaction_receipt(raw_transaction_receipt)
end

def get_transaction_count(wallet) when is_map(wallet) do
case HttpClient.eth_get_transaction_count(wallet.eth_address) do
def get_transaction_count(target_wallet, state \\ "latest")

def get_transaction_count(wallet, state) when is_map(wallet) do
case HttpClient.eth_get_transaction_count(wallet.eth_address, state) do
{:ok, hex_transaction_count} -> {:ok, convert_to_number(hex_transaction_count)}
error -> error
end
end

def get_transaction_count(eth_address) do
case HttpClient.eth_get_transaction_count(eth_address) do
def get_transaction_count(eth_address, state) do
case HttpClient.eth_get_transaction_count(eth_address, state) do
{:ok, hex_transaction_count} -> {:ok, convert_to_number(hex_transaction_count)}
error -> error
end
end

def get_transaction_count!(wallet) when is_map(wallet) do
{:ok, hex_transaction_count} = HttpClient.eth_get_transaction_count(wallet.eth_address)
def get_transaction_count!(target_wallet, state \\ "latest")

def get_transaction_count!(wallet, state) when is_map(wallet) do
{:ok, hex_transaction_count} = HttpClient.eth_get_transaction_count(wallet.eth_address, state)

convert_to_number(hex_transaction_count)
end

def get_transaction_count!(eth_address) do
{:ok, hex_transaction_count} = HttpClient.eth_get_transaction_count(eth_address)
def get_transaction_count!(eth_address, state) do
{:ok, hex_transaction_count} = HttpClient.eth_get_transaction_count(eth_address, state)

convert_to_number(hex_transaction_count)
end

defp convert_to_number(result) do
def convert_to_number(result) do
result
|> String.slice(2..-1)
|> Hexate.to_integer()
end

def convert_transaction_receipt(result) do
defp convert_transaction_receipt(result) do
result
|> Enum.reduce(%{}, fn tuple, acc ->
{key, value} = tuple
Expand Down
3 changes: 0 additions & 3 deletions test.sh

This file was deleted.

6 changes: 6 additions & 0 deletions test/eth/eth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ defmodule ETH.Test do
build: 2,
build: 3,
call: 1,
call: 2,
call!: 1,
call!: 2,
convert: 2,
decode: 1,
decode16: 1,
Expand All @@ -27,8 +29,10 @@ defmodule ETH.Test do
get_address: 1,
get_balance: 1,
get_balance: 2,
get_balance: 3,
get_balance!: 1,
get_balance!: 2,
get_balance!: 3,
get_block: 0,
get_block: 1,
get_block!: 0,
Expand All @@ -45,7 +49,9 @@ defmodule ETH.Test do
get_transaction: 1,
get_transaction!: 1,
get_transaction_count: 1,
get_transaction_count: 2,
get_transaction_count!: 1,
get_transaction_count!: 2,
get_transaction_from_block: 2,
get_transaction_from_block!: 2,
get_transaction_receipt: 1,
Expand Down
Loading

0 comments on commit 78f4a8e

Please # to comment.