Skip to content

Commit

Permalink
Add types related to the Stellar.transaction.x file from CAP46 part 2 (
Browse files Browse the repository at this point in the history
…#239)

Co-authored-by: norn <keliumJU@users.noreply.github.com>
  • Loading branch information
FelipeGuzmanSierra and keliumJU committed Mar 28, 2023
1 parent ff88029 commit 4b5a20f
Show file tree
Hide file tree
Showing 23 changed files with 1,988 additions and 2 deletions.
55 changes: 55 additions & 0 deletions lib/xdr/transactions/address_with_nonce.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
defmodule StellarBase.XDR.AddressWithNonce do
@moduledoc """
Representation of Stellar `AddressWithNonce` type.
"""
alias StellarBase.XDR.{SCAddress, UInt64}

@behaviour XDR.Declaration

@struct_spec XDR.Struct.new(address: SCAddress, nonce: UInt64)

@type t :: %__MODULE__{address: SCAddress.t(), nonce: UInt64.t()}

defstruct [:address, :nonce]

@spec new(address :: SCAddress.t(), nonce :: UInt64.t()) :: t()
def new(%SCAddress{} = address, %UInt64{} = nonce),
do: %__MODULE__{address: address, nonce: nonce}

@impl true
def encode_xdr(%__MODULE__{address: address, nonce: nonce}) do
[address: address, nonce: nonce]
|> XDR.Struct.new()
|> XDR.Struct.encode_xdr()
end

@impl true
def encode_xdr!(%__MODULE__{address: address, nonce: nonce}) do
[address: address, nonce: nonce]
|> 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: [address: address, nonce: nonce]}, rest}} ->
{:ok, {new(address, nonce), rest}}

error ->
error
end
end

@impl true
def decode_xdr!(bytes, struct \\ @struct_spec)

def decode_xdr!(bytes, struct) do
{%XDR.Struct{components: [address: address, nonce: nonce]}, rest} =
XDR.Struct.decode_xdr!(bytes, struct)

{new(address, nonce), rest}
end
end
120 changes: 120 additions & 0 deletions lib/xdr/transactions/authorized_invocation.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
defmodule StellarBase.XDR.AuthorizedInvocation do
@moduledoc """
Representation of Stellar `AuthorizedInvocation` type.
"""

alias StellarBase.XDR.{Hash, SCSymbol, SCVec, AuthorizedInvocationList}

@behaviour XDR.Declaration

@struct_spec XDR.Struct.new(
contract_id: Hash,
function_name: SCSymbol,
args: SCVec,
sub_invocations: AuthorizedInvocationList
)

@type contract_id :: Hash.t()
@type function_name :: SCSymbol.t()
@type args :: SCVec.t()
@type sub_invocations :: AuthorizedInvocationList.t()

@type t :: %__MODULE__{
contract_id: contract_id(),
function_name: function_name(),
args: args(),
sub_invocations: sub_invocations()
}

defstruct [:contract_id, :function_name, :args, :sub_invocations]

@spec new(
contract_id :: contract_id(),
function_name :: function_name(),
args :: args(),
sub_invocations :: sub_invocations()
) :: t()
def new(
%Hash{} = contract_id,
%SCSymbol{} = function_name,
%SCVec{} = args,
%AuthorizedInvocationList{} = sub_invocations
),
do: %__MODULE__{
contract_id: contract_id,
function_name: function_name,
args: args,
sub_invocations: sub_invocations
}

@impl true
def encode_xdr(%__MODULE__{
contract_id: contract_id,
function_name: function_name,
args: args,
sub_invocations: sub_invocations
}) do
[
contract_id: contract_id,
function_name: function_name,
args: args,
sub_invocations: sub_invocations
]
|> XDR.Struct.new()
|> XDR.Struct.encode_xdr()
end

@impl true
def encode_xdr!(%__MODULE__{
contract_id: contract_id,
function_name: function_name,
args: args,
sub_invocations: sub_invocations
}) do
[
contract_id: contract_id,
function_name: function_name,
args: args,
sub_invocations: sub_invocations
]
|> 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: [
contract_id: contract_id,
function_name: function_name,
args: args,
sub_invocations: sub_invocations
]
}, rest}} ->
{:ok, {new(contract_id, function_name, args, sub_invocations), rest}}

error ->
error
end
end

@impl true
def decode_xdr!(bytes, struct \\ @struct_spec)

def decode_xdr!(bytes, struct) do
{%XDR.Struct{
components: [
contract_id: contract_id,
function_name: function_name,
args: args,
sub_invocations: sub_invocations
]
}, rest} = XDR.Struct.decode_xdr!(bytes, struct)

{new(contract_id, function_name, args, sub_invocations), rest}
end
end
53 changes: 53 additions & 0 deletions lib/xdr/transactions/authorized_invocation_list.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
defmodule StellarBase.XDR.AuthorizedInvocationList do
@moduledoc """
Representation of a Stellar `AuthorizedInvocationList` list.
"""
alias StellarBase.XDR.AuthorizedInvocation

@behaviour XDR.Declaration

@max_length 4_294_967_295

