Skip to content

Commit

Permalink
transaction builder tests complete with version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Jan 28, 2018
1 parent 78f4a8e commit 085ddf9
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 90 deletions.
8 changes: 5 additions & 3 deletions lib/eth/transaction/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ 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, "")
# NOTE: this probably causes bug
nonce = Keyword.get(params, :nonce, ETH.get_transaction_count!(params[:from], "pending"))
nonce = Keyword.get(params, :nonce, generate_nonce(Keyword.get(params, :from)))
chain_id = Keyword.get(params, :chain_id, 3)

gas_limit =
Expand All @@ -92,7 +91,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, "pending"))
nonce = Map.get(params, :nonce, generate_nonce(Map.get(params, :from)))
chain_id = Map.get(params, :chain_id, 3)

gas_limit =
Expand All @@ -110,4 +109,7 @@ defmodule ETH.Transaction.Builder do

%{nonce: nonce, gas_price: gas_price, gas_limit: gas_limit, to: to, value: value, data: data}
end

defp generate_nonce(nil), do: 0
defp generate_nonce(address), do: ETH.get_transaction_count!(address, "pending")
end
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.5",
version: "0.4.0",
elixir: "~> 1.6",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down
242 changes: 156 additions & 86 deletions test/eth/transaction/builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,163 @@ defmodule ETH.Transaction.Setter.Test do
use ExUnit.Case
import ETH.Utils

alias ETH.Transaction
setup_all do
ETH.TestClient.start()

# make these raw data
# [nonce, gas_price, gas_limit, to, value, data, v, r, s]
@not_signed_transaction_list [
"",
"0x04a817c800",
"0x5208",
"0x3535353535353535353535353535353535353535",
"",
""
]
# @signed_transaction_list [
# "",
# "0x04a817c800",
# "0x5208",
# "0x3535353535353535353535353535353535353535",
# "",
# "",
# "0x25",
# "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d",
# "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"
# ]
# @not_signed_transaction_map %{
# nonce: 8,
# gas_price: 10,
# gas_limit: 11,
# to: "0x3535353535353535353535353535353535353535",
# value: 20,
# data: ""
# }
# @signed_transaction_map %{
# nonce: "0x08",
# gas_price: "0x04a817c800",
# gas_limit: "0x5208",
# to: "0x3535353535353535353535353535353535353535",
# value: "0x08",
# data: "",
# v: "0x25",
# r: "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d",
# s: "0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"
# }
#
test "Transaction.set(params) works when params is a list" do
assert Transaction.build(@not_signed_transaction_list) == %{
nonce: "",
gas_price: to_buffer("0x04a817c800"),
gas_limit: to_buffer("0x5208"),
to: to_buffer("0x3535353535353535353535353535353535353535"),
value: "",
data: ""
}
on_exit(fn ->
ETH.TestClient.stop()
end)

:ok
end

@sender_wallet %{
eth_address: "0x816307AD84BB5393923BD9D43B71580584F1EBE1",
mnemonic_phrase:
"select afraid fatal cost kite prison early doll casual use glory guide guess field inch wasp poet gorilla awkward season bullet tiny mad opinion",
private_key: "C340914E1857B356114A0523BDF58E33C676ABDC97BCA72C94436111E3C4E16C",
public_key:
"043A0E62BB5F40FEAC2B0395B9F529E0AD0AEF4F28EFB9268F69ECADA951371A1DA40C6C6FDC6824DF91D07A664CE18E428CEA068438D7CD01643AE205E99B6CF2"
}
@receiver_wallet %{
eth_address: "0x619C0FA246D04A4B34DBEF7088EA1089B9E16EBD",
mnemonic_phrase:
"sister dash elder today shoe before frog boil jungle aerobic echo decide volume crunch clutch drive uniform pave hero negative child claw audit detail",
private_key: "C986F51D71AC66285748C8790085179C6F5C69CB221AED7431AD49F27E5403C1",
public_key:
"047A2E098A3B536F041FF2DEA12EFBD47103E2ACC6AA247ECCD20D5C172F575081017DDF19198A7221AADA56B5F486EFA69C5AA81BAD061FDDCB57E8979DD1BE95"
}

test "ETH.build(transaction_list) works when transaction is a list" do
assert ETH.build(
nonce: 12,
gas_price: 10,
gas_limit: 11,
to: "0x3535353535353535353535353535353535353535",
value: 20,
data: "Elixir is awesome"
) == %{
nonce: to_buffer(12),
gas_price: to_buffer(10),
gas_limit: to_buffer(11),
to: to_buffer("0x3535353535353535353535353535353535353535"),
value: to_buffer(20),
data: to_buffer("Elixir is awesome")
}
end

test "ETH.build(transaction_list) works with default values when transaction is a list" do
assert ETH.build(to: "0x3535353535353535353535353535353535353535") == %{
nonce: to_buffer(0),
gas_price: to_buffer(20_000_000_000),
gas_limit: to_buffer(21000),
to: to_buffer("0x3535353535353535353535353535353535353535"),
value: to_buffer(0),
data: ""
}
end

