Current section

Files

Jump to
stellar_sdk lib horizon transactions.ex
Raw

lib/horizon/transactions.ex

defmodule Stellar.Horizon.Transactions do
@moduledoc """
Exposes functions to interact with Transactions in Horizon.
You can:
- Create a transaction.
Horizon API reference: https://developers.stellar.org/api/resources/transactions/
"""
alias Stellar.Horizon.{Error, Transaction}
alias Stellar.Horizon.Client, as: Horizon
@type hash :: String.t()
@type response :: {:ok, Transaction.t()} | {:error, Error.t()}
@endpoint "/transactions/"
@spec create(base64_envelope :: String.t()) :: response()
def create(base64_envelope) do
headers = [{"Content-Type", "application/x-www-form-urlencoded"}]
body = URI.encode_query(tx: base64_envelope)
case Horizon.request(:post, @endpoint, headers, body) do
{:ok, tx} -> {:ok, Transaction.new(tx)}
error -> error
end
end
@spec retrieve(hash :: hash()) :: response()
def retrieve(hash) do
case Horizon.request(:get, @endpoint <> hash) do
{:ok, tx} -> {:ok, Transaction.new(tx)}
error -> error
end
end
@spec all() :: response()
def all() do
body = URI.encode_query(limit: 2)
case Horizon.request(:get, @endpoint, [], {:limit, 2}) do
{:ok, tx} -> {:ok, tx}
error -> error
end
end
end