4
0
Fork 0

Amend options encoding

Former version would leave leading zeroes which isn't correct Ethereum JSON RPC encoding.
pull/16/head
Piotr Żelazko 6 years ago
parent cc6b197fb1
commit ecf93db587
No known key found for this signature in database
GPG Key ID: 27E11A19853A7913
  1. 17
      lib/exw3.ex

@ -266,6 +266,15 @@ defmodule ExW3 do
) )
end 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() @spec encode_method_call(%{}, binary(), []) :: binary()
@doc "Encodes data and appends it to the encoded method id" @doc "Encodes data and appends it to the encoded method id"
def encode_method_call(abi, name, input) do def encode_method_call(abi, name, input) do
@ -505,12 +514,12 @@ defmodule ExW3 do
bin bin
end end
gas = ExW3.encode_data("(uint)", [args[:options][:gas]]) |> Base.encode16(case: :lower) gas = ExW3.encode_option(args[:options][:gas])
tx = %{ tx = %{
from: args[:options][:from], from: args[:options][:from],
data: "0x#{constructor_arg_data}", data: "0x#{constructor_arg_data}",
gas: "0x#{gas}" gas: gas
} }
{:ok, tx_hash} = Ethereumex.HttpClient.eth_send_transaction(tx) {:ok, tx_hash} = Ethereumex.HttpClient.eth_send_transaction(tx)
@ -534,14 +543,14 @@ defmodule ExW3 do
end end
def eth_send_helper(address, abi, method_name, args, options) do 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( Ethereumex.HttpClient.eth_send_transaction(
Map.merge( Map.merge(
%{ %{
to: address, to: address,
data: "0x#{ExW3.encode_method_call(abi, method_name, args)}" data: "0x#{ExW3.encode_method_call(abi, method_name, args)}"
}, },
Map.put(options, :gas, "0x#{gas}") Map.put(options, :gas, gas)
) )
) )
end end

Loading…
Cancel
Save