Skip to content

Commit

Permalink
Updated SCVal and SCValType structs, and created optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
CristhianRodriguezMolina committed Apr 20, 2023
1 parent 99d7b78 commit 11efceb
Show file tree
Hide file tree
Showing 32 changed files with 534 additions and 644 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
defmodule StellarBase.XDR.OptionalSCObject do
defmodule StellarBase.XDR.OptionalSCMap do
@moduledoc """
Representation of Stellar `OptionalSCObject` type.
Representation of Stellar `OptionalSCMap` type.
"""

alias StellarBase.XDR.SCObject
alias StellarBase.XDR.SCMap

@behaviour XDR.Declaration

@optional_spec XDR.Optional.new(SCObject)
@optional_spec XDR.Optional.new(SCMap)

@type sc_object :: SCObject.t() | nil
@type sc_map :: SCMap.t() | nil

@type t :: %__MODULE__{sc_object: sc_object()}
@type t :: %__MODULE__{sc_map: sc_map()}

defstruct [:sc_object]
defstruct [:sc_map]

@spec new(sc_object :: sc_object()) :: t()
def new(sc_object \\ nil), do: %__MODULE__{sc_object: sc_object}
@spec new(sc_map :: sc_map()) :: t()
def new(sc_map \\ nil), do: %__MODULE__{sc_map: sc_map}

@impl true
def encode_xdr(%__MODULE__{sc_object: sc_object}) do
sc_object
def encode_xdr(%__MODULE__{sc_map: sc_map}) do
sc_map
|> XDR.Optional.new()
|> XDR.Optional.encode_xdr()
end

@impl true
def encode_xdr!(%__MODULE__{sc_object: sc_object}) do
sc_object
def encode_xdr!(%__MODULE__{sc_map: sc_map}) do
sc_map
|> XDR.Optional.new()
|> XDR.Optional.encode_xdr!()
end
Expand All @@ -37,8 +37,8 @@ defmodule StellarBase.XDR.OptionalSCObject do

def decode_xdr(bytes, optional_spec) do
case XDR.Optional.decode_xdr(bytes, optional_spec) do
{:ok, {%XDR.Optional{type: sc_object}, rest}} ->
{:ok, {new(sc_object), rest}}
{:ok, {%XDR.Optional{type: sc_map}, rest}} ->
{:ok, {new(sc_map), rest}}

{:ok, {nil, rest}} ->
{:ok, {new(), rest}}
Expand All @@ -53,7 +53,7 @@ defmodule StellarBase.XDR.OptionalSCObject do

def decode_xdr!(bytes, optional_spec) do
case XDR.Optional.decode_xdr!(bytes, optional_spec) do
{%XDR.Optional{type: sc_object}, rest} -> {new(sc_object), rest}
{%XDR.Optional{type: sc_map}, rest} -> {new(sc_map), rest}
{nil, rest} -> {new(), rest}
end
end
Expand Down
60 changes: 60 additions & 0 deletions lib/xdr/contract/optional_sc_vec.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
defmodule StellarBase.XDR.OptionalSCVec do
@moduledoc """
Representation of Stellar `OptionalSCVec` type.
"""

alias StellarBase.XDR.SCVec

@behaviour XDR.Declaration

@optional_spec XDR.Optional.new(SCVec)

@type sc_vec :: SCVec.t() | nil

@type t :: %__MODULE__{sc_vec: sc_vec()}

defstruct [:sc_vec]

@spec new(sc_vec :: sc_vec()) :: t()
def new(sc_vec \\ nil), do: %__MODULE__{sc_vec: sc_vec}

@impl true
def encode_xdr(%__MODULE__{sc_vec: sc_vec}) do
sc_vec
|> XDR.Optional.new()
|> XDR.Optional.encode_xdr()
end

@impl true
def encode_xdr!(%__MODULE__{sc_vec: sc_vec}) do
sc_vec
|> XDR.Optional.new()
|> XDR.Optional.encode_xdr!()
end

@impl true
def decode_xdr(bytes, optional_spec \\ @optional_spec)

def decode_xdr(bytes, optional_spec) do
case XDR.Optional.decode_xdr(bytes, optional_spec) do
{:ok, {%XDR.Optional{type: sc_vec}, rest}} ->
{:ok, {new(sc_vec), rest}}

{:ok, {nil, rest}} ->
{:ok, {new(), rest}}

error ->
error
end
end

@impl true
def decode_xdr!(bytes, optional_spec \\ @optional_spec)

def decode_xdr!(bytes, optional_spec) do
case XDR.Optional.decode_xdr!(bytes, optional_spec) do
{%XDR.Optional{type: sc_vec}, rest} -> {new(sc_vec), rest}
{nil, rest} -> {new(), rest}
end
end
end
88 changes: 0 additions & 88 deletions lib/xdr/contract/sc_object.ex

This file was deleted.

61 changes: 0 additions & 61 deletions lib/xdr/contract/sc_object_type.ex

This file was deleted.

62 changes: 49 additions & 13 deletions lib/xdr/contract/sc_val.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,75 @@ defmodule StellarBase.XDR.SCVal do
"""

alias StellarBase.XDR.{
Bool,
Duration,
Int128Parts,
Int64,
Int32,
OptionalSCObject,
UInt256,
UInt32,
UInt64,
SCAddress,
SCBytes,
SCContractExecutable,
SCNonceKey,
SCValType,
SCStatic,
SCStatus,
SCSymbol
SCString,
SCSymbol,
TimePoint,
OptionalSCVec,
OptionalSCMap,
Void
}

@behaviour XDR.Declaration

@arms [
SCV_U63: Int64,
SCV_BOOL: Bool,
SCV_VOID: Void,
SCV_STATUS: SCStatus,
SCV_U32: UInt32,
SCV_I32: Int32,
SCV_STATIC: SCStatic,
SCV_OBJECT: OptionalSCObject,
SCV_U64: UInt64,
SCV_I64: Int64,
SCV_TIMEPOINT: TimePoint,
SCV_DURATION: Duration,
SCV_U128: Int128Parts,
SCV_I128: Int128Parts,
SCV_U256: UInt256,
SCV_I256: UInt256,
SCV_BYTES: SCBytes,
SCV_STRING: SCString,
SCV_SYMBOL: SCSymbol,
SCV_BITSET: UInt64,
SCV_STATUS: SCStatus
SCV_VEC: OptionalSCVec,
SCV_MAP: OptionalSCMap,
SCV_CONTRACT_EXECUTABLE: SCContractExecutable,
SCV_ADDRESS: SCAddress,
SCV_LEDGER_KEY_CONTRACT_EXECUTABLE: Void,
SCV_LEDGER_KEY_NONCE: SCNonceKey
]

@type value ::
Int64.t()
Bool.t()
| Void.t()
| SCStatus.t()
| UInt32.t()
| Int32.t()
| SCStatic.t()
| OptionalSCObject.t()
| SCSymbol.t()
| UInt64.t()
| SCStatus.t()
| Int64.t()
| TimePoint.t()
| Duration.t()
| Int128Parts.t()
| UInt256.t()
| SCBytes.t()
| SCString.t()
| SCSymbol.t()
| OptionalSCVec.t()
| OptionalSCMap.t()
| SCContractExecutable.t()
| SCAddress.t()
| SCNonceKey.t()

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

Expand Down
Loading

0 comments on commit 11efceb

Please sign in to comment.