Skip to content

Commit

Permalink
0.3.0 release tuple queries, more tests for transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Jan 23, 2018
1 parent 40513f8 commit 4810dd0
Show file tree
Hide file tree
Showing 14 changed files with 553 additions and 93 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ The essential Elixir library for interacting with Ethereum blockchain. You can n
```elixir
wallet = ETH.Wallet.create
# %{eth_address: "0x31CF67A272A23C7A11128C97FC3B2F4C13AFD87F",
# mnemonic_phrase: "similar bleak naive absorb picnic avoid sponsor canoe inform misery crisp hotel critic have parent couch wrong survey staff primary wet wear velvet horse",
# private_key: "C8E2F24A806A422034990C7391B4CEB7133CD3680987FEBB5750555F99F0FC83",
# public_key: "04C4AA07F234226CA90FB3E8BB1590D5BEB703E449700FE0B2DF539A948289EA75220CC837CA68F429F3FB3D6677B2D63CF66277888B8209D0B3F3229CE339654C"}

specific_private_key = "9756371A51D7FC25EDFC95A49DEF3806ED34DF2EBCA2065E543369E708C47374"
another_wallet = ETH.Wallet.create(specific_private_key)
# %{eth_address: "0x6A26B49D8046DC5B74D41E29F9A5CA7AD78EEC9B",
# mnemonic_phrase: "nuclear random shoot photo lemon base retire naive pig urge sock assist spy hurdle road nephew alien verify art stable identify giraffe soccer mushroom",
# private_key: "9756371A51D7FC25EDFC95A49DEF3806ED34DF2EBCA2065E543369E708C47374",
# public_key: "04D1F70F6048D1E22FBEBBF3AF462E3356747AB2BB81EC269C600BE6A53C3223472AA336DF0060719C6F3AEC45E40AE57ED39735B61B8F5EF989466D46CA1B72C0"}

Expand All @@ -32,26 +34,26 @@ The essential Elixir library for interacting with Ethereum blockchain. You can n
first_account_in_your_client = List.first(accounts_in_your_client)
first_account_private_key = "f121f608500f7e3379c813aa6df62864e35aa0b6cd11a2ff2c20ac84b5771fb2"

ETH.get_balance("0x31CF67A272A23C7A11128C97FC3B2F4C13AFD87F") # 0 # this account holds no ether
ETH.get_balance(first_account_in_your_client, :wei) # 1.0e20 -> in this example this address holds 100 ether / 1.0e20 wei
ETH.get_balance("0x31CF67A272A23C7A11128C97FC3B2F4C13AFD87F") # {:ok, 0} # this account holds no ether
ETH.get_balance!(first_account_in_your_client, :wei) # 1.0e20 -> in this example this address holds 100 ether / 1.0e20 wei

ETH.get_transaction_count(first_account_in_your_client) # 0
ETH.get_transaction_count(first_account_in_your_client) # {:ok, 0}

{:ok, tx_hash} = ETH.send_transaction(%{
from: first_account_in_your_client, to: wallet[:eth_address], value: 22
}, first_account_private_key)
# {:ok, "0x13893d677251ddb9259263490504f3e611a0a7bff23b108641d2cb08b7af21dc"}

ETH.get_transaction(tx_hash)
ETH.get_transaction!(tx_hash)
# %{block_hash: "0xf9917088fc6750677cc1cfb4f7dcab453b21c7de2cb22ed7e6753df058bec5cf",
# block_number: 1, from: "0xfdf5d02f2082753dda0817492d6efff7e76e47aa",
# gas: 21000, gas_price: 20000000000,
# hash: "0x13893d677251ddb9259263490504f3e611a0a7bff23b108641d2cb08b7af21dc",
# input: "0", nonce: 0, to: "0x725316bb37d202b0eb203cd83238c31e983a7936",
# transaction_index: 0, value: 22}

ETH.get_balance(first_account_in_your_client) # 99.99958 # in ether
ETH.get_balance(wallet[:eth_address], :wei) # 22.0 # in wei
ETH.get_balance!(first_account_in_your_client) # 99.99958 # in ether
ETH.get_balance(wallet[:eth_address], :wei) # {:ok, 22.0} # in wei
```

