From b999371ea17a3053f5a8caf5aff235b596fd1ae5 Mon Sep 17 00:00:00 2001 From: Izel Nakri Date: Fri, 8 Sep 2017 21:02:13 +0200 Subject: [PATCH] bugfix for the to_buffer --- lib/eth/transaction.ex | 45 +++++++++++++++++++---------------- lib/eth/utils.ex | 8 ++++++- test/eth/transaction_test.exs | 12 ++++++++++ 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/lib/eth/transaction.ex b/lib/eth/transaction.ex index 529aae5..56c745a 100644 --- a/lib/eth/transaction.ex +++ b/lib/eth/transaction.ex @@ -25,45 +25,48 @@ defmodule ETH.Transaction do end # TODO: what if its a wallet - def set(params = %{from: from, to: to, value: value}) do + def set(params) when is_list(params) do gas_price = Keyword.get(params, :gas_price, ETH.Query.gas_price()) data = Keyword.get(params, :data, "") - nonce = Keyword.get(params, :nonce, ETH.Query.get_transaction_count(from)) + nonce = Keyword.get(params, :nonce, ETH.Query.get_transaction_count(params[:from])) chain_id = Keyword.get(params, :chain_id, 3) gas_limit = Keyword.get(params, :gas_limit, ETH.Query.estimate_gas(%{ - to: to, value: value, data: data, nonce: nonce, chain_id: chain_id + to: params[:to], value: params[:value], data: data, nonce: nonce, chain_id: chain_id })) + # gas_limit = 100000000 - %{nonce: nonce, gas_price: gas_price, gas_limit: gas_limit, to: to, value: value, data: data} + %{ + nonce: nonce, gas_price: gas_price, gas_limit: gas_limit, to: params[:to], + value: params[:value], data: params[:data] + } |> parse end - def set(params = [from: from, to: to, value: value]) do - gas_price = Keyword.get(params, :gas_price, ETH.Query.gas_price()) - data = Keyword.get(params, :data, "") - nonce = Keyword.get(params, :nonce, ETH.Query.get_transaction_count(from)) - chain_id = Keyword.get(params, :chain_id, 3) - gas_limit = Keyword.get(params, :gas_limit, ETH.Query.estimate_gas(%{ - to: to, value: value, data: data, nonce: nonce, chain_id: chain_id + def set(params) do + gas_price = Map.get(params, :gas_price, ETH.Query.gas_price()) + data = Map.get(params, :data, "") + nonce = Map.get(params, :nonce, ETH.Query.get_transaction_count(params.from)) + chain_id = Map.get(params, :chain_id, 3) + gas_limit = Map.get(params, :gas_limit, ETH.Query.estimate_gas(%{ + to: params.to, value: params.value, data: data, nonce: nonce, chain_id: chain_id })) + # gas_limit = 100000000 - %{nonce: nonce, gas_price: gas_price, gas_limit: gas_limit, to: to, value: value, data: data} + %{nonce: nonce, gas_price: gas_price, gas_limit: gas_limit, to: params.to, value: params.value, data: data} |> parse end - # TODO: what if its a wallet - def send_transaction(params = %{from: _from, to: _to, value: _value}, private_key) do + def send_transaction(params, private_key) when is_list(params) do params |> set - |> to_list - |> sign_transaction_list(private_key) - |> send + |> sign_transaction(private_key) + # |> send end - def send_transaction(params = [from: _from, to: _to, value: _value], private_key) do + # TODO: what if its a wallet + def send_transaction(params, private_key) do params |> set - |> to_list - |> sign_transaction_list(private_key) - |> send + |> sign_transaction(private_key) + # |> send end def send(signature), do: Ethereumex.HttpClient.eth_send_raw_transaction([signature]) diff --git a/lib/eth/utils.ex b/lib/eth/utils.ex index e06860e..d727e14 100644 --- a/lib/eth/utils.ex +++ b/lib/eth/utils.ex @@ -62,7 +62,13 @@ defmodule ETH.Utils do def decode16(value), do: Base.decode16!(value, case: :mixed) def to_buffer(nil), do: "" - def to_buffer(data) when is_number(data), do: pad_to_even(Integer.to_string(data, 16)) + def to_buffer(0), do: "" + def to_buffer(data) when is_number(data) do + data + |> Integer.to_string(16) + |> pad_to_even + |> Base.decode16!(case: :mixed) + end def to_buffer("0x00"), do: "" def to_buffer("0x" <> data) do padded_data = pad_to_even(data) diff --git a/test/eth/transaction_test.exs b/test/eth/transaction_test.exs index ad97664..96fbad8 100644 --- a/test/eth/transaction_test.exs +++ b/test/eth/transaction_test.exs @@ -119,6 +119,18 @@ defmodule ETH.TransactionTest do end) end + + + test "send_transaction_works" do + output = ETH.Transaction.send_transaction(%{ + nonce: 1, to: "0x0dcd857b3c5db88cb7c025f0ef229331cfadffe5", value: 22, gas_limit: 100000, + gas_price: 1000, from: "0x42c343d8b77a9106d7112b71ba6b3030a34ba560" + }, "75c3b11e480f8ba3db792424bebda1fc8dea2b254287e3a9af9ed50c7d255720") |> Base.encode16(case: :lower) + + serialized_hash = "f862018203e8830186a0940dcd857b3c5db88cb7c025f0ef229331cfadffe516801ba09b35467cf48151683b41ed8425d59317716f4f639126d7eb69167ac95c8c3ba3a00d5d21f4c6fc400202dadc09a192b011cc16aefa6155d4e5df15d77d9f6c8f9f" + assert output == serialized_hash + end + # NOTE: probably not needed changes th API # test "can sign an empty transaction with right chain id" do # ETH.Transaction.hash_transaction(%{chain_id: 42 })