Skip to content

Commit

Permalink
Merge pull request #290 from kommitters/v0.13
Browse files Browse the repository at this point in the history
Release v0.13.0
  • Loading branch information
FelipeGuzmanSierra authored Apr 26, 2023
2 parents eb4243e + 289596d commit 4f93d47
Show file tree
Hide file tree
Showing 20 changed files with 723 additions and 894 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.13.0 (26.04.2023)
* Add Soroban Preview 8 support.

## 0.12.0 (19.04.2023)
* CAP-0046 Add support for smart contracts (Soroban preview 7).

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The **Stellar SDK** is composed of two complementary components: **`TxBuild`** +
```elixir
def deps do
[
{:stellar_sdk, "~> 0.12.0"}
{:stellar_sdk, "~> 0.13.0"}
]
end
```
Expand Down
9 changes: 3 additions & 6 deletions lib/tx_build/contract_auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ defmodule Stellar.TxBuild.ContractAuth do
alias Stellar.TxBuild.{
AuthorizedInvocation,
OptionalAddressWithNonce,
SCObject,
SCMapEntry,
HashIDPreimage
}
Expand Down Expand Up @@ -76,7 +75,6 @@ defmodule Stellar.TxBuild.ContractAuth do

signature_args =
[vec: signature_args]
|> SCObject.new()
|> SCVal.new()
|> SCVal.to_xdr()
|> (&SCVec.new([&1])).()
Expand Down Expand Up @@ -118,17 +116,16 @@ defmodule Stellar.TxBuild.ContractAuth do
public_key_map_entry =
SCMapEntry.new(
SCVal.new(symbol: "public_key"),
SCVal.new(object: SCObject.new(bytes: raw_public_key))
SCVal.new(bytes: raw_public_key)
)

signature_map_entry =
SCMapEntry.new(
SCVal.new(symbol: "signature"),
SCVal.new(object: SCObject.new(bytes: signature))
SCVal.new(bytes: signature)
)

signature_sc_val =
SCVal.new(object: SCObject.new(map: [public_key_map_entry, signature_map_entry]))
signature_sc_val = SCVal.new(map: [public_key_map_entry, signature_map_entry])

%{contract_auth | signature_args: signature_args ++ [signature_sc_val]}
end
Expand Down
8 changes: 4 additions & 4 deletions lib/tx_build/hash_id_preimage_create_contract_args.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ defmodule Stellar.TxBuild.HashIDPreimageCreateContractArgs do
]

alias StellarBase.XDR.{HashIDPreimageCreateContractArgs, Hash, UInt256}
alias Stellar.TxBuild.SCContractCode
alias Stellar.TxBuild.SCContractExecutable

@type validation :: {:ok, any()} | {:error, atom()}
@type t :: %__MODULE__{
network_id: binary(),
source: SCContractCode.t(),
source: SCContractExecutable.t(),
salt: non_neg_integer()
}

Expand Down Expand Up @@ -50,7 +50,7 @@ defmodule Stellar.TxBuild.HashIDPreimageCreateContractArgs do
salt: salt
}) do
network_id = Hash.new(network_id)
source = SCContractCode.to_xdr(source)
source = SCContractExecutable.to_xdr(source)
salt = UInt256.new(salt)

HashIDPreimageCreateContractArgs.new(network_id, source, salt)
Expand All @@ -59,7 +59,7 @@ defmodule Stellar.TxBuild.HashIDPreimageCreateContractArgs do
def to_xdr(_error), do: {:error, :invalid_struct_hash_id_preimage_contract_args}

@spec validate_contract_code(tuple :: tuple()) :: validation()
defp validate_contract_code({_field, %SCContractCode{} = value}),
defp validate_contract_code({_field, %SCContractExecutable{} = value}),
do: {:ok, value}

defp validate_contract_code({field, _}), do: {:error, :"invalid_#{field}"}
Expand Down
38 changes: 12 additions & 26 deletions lib/tx_build/host_function.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,18 @@ defmodule Stellar.TxBuild.HostFunction do
validate_string: 1
]

alias Stellar.TxBuild.{SCVal, Asset, SCContractCode}

alias StellarBase.XDR.SCVal, as: SCValXDR
alias Stellar.TxBuild.{Asset, SCVal, SCContractExecutable}
alias StellarBase.XDR.HostFunction, as: HostFunctionXDR
alias StellarBase.XDR.InstallContractCodeArgs

