Skip to content

Commit 64e0f9a

Browse files
authored
Add XDR types derived from InvokeHostFunctionOp - part 3 (#240)
* Add update according to issue #277 * Update names and reduce the number of lines in modules * Reduce double alias
1 parent 4b5a20f commit 64e0f9a

15 files changed

+936
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
defmodule StellarBase.XDR.Ed25519ContractID do
2+
@moduledoc """
3+
Representation of Stellar `Ed25519ContractID` type.
4+
"""
5+
alias StellarBase.XDR.{Hash, UInt256}
6+
7+
@behaviour XDR.Declaration
8+
9+
@struct_spec XDR.Struct.new(network_id: Hash, ed25519: UInt256, salt: UInt256)
10+
11+
@type t :: %__MODULE__{network_id: Hash.t(), ed25519: UInt256.t(), salt: UInt256.t()}
12+
13+
defstruct [:network_id, :ed25519, :salt]
14+
15+
@spec new(network_id :: Hash.t(), ed25519 :: UInt256.t(), salt :: UInt256.t()) :: t()
16+
def new(%Hash{} = network_id, %UInt256{} = ed25519, %UInt256{} = salt),
17+
do: %__MODULE__{network_id: network_id, ed25519: ed25519, salt: salt}
18+
19+
@impl true
20+
def encode_xdr(%__MODULE__{network_id: network_id, ed25519: ed25519, salt: salt}) do
21+
[network_id: network_id, ed25519: ed25519, salt: salt]
22+
|> XDR.Struct.new()
23+
|> XDR.Struct.encode_xdr()
24+
end
25+
26+
@impl true
27+
def encode_xdr!(%__MODULE__{network_id: network_id, ed25519: ed25519, salt: salt}) do
28+
[network_id: network_id, ed25519: ed25519, salt: salt]
29+
|> XDR.Struct.new()
30+
|> XDR.Struct.encode_xdr!()
31+
end
32+
33+
@impl true
34+
def decode_xdr(bytes, struct \\ @struct_spec)
35+
36+
def decode_xdr(bytes, struct) do
37+
case XDR.Struct.decode_xdr(bytes, struct) do
38+
{:ok,
39+
{%XDR.Struct{
40+
components: [
41+
network_id: network_id,
42+
ed25519: ed25519,
43+
salt: salt
44+
]
45+
}, rest}} ->
46+
{:ok, {new(network_id, ed25519, salt), rest}}
47+
48+
error ->
49+
error
50+
end
51+
end
52+
53+
@impl true
54+
def decode_xdr!(bytes, struct \\ @struct_spec)
55+
56+
def decode_xdr!(bytes, struct) do
57+
{%XDR.Struct{
58+
components: [
59+
network_id: network_id,
60+
ed25519: ed25519,
61+
salt: salt
62+
]
63+
}, rest} = XDR.Struct.decode_xdr!(bytes, struct)
64+
65+
{new(network_id, ed25519, salt), rest}
66+
end
67+
end

lib/xdr/transactions/from_asset.ex

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
defmodule StellarBase.XDR.FromAsset do
2+
@moduledoc """
3+
Representation of Stellar `FromAsset` type.
4+
"""
5+
alias StellarBase.XDR.{Hash, Asset}
6+
7+
@behaviour XDR.Declaration
8+
9+
@struct_spec XDR.Struct.new(network_id: Hash, asset: Asset)
10+
11+
@type t :: %__MODULE__{network_id: Hash.t(), asset: Asset.t()}
12+
13+
defstruct [:network_id, :asset]
14+
15+
@spec new(network_id :: Hash.t(), asset :: Asset.t()) :: t()
16+
def new(%Hash{} = network_id, %Asset{} = asset),
17+
do: %__MODULE__{network_id: network_id, asset: asset}
18+
19+
@impl true
20+
def encode_xdr(%__MODULE__{network_id: network_id, asset: asset}) do
21+
[network_id: network_id, asset: asset]
22+
|> XDR.Struct.new()
23+
|> XDR.Struct.encode_xdr()
24+
end
25+
26+
@impl true
27+
def encode_xdr!(%__MODULE__{network_id: network_id, asset: asset}) do
28+
[network_id: network_id, asset: asset]
29+
|> XDR.Struct.new()
30+
|> XDR.Struct.encode_xdr!()
31+
end
32+
33+
@impl true
34+
def decode_xdr(bytes, struct \\ @struct_spec)
35+
36+
def decode_xdr(bytes, struct) do
37+
case XDR.Struct.decode_xdr(bytes, struct) do
38+
{:ok,
39+
{%XDR.Struct{
40+
components: [network_id: network_id, asset: asset]
41+
}, rest}} ->
42+
{:ok, {new(network_id, asset), rest}}
43+
44+
error ->
45+
error
46+
end
47+
end
48+
49+
@impl true
50+
def decode_xdr!(bytes, struct \\ @struct_spec)
51+
52+
def decode_xdr!(bytes, struct) do
53+
{%XDR.Struct{
54+
components: [network_id: network_id, asset: asset]
55+
}, rest} = XDR.Struct.decode_xdr!(bytes, struct)
56+
57+
{new(network_id, asset), rest}
58+
end
59+
end

lib/xdr/transactions/hash_id_preimage.ex

+27-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,40 @@ defmodule StellarBase.XDR.HashIDPreimage do
22
@moduledoc """
33
Representation of Stellar `HashIDPreimage` type.
44
"""
5-
alias StellarBase.XDR.{EnvelopeType, OperationID, RevokeID}
5+
alias StellarBase.XDR.{
6+
EnvelopeType,
7+
OperationID,
8+
RevokeID,
9+
Ed25519ContractID,
10+
StructContractID,
11+
FromAsset,
12+
SourceAccountContractID,
13+
HashIDPreimageCreateContractArgs,
14+
HashIDPreimageContractAuth
15+
}
616

717
@behaviour XDR.Declaration
818

919
@arms [
1020
ENVELOPE_TYPE_OP_ID: OperationID,
11-
ENVELOPE_TYPE_POOL_REVOKE_OP_ID: RevokeID
21+
ENVELOPE_TYPE_POOL_REVOKE_OP_ID: RevokeID,
22+
ENVELOPE_TYPE_CONTRACT_ID_FROM_ED25519: Ed25519ContractID,
23+
ENVELOPE_TYPE_CONTRACT_ID_FROM_CONTRACT: StructContractID,
24+
ENVELOPE_TYPE_CONTRACT_ID_FROM_ASSET: FromAsset,
25+
ENVELOPE_TYPE_CONTRACT_ID_FROM_SOURCE_ACCOUNT: SourceAccountContractID,
26+
ENVELOPE_TYPE_CREATE_CONTRACT_ARGS: HashIDPreimageCreateContractArgs,
27+
ENVELOPE_TYPE_CONTRACT_AUTH: HashIDPreimageContractAuth
1228
]
1329

14-
@type hash_id :: OperationID.t() | RevokeID.t()
30+
@type hash_id ::
31+
OperationID.t()
32+
| RevokeID.t()
33+
| Ed25519ContractID.t()
34+
| StructContractID.t()
35+
| FromAsset.t()
36+
| SourceAccountContractID.t()
37+
| HashIDPreimageCreateContractArgs.t()
38+
| HashIDPreimageContractAuth.t()
1539

1640
@type t :: %__MODULE__{hash_id: hash_id(), type: EnvelopeType.t()}
1741

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
defmodule StellarBase.XDR.HashIDPreimageContractAuth do
2+
@moduledoc """
3+
Representation of Stellar `HashIDPreimageContractAuth` type.
4+
"""
5+
alias StellarBase.XDR.{Hash, UInt64, AuthorizedInvocation}
6+
7+
@behaviour XDR.Declaration
8+
9+
@struct_spec XDR.Struct.new(network_id: Hash, nonce: UInt64, invocation: AuthorizedInvocation)
10+
11+
@type t :: %__MODULE__{
12+
network_id: Hash.t(),
13+
nonce: UInt64.t(),
14+
invocation: AuthorizedInvocation.t()
15+
}
16+
17+
defstruct [:network_id, :nonce, :invocation]
18+
19+
@spec new(
20+
network_id :: Hash.t(),
21+
nonce :: UInt64.t(),
22+
invocation :: AuthorizedInvocation.t()
23+
) :: t()
24+
def new(%Hash{} = network_id, %UInt64{} = nonce, %AuthorizedInvocation{} = invocation),
25+
do: %__MODULE__{network_id: network_id, nonce: nonce, invocation: invocation}
26+
27+
@impl true
28+
def encode_xdr(%__MODULE__{network_id: network_id, nonce: nonce, invocation: invocation}) do
29+
[network_id: network_id, nonce: nonce, invocation: invocation]
30+
|> XDR.Struct.new()
31+
|> XDR.Struct.encode_xdr()
32+
end
33+
34+
@impl true
35+
def encode_xdr!(%__MODULE__{network_id: network_id, nonce: nonce, invocation: invocation}) do
36+
[network_id: network_id, nonce: nonce, invocation: invocation]
37+
|> XDR.Struct.new()
38+
|> XDR.Struct.encode_xdr!()
39+
end
40+
41+
@impl true
42+
def decode_xdr(bytes, struct \\ @struct_spec)
43+
44+
def decode_xdr(bytes, struct) do
45+
case XDR.Struct.decode_xdr(bytes, struct) do
46+
{:ok,
47+
{%XDR.Struct{
48+
components: [
49+
network_id: network_id,
50+
nonce: nonce,
51+
invocation: invocation
52+
]
53+
}, rest}} ->
54+
{:ok, {new(network_id, nonce, invocation), rest}}
55+
56+
error ->
57+
error
58+
end
59+
end
60+
61+
@impl true
62+
def decode_xdr!(bytes, struct \\ @struct_spec)
63+
64+
def decode_xdr!(bytes, struct) do
65+
{%XDR.Struct{
66+
components: [
67+
network_id: network_id,
68+
nonce: nonce,
69+
invocation: invocation
70+
]
71+
}, rest} = XDR.Struct.decode_xdr!(bytes, struct)
72+
73+
{new(network_id, nonce, invocation), rest}
74+
end
75+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
defmodule StellarBase.XDR.HashIDPreimageCreateContractArgs do
2+
@moduledoc """
3+
Representation of Stellar `HashIDPreimageCreateContractArgs` type.
4+
"""
5+
alias StellarBase.XDR.{Hash, SCContractCode, UInt256}
6+
7+
@behaviour XDR.Declaration
8+
9+
@struct_spec XDR.Struct.new(network_id: Hash, source: SCContractCode, salt: UInt256)
10+
11+
@type t :: %__MODULE__{network_id: Hash.t(), source: SCContractCode.t(), salt: UInt256.t()}
12+
13+
defstruct [:network_id, :source, :salt]
14+
15+
@spec new(network_id :: Hash.t(), source :: SCContractCode.t(), salt :: UInt256.t()) :: t()
16+
def new(%Hash{} = network_id, %SCContractCode{} = source, %UInt256{} = salt),
17+
do: %__MODULE__{network_id: network_id, source: source, salt: salt}
18+
19+
@impl true
20+
def encode_xdr(%__MODULE__{network_id: network_id, source: source, salt: salt}) do
21+
[network_id: network_id, source: source, salt: salt]
22+
|> XDR.Struct.new()
23+
|> XDR.Struct.encode_xdr()
24+
end
25+
26+
@impl true
27+
def encode_xdr!(%__MODULE__{network_id: network_id, source: source, salt: salt}) do
28+
[network_id: network_id, source: source, salt: salt]
29+
|> XDR.Struct.new()
30+
|> XDR.Struct.encode_xdr!()
31+
end
32+
33+
@impl true
34+
def decode_xdr(bytes, struct \\ @struct_spec)
35+
36+
def decode_xdr(bytes, struct) do
37+
case XDR.Struct.decode_xdr(bytes, struct) do
38+
{:ok,
39+
{%XDR.Struct{
40+
components: [
41+
network_id: network_id,
42+
source: source,
43+
salt: salt
44+
]
45+
}, rest}} ->
46+
{:ok, {new(network_id, source, salt), rest}}
47+
48+
error ->
49+
error
50+
end
51+
end
52+
53+
@impl true
54+
def decode_xdr!(bytes, struct \\ @struct_spec)
55+
56+
def decode_xdr!(bytes, struct) do
57+
{%XDR.Struct{
58+
components: [
59+
network_id: network_id,
60+
source: source,
61+
salt: salt
62+
]
63+
}, rest} = XDR.Struct.decode_xdr!(bytes, struct)
64+
65+
{new(network_id, source, salt), rest}
66+
end
67+
end

lib/xdr/transactions/operation_inner_result.ex

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule StellarBase.XDR.OperationInnerResult do
22
@moduledoc """
33
Representation of Stellar `OperationInnerResult` type.
44
"""
5-
alias StellarBase.XDR.OperationType
5+
alias StellarBase.XDR.{InvokeHostFunctionResult, OperationType}
66

77
alias StellarBase.XDR.Operations.{
88
AccountMergeResult,
@@ -56,7 +56,8 @@ defmodule StellarBase.XDR.OperationInnerResult do
5656
CLAWBACK_CLAIMABLE_BALANCE: ClawbackClaimableBalanceResult,
5757
SET_TRUST_LINE_FLAGS: SetTrustLineFlagsResult,
5858
LIQUIDITY_POOL_DEPOSIT: LiquidityPoolDepositResult,
59-
LIQUIDITY_POOL_WITHDRAW: LiquidityPoolWithdrawResult
59+
LIQUIDITY_POOL_WITHDRAW: LiquidityPoolWithdrawResult,
60+
INVOKE_HOST_FUNCTION: InvokeHostFunctionResult
6061
]
6162

6263
@type t :: %__MODULE__{result: any(), type: OperationType.t()}

lib/xdr/transactions/operation_type.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ defmodule StellarBase.XDR.OperationType do
2929
CLAWBACK_CLAIMABLE_BALANCE: 20,
3030
SET_TRUST_LINE_FLAGS: 21,
3131
LIQUIDITY_POOL_DEPOSIT: 22,
32-
LIQUIDITY_POOL_WITHDRAW: 23
32+
LIQUIDITY_POOL_WITHDRAW: 23,
33+
INVOKE_HOST_FUNCTION: 24
3334
]
3435

3536
@enum_spec %XDR.Enum{declarations: @declarations, identifier: nil}

0 commit comments

Comments
 (0)