Skip to content

Commit

Permalink
Updated contract code struct names and created bool struct
Browse files Browse the repository at this point in the history
  • Loading branch information
CristhianRodriguezMolina committed Apr 20, 2023
1 parent 91fc9b4 commit df2dd70
Show file tree
Hide file tree
Showing 15 changed files with 299 additions and 182 deletions.
63 changes: 0 additions & 63 deletions lib/xdr/contract/sc_contract_code.ex

This file was deleted.

67 changes: 67 additions & 0 deletions lib/xdr/contract/sc_contract_executable.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
defmodule StellarBase.XDR.SCContractExecutable do
@moduledoc """
Representation of Stellar `SCContractExecutable` type.
"""

alias StellarBase.XDR.{Hash, SCContractExecutableType, Void}

@behaviour XDR.Declaration

@arms [
SCCONTRACT_EXECUTABLE_WASM_REF: Hash,
SCCONTRACT_EXECUTABLE_TOKEN: Void
]

@type contract_executable :: Hash.t() | Void.t()

@type t :: %__MODULE__{
contract_executable: contract_executable(),
type: SCContractExecutableType.t()
}

defstruct [:contract_executable, :type]

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

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

@impl true
def encode_xdr!(%__MODULE__{contract_executable: contract_executable, type: type}) do
type
|> XDR.Union.new(@arms, contract_executable)
|> 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, contract_executable}, rest}} -> {:ok, {new(contract_executable, type), rest}}
error -> error
end
end

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

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

@spec union_spec() :: XDR.Union.t()
defp union_spec do
nil
|> SCContractExecutableType.new()
|> XDR.Union.new(@arms)
end
end
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
defmodule StellarBase.XDR.SCContractCodeType do
defmodule StellarBase.XDR.SCContractExecutableType do
@moduledoc """
Representation of Stellar `SCContractCodeType` type.
Representation of Stellar `SCContractExecutableType` type.
"""

@behaviour XDR.Declaration

@declarations [
SCCONTRACT_CODE_WASM_REF: 0,
SCCONTRACT_CODE_TOKEN: 1
SCCONTRACT_EXECUTABLE_WASM_REF: 0,
SCCONTRACT_EXECUTABLE_TOKEN: 1
]

@enum_spec %XDR.Enum{declarations: @declarations, identifier: nil}
Expand Down
6 changes: 3 additions & 3 deletions lib/xdr/contract/sc_object.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule StellarBase.XDR.SCObject do
Int128Parts,
Int64,
SCAddress,
SCContractCode,
SCContractExecutable,
SCMap,
SCObjectType,
SCVec,
Expand All @@ -25,7 +25,7 @@ defmodule StellarBase.XDR.SCObject do
SCO_U128: Int128Parts,
SCO_I128: Int128Parts,
SCO_BYTES: VariableOpaque256000,
SCO_CONTRACT_CODE: SCContractCode,
SCO_CONTRACT_CODE: SCContractExecutable,
SCO_ADDRESS: SCAddress,
SCO_NONCE_KEY: SCAddress
]
Expand All @@ -37,7 +37,7 @@ defmodule StellarBase.XDR.SCObject do
| Int64.t()
| Int128Parts.t()
| VariableOpaque256000.t()
| SCContractCode.t()
| SCContractExecutable.t()
| SCAddress.t()

@type t :: %__MODULE__{sc_object: sc_object(), type: SCObjectType.t()}
Expand Down
10 changes: 5 additions & 5 deletions lib/xdr/transactions/create_contract_args.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ defmodule StellarBase.XDR.CreateContractArgs do
@moduledoc """
Representation of Stellar `CreateContractArgs` type.
"""
alias StellarBase.XDR.{ContractID, SCContractCode}
alias StellarBase.XDR.{ContractID, SCContractExecutable}

@behaviour XDR.Declaration

@struct_spec XDR.Struct.new(contract_id: ContractID, source: SCContractCode)
@struct_spec XDR.Struct.new(contract_id: ContractID, source: SCContractExecutable)

@type t :: %__MODULE__{contract_id: ContractID.t(), source: SCContractCode.t()}
@type t :: %__MODULE__{contract_id: ContractID.t(), source: SCContractExecutable.t()}

defstruct [:contract_id, :source]

@spec new(contract_id :: ContractID.t(), source :: SCContractCode.t()) :: t()
def new(%ContractID{} = contract_id, %SCContractCode{} = source),
@spec new(contract_id :: ContractID.t(), source :: SCContractExecutable.t()) :: t()
def new(%ContractID{} = contract_id, %SCContractExecutable{} = source),
do: %__MODULE__{contract_id: contract_id, source: source}

@impl true
Expand Down
15 changes: 10 additions & 5 deletions lib/xdr/transactions/hash_id_preimage_create_contract_args.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ defmodule StellarBase.XDR.HashIDPreimageCreateContractArgs do
@moduledoc """
Representation of Stellar `HashIDPreimageCreateContractArgs` type.
"""
alias StellarBase.XDR.{Hash, SCContractCode, UInt256}
alias StellarBase.XDR.{Hash, SCContractExecutable, UInt256}

@behaviour XDR.Declaration

@struct_spec XDR.Struct.new(network_id: Hash, source: SCContractCode, salt: UInt256)
@struct_spec XDR.Struct.new(network_id: Hash, source: SCContractExecutable, salt: UInt256)

@type t :: %__MODULE__{network_id: Hash.t(), source: SCContractCode.t(), salt: UInt256.t()}
@type t :: %__MODULE__{
network_id: Hash.t(),
source: SCContractExecutable.t(),
salt: UInt256.t()
}

defstruct [:network_id, :source, :salt]

@spec new(network_id :: Hash.t(), source :: SCContractCode.t(), salt :: UInt256.t()) :: t()
def new(%Hash{} = network_id, %SCContractCode{} = source, %UInt256{} = salt),
@spec new(network_id :: Hash.t(), source :: SCContractExecutable.t(), salt :: UInt256.t()) ::
t()
def new(%Hash{} = network_id, %SCContractExecutable{} = source, %UInt256{} = salt),
do: %__MODULE__{network_id: network_id, source: source, salt: salt}

@impl true
Expand Down
42 changes: 42 additions & 0 deletions lib/xdr/types/bool.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
defmodule StellarBase.XDR.Bool do
@moduledoc """
Representation of Stellar `Bool` type.
"""

@behaviour XDR.Declaration

@type t :: %__MODULE__{value: boolean()}

defstruct [:value]

@spec new(value :: boolean()) :: t()
def new(val), do: %__MODULE__{value: val}

@impl true
def encode_xdr(%__MODULE__{value: value}) do
XDR.Bool.encode_xdr(%XDR.Bool{identifier: value})
end

@impl true
def encode_xdr!(%__MODULE__{value: value}) do
XDR.Bool.encode_xdr!(%XDR.Bool{identifier: value})
end

@impl true
def decode_xdr(bytes, term \\ nil)

def decode_xdr(bytes, _term) do
case XDR.Bool.decode_xdr(bytes) do
{:ok, {%XDR.Bool{identifier: val}, rest}} -> {:ok, {new(val), rest}}
error -> error
end
end

@impl true
def decode_xdr!(bytes, term \\ nil)

def decode_xdr!(bytes, _term) do
{%XDR.Bool{identifier: val}, rest} = XDR.Bool.decode_xdr!(bytes)
{new(val), rest}
end
end
59 changes: 0 additions & 59 deletions test/xdr/contract/sc_contract_code_test.exs

This file was deleted.

Loading

0 comments on commit df2dd70

Please sign in to comment.