Add parser function to handle addresses arguments when calling the blockchain

pull/279/head
Amanda Sposito 6 years ago
parent e545841407
commit b8a8f083d2
  1. 13
      apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/encoder.ex
  2. 14
      apps/ethereum_jsonrpc/test/ethereum_jsonrpc/encoder_test.exs

@ -45,12 +45,23 @@ defmodule EthereumJSONRPC.Encoder do
def encode_function_call({function_selector, args}) do
encoded_args =
function_selector
|> ABI.encode(args)
|> ABI.encode(parse_args(args))
|> Base.encode16(case: :lower)
{function_selector.function, "0x" <> encoded_args}
end
defp parse_args(args) do
args
|> Enum.map(fn
<<"0x", hexadecimal_digits::binary>> ->
Base.decode16!(hexadecimal_digits, case: :mixed)
item ->
item
end)
end
@doc """
Given a result set from the blockchain, and the functions selectors, returns the results decoded.

@ -26,6 +26,20 @@ defmodule EthereumJSONRPC.EncoderTest do
assert Encoder.encode_function_call({function_selector, [10]}) ==
{"get", "0x9507d39a000000000000000000000000000000000000000000000000000000000000000a"}
end
test "generates the correct encoding with addresses arguments" do
function_selector = %ABI.FunctionSelector{
function: "tokens",
returns: {:uint, 256},
types: [:address, :address]
}
args = ["0xdab1c67232f92b7707f49c08047b96a4db7a9fc6", "0x6937cb25eb54bc013b9c13c47ab38eb63edd1493"]
assert Encoder.encode_function_call({function_selector, args}) ==
{"tokens",
"0x508493bc000000000000000000000000dab1c67232f92b7707f49c08047b96a4db7a9fc60000000000000000000000006937cb25eb54bc013b9c13c47ab38eb63edd1493"}
end
end
describe "get_selectors/2" do

Loading…
Cancel
Save