Skip to content

Commit

Permalink
major transaction sending bug fixes with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Jan 23, 2018
1 parent 5adc076 commit 3620c6e
Show file tree
Hide file tree
Showing 12 changed files with 509 additions and 379 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
echo 'nvm install v7.10.1' >> $BASH_ENV
echo 'nvm alias default 7.10.1' >> $BASH_ENV
- run: node -v
- run: npm install -g testrpc
- run: npm install -g ganache-cli
- run: mix local.hex --force
- run: mix local.rebar --force
- run: mix deps.get
Expand Down
3 changes: 3 additions & 0 deletions lib/eth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ defmodule ETH do
defdelegate get_transaction_from_block(identifier, index), to: ETH.TransactionQueries
defdelegate get_transaction_from_block!(identifier, index), to: ETH.TransactionQueries
defdelegate get_transaction(transaction_hash), to: ETH.TransactionQueries
defdelegate get_transaction!(transaction_hash), to: ETH.TransactionQueries
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), to: ETH.TransactionQueries

Expand All @@ -72,6 +74,7 @@ defmodule ETH do
to: ETH.Transaction

defdelegate send(signature), to: ETH.Transaction
defdelegate send!(signature), to: ETH.Transaction
defdelegate get_senders_public_key(transaction_input), to: ETH.Transaction
defdelegate get_sender_address(transaction_input), to: ETH.Transaction

Expand Down
27 changes: 13 additions & 14 deletions lib/eth/transaction.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule ETH.Transaction do
import ETH.Utils
alias Ethereumex.HttpClient

defdelegate parse(data), to: ETH.Transaction.Parser
defdelegate to_list(data), to: ETH.Transaction.Parser
Expand Down Expand Up @@ -66,7 +67,11 @@ defmodule ETH.Transaction do
|> to_transaction(private_key)
end

def send(signature), do: Ethereumex.HttpClient.eth_send_raw_transaction([signature])
def send(signature), do: HttpClient.eth_send_raw_transaction(signature)
def send!(signature) do
{:ok, transaction_hash} = HttpClient.eth_send_raw_transaction(signature)
transaction_hash
end

# NOTE: not tested
def get_senders_public_key("0x" <> rlp_encoded_transaction_list) do
Expand Down Expand Up @@ -158,24 +163,18 @@ defmodule ETH.Transaction do
defp to_transaction(params, private_key) do
target_params = set_default_from(params, private_key)

result =
target_params
|> build
|> sign_transaction(private_key)
|> Base.encode16()
|> send

case result do
{:ok, transaction_details} -> transaction_details["result"]
_ -> result
end
target_params
|> build
|> sign_transaction(private_key)
|> Base.encode16()
|> send
end

defp set_default_from(params, private_key) when is_list(params) do
Keyword.get(params, :from, get_address(private_key))
put_in(params, [:from], Keyword.get(params, :from, get_address(private_key)))
end

defp set_default_from(params, private_key) when is_map(params) do
Map.get(params, :from, get_address(private_key))
Map.merge(params, %{from: Map.get(params, :from, get_address(private_key))})
end
end
2 changes: 1 addition & 1 deletion lib/eth/transaction/signer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ defmodule ETH.Transaction.Signer do
],
<<private_key::binary-size(32)>>
)
when is_map(transaction_list) do
when is_list(transaction_list) do
sign_transaction_list(transaction_list, private_key)
end

Expand Down
70 changes: 41 additions & 29 deletions lib/eth/transaction_queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,36 +91,16 @@ defmodule ETH.TransactionQueries do
end

def get_transaction_receipt(transaction_hash) do
HttpClient.eth_get_transaction_receipt([transaction_hash])
|> Enum.reduce(%{}, fn tuple, acc ->
{key, value} = tuple

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

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

"logs" ->
Map.put(
acc,
:logs,
Enum.map(value, fn log ->
convert_transaction_log(log)
end)
)
case HttpClient.eth_get_transaction_receipt(transaction_hash) do
{:ok, raw_transaction_receipt} ->
{:ok, convert_transaction_receipt(raw_transaction_receipt)}
error -> error
end
end

_ ->
Map.put(acc, key |> Macro.underscore() |> String.to_atom(), value)
end
end)
def get_transaction_receipt!(transaction_hash) do
{:ok, raw_transaction_receipt} = HttpClient.eth_get_transaction_receipt(transaction_hash)
convert_transaction_receipt(raw_transaction_receipt)
end

def get_transaction_count(wallet) when is_map(wallet) do
Expand Down Expand Up @@ -154,4 +134,36 @@ defmodule ETH.TransactionQueries do
|> String.slice(2..-1)
|> Hexate.to_integer()
end

def convert_transaction_receipt(result) do
result |> Enum.reduce(%{}, fn tuple, acc ->
{key, value} = tuple

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

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

"logs" ->
Map.put(
acc,
:logs,
Enum.map(value, fn log ->
convert_transaction_log(log)
end)
)

_ ->
Map.put(acc, key |> Macro.underscore() |> String.to_atom(), value)
end
end)
end
end
2 changes: 1 addition & 1 deletion lib/eth/wallet.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# NOTE: maybe do HD Wallets + create(mnemonic phrase)
# NOTE: maybe do HD Wallets + create(mnemonic phrase) + ICAP
defmodule ETH.Wallet do
import ETH.Utils

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.3.2",
version: "0.3.3",
elixir: "~> 1.6",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down
3 changes: 3 additions & 0 deletions test/eth/eth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,21 @@ defmodule ETH.Test do
get_sender_address: 1,
get_senders_public_key: 1,
get_transaction: 1,
get_transaction!: 1,
get_transaction_count: 1,
get_transaction_count!: 1,
get_transaction_from_block: 2,
get_transaction_from_block!: 2,
get_transaction_receipt: 1,
get_transaction_receipt!: 1,
hash: 1,
hash: 2,
keccak256: 1,
pad_to_even: 1,
parse: 1,
secp256k1_signature: 2,
send: 1,
send!: 1,
send_transaction: 2,
send_transaction: 3,
send_transaction: 4,
Expand Down
Loading

0 comments on commit 3620c6e

Please # to comment.