Packages
stellar_sdk
0.6.0
0.23.0
0.22.0
0.21.2
0.21.1
0.21.0
0.20.0
0.19.0
0.18.1
0.18.0
0.17.1
0.17.0
0.16.1
0.16.0
0.15.1
0.15.0
0.14.0
0.13.1
0.13.0
0.12.0
0.11.8
0.11.7
0.11.6
0.11.5
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.2
0.10.1
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.1
0.8.0
0.7.1
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.0
Elixir SDK for the Stellar network.
Current section
Files
Jump to
Current section
Files
lib/tx_build/predicate.ex
defmodule Stellar.TxBuild.Claimants do
end
# ClaimPredicate
# ----
# Stellar.TxBuild.Claimant.new(destination: "ABC", predicate: :unconditional)
# Stellar.TxBuild.Claimant.new(destination: "ABC", predicate: conditional: [time: {:relative, 123}])
#
defmodule Stellar.TxBuild.UnconditionalPredicate do
@moduledoc """
"""
@behaviour Stellar.TxBuild.XDR
end
defmodule Stellar.TxBuild.ConditionalPredicate do
@moduledoc """
"""
@behaviour Stellar.TxBuild.XDR
end
defmodule Stellar.TxBuild.Predicate do
@moduledoc """
`Predicate` struct definition.
"""
@type predicate :: :conditional | :unconditional
@type type :: :time | :and | :or | :not
@type t :: %__MODULE__{predicate: predicate(), type: type(), value: any()}
defstruct [:predicate, :type, :value]
def new(:unconditional), do: %__MODULE__{type: :unconditional}
def new(conditional: predicate) do
with {:ok, {type, value}} <- validate_predicate(predicate) do
%__MODULE__{type: :time, value: value}
end
end
def new(_predicate), do: {:error, :invalid_predicate}
defp validate_predicate(time: {type, value}) when type in ~w(relative absolute)a and is_integer(value) do
{:ok, {:time, {type, value}}}
end
defp validate_predicate(and: {cond1, cond2}) do
with {:ok, predicate1} <- new(cond1),
{:ok, predicate2} <- new(cond2) do
{:ok, {:and, {predicate1, predicate2}}}
end
end
defp validate_predicate(or: {cond1, cond2}) do
with {:ok, predicate1} <- new(cond1),
{:ok, predicate2} <- new(cond2) do
{:ok, {:or, {predicate1, predicate2}}}
end
end
defp validate_predicate(not: conditional) do
{:ok, {:and, new(conditional)}}
end
end
defmodule Stellar.TxBuild.Claimant do
@moduledoc """
"""
@behaviour Stellar.TxBuild.XDR
alias Stellar.TxBuild.{AccountID, Predicate}
@type account_id :: String.t()
@type validation :: {:ok, any()} | {:error, atom()}
@type t :: %__MODULE__{destination: account_id(), predicate: Predicate.t()}
defstruct [:destination, :predicate]
def new(args, opts \\ [])
def new(args, _opts) do
destination = Keyword.get(args, :destination)
predicate = Keyword.get(args, :predicate)
with {:ok, destination} <- validate_destionation(destination),
{:ok, predicate} <- validate_predicate(predicate) do
%__MODULE__{destination: destination, predicate: predicate}
end
end
@spec validate_destionation(account_id :: account_id()) :: validation()
defp validate_destionation(account_id) do
case AccountID.new(account_id) do
%AccountID{} = account_id -> {:ok, account_id}
_error -> {:error, :invalid_destination}
end
end
@spec validate_predicate(predicate :: any()) :: validation()
defp validate_predicate(predicate) do
case Predicate.new(predicate) do
%Predicate{} = predicate -> {:ok, predicate}
_error -> {:error, :invalid_predicate}
end
end
# defp validate_predicate({:unconditional, _predicate}), do: validate_predicate(:unconditional)
# defp validate_predicate(:unconditional), do: :unconditional
# defp validate_predicate(_predicate), do: {:error, :invalid_predicate}
end