From ecf93db587bf9015f05815fcea5f7fdc9bc99644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20=C5=BBelazko?= Date: Mon, 3 Sep 2018 16:10:00 +0200 Subject: [PATCH] Amend options encoding Former version would leave leading zeroes which isn't correct Ethereum JSON RPC encoding. --- lib/exw3.ex | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/exw3.ex b/lib/exw3.ex index 9e1fe3f..e869d1b 100644 --- a/lib/exw3.ex +++ b/lib/exw3.ex @@ -266,6 +266,15 @@ defmodule ExW3 do ) end + + @spec encode_option(integer()) :: binary() + @doc "Encodes options into Ethereum JSON RPC hex string" + def encode_option(0), do: "0x0" + + def encode_option(value) do + "0x" <> (value |> :binary.encode_unsigned() |> Base.encode16(case: :lower) |> String.trim_leading("0")) + end + @spec encode_method_call(%{}, binary(), []) :: binary() @doc "Encodes data and appends it to the encoded method id" def encode_method_call(abi, name, input) do @@ -505,12 +514,12 @@ defmodule ExW3 do bin end - gas = ExW3.encode_data("(uint)", [args[:options][:gas]]) |> Base.encode16(case: :lower) + gas = ExW3.encode_option(args[:options][:gas]) tx = %{ from: args[:options][:from], data: "0x#{constructor_arg_data}", - gas: "0x#{gas}" + gas: gas } {:ok, tx_hash} = Ethereumex.HttpClient.eth_send_transaction(tx) @@ -534,14 +543,14 @@ defmodule ExW3 do end def eth_send_helper(address, abi, method_name, args, options) do - gas = ExW3.encode_data("(uint)", [options[:gas]]) |> Base.encode16(case: :lower) + gas = ExW3.encode_option(options[:gas]) Ethereumex.HttpClient.eth_send_transaction( Map.merge( %{ to: address, data: "0x#{ExW3.encode_method_call(abi, method_name, args)}" }, - Map.put(options, :gas, "0x#{gas}") + Map.put(options, :gas, gas) ) ) end