@array_type AuthorizedInvocation

@array_spec %{type: @array_type, max_length: @max_length}

@type t :: %__MODULE__{sub_invocations: list(AuthorizedInvocation.t())}

defstruct [:sub_invocations]

@spec new(sub_invocations :: list(AuthorizedInvocation.t())) :: t()
def new(sub_invocations \\ []), do: %__MODULE__{sub_invocations: sub_invocations}

@impl true
def encode_xdr(%__MODULE__{sub_invocations: sub_invocations}) do
sub_invocations
|> XDR.VariableArray.new(@array_type, @max_length)
|> XDR.VariableArray.encode_xdr()
end

@impl true
def encode_xdr!(%__MODULE__{sub_invocations: sub_invocations}) do
sub_invocations
|> XDR.VariableArray.new(@array_type, @max_length)
|> XDR.VariableArray.encode_xdr!()
end

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

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

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

def decode_xdr!(bytes, spec) do
{sub_invocations, rest} = XDR.VariableArray.decode_xdr!(bytes, spec)
{new(sub_invocations), rest}
end
end
103 changes: 103 additions & 0 deletions lib/xdr/transactions/contract_auth.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
defmodule StellarBase.XDR.ContractAuth do
@moduledoc """
Representation of Stellar `ContractAuth` type.
"""
alias StellarBase.XDR.{OptionalAddressWithNonce, AuthorizedInvocation, SCVec}

@behaviour XDR.Declaration

@struct_spec XDR.Struct.new(
address_with_nonce: OptionalAddressWithNonce,
authorized_invocation: AuthorizedInvocation,
signature_args: SCVec
)

@type t :: %__MODULE__{
address_with_nonce: OptionalAddressWithNonce.t(),
authorized_invocation: AuthorizedInvocation.t(),
signature_args: SCVec.t()
}

defstruct [:address_with_nonce, :authorized_invocation, :signature_args]

@spec new(
address_with_nonce :: OptionalAddressWithNonce.t(),
authorized_invocation :: AuthorizedInvocation.t(),
signature_args :: SCVec.t()
) :: t()
def new(
%OptionalAddressWithNonce{} = address_with_nonce,
%AuthorizedInvocation{} = authorized_invocation,
%SCVec{} = signature_args
),
do: %__MODULE__{
address_with_nonce: address_with_nonce,
authorized_invocation: authorized_invocation,
signature_args: signature_args
}

@impl true
def encode_xdr(%__MODULE__{
address_with_nonce: address_with_nonce,
authorized_invocation: authorized_invocation,
signature_args: signature_args
}) do
[
address_with_nonce: address_with_nonce,
authorized_invocation: authorized_invocation,
signature_args: signature_args
]
|> XDR.Struct.new()
|> XDR.Struct.encode_xdr()
end

@impl true
def encode_xdr!(%__MODULE__{
address_with_nonce: address_with_nonce,
authorized_invocation: authorized_invocation,
signature_args: signature_args
}) do
[
address_with_nonce: address_with_nonce,
authorized_invocation: authorized_invocation,
signature_args: signature_args
]
|> 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: [
address_with_nonce: address_with_nonce,
authorized_invocation: authorized_invocation,
signature_args: signature_args
]
}, rest}} ->
{:ok, {new(address_with_nonce, authorized_invocation, signature_args), rest}}

error ->
error
end
end

@impl true
def decode_xdr!(bytes, struct \\ @struct_spec)

def decode_xdr!(bytes, struct) do
{%XDR.Struct{
components: [
address_with_nonce: address_with_nonce,
authorized_invocation: authorized_invocation,
signature_args: signature_args
]
}, rest} = XDR.Struct.decode_xdr!(bytes, struct)

{new(address_with_nonce, authorized_invocation, signature_args), rest}
end
end
53 changes: 53 additions & 0 deletions lib/xdr/transactions/contract_auth_list.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
defmodule StellarBase.XDR.ContractAuthList do
@moduledoc """
Representation of a Stellar `ContractAuthList` list.
"""
alias StellarBase.XDR.ContractAuth

@behaviour XDR.Declaration

@max_length 4_294_967_295

@array_type ContractAuth

@array_spec %{type: @array_type, max_length: @max_length}

@type t :: %__MODULE__{auth: list(ContractAuth.t())}

defstruct [:auth]

@spec new(auth :: list(ContractAuth.t())) :: t()
def new(auth), do: %__MODULE__{auth: auth}

@impl true
def encode_xdr(%__MODULE__{auth: auth}) do
auth
|> XDR.VariableArray.new(@array_type, @max_length)
|> XDR.VariableArray.encode_xdr()
end

@impl true
def encode_xdr!(%__MODULE__{auth: auth}) do
auth
|> XDR.VariableArray.new(@array_type, @max_length)
|> XDR.VariableArray.encode_xdr!()
end

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

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

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

def decode_xdr!(bytes, spec) do
{auth, rest} = XDR.VariableArray.decode_xdr!(bytes, spec)
{new(auth), rest}
end
end

0 comments on commit 4b5a20f

Please sign in to comment.