Warning: This library uses the Ethereum JSON-RPC under the hood, so you need an ethereum client such as parity/geth or testrpc to use of most of the API.
Expand All @@ -60,7 +62,7 @@ Warning: This library uses the Ethereum JSON-RPC under the hood, so you need an

- [Izel Nakri](https://github.com/izelnakri) - I reverse engineered ethereum JavaScript libraries in Elixir so you don't have to.

Additionally this library wouldnt exists without the libraries below:
Additionally this library wouldnt exist without the libraries below:
- ExRLP
- Ethereumex
- keccakf1600
Expand All @@ -77,7 +79,7 @@ by adding `eth` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:eth, "~> 0.1.0"}
{:eth, "~> 0.3.0"}
]
end
```
Expand Down
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
use Mix.Config

config :ethereumex, scheme: "https", host: "localhost", port: 8545
config :ethereumex, scheme: "http", host: "localhost", port: 8545
41 changes: 3 additions & 38 deletions lib/eth/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,11 @@ defmodule ETH.Query do
{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)
_ -> Map.put(acc, key |> Macro.underscore() |> String.to_atom(), value)
end
end)
end
Expand All @@ -194,16 +192,12 @@ defmodule ETH.Query do

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)
_ -> Map.put(acc, key |> Macro.underscore() |> String.to_atom(), value)
end
end)
end
Expand All @@ -217,36 +211,9 @@ defmodule ETH.Query 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)

"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))

Expand All @@ -272,7 +239,7 @@ defmodule ETH.Query do
)

_ ->
Map.put(acc, String.to_atom(key), value)
Map.put(acc, key |> Macro.underscore() |> String.to_atom(), value)
end
end)
end
Expand All @@ -283,5 +250,3 @@ defmodule ETH.Query do
|> Hexate.to_integer()
end
end

# TODO: use Macro.underscore
20 changes: 10 additions & 10 deletions lib/eth/transaction/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ defmodule ETH.Transaction.Parser do
gas_limit: gas_limit,
to: to,
value: value,
data: data
data: data,
v: v,
r: r,
s: s
}
) do
%{
Expand All @@ -79,7 +82,10 @@ defmodule ETH.Transaction.Parser do
gas_limit: to_buffer(gas_limit),
to: to_buffer(to),
value: to_buffer(value),
data: to_buffer(data)
data: to_buffer(data),
v: to_buffer(v),
r: to_buffer(r),
s: to_buffer(s)
}
end

Expand All @@ -90,10 +96,7 @@ defmodule ETH.Transaction.Parser do
gas_limit: gas_limit,
to: to,
value: value,
data: data,
v: v,
r: r,
s: s
data: data
}
) do
%{
Expand All @@ -102,10 +105,7 @@ defmodule ETH.Transaction.Parser do
gas_limit: to_buffer(gas_limit),
to: to_buffer(to),
value: to_buffer(value),
data: to_buffer(data),
v: to_buffer(v),
r: to_buffer(r),
s: to_buffer(s)
data: to_buffer(data)
}
end

Expand Down
12 changes: 6 additions & 6 deletions lib/eth/transaction/setter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ defmodule ETH.Transaction.Setter do
defp set_params_from_list(params) do
to = Keyword.get(params, :to, "")
value = Keyword.get(params, :value, 0)
gas_price = Keyword.get(params, :gas_price, ETH.Query.gas_price())
gas_price = Keyword.get(params, :gas_price, ETH.gas_price!())
data = Keyword.get(params, :data, "")
nonce = Keyword.get(params, :nonce, ETH.Query.get_transaction_count(params[:from]))
nonce = Keyword.get(params, :nonce, ETH.get_transaction_count!(params[:from]))
chain_id = Keyword.get(params, :chain_id, 3)

gas_limit =
Keyword.get(
params,
:gas_limit,
ETH.Query.estimate_gas(%{
ETH.estimate_gas!(%{
to: to,
value: value,
data: data,
Expand All @@ -82,16 +82,16 @@ defmodule ETH.Transaction.Setter do
defp set_params_from_map(params) do
to = Map.get(params, :to, "")
value = Map.get(params, :value, 0)
gas_price = Map.get(params, :gas_price, ETH.Query.gas_price())
gas_price = Map.get(params, :gas_price, ETH.gas_price!())
data = Map.get(params, :data, "")
nonce = Map.get(params, :nonce, ETH.Query.get_transaction_count(params.from))
nonce = Map.get(params, :nonce, ETH.get_transaction_count!(params.from))
chain_id = Map.get(params, :chain_id, 3)

gas_limit =
Map.get(
params,
:gas_limit,
ETH.Query.estimate_gas(%{
ETH.estimate_gas!(%{
to: to,
value: value,
data: data,
Expand Down
24 changes: 5 additions & 19 deletions lib/eth/transaction_queries.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule ETH.TransactionQueries do
import ETH.Utils
import ETH.Query

alias Ethereumex.HttpClient
Expand All @@ -8,7 +7,7 @@ defmodule ETH.TransactionQueries do
case get_block(identifier) do
{:ok, block} ->
{:ok,
block |> Map.get("transactions")
(block.transactions || [])
|> Enum.map(fn transaction ->
convert_transaction_details(transaction)
end)}
Expand All @@ -19,7 +18,7 @@ defmodule ETH.TransactionQueries do
end

def get_block_transactions!(identifier) do
get_block!(identifier) |> Map.get("transactions")
get_block!(identifier) |> Map.get(:transactions, [])
|> Enum.map(fn transaction ->
convert_transaction_details(transaction)
end)
Expand Down Expand Up @@ -59,7 +58,7 @@ defmodule ETH.TransactionQueries do
end

def get_transaction_from_block(block_hash, index) do
case HttpClient.eth_get_transaction_by_block_hash_and_index(block_number, index) do
case HttpClient.eth_get_transaction_by_block_hash_and_index(block_hash, index) do
{:ok, transaction} -> {:ok, transaction}
error -> error
end
Expand All @@ -73,8 +72,7 @@ defmodule ETH.TransactionQueries do
end

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

transaction
end
Expand All @@ -98,15 +96,9 @@ defmodule ETH.TransactionQueries do
{key, value} = tuple

case key do
"transactionHash" ->
Map.put(acc, :transaction_hash, value)

"transactionIndex" ->
Map.put(acc, :transaction_index, convert_to_number(value))

"blockHash" ->
Map.put(acc, :block_hash, value)

"blockNumber" ->
Map.put(acc, :block_number, convert_to_number(value))

Expand All @@ -116,9 +108,6 @@ defmodule ETH.TransactionQueries do
"gasUsed" ->
Map.put(acc, :gas_used, convert_to_number(value))

"contractAddress" ->
Map.put(acc, :contract_address, value)

"logs" ->
Map.put(
acc,
Expand All @@ -128,11 +117,8 @@ defmodule ETH.TransactionQueries do
end)
)

"logsBloom" ->
Map.put(acc, :logs_bloom, value)

_ ->
Map.put(acc, String.to_atom(key), value)
Map.put(acc, key |> Macro.underscore() |> String.to_atom(), value)
end
end)
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Eth.Mixfile do
def project do
[
app: :eth,
version: "0.2.2",
version: "0.3.0",
elixir: "~> 1.5",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down
9 changes: 4 additions & 5 deletions test/eth/query_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# TODO: write tests for get_transaction\1 and get_transaction_receipt\1
# TODO: ETH.call
defmodule ETH.Query.Test do
use ExUnit.Case
Expand Down Expand Up @@ -79,7 +78,7 @@ defmodule ETH.Query.Test do
:nonce,
:number,
:parent_hash,
:receipt_root,
:receipts_root,
:sha3_uncles,
:size,
:state_root,
Expand Down Expand Up @@ -112,7 +111,7 @@ defmodule ETH.Query.Test do
:nonce,
:number,
:parent_hash,
:receipt_root,
:receipts_root,
:sha3_uncles,
:size,
:state_root,
Expand Down Expand Up @@ -149,7 +148,7 @@ defmodule ETH.Query.Test do
:nonce,
:number,
:parent_hash,
:receipt_root,
:receipts_root,
:sha3_uncles,
:size,
:state_root,
Expand Down Expand Up @@ -180,7 +179,7 @@ defmodule ETH.Query.Test do
:nonce,
:number,
:parent_hash,
:receipt_root,
:receipts_root,
:sha3_uncles,
:size,
:state_root,
Expand Down
Loading

0 comments on commit 4810dd0

Please # to comment.