Skip to content

Commit

Permalink
important upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Sep 17, 2017
1 parent b0bc7ac commit d9f063f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 27 deletions.
80 changes: 58 additions & 22 deletions lib/eth/query.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# TODO: use Macro.underscore
defmodule ETH.Query do
import ETH.Utils

Expand Down Expand Up @@ -27,7 +28,7 @@ defmodule ETH.Query do
end

def get_block(block_number) when is_number(block_number) do
Ethereumex.HttpClient.eth_get_block_by_number([block_number, true])
Ethereumex.HttpClient.eth_get_block_by_number(["0x" <> Hexate.encode(block_number), true])
|> get_result # TODO: make camelCased to snake_cased
|> convert_block_details
end
Expand Down Expand Up @@ -75,20 +76,7 @@ defmodule ETH.Query do
def get_transaction(transaction_hash) do
Ethereumex.HttpClient.eth_get_transaction_by_hash([transaction_hash])
|> get_result
|> Enum.reduce(%{}, fn(tuple, acc) ->
{key, value} = tuple

case key do
"nonce" -> Map.put(acc, :nonce, convert_to_number(value))
"blockHash" -> Map.put(acc, :block_hash, value)
"blockNumber" -> Map.put(acc, :block_number, convert_to_number(value))
"transactionIndex" -> Map.put(acc, :transaction_index, convert_to_number(value))
"value" -> Map.put(acc, :value, convert_to_number(value))
"gasPrice" -> Map.put(acc, :gas_price, convert_to_number(value))
"gas" -> Map.put(acc, :gas, convert_to_number(value))
_ -> Map.put(acc, String.to_atom(key), value)
end
end)
|> convert_transaction_details
end

def get_transaction_receipt(transaction_hash) do
Expand All @@ -104,8 +92,13 @@ defmodule ETH.Query do
"blockNumber" -> Map.put(acc, :block_number, convert_to_number(value))
"cumulativeGasUsed" -> Map.put(acc, :cumulative_gas_used, convert_to_number(value))
"gasUsed" -> Map.put(acc, :gas_used, convert_to_number(value))
"contractAddress" -> Map.put(acc, :contract_address, value)
"logs" -> Map.put(acc, :logs, value)
"contractAddress" -> Map.put(acc, :contract_address, value)
"logs" ->
Map.put(acc, :logs, Enum.map(value, fn(log) ->
convert_transaction_log(log)
end))
"logsBloom" -> Map.put(acc, :logs_bloom, value)
_ -> Map.put(acc, String.to_atom(key), value)
end
end)
end
Expand All @@ -125,7 +118,8 @@ defmodule ETH.Query do
|> get_number_result
end

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

defp get_number_result(eth_result) do
get_result(eth_result) |> convert_to_number
Expand All @@ -137,6 +131,42 @@ defmodule ETH.Query do
|> Hexate.to_integer
end

defp convert_transaction_log(log) do
Enum.reduce(log, %{}, fn(tuple, acc) ->

{key, value} = tuple

case key do
"blockHash" -> Map.put(acc, :block_hash, value)
"blockNumber" -> Map.put(acc, :block_number, convert_to_number(value))
"logIndex" -> Map.put(acc, :log_index, convert_to_number(value))
"transactionHash" -> Map.put(acc, :transaction_hash, value)
"transactionIndex" -> Map.put(acc, :transaction_index, convert_to_number(value))
"transactionLogIndex" -> Map.put(acc, :transaction_log_index, convert_to_number(value))
_ -> Map.put(acc, String.to_atom(key), value)
end
end)
end
defp convert_transaction_details(transaction) do
Enum.reduce(transaction, %{}, fn(tuple, acc) ->
{key, value} = tuple

case key do
"nonce" -> Map.put(acc, :nonce, convert_to_number(value))
"blockHash" -> Map.put(acc, :block_hash, value)
"blockNumber" -> Map.put(acc, :block_number, convert_to_number(value))
"transactionIndex" -> Map.put(acc, :transaction_index, convert_to_number(value))
"value" -> Map.put(acc, :value, convert_to_number(value))
"gasPrice" -> Map.put(acc, :gas_price, convert_to_number(value))
"gas" -> Map.put(acc, :gas, convert_to_number(value))
"networkId" -> Map.put(acc, :network_id, value)
"publicKey" -> Map.put(acc, :public_key, value)
"standardV" -> Map.put(acc, :standard_v, value)
_ -> Map.put(acc, String.to_atom(key), value)
end
end)
end