alias StellarBase.XDR.{
ContractID,
ContractIDType,
CreateContractArgs,
InstallContractCodeArgs,
SCVec,
HostFunctionType,
OptionalSCObject,
SCObject,
SCValType,
SCSymbol,
SCObjectType,
UInt256,
VariableOpaque256000,
SCValType
VariableOpaque256000
}

@behaviour Stellar.TxBuild.XDR
Expand Down Expand Up @@ -148,23 +140,16 @@ defmodule Stellar.TxBuild.HostFunction do
function_name: function_name,
args: args
}) do
sc_object_type = SCObjectType.new(:SCO_BYTES)
sc_val_type = SCValType.new(:SCV_OBJECT)

contract_id_scval =
contract_id
|> Base.decode16!(case: :lower)
|> VariableOpaque256000.new()
|> SCObject.new(sc_object_type)
|> OptionalSCObject.new()
|> SCValXDR.new(sc_val_type)

sc_symbol_type = SCValType.new(:SCV_SYMBOL)
|> (&SCVal.new(bytes: &1)).()
|> SCVal.to_xdr()

sc_symbol_scval =
function_name
|> SCSymbol.new()
|> SCValXDR.new(sc_symbol_type)
|> (&SCVal.new(symbol: &1)).()
|> SCVal.to_xdr()

args_scvalxdr = Enum.map(args, &SCVal.to_xdr/1)

Expand Down Expand Up @@ -200,10 +185,11 @@ defmodule Stellar.TxBuild.HostFunction do
contract_id_type = ContractIDType.new(:CONTRACT_ID_FROM_SOURCE_ACCOUNT)
contract_id = salt |> UInt256.new() |> ContractID.new(contract_id_type)

sc_contract_code = [wasm_ref: wasm_id] |> SCContractCode.new() |> SCContractCode.to_xdr()
sc_contract_executable =
[wasm_ref: wasm_id] |> SCContractExecutable.new() |> SCContractExecutable.to_xdr()

contract_id
|> CreateContractArgs.new(sc_contract_code)
|> CreateContractArgs.new(sc_contract_executable)
|> HostFunctionXDR.new(host_function_type)
end

Expand All @@ -216,10 +202,10 @@ defmodule Stellar.TxBuild.HostFunction do
contract_id_type = ContractIDType.new(:CONTRACT_ID_FROM_ASSET)
contract_id = asset |> Asset.to_xdr() |> ContractID.new(contract_id_type)

sc_contract_code = :token |> SCContractCode.new() |> SCContractCode.to_xdr()
sc_contract_executable = :token |> SCContractExecutable.new() |> SCContractExecutable.to_xdr()

contract_id
|> CreateContractArgs.new(sc_contract_code)
|> CreateContractArgs.new(sc_contract_executable)
|> HostFunctionXDR.new(host_function_type)
end

Expand Down
58 changes: 0 additions & 58 deletions lib/tx_build/sc_contract_code.ex

This file was deleted.

58 changes: 58 additions & 0 deletions lib/tx_build/sc_contract_executable.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
defmodule Stellar.TxBuild.SCContractExecutable do
@moduledoc """
`SCContractExecutable` struct definition.
"""

@behaviour Stellar.TxBuild.XDR

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

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

defstruct [:type, :value]

@impl true
def new(args, opts \\ nil)

def new(:token, _opts), do: %__MODULE__{type: :token, value: nil}

def new([{:wasm_ref, value}], _opts) do
with {:ok, _value} <- validate_sc_contract_executable({:wasm_ref, value}) do
%__MODULE__{
type: :wasm_ref,
value: value
}
end
end

def new(_args, _opts), do: {:error, :invalid_sc_contract_executable}

@impl true
def to_xdr(%__MODULE__{type: :wasm_ref, value: value}) do
type = SCContractExecutableType.new(:SCCONTRACT_EXECUTABLE_WASM_REF)

value
|> Hash.new()
|> SCContractExecutable.new(type)
end

def to_xdr(%__MODULE__{type: :token, value: nil}) do
type = SCContractExecutableType.new(:SCCONTRACT_EXECUTABLE_TOKEN)
void = Void.new()

SCContractExecutable.new(void, type)
end

def to_xdr(_error), do: {:error, :invalid_sc_contract_executable}

@spec validate_sc_contract_executable(tuple()) :: validation()
defp validate_sc_contract_executable({:wasm_ref, value}) when is_binary(value), do: {:ok, value}

defp validate_sc_contract_executable({:wasm_ref, _value}), do: {:error, :invalid_contract_hash}
end
Loading

0 comments on commit 4f93d47

Please sign in to comment.