Skip to content

Commit

Permalink
Fix cast output for json rpc call
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix committed Dec 20, 2024
1 parent 3b8beb9 commit b79a13f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
9 changes: 3 additions & 6 deletions lib/archethic/contracts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,9 @@ defmodule Archethic.Contracts do
logs: []
}}

%WasmSpec.Function{input: input, output: output} ->
{:ok, %WasmSpec.Function{input: input}} ->
with {:ok, arg} <- WasmSpec.cast_wasm_input(args_values, input),
{:ok, value} <-
{:ok, %ReadResult{value: value}} <-
WasmModule.execute(module, function_name,
state: state,
balance: UTXO.get_balance(inputs),
Expand All @@ -487,10 +487,7 @@ defmodule Archethic.Contracts do
Archethic.fetch_genesis_address(contract_tx.address) |> elem(1)
)
) do
case value do
%ReadResult{value: value} -> {:ok, WasmSpec.cast_wasm_output(value, output), []}
nil -> {:ok, nil, []}
end
{:ok, value, []}
else
{:error, reason} ->
{:error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ defmodule Archethic.Contracts.Interpreter.Library.Common.ContractImpl do
alias Archethic.Contracts.Contract.Failure
alias Archethic.Contracts.Interpreter.Legacy.UtilsInterpreter
alias Archethic.Contracts.Interpreter.Library
alias Archethic.Contracts.WasmContract
alias Archethic.Contracts.WasmModule
alias Archethic.Contracts.WasmSpec

use Archethic.Tag

Expand All @@ -31,9 +34,9 @@ defmodule Archethic.Contracts.Interpreter.Library.Common.ContractImpl do
{:ok, genesis_address} <- Archethic.fetch_genesis_address(address),
{:ok, contract} <- Contracts.from_transaction(tx),
unspent_outputs = Archethic.get_unspent_outputs(genesis_address),
{:ok, value, _logs} <-
{:ok, output, _logs} <-
Contracts.execute_function(contract, function, args, unspent_outputs) do
value
cast_function_output(contract, function, output)
else
{:error, reason} -> raise Library.Error, message: error_to_message(reason)
end
Expand All @@ -46,4 +49,11 @@ defmodule Archethic.Contracts.Interpreter.Library.Common.ContractImpl do
defp error_to_message(reason) do
"Contract.call_function failed with #{inspect(reason)}"
end

defp cast_function_output(function, %WasmContract{module: %WasmModule{spec: spec}}, output) do
{:ok, function_spec} = WasmSpec.get_function_spec(spec, function)
WasmSpec.cast_wasm_output(output, function_spec)
end

defp cast_function_output(_, _, output), do: output
end
10 changes: 3 additions & 7 deletions lib/archethic/contracts/wasm/module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,13 @@ defmodule Archethic.Contracts.WasmModule do
end

@spec execute(module :: t(), functionName :: binary(), opts :: execution_opts()) ::
{:ok, ReadResult.t() | UpdateResult.t() | nil}
| {:error, any()}
{:ok, ReadResult.t() | UpdateResult.t()} | {:error, any()}
def execute(%__MODULE__{module: module, store: store}, function_name, opts \\ [])
when is_binary(function_name) do
input =
%{
state: Keyword.get(opts, :state, %{}),
transaction:
opts
|> Keyword.get(:transaction)
|> cast_transaction(),
transaction: opts |> Keyword.get(:transaction) |> cast_transaction(),
arguments: Keyword.get(opts, :arguments),
balance: Keyword.get(opts, :balance, %{uco: 0, tokens: []}),
contract: opts |> Keyword.get(:contract) |> cast_transaction()
Expand Down Expand Up @@ -230,7 +226,7 @@ defmodule Archethic.Contracts.WasmModule do
}
end

defp cast_output(nil), do: {:ok, nil}
defp cast_output(nil), do: {:ok, WasmResult.cast(nil)}

defp cast_output(output) do
with {:ok, json} <- Jason.decode(output) do
Expand Down
2 changes: 1 addition & 1 deletion lib/archethic/contracts/wasm/result.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule Archethic.Contracts.WasmResult do
@doc """
Cast JSON WebAssembly result in `UpdateResult` or `ReadResult`
"""
@spec cast(map()) :: UpdateResult.t() | ReadResult.t()
@spec cast(map() | nil) :: UpdateResult.t() | ReadResult.t()
def cast(result) when is_map_key(result, "state") or is_map_key(result, "transaction") do
%UpdateResult{
state: Map.get(result, "state") |> cast_state(),
Expand Down

0 comments on commit b79a13f

Please sign in to comment.