Skip to content

Commit

Permalink
review ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
lucca65 committed Mar 7, 2024
1 parent 37d5068 commit c7115c0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 68 deletions.
98 changes: 47 additions & 51 deletions lib/ledger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,25 @@ defmodule XRPL.Ledger do

use XRPL

@ledger_type ~w(account amendments amm check deposit_preauth directory escrow fee hashes nft_offer offer payment_channel signer_list state ticket)

@type currency_without_amount ::
%{currency: String.t()} | %{currency: String.t(), issuer: String.t()}

@doc """
Retrieve information about the public ledger.
Official documentation: https://xrpl.org/ledger.html
"""
@spec ledger(Keyword.t()) :: Tesla.Env.result()
def ledger(opts \\ []) do
opts =
Keyword.validate!(opts, [
:binary,
:queue,
:ledger_hash,
:ledger_index,
full: false,
accounts: false,
transactions: false,
expand: false,
owner_funds: false,
type: "account"
])

# Validate ledger type
if Enum.member?(@ledger_type, Keyword.get(opts, :type)) do
xrpl("ledger", Map.new(opts))
else
{:error, "Invalid ledger type"}
end
def ledger(params) do
xrpl("ledger", params)
end

def ledger!(opts \\ []), do: unwrap_or_raise(ledger(opts))
def ledger!(params), do: params |> ledger() |> unwrap_or_raise()

defparams "ledger" do
optional(:ledger_hash, :string, format: XRPL.is_hash())
optional(:ledger_index, :string, format: XRPL.ledger_index_regex())
optional(:transactions, :boolean, default: false)
optional(:expand, :boolean, default: false)
optional(:owner_funds, :boolean, default: false)
optional(:binary, :boolean)
optional(:queue, :boolean)
end

@doc """
Expand All @@ -54,54 +36,68 @@ defmodule XRPL.Ledger do
Official documentation: https://xrpl.org/ledger_closed.html
"""
@spec ledger_closed() :: Tesla.Env.result()
def ledger_closed do
xrpl("ledger_closed", %{})
end

def ledger_closed!, do: unwrap_or_raise(ledger_closed())

defparams "ledger_closed" do
end

@doc """
The ledger_current method returns the unique identifiers of the current in-progress ledger.
This command is mostly useful for testing, because the ledger returned is still in flux.
Official documentation: https://xrpl.org/ledger_current.html
"""
@spec ledger_current() :: Tesla.Env.result()
def ledger_current do
xrpl("ledger_current", %{})
end

def ledger_current!, do: unwrap_or_raise(ledger_current())

defparams "ledger_current" do
end

@doc """
The ledger_data method retrieves contents of the specified ledger.
You can iterate through several calls to retrieve the entire contents of a single ledger version.
Official documentation: https://xrpl.org/ledger_data.html
"""
@spec ledger_data(Keyword.t()) :: Tesla.Env.result()
def ledger_data(opts \\ []) do
limit = if Keyword.get(opts, :binary), do: 2048, else: 256

opts =
Keyword.validate!(opts, [
:ledger_index,
:ledger_hash,
:marker,
:type,
binary: false,
limit: limit
])

# Check if sent type is valid
if Keyword.has_key?(opts, :type) and not Enum.member?(@ledger_type, Keyword.get(opts, :type)) do
{:error, "Invalid ledger type"}
else
xrpl("ledger_data", Map.new(opts))
end
def ledger_data(params) do
xrpl("ledger_data", params)
end

def ledger_data!(opts \\ []), do: unwrap_or_raise(ledger_data(opts))
def ledger_data!(params), do: params |> ledger_data() |> unwrap_or_raise()

defparams "ledger_data" do
optional(:ledger_hash, :string, format: XRPL.is_hash())
optional(:ledger_index, :string, format: XRPL.ledger_index_regex())
optional(:binary, :boolean, default: false)
optional(:limit, :integer, max: 256, default: 256)
optional(:marker, :string)

optional(:type, :enum,
values: [
"account",
"amendments",
"amm",
"check",
"deposit_preauth",
"directory",
"escrow",
"fee",
"hashes",
"nft_offer",
"offer",
"payment_channel",
"signer_list",
"state",
"ticket"
]
)
end
end
26 changes: 9 additions & 17 deletions test/ledger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,40 @@ defmodule XRPL.LedgerTest do

describe "ledger/2" do
test "returns ledger info for a given index" do
assert({:ok, %Tesla.Env{} = env} = Ledger.ledger(ledger_index: "validated"))

assert env.status == 200
assert({:ok, %Tesla.Env{status: 200}} = Ledger.ledger(%{ledger_index: "validated"}))
end

test "returns error for invalid index" do
assert {:error, %Tesla.Env{} = env} = Ledger.ledger(ledger_index: "invalid_index")
assert(env.status == 200)
assert {:error, %Tesla.Env{} = env} = Ledger.ledger(%{ledger_index: "invalid_index"})
assert env.body["result"]["error"] == "invalidParams"
end
end

describe "ledger_closed/0" do
test "returns ledger info for the latest closed index" do
assert {:ok, %Tesla.Env{} = env} = Ledger.ledger_closed()
assert env.body["result"]["status"] == "success"
assert {:ok, %Tesla.Env{status: 200}} = Ledger.ledger_closed()
end
end

describe "ledger_current/0" do
test "returns current in-progress index" do
assert {:ok, %Tesla.Env{} = env} = Ledger.ledger_current()
assert env.body["result"]["status"] == "success"
assert {:ok, %Tesla.Env{status: 200}} = Ledger.ledger_current()
end
end

describe "ledger_data/1" do
test "returns contents for a given ledger index" do
assert {:ok, %Tesla.Env{} = env} = Ledger.ledger_data(ledger_index: "validated")
assert env.body["result"]["status"] == "success"
assert {:ok, %Tesla.Env{status: 200}} = Ledger.ledger_data(%{ledger_index: "validated"})
end

test "uses type to filter results" do
assert {:ok, %Tesla.Env{} = env} =
Ledger.ledger_data(ledger_index: "validated", type: "account")

assert env.body["result"]["status"] == "success"
assert {:ok, %Tesla.Env{status: 200}} =
Ledger.ledger_data(%{ledger_index: "validated", type: "account"})
end

test "invalid type return error" do
assert {:error, "Invalid ledger type"} =
Ledger.ledger_data(ledger_index: "validated", type: "invalid_type")
assert {:error, %{errors: [type: {"is invalid", _}]}} =
Ledger.ledger_data(%{ledger_index: "validated", type: "invalid_type"})
end
end
end

0 comments on commit c7115c0

Please sign in to comment.