Skip to content

Commit a23fbfa

Browse files
Add new contract XDR types - Part 3 (#254)
* Updated SCVal and SCValTypes XDR types. * Created OptionalSCMap and OptionalSCVec XDR types. * Removed SCObject, SCObjectType, and OptionalSCObject XDR types. * Updated the respective tests. Co-authored-by: Felipe Guzman Sierra <[email protected]>
1 parent 99d7b78 commit a23fbfa

32 files changed

Lines changed: 534 additions & 644 deletions
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
defmodule StellarBase.XDR.OptionalSCObject do
1+
defmodule StellarBase.XDR.OptionalSCMap do
22
@moduledoc """
3-
Representation of Stellar `OptionalSCObject` type.
3+
Representation of Stellar `OptionalSCMap` type.
44
"""
55

6-
alias StellarBase.XDR.SCObject
6+
alias StellarBase.XDR.SCMap
77

88
@behaviour XDR.Declaration
99

10-
@optional_spec XDR.Optional.new(SCObject)
10+
@optional_spec XDR.Optional.new(SCMap)
1111

12-
@type sc_object :: SCObject.t() | nil
12+
@type sc_map :: SCMap.t() | nil
1313

14-
@type t :: %__MODULE__{sc_object: sc_object()}
14+
@type t :: %__MODULE__{sc_map: sc_map()}
1515

16-
defstruct [:sc_object]
16+
defstruct [:sc_map]
1717

18-
@spec new(sc_object :: sc_object()) :: t()
19-
def new(sc_object \\ nil), do: %__MODULE__{sc_object: sc_object}
18+
@spec new(sc_map :: sc_map()) :: t()
19+
def new(sc_map \\ nil), do: %__MODULE__{sc_map: sc_map}
2020

2121
@impl true
22-
def encode_xdr(%__MODULE__{sc_object: sc_object}) do
23-
sc_object
22+
def encode_xdr(%__MODULE__{sc_map: sc_map}) do
23+
sc_map
2424
|> XDR.Optional.new()
2525
|> XDR.Optional.encode_xdr()
2626
end
2727

2828
@impl true
29-
def encode_xdr!(%__MODULE__{sc_object: sc_object}) do
30-
sc_object
29+
def encode_xdr!(%__MODULE__{sc_map: sc_map}) do
30+
sc_map
3131
|> XDR.Optional.new()
3232
|> XDR.Optional.encode_xdr!()
3333
end
@@ -37,8 +37,8 @@ defmodule StellarBase.XDR.OptionalSCObject do
3737

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

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

5454
def decode_xdr!(bytes, optional_spec) do
5555
case XDR.Optional.decode_xdr!(bytes, optional_spec) do
56-
{%XDR.Optional{type: sc_object}, rest} -> {new(sc_object), rest}
56+
{%XDR.Optional{type: sc_map}, rest} -> {new(sc_map), rest}
5757
{nil, rest} -> {new(), rest}
5858
end
5959
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
defmodule StellarBase.XDR.OptionalSCVec do
2+
@moduledoc """
3+
Representation of Stellar `OptionalSCVec` type.
4+
"""
5+
6+
alias StellarBase.XDR.SCVec
7+
8+
@behaviour XDR.Declaration
9+
10+
@optional_spec XDR.Optional.new(SCVec)
11+
12+
@type sc_vec :: SCVec.t() | nil
13+
14+
@type t :: %__MODULE__{sc_vec: sc_vec()}
15+
16+
defstruct [:sc_vec]
17+
18+
@spec new(sc_vec :: sc_vec()) :: t()
19+
def new(sc_vec \\ nil), do: %__MODULE__{sc_vec: sc_vec}
20+
21+
@impl true
22+
def encode_xdr(%__MODULE__{sc_vec: sc_vec}) do
23+
sc_vec
24+
|> XDR.Optional.new()
25+
|> XDR.Optional.encode_xdr()
26+
end
27+
28+
@impl true
29+
def encode_xdr!(%__MODULE__{sc_vec: sc_vec}) do
30+
sc_vec
31+
|> XDR.Optional.new()
32+
|> XDR.Optional.encode_xdr!()
33+
end
34+
35+
@impl true
36+
def decode_xdr(bytes, optional_spec \\ @optional_spec)
37+
38+
def decode_xdr(bytes, optional_spec) do
39+
case XDR.Optional.decode_xdr(bytes, optional_spec) do
40+
{:ok, {%XDR.Optional{type: sc_vec}, rest}} ->
41+
{:ok, {new(sc_vec), rest}}
42+
43+
{:ok, {nil, rest}} ->
44+
{:ok, {new(), rest}}
45+
46+
error ->
47+
error
48+
end
49+
end
50+
51+
@impl true
52+
def decode_xdr!(bytes, optional_spec \\ @optional_spec)
53+
54+
def decode_xdr!(bytes, optional_spec) do
55+
case XDR.Optional.decode_xdr!(bytes, optional_spec) do
56+
{%XDR.Optional{type: sc_vec}, rest} -> {new(sc_vec), rest}
57+
{nil, rest} -> {new(), rest}
58+
end
59+
end
60+
end

lib/xdr/contract/sc_object.ex

Lines changed: 0 additions & 88 deletions
This file was deleted.

lib/xdr/contract/sc_object_type.ex

Lines changed: 0 additions & 61 deletions
This file was deleted.

lib/xdr/contract/sc_val.ex

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,75 @@ defmodule StellarBase.XDR.SCVal do
44
"""
55

66
alias StellarBase.XDR.{
7+
Bool,
8+
Duration,
9+
Int128Parts,
710
Int64,
811
Int32,
9-
OptionalSCObject,
12+
UInt256,
1013
UInt32,
1114
UInt64,
15+
SCAddress,
16+
SCBytes,
17+
SCContractExecutable,
18+
SCNonceKey,
1219
SCValType,
13-
SCStatic,
1420
SCStatus,
15-
SCSymbol
21+
SCString,
22+
SCSymbol,
23+
TimePoint,
24+
OptionalSCVec,
25+
OptionalSCMap,
26+
Void
1627
}
1728

1829
@behaviour XDR.Declaration
1930

2031
@arms [
21-
SCV_U63: Int64,
32+
SCV_BOOL: Bool,
33+
SCV_VOID: Void,
34+
SCV_STATUS: SCStatus,
2235
SCV_U32: UInt32,
2336
SCV_I32: Int32,
24-
SCV_STATIC: SCStatic,
25-
SCV_OBJECT: OptionalSCObject,
37+
SCV_U64: UInt64,
38+
SCV_I64: Int64,
39+
SCV_TIMEPOINT: TimePoint,
40+
SCV_DURATION: Duration,
41+
SCV_U128: Int128Parts,
42+
SCV_I128: Int128Parts,
43+
SCV_U256: UInt256,
44+
SCV_I256: UInt256,
45+
SCV_BYTES: SCBytes,
46+
SCV_STRING: SCString,
2647
SCV_SYMBOL: SCSymbol,
27-
SCV_BITSET: UInt64,
28-
SCV_STATUS: SCStatus
48+
SCV_VEC: OptionalSCVec,
49+
SCV_MAP: OptionalSCMap,
50+
SCV_CONTRACT_EXECUTABLE: SCContractExecutable,
51+
SCV_ADDRESS: SCAddress,
52+
SCV_LEDGER_KEY_CONTRACT_EXECUTABLE: Void,
53+
SCV_LEDGER_KEY_NONCE: SCNonceKey
2954
]
3055

3156
@type value ::
32-
Int64.t()
57+
Bool.t()
58+
| Void.t()
59+
| SCStatus.t()
3360
| UInt32.t()
3461
| Int32.t()
35-
| SCStatic.t()
36-
| OptionalSCObject.t()
37-
| SCSymbol.t()
3862
| UInt64.t()
39-
| SCStatus.t()
63+
| Int64.t()
64+
| TimePoint.t()
65+
| Duration.t()
66+
| Int128Parts.t()
67+
| UInt256.t()
68+
| SCBytes.t()
69+
| SCString.t()
70+
| SCSymbol.t()
71+
| OptionalSCVec.t()
72+
| OptionalSCMap.t()
73+
| SCContractExecutable.t()
74+
| SCAddress.t()
75+
| SCNonceKey.t()
4076

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

0 commit comments

Comments
 (0)