test "ETH.build(transaction_map) works when params is a transaction map" do
assert ETH.build(%{
nonce: 12,
gas_price: 10,
gas_limit: 11,
to: "0x3535353535353535353535353535353535353535",
value: 20,
data: "Elixir is awesome"
}) == %{
nonce: to_buffer(12),
gas_price: to_buffer(10),
gas_limit: to_buffer(11),
to: to_buffer("0x3535353535353535353535353535353535353535"),
value: to_buffer(20),
data: to_buffer("Elixir is awesome")
}
end

test "ETH.build(transaction_map) works with default values when transaction is a map" do
assert ETH.build(%{
to: "0x3535353535353535353535353535353535353535"
}) == %{
nonce: to_buffer(0),
gas_price: to_buffer(20_000_000_000),
gas_limit: to_buffer(21000),
to: to_buffer("0x3535353535353535353535353535353535353535"),
value: to_buffer(0),
data: ""
}
end

# test "Transaction.set(params) works with default values when params is a list" do
#
# end
#
# test "Transaction.set(params) works when params is a map" do
#
# end
#
# test "Transaction.set(params) works with default values when params is a map" do
#
# end
#
# test "Transaction.set(sender_wallet, receiver_wallet, value) works" do
#
# end
#
# test "Transaction.set(sender_wallet, receiver_wallet, value) assigns default values when necessary" do
#
# end
#
# test "Transaction.set(sender_wallet, receiver_wallet, params) works when params is a list" do
#
# end
#
# test "Transaction.set(sender_wallet, receiver_wallet, params) works when params is a list and assigns default values" do
#
# end
#
# test "Transaction.set(sender_wallet, receiver_wallet, params) works when params is a map" do
#
# end
#
# test "Transaction.set(sender_wallet, receiver_wallet, params) works when params is a map and assigns default values" do
#
# end
test "ETH.build(sender_wallet, receiver_wallet, value) works" do
assert ETH.build(@sender_wallet, @receiver_wallet, 570) == %{
nonce: to_buffer(0),
gas_price: to_buffer(20_000_000_000),
gas_limit: to_buffer(21000),
to: to_buffer(@receiver_wallet.eth_address),
value: to_buffer(570),
data: ""
}
end

test "ETH.build(sender_wallet, receiver_wallet, transaction_list) works when transaction params is a list" do
assert ETH.build(
@sender_wallet,
@receiver_wallet,
nonce: 12,
gas_price: 10,
gas_limit: 11,
to: "0x3535353535353535353535353535353535353535",
value: 20,
data: "Elixir is awesome"
) == %{
nonce: to_buffer(12),
gas_price: to_buffer(10),
gas_limit: to_buffer(11),
to: to_buffer(@receiver_wallet.eth_address),
value: to_buffer(20),
data: to_buffer("Elixir is awesome")
}
end

test "ETH.build(sender_wallet, receiver_wallet, transaction_list) works when transaction params is a list and assigns default values" do
assert ETH.build(@sender_wallet, @receiver_wallet, value: 660) == %{
nonce: to_buffer(0),
gas_price: to_buffer(20_000_000_000),
gas_limit: to_buffer(21000),
to: to_buffer(@receiver_wallet.eth_address),
value: to_buffer(660),
data: ""
}
end

test "ETH.build(sender_wallet, receiver_wallet, transaction_params) works when transaction params is a map" do
assert ETH.build(@sender_wallet, @receiver_wallet, %{
nonce: 12,
gas_price: 10,
gas_limit: 11,
to: "0x3535353535353535353535353535353535353535",
value: 20,
data: "Elixir is awesome"
}) == %{
nonce: to_buffer(12),
gas_price: to_buffer(10),
gas_limit: to_buffer(11),
to: to_buffer(@receiver_wallet.eth_address),
value: to_buffer(20),
data: to_buffer("Elixir is awesome")
}
end

test "Transaction.set(sender_wallet, receiver_wallet, trasnsaction_params) works when transaction params is a map and assigns default values" do
assert ETH.build(@sender_wallet, @receiver_wallet, %{
value: 730
}) == %{
nonce: to_buffer(0),
gas_price: to_buffer(20_000_000_000),
gas_limit: to_buffer(21000),
to: to_buffer(@receiver_wallet.eth_address),
value: to_buffer(730),
data: ""
}
end
end
1 change: 1 addition & 0 deletions test/eth/transaction_queries_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ defmodule ETH.TransactionQueries.Test do
assert ETH.get_transaction_from_block(first_block_hash, 0) |> elem(0) == :error

first_tx_hash = ETH.send_transaction!(@first_wallet_in_client, @second_wallet_in_client, 500)

second_tx_hash =
ETH.send_transaction!(@second_wallet_in_client, @first_wallet_in_client, 2000)

Expand Down

0 comments on commit 085ddf9

Please sign in to comment.