defmodule Forge.Rpc do @moduledoc """ RPC lib to send tx. Using localhost:26657 for simplicity. """ require Logger @doc """ Send out the signed transaction with tendermint http RPC interface: localhost:26657/broadcast_tx_commit?tx= """ @spec send(binary()) :: any() def send(tx) do url = "http://#{rpc_host()}/broadcast_tx_commit?tx=\"#{Base.url_encode64(tx, padding: false)}\"" get_url(url) end def get_tx(hash) do url = "http://#{rpc_host()}/tx?hash=0x#{hash}" get_url(url) end def query_tx(key, value) do url = "http://#{rpc_host()}/tx_search?query=\"#{key}='#{value}'\"" get_url(url) end # private function defp get_url(url) do case HTTPoison.get(url) do {:ok, %HTTPoison.Response{body: body}} -> Jason.decode!(body) {:error, error} -> Logger.error("Failed to call tendermint RPC. Error: #{inspect(error)}") nil end end defp rpc_host do Application.get_env(:forge, :rpc_host, "localhost:26657") end end