-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add contract spec XDR types - Part 1 (#230)
* First sc spec structs and string structs * Added sc spec udt types * Fixed format * Fixed error in test by version * Changed folder structure * Changed code by type variable names * Fixed pipe operator styles * Fixed capture operator Co-authored-by: Elisabet Castaño elisabet.castano@kommit.co
- Loading branch information
1 parent
df3c02c
commit ac147e8
Showing
24 changed files
with
1,408 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
defmodule StellarBase.XDR.SCSpecEntryKind do | ||
@moduledoc """ | ||
Representation of Stellar `SCSpecEntryKind` type. | ||
""" | ||
|
||
@behaviour XDR.Declaration | ||
|
||
@declarations [ | ||
SC_SPEC_ENTRY_FUNCTION_V0: 0, | ||
SC_SPEC_ENTRY_UDT_STRUCT_V0: 1, | ||
SC_SPEC_ENTRY_UDT_UNION_V0: 2, | ||
SC_SPEC_ENTRY_UDT_ENUM_V0: 3, | ||
SC_SPEC_ENTRY_UDT_ERROR_ENUM_V0: 4 | ||
] | ||
|
||
@enum_spec %XDR.Enum{declarations: @declarations, identifier: nil} | ||
|
||
@type t :: %__MODULE__{identifier: atom()} | ||
|
||
defstruct [:identifier] | ||
|
||
@spec new(type :: atom()) :: t() | ||
def new(type), do: %__MODULE__{identifier: type} | ||
|
||
@impl true | ||
def encode_xdr(%__MODULE__{identifier: type}) do | ||
@declarations | ||
|> XDR.Enum.new(type) | ||
|> XDR.Enum.encode_xdr() | ||
end | ||
|
||
@impl true | ||
def encode_xdr!(%__MODULE__{identifier: type}) do | ||
@declarations | ||
|> XDR.Enum.new(type) | ||
|> XDR.Enum.encode_xdr!() | ||
end | ||
|
||
@impl true | ||
def decode_xdr(bytes, spec \\ @enum_spec) | ||
|
||
def decode_xdr(bytes, spec) do | ||
case XDR.Enum.decode_xdr(bytes, spec) do | ||
{:ok, {%XDR.Enum{identifier: type}, rest}} -> {:ok, {new(type), rest}} | ||
error -> error | ||
end | ||
end | ||
|
||
@impl true | ||
def decode_xdr!(bytes, spec \\ @enum_spec) | ||
|
||
def decode_xdr!(bytes, spec) do | ||
{%XDR.Enum{identifier: type}, rest} = XDR.Enum.decode_xdr!(bytes, spec) | ||
{new(type), rest} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
defmodule StellarBase.XDR.SCSpecType do | ||
@moduledoc """ | ||
Representation of Stellar `SCSpecType` type. | ||
""" | ||
|
||
@behaviour XDR.Declaration | ||
|
||
@declarations [ | ||
SC_SPEC_TYPE_VAL: 0, | ||
|
||
# Types with no parameters. | ||
SC_SPEC_TYPE_U32: 1, | ||
SC_SPEC_TYPE_I32: 2, | ||
SC_SPEC_TYPE_U64: 3, | ||
SC_SPEC_TYPE_I64: 4, | ||
SC_SPEC_TYPE_U128: 5, | ||
SC_SPEC_TYPE_I128: 6, | ||
SC_SPEC_TYPE_BOOL: 7, | ||
SC_SPEC_TYPE_SYMBOL: 8, | ||
SC_SPEC_TYPE_BITSET: 9, | ||
SC_SPEC_TYPE_STATUS: 10, | ||
SC_SPEC_TYPE_BYTES: 11, | ||
SC_SPEC_TYPE_INVOKER: 12, | ||
SC_SPEC_TYPE_ADDRESS: 13, | ||
|
||
# Types with parameters. | ||
SC_SPEC_TYPE_OPTION: 1000, | ||
SC_SPEC_TYPE_RESULT: 1001, | ||
SC_SPEC_TYPE_VEC: 1002, | ||
SC_SPEC_TYPE_SET: 1003, | ||
SC_SPEC_TYPE_MAP: 1004, | ||
SC_SPEC_TYPE_TUPLE: 1005, | ||
SC_SPEC_TYPE_BYTES_N: 1006, | ||
|
||
# User defined types. | ||
SC_SPEC_TYPE_UDT: 2000 | ||
] | ||
|
||
@enum_spec %XDR.Enum{declarations: @declarations, identifier: nil} | ||
|
||
@type t :: %__MODULE__{identifier: atom()} | ||
|
||
defstruct [:identifier] | ||
|
||
@spec new(type :: atom()) :: t() | ||
def new(type), do: %__MODULE__{identifier: type} | ||
|
||
@impl true | ||
def encode_xdr(%__MODULE__{identifier: type}) do | ||
@declarations | ||
|> XDR.Enum.new(type) | ||
|> XDR.Enum.encode_xdr() | ||
end | ||
|
||
@impl true | ||
def encode_xdr!(%__MODULE__{identifier: type}) do | ||
@declarations | ||
|> XDR.Enum.new(type) | ||
|> XDR.Enum.encode_xdr!() | ||
end | ||
|
||
@impl true | ||
def decode_xdr(bytes, spec \\ @enum_spec) | ||
|
||
def decode_xdr(bytes, spec) do | ||
case XDR.Enum.decode_xdr(bytes, spec) do | ||
{:ok, {%XDR.Enum{identifier: type}, rest}} -> {:ok, {new(type), rest}} | ||
error -> error | ||
end | ||
end | ||
|
||
@impl true | ||
def decode_xdr!(bytes, spec \\ @enum_spec) | ||
|
||
def decode_xdr!(bytes, spec) do | ||
{%XDR.Enum{identifier: type}, rest} = XDR.Enum.decode_xdr!(bytes, spec) | ||
{new(type), rest} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
defmodule StellarBase.XDR.SCSpecTypeBytesN do | ||
@moduledoc """ | ||
Representation of Stellar `SCSpecTypeBytesN` type. | ||
""" | ||
|
||
alias StellarBase.XDR.UInt32 | ||
|
||
@behaviour XDR.Declaration | ||
|
||
@struct_spec XDR.Struct.new(number: UInt32) | ||
|
||
@type t :: %__MODULE__{number: UInt32.t()} | ||
|
||
defstruct [:number] | ||
|
||
@spec new(number :: UInt32.t()) :: t() | ||
def new(%UInt32{} = number), do: %__MODULE__{number: number} | ||
|
||
@impl true | ||
def encode_xdr(%__MODULE__{number: number}) do | ||
[number: number] | ||
|> XDR.Struct.new() | ||
|> XDR.Struct.encode_xdr() | ||
end | ||
|
||
@impl true | ||
def encode_xdr!(%__MODULE__{number: number}) do | ||
[number: number] | ||
|> XDR.Struct.new() | ||
|> XDR.Struct.encode_xdr!() | ||
end | ||
|
||
@impl true | ||
def decode_xdr(bytes, struct \\ @struct_spec) | ||
|
||
def decode_xdr(bytes, struct) do | ||
case XDR.Struct.decode_xdr(bytes, struct) do | ||
{:ok, {%XDR.Struct{components: [number: number]}, rest}} -> | ||
{:ok, {new(number), rest}} | ||
|
||
error -> | ||
error | ||
end | ||
end | ||
|
||
@impl true | ||
def decode_xdr!(bytes, struct \\ @struct_spec) | ||
|
||
def decode_xdr!(bytes, struct) do | ||
{%XDR.Struct{components: [number: number]}, rest} = XDR.Struct.decode_xdr!(bytes, struct) | ||
|
||
{new(number), rest} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
defmodule StellarBase.XDR.SCSpecTypeUDT do | ||
@moduledoc """ | ||
Representation of Stellar `SCSpecTypeUDT` type. | ||
""" | ||
|
||
alias StellarBase.XDR.String60 | ||
|
||
@behaviour XDR.Declaration | ||
|
||
@struct_spec XDR.Struct.new(name: String60) | ||
|
||
@type t :: %__MODULE__{name: String60.t()} | ||
|
||
defstruct [:name] | ||
|
||
@spec new(name :: String60.t()) :: t() | ||
def new(%String60{} = name), do: %__MODULE__{name: name} | ||
|
||
@impl true | ||
def encode_xdr(%__MODULE__{name: name}) do | ||
[name: name] | ||
|> XDR.Struct.new() | ||
|> XDR.Struct.encode_xdr() | ||
end | ||
|
||
@impl true | ||
def encode_xdr!(%__MODULE__{name: name}) do | ||
[name: name] | ||
|> XDR.Struct.new() | ||
|> XDR.Struct.encode_xdr!() | ||
end | ||
|
||
@impl true | ||
def decode_xdr(bytes, struct \\ @struct_spec) | ||
|
||
def decode_xdr(bytes, struct) do | ||
case XDR.Struct.decode_xdr(bytes, struct) do | ||
{:ok, {%XDR.Struct{components: [name: name]}, rest}} -> | ||
{:ok, {new(name), rest}} | ||
|
||
error -> | ||
error | ||
end | ||
end | ||
|
||
@impl true | ||
def decode_xdr!(bytes, struct \\ @struct_spec) | ||
|
||
def decode_xdr!(bytes, struct) do | ||
{%XDR.Struct{components: [name: name]}, rest} = XDR.Struct.decode_xdr!(bytes, struct) | ||
|
||
{new(name), rest} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
defmodule StellarBase.XDR.SCSpecUDTEnumCaseV0 do | ||
@moduledoc """ | ||
Representation of Stellar `SCSpecUDTEnumCaseV0` type. | ||
""" | ||
|
||
alias StellarBase.XDR.{String1024, String60, UInt32} | ||
|
||
@behaviour XDR.Declaration | ||
|
||
@struct_spec XDR.Struct.new(doc: String1024, name: String60, value: UInt32) | ||
|
||
@type doc :: String1024.t() | ||
@type name :: String60.t() | ||
@type value :: UInt32.t() | ||
|
||
@type t :: %__MODULE__{doc: doc(), name: name(), value: value()} | ||
|
||
defstruct [:doc, :name, :value] | ||
|
||
@spec new(doc :: String1024.t(), name :: String60.t(), value :: UInt32.t()) :: t() | ||
def new(%String1024{} = doc, %String60{} = name, %UInt32{} = value), | ||
do: %__MODULE__{doc: doc, name: name, value: value} | ||
|
||
@impl true | ||
def encode_xdr(%__MODULE__{doc: doc, name: name, value: value}) do | ||
[doc: doc, name: name, value: value] | ||
|> XDR.Struct.new() | ||
|> XDR.Struct.encode_xdr() | ||
end | ||
|
||
@impl true | ||
def encode_xdr!(%__MODULE__{doc: doc, name: name, value: value}) do | ||
[doc: doc, name: name, value: value] | ||
|> XDR.Struct.new() | ||
|> XDR.Struct.encode_xdr!() | ||
end | ||
|
||
@impl true | ||
def decode_xdr(bytes, struct \\ @struct_spec) | ||
|
||
def decode_xdr(bytes, struct) do | ||
case XDR.Struct.decode_xdr(bytes, struct) do | ||
{:ok, {%XDR.Struct{components: [doc: doc, name: name, value: value]}, rest}} -> | ||
{:ok, {new(doc, name, value), rest}} | ||
|
||
error -> | ||
error | ||
end | ||
end | ||
|
||
@impl true | ||
def decode_xdr!(bytes, struct \\ @struct_spec) | ||
|
||
def decode_xdr!(bytes, struct) do | ||
{%XDR.Struct{components: [doc: doc, name: name, value: value]}, rest} = | ||
XDR.Struct.decode_xdr!(bytes, struct) | ||
|
||
{new(doc, name, value), rest} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
defmodule StellarBase.XDR.SCSpecUDTErrorEnumCaseV0 do | ||
@moduledoc """ | ||
Representation of Stellar `SCSpecUDTErrorEnumCaseV0` type. | ||
""" | ||
|
||
alias StellarBase.XDR.{String1024, String60, UInt32} | ||
|
||
@behaviour XDR.Declaration | ||
|
||
@struct_spec XDR.Struct.new(doc: String1024, name: String60, value: UInt32) | ||
|
||
@type doc :: String1024.t() | ||
@type name :: String60.t() | ||
@type value :: UInt32.t() | ||
|
||
@type t :: %__MODULE__{doc: doc(), name: name(), value: value()} | ||
|
||
defstruct [:doc, :name, :value] | ||
|
||
@spec new(doc :: String1024.t(), name :: String60.t(), value :: UInt32.t()) :: t() | ||
def new(%String1024{} = doc, %String60{} = name, %UInt32{} = value), | ||
do: %__MODULE__{doc: doc, name: name, value: value} | ||
|
||
@impl true | ||
def encode_xdr(%__MODULE__{doc: doc, name: name, value: value}) do | ||
[doc: doc, name: name, value: value] | ||
|> XDR.Struct.new() | ||
|> XDR.Struct.encode_xdr() | ||
end | ||
|
||
@impl true | ||
def encode_xdr!(%__MODULE__{doc: doc, name: name, value: value}) do | ||
[doc: doc, name: name, value: value] | ||
|> XDR.Struct.new() | ||
|> XDR.Struct.encode_xdr!() | ||
end | ||
|
||
@impl true | ||
def decode_xdr(bytes, struct \\ @struct_spec) | ||
|
||
def decode_xdr(bytes, struct) do | ||
case XDR.Struct.decode_xdr(bytes, struct) do | ||
{:ok, {%XDR.Struct{components: [doc: doc, name: name, value: value]}, rest}} -> | ||
{:ok, {new(doc, name, value), rest}} | ||
|
||
error -> | ||
error | ||
end | ||
end | ||
|
||
@impl true | ||
def decode_xdr!(bytes, struct \\ @struct_spec) | ||
|
||
def decode_xdr!(bytes, struct) do | ||
{%XDR.Struct{components: [doc: doc, name: name, value: value]}, rest} = | ||
XDR.Struct.decode_xdr!(bytes, struct) | ||
|
||
{new(doc, name, value), rest} | ||
end | ||
end |
Oops, something went wrong.