~dcrck/solana

9d79589655fc7e3ae470285eef5716a440aa1292 — dcrck 6 months ago cf2a991
add Request.get_multiple_accounts
2 files changed, 22 insertions(+), 1 deletions(-)

M lib/solana/rpc/middleware.ex
M lib/solana/rpc/request.ex
M lib/solana/rpc/middleware.ex => lib/solana/rpc/middleware.ex +9 -0
@@ 70,6 70,15 @@ defmodule Solana.RPC.Middleware do
    {:ok, Map.put(result, "transaction", tx)}
  end

  defp decode_result({"getAccountInfo", %{} = result}) do
    {:ok, Map.update!(result, "owner", &B58.decode58!/1)}
  end

  # just run the decoding for getAccountInfo for each item in the list
  defp decode_result({"getMultipleAccounts", result}) when is_list(result) do
    {:ok, Enum.map(result, &elem(decode_result({"getAccountInfo", &1}), 1))}
  end

  defp decode_result({_method, result}), do: {:ok, result}

  defp decode_b58_list(list), do: Enum.map(list, &B58.decode58!/1)

M lib/solana/rpc/request.ex => lib/solana/rpc/request.ex +13 -1
@@ 159,7 159,7 @@ defmodule Solana.RPC.Request do
  docs](https://docs.solana.com/developing/clients/jsonrpc-api#getsignaturestatuses).
  """
  @spec get_signature_statuses(signatures :: [Solana.key()], opts :: keyword) :: t
  def get_signature_statuses(signatures, opts \\ []) do
  def get_signature_statuses(signatures, opts \\ []) when is_list(signatures) do
    {"getSignatureStatuses", [Enum.map(signatures, &B58.encode58/1), encode_opts(opts)]}
  end



@@ 196,6 196,18 @@ defmodule Solana.RPC.Request do
    {"getTokenLargestAccounts", [B58.encode58(mint), encode_opts(opts)]}
  end

  @doc """
  Returns the account information for a list of pubkeys.

  For more information, see [the Solana
  docs](https://docs.solana.com/developing/clients/jsonrpc-api#getmultipleaccounts).
  """
  @spec get_multiple_accounts(accounts :: [Solana.key()], opts :: keyword) :: t
  def get_multiple_accounts(accounts, opts \\ []) when is_list(accounts) do
    {"getMultipleAccounts",
     [Enum.map(accounts, &B58.encode58/1), encode_opts(opts, %{"encoding" => "base64"})]}
  end

  defp encode_opts(opts, defaults \\ %{}) do
    Enum.into(opts, defaults, fn {k, v} -> {camelize(k), encode_value(v)} end)
  end