defp convert_block_details(result) do
result
|> Enum.reduce(%{}, fn(tuple, acc) ->
Expand All @@ -145,19 +175,25 @@ defmodule ETH.Query do
case key do
"number" -> Map.put(acc, :number, convert_to_number(value))
"parentHash" -> Map.put(acc, :parent_hash, value)
"mixHash" -> Map.put(acc, :mix_hash, value)
"sha3Uncles" -> Map.put(acc, :sha3_uncles, value)
"logsBloom" -> Map.put(acc, :logs_bloom, value)
"transactionsRoot" -> Map.put(acc, :transactions_root, value)
"stateRoot" -> Map.put(acc, :state_root, value)
"receiptRoot" -> Map.put(acc, :receipt_root, value)
"difficulty" -> Map.put(acc, :difficulty, convert_to_number(value))
"totalDifficulty" -> Map.put(acc, :total_difficulty, convert_to_number(value))
"receiptsRoot" -> Map.put(acc, :receipt_root, value)
"extraData" -> Map.put(acc, :extra_data, value)
"size" -> Map.put(acc, :size, convert_to_number(value))
"sealFields" -> Map.put(acc, :seal_fields, value)
"gasLimit" -> Map.put(acc, :gas_limit, convert_to_number(value))
"gasUsed" -> Map.put(acc, :gas_used, convert_to_number(value))
"timestamp" -> Map.put(acc, :timestamp, convert_to_number(value))
_ -> Map.put(acc, String.to_atom(key), value)
"difficulty" -> Map.put(acc, :difficulty, convert_to_number(value))
"totalDifficulty" -> Map.put(acc, :total_difficulty, convert_to_number(value))
"transactions" ->
Map.put(acc, String.to_atom(key), Enum.map(value, fn(transaction) ->
convert_transaction_details(transaction)
end))
_ -> Map.put(acc, String.to_atom(key), value)
end
end)
end
Expand Down
6 changes: 3 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ defmodule Eth.Mixfile do
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
extra_applications: [:logger, :ethereumex]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:libsecp256k1, [github: "mbrix/libsecp256k1", manager: :rebar]},
{:keccakf1600, git: "https://github.com/jur0/erlang-keccakf1600", branch: "original-keccak"},
{:keccakf1600, "~> 2.0", hex: :keccakf1600_orig},
{:ex_rlp, "~> 0.2.1"},
{:hexate, "~> 0.6.1"},
{:ethereumex, "~> 0.1.0"},
{:ethereumex, "~> 0.1.1"},
{:poison, "~> 3.1"},
{:ex_doc, ">= 0.0.0", only: :dev}
]
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
%{"certifi": {:hex, :certifi, "1.2.1", "c3904f192bd5284e5b13f20db3ceac9626e14eeacfbb492e19583cf0e37b22be", [:rebar3], []},
"earmark": {:hex, :earmark, "1.2.3", "206eb2e2ac1a794aa5256f3982de7a76bf4579ff91cb28d0e17ea2c9491e46a4", [:mix], []},
"ethereumex": {:hex, :ethereumex, "0.1.0", "05764dfdfcc3c98a0cd2c5452f8bd086f77187df5994d07ebbb761a189f1afc8", [:mix], [{:httpoison, "~> 0.11.1", [hex: :httpoison, optional: false]}, {:poison, "~> 3.1.0", [hex: :poison, optional: false]}]},
"ethereumex": {:hex, :ethereumex, "0.1.1", "9b308ab4d5f13e784165f977c1117ee3ac8964f5897f67bc7356959569ba9a8b", [:mix], [{:httpoison, "~> 0.11.1", [hex: :httpoison, optional: false]}, {:poison, "~> 3.1.0", [hex: :poison, optional: false]}]},
"ex_doc": {:hex, :ex_doc, "0.16.3", "cd2a4cfe5d26e37502d3ec776702c72efa1adfa24ed9ce723bb565f4c30bd31a", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}]},
"ex_rlp": {:hex, :ex_rlp, "0.2.1", "bd320900d6316cdfe01d365d4bda22eb2f39b359798daeeffd3bd1ca7ba958ec", [:mix], []},
"hackney": {:hex, :hackney, "1.8.6", "21a725db3569b3fb11a6af17d5c5f654052ce9624219f1317e8639183de4a423", [:rebar3], [{:certifi, "1.2.1", [hex: :certifi, optional: false]}, {:idna, "5.0.2", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
"hex_prefix": {:hex, :hex_prefix, "0.1.0", "e96b5cbb6ad8493196ce193726240023f5ce0ae0753118a19a5b43e2db0267ca", [:mix], []},
"hexate": {:hex, :hexate, "0.6.1", "1cea42e462c1daa32223127d4752e71016c3d933d492b9bb7fa4709a4a0fd50d", [:mix], []},
"httpoison": {:hex, :httpoison, "0.11.2", "9e59f17a473ef6948f63c51db07320477bad8ba88cf1df60a3eee01150306665", [:mix], [{:hackney, "~> 1.8.0", [hex: :hackney, optional: false]}]},
"idna": {:hex, :idna, "5.0.2", "ac203208ada855d95dc591a764b6e87259cb0e2a364218f215ad662daa8cd6b4", [:rebar3], [{:unicode_util_compat, "0.2.0", [hex: :unicode_util_compat, optional: false]}]},
"keccakf1600": {:git, "https://github.com/jur0/erlang-keccakf1600", "a87d31d32250091262e0da5ee2c2a4535bc50945", [branch: "original-keccak"]},
"keccakf1600": {:hex, :keccakf1600_orig, "2.0.0", "0a7217ddb3ee8220d449bbf7575ec39d4e967099f220a91e3dfca4dbaef91963", [:rebar3], []},
"libsecp256k1": {:git, "https://github.com/mbrix/libsecp256k1.git", "671be513a6c19db47fbeea0ceefbf61421d196cc", []},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
Expand Down

0 comments on commit d9f063f

Please sign in to comment.