Skip to content

Commit

Permalink
Merge pull request #28 from kommitters/v0.2
Browse files Browse the repository at this point in the history
Release v0.2.0
  • Loading branch information
CristhianRodriguezMolina committed May 3, 2023
2 parents 648fb8c + 7d5b32b commit 864e146
Show file tree
Hide file tree
Showing 43 changed files with 1,503 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.2.0 (03.05.2023)

- [Initial Types](https://github.com/kommitters/soroban.ex/issues/9)

## 0.1.0 (04.28.2023)

- [Project setup](https://github.com/kommitters/soroban.ex/issues/1)
18 changes: 18 additions & 0 deletions lib/rpc/error.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Soroban.RPC.Error do
@moduledoc """
Represents an error which occurred during a Soroban RPC call.
"""

@type code :: -32_099..-32_000 | -32_603..-32_600 | -32_700
@type message :: String.t()
@type t :: %__MODULE__{
code: code(),
message: message()
}

defstruct [:code, :message]

@spec new(error :: map()) :: t()
def new(%{code: code, message: message}),
do: %__MODULE__{code: code, message: message}
end
51 changes: 51 additions & 0 deletions lib/rpc/responses/get_transaction_response.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
defmodule Soroban.RPC.GetTransactionResponse do
@moduledoc """
`GetTransactionResponse` struct definition.
"""
@behaviour Soroban.RPC.Response.Spec

@type status :: String.t()
@type latest_ledger :: String.t()
@type latest_ledger_close_time :: String.t()
@type oldest_ledger :: String.t()
@type oldest_ledger_close_time :: String.t()
@type ledger :: String.t() | nil
@type created_at :: String.t() | nil
@type application_order :: number() | nil
@type fee_bump :: boolean() | nil
@type envelope_xdr :: String.t() | nil
@type result_xdr :: String.t() | nil
@type result_meta_xdr :: String.t() | nil
@type t :: %__MODULE__{
status: status(),
latest_ledger: latest_ledger(),
latest_ledger_close_time: latest_ledger_close_time(),
oldest_ledger: oldest_ledger(),
oldest_ledger_close_time: oldest_ledger_close_time(),
ledger: ledger(),
created_at: created_at(),
application_order: application_order(),
fee_bump: fee_bump(),
envelope_xdr: envelope_xdr(),
result_xdr: result_xdr(),
result_meta_xdr: result_meta_xdr()
}

defstruct [
:status,
:latest_ledger,
:latest_ledger_close_time,
:oldest_ledger,
:oldest_ledger_close_time,
:ledger,
:created_at,
:application_order,
:fee_bump,
:envelope_xdr,
:result_xdr,
:result_meta_xdr
]

@impl true
def new(attrs), do: struct(%__MODULE__{}, attrs)
end
24 changes: 24 additions & 0 deletions lib/rpc/responses/send_transaction_response.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Soroban.RPC.SendTransactionResponse do
@moduledoc """
`SendTransactionResponse` struct definition.
"""
@behaviour Soroban.RPC.Response.Spec

@type status :: String.t()
@type hash :: binary()
@type latest_ledger :: String.t()
@type latest_ledger_close_time :: String.t()
@type error_result_xdr :: String.t() | nil
@type t :: %__MODULE__{
status: status(),
hash: hash(),
latest_ledger: latest_ledger(),
latest_ledger_close_time: latest_ledger_close_time(),
error_result_xdr: error_result_xdr()
}

defstruct [:status, :hash, :latest_ledger, :latest_ledger_close_time, :error_result_xdr]

@impl true
def new(attrs), do: struct(%__MODULE__{}, attrs)
end
21 changes: 21 additions & 0 deletions lib/rpc/responses/simulate_transaction_response.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Soroban.RPC.SimulateTransactionResponse do
@moduledoc """
`SimulateTransactionResponse` struct definition.
"""
@behaviour Soroban.RPC.Response.Spec

@type results :: list(map())
@type cost :: map()
@type latest_ledger :: String.t()
@type error :: String.t() | nil
@type t :: %__MODULE__{
results: results(),
cost: cost(),
latest_ledger: latest_ledger(),
error: error()
}
defstruct [:results, :cost, :latest_ledger, :error]

@impl true
def new(attrs), do: struct(%__MODULE__{}, attrs)
end
7 changes: 7 additions & 0 deletions lib/rpc/responses/spec.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Soroban.RPC.Response.Spec do
@moduledoc """
Defines RPC response type constructions.
"""

@callback new(map()) :: struct()
end
43 changes: 43 additions & 0 deletions lib/types/address.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule Soroban.Types.Address do
@moduledoc """
`Address` struct definition.
"""

@behaviour Soroban.Types.Spec

alias Stellar.TxBuild.{SCAddress, SCVal}

defstruct [:value]

@type value :: binary()
@type type :: atom()
@type errors :: atom()
@type validation :: {:ok, value()} | {:error, errors()}
@type t :: %__MODULE__{value: value()}

@impl true
def new(value) when is_binary(value) do
with {:ok, value} <- validate_address(value) do
%__MODULE__{value: value}
end
end

def new(_value), do: {:error, :invalid}

@impl true
def to_sc_val(%__MODULE__{value: value}) do
value
|> SCAddress.new()
|> (&SCVal.new(address: &1)).()
end

def to_sc_val(_error), do: {:error, :invalid_struct_address}

@spec validate_address(value :: value()) :: validation()
defp validate_address(value) do
case SCAddress.new(value) do
%SCAddress{} -> {:ok, value}
_error -> {:error, :invalid_address}
end
end
end
23 changes: 23 additions & 0 deletions lib/types/bool.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Soroban.Types.Bool do
@moduledoc """
`Bool` struct definition.
"""

@behaviour Soroban.Types.Spec

alias Stellar.TxBuild.SCVal

defstruct [:value]

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

@impl true
def new(value) when is_boolean(value), do: %__MODULE__{value: value}

def new(_value), do: {:error, :invalid}

@impl true
def to_sc_val(%__MODULE__{value: value}), do: SCVal.new(bool: value)
def to_sc_val(_error), do: {:error, :invalid_struct_bool}
end
23 changes: 23 additions & 0 deletions lib/types/bytes.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Soroban.Types.Bytes do
@moduledoc """
`Bytes` struct definition.
"""

@behaviour Soroban.Types.Spec

alias Stellar.TxBuild.SCVal

defstruct [:value]

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

@impl true
def new(value) when is_binary(value), do: %__MODULE__{value: value}

def new(_value), do: {:error, :invalid}

@impl true
def to_sc_val(%__MODULE__{value: value}), do: SCVal.new(bytes: value)
def to_sc_val(_error), do: {:error, :invalid_struct_bytes}
end
36 changes: 36 additions & 0 deletions lib/types/duration.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Soroban.Types.Duration do
@moduledoc """
`Duration` struct definition.
"""

@behaviour Soroban.Types.Spec

alias Stellar.TxBuild.SCVal

defstruct [:value]

@type value :: non_neg_integer()
@type errors :: atom()
@type validation :: {:ok, value()} | {:error, errors()}
@type t :: %__MODULE__{value: value()}

@int_range 0..18_446_744_073_709_551_615

@impl true
def new(value) when is_integer(value) do
case validate_duration_range(value) do
{:ok, duration} -> %__MODULE__{value: duration}
error -> error
end
end

def new(_value), do: {:error, :invalid}

@impl true
def to_sc_val(%__MODULE__{value: value}), do: SCVal.new(duration: value)
def to_sc_val(_error), do: {:error, :invalid_struct_duration}

@spec validate_duration_range(value :: value()) :: validation()
defp validate_duration_range(value) when value in @int_range, do: {:ok, value}
defp validate_duration_range(_value), do: {:error, :not_in_duration_range}
end
40 changes: 40 additions & 0 deletions lib/types/int128.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule Soroban.Types.Int128 do
@moduledoc """
`Int128` struct definition.
"""

@behaviour Soroban.Types.Spec

alias Stellar.TxBuild.SCVal

defstruct [:value]

@type value :: integer()
@type errors :: atom()
@type validation :: {:ok, value()} | {:error, errors()}
@type t :: %__MODULE__{value: value()}

@int_range -170_141_183_460_469_231_731_687_303_715_884_105_728..170_141_183_460_469_231_731_687_303_715_884_105_727

@impl true
def new(value) when is_integer(value) do
case validate_int128_range(value) do
{:ok, i128} -> %__MODULE__{value: i128}
error -> error
end
end

def new(_value), do: {:error, :invalid}

@impl true
def to_sc_val(%__MODULE__{value: value}) do
<<hi::size(64), lo::size(64)>> = <<value::size(128)>>
SCVal.new(i128: %{lo: lo, hi: hi})
end

def to_sc_val(_error), do: {:error, :invalid_struct_int128}

@spec validate_int128_range(value :: value()) :: validation()
defp validate_int128_range(value) when value in @int_range, do: {:ok, value}
defp validate_int128_range(_value), do: {:error, :not_in_i128_range}
end
22 changes: 22 additions & 0 deletions lib/types/int256.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule Soroban.Types.Int256 do
@moduledoc """
`Int256` struct definition.
"""

@behaviour Soroban.Types.Spec

alias Stellar.TxBuild.SCVal

defstruct [:value]

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

@impl true
def new(value) when is_integer(value), do: %__MODULE__{value: value}
def new(_value), do: {:error, :invalid}

@impl true
def to_sc_val(%__MODULE__{value: value}), do: SCVal.new(i256: <<value::size(256)>>)
def to_sc_val(_error), do: {:error, :invalid_struct_int256}
end
36 changes: 36 additions & 0 deletions lib/types/int32.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Soroban.Types.Int32 do
@moduledoc """
`Int32` struct definition.
"""

@behaviour Soroban.Types.Spec

alias Stellar.TxBuild.SCVal

defstruct [:value]

@type value :: integer()
@type errors :: atom()
@type validation :: {:ok, value()} | {:error, errors()}
@type t :: %__MODULE__{value: value()}

@int_range -2_147_483_648..2_147_483_647

@impl true
def new(value) when is_integer(value) do
case validate_int32_range(value) do
{:ok, i32} -> %__MODULE__{value: i32}
error -> error
end
end

def new(_value), do: {:error, :invalid}

@impl true
def to_sc_val(%__MODULE__{value: value}), do: SCVal.new(i32: value)
def to_sc_val(_error), do: {:error, :invalid_struct_int32}

@spec validate_int32_range(value :: value()) :: validation()
defp validate_int32_range(value) when value in @int_range, do: {:ok, value}
defp validate_int32_range(_value), do: {:error, :not_in_i32_range}
end
Loading

0 comments on commit 864e146

Please sign in to comment.