lib/xdr/transactions/operation_body.ex

defmodule StellarBase.XDR.OperationBody do
  @moduledoc """
  Automatically generated by xdrgen
  DO NOT EDIT or your changes may be overwritten

  Target implementation: elixir_xdr at https://hex.pm/packages/elixir_xdr

  Representation of Stellar `OperationBody` type.
  """

  @behaviour XDR.Declaration

  alias StellarBase.XDR.{
    OperationType,
    MuxedAccount,
    Void
  }

  alias StellarBase.XDR.Operations.{
    AllowTrust,
    BeginSponsoringFutureReserves,
    ExtendFootprintTTL,
    BumpSequence,
    ChangeTrust,
    Clawback,
    ClawbackClaimableBalance,
    ClaimClaimableBalance,
    CreateClaimableBalance,
    CreateAccount,
    CreatePassiveSellOffer,
    InvokeHostFunction,
    LiquidityPoolDeposit,
    LiquidityPoolWithdraw,
    ManageData,
    ManageBuyOffer,
    ManageSellOffer,
    Payment,
    PathPaymentStrictReceive,
    PathPaymentStrictSend,
    RestoreFootprint,
    RevokeSponsorship,
    SetOptions,
    SetTrustLineFlags
  }

  @arms [
    CREATE_ACCOUNT: CreateAccount,
    PAYMENT: Payment,
    PATH_PAYMENT_STRICT_RECEIVE: PathPaymentStrictReceive,
    MANAGE_SELL_OFFER: ManageSellOffer,
    CREATE_PASSIVE_SELL_OFFER: CreatePassiveSellOffer,
    SET_OPTIONS: SetOptions,
    CHANGE_TRUST: ChangeTrust,
    ALLOW_TRUST: AllowTrust,
    ACCOUNT_MERGE: MuxedAccount,
    INFLATION: Void,
    MANAGE_DATA: ManageData,
    BUMP_SEQUENCE: BumpSequence,
    MANAGE_BUY_OFFER: ManageBuyOffer,
    PATH_PAYMENT_STRICT_SEND: PathPaymentStrictSend,
    CREATE_CLAIMABLE_BALANCE: CreateClaimableBalance,
    CLAIM_CLAIMABLE_BALANCE: ClaimClaimableBalance,
    BEGIN_SPONSORING_FUTURE_RESERVES: BeginSponsoringFutureReserves,
    END_SPONSORING_FUTURE_RESERVES: Void,
    REVOKE_SPONSORSHIP: RevokeSponsorship,
    CLAWBACK: Clawback,
    CLAWBACK_CLAIMABLE_BALANCE: ClawbackClaimableBalance,
    SET_TRUST_LINE_FLAGS: SetTrustLineFlags,
    LIQUIDITY_POOL_DEPOSIT: LiquidityPoolDeposit,
    LIQUIDITY_POOL_WITHDRAW: LiquidityPoolWithdraw,
    INVOKE_HOST_FUNCTION: InvokeHostFunction,
    EXTEND_FOOTPRINT_TTL: ExtendFootprintTTL,
    RESTORE_FOOTPRINT: RestoreFootprint
  ]

  @type value ::
          CreateAccount.t()
          | Payment.t()
          | PathPaymentStrictReceive.t()
          | ManageSellOffer.t()
          | CreatePassiveSellOffer.t()
          | SetOptions.t()
          | ChangeTrust.t()
          | AllowTrust.t()
          | MuxedAccount.t()
          | Void.t()
          | ManageData.t()
          | BumpSequence.t()
          | ManageBuyOffer.t()
          | PathPaymentStrictSend.t()
          | CreateClaimableBalance.t()
          | ClaimClaimableBalance.t()
          | BeginSponsoringFutureReserves.t()
          | RevokeSponsorship.t()
          | Clawback.t()
          | ClawbackClaimableBalance.t()
          | SetTrustLineFlags.t()
          | LiquidityPoolDeposit.t()
          | LiquidityPoolWithdraw.t()
          | InvokeHostFunction.t()
          | ExtendFootprintTTL.t()
          | RestoreFootprint.t()

  @type t :: %__MODULE__{value: value(), type: OperationType.t()}

  defstruct [:value, :type]

  @spec new(value :: value(), type :: OperationType.t()) :: t()
  def new(value, %OperationType{} = type), do: %__MODULE__{value: value, type: type}

  @impl true
  def encode_xdr(%__MODULE__{value: value, type: type}) do
    type
    |> XDR.Union.new(@arms, value)
    |> XDR.Union.encode_xdr()
  end

  @impl true
  def encode_xdr!(%__MODULE__{value: value, type: type}) do
    type
    |> XDR.Union.new(@arms, value)
    |> XDR.Union.encode_xdr!()
  end

  @impl true
  def decode_xdr(bytes, spec \\ union_spec())

  def decode_xdr(bytes, spec) do
    case XDR.Union.decode_xdr(bytes, spec) do
      {:ok, {{type, value}, rest}} -> {:ok, {new(value, type), rest}}
      error -> error
    end
  end

  @impl true
  def decode_xdr!(bytes, spec \\ union_spec())

  def decode_xdr!(bytes, spec) do
    {{type, value}, rest} = XDR.Union.decode_xdr!(bytes, spec)
    {new(value, type), rest}
  end

  @spec union_spec() :: XDR.Union.t()
  defp union_spec do
    nil
    |> OperationType.new()
    |> XDR.Union.new(@arms)
  end
end