diff --git a/lib/archethic/contracts/interpreter/library/common/contract.ex b/lib/archethic/contracts/interpreter/library/common/contract.ex index c11e38554..522ff3779 100644 --- a/lib/archethic/contracts/interpreter/library/common/contract.ex +++ b/lib/archethic/contracts/interpreter/library/common/contract.ex @@ -6,7 +6,6 @@ defmodule Archethic.Contracts.Interpreter.Library.Common.Contract do """ @behaviour Archethic.Contracts.Interpreter.Library - alias Archethic.Contracts alias Archethic.Contracts.Interpreter.ASTHelper, as: AST alias Archethic.Contracts.Interpreter.Legacy.UtilsInterpreter alias Archethic.Contracts.Interpreter.Legacy.TransactionStatements @@ -21,6 +20,13 @@ defmodule Archethic.Contracts.Interpreter.Library.Common.Contract do use Tag + # we do not use knigge because we do not mock the entire module + @contract_impl Application.compile_env( + :archethic, + __MODULE__, + Archethic.Contracts.Interpreter.Library.Common.ContractImpl + ) + @tag [:write_contract] @spec set_type(Transaction.t(), binary()) :: Transaction.t() defdelegate set_type(next_tx, type), @@ -128,28 +134,8 @@ defmodule Archethic.Contracts.Interpreter.Library.Common.Contract do @tag [:io] @spec call_function(address :: binary(), function :: binary(), args :: list()) :: any() def call_function(address, function, args) do - address = UtilsInterpreter.get_address(address, :call_function) - - unless is_binary(function), - do: - raise(Library.Error, - message: "Contract.call_function must have binary function got #{inspect(function)}" - ) - - unless is_list(args), - do: - raise(Library.Error, - message: "Contract.call_function must have list for args got #{inspect(args)}" - ) - - with {:ok, tx} <- Archethic.get_last_transaction(address), - {:ok, contract} <- Contracts.from_transaction(tx), - {:ok, result} <- Contracts.execute_function(contract, function, args) do - result - else - {:error, reason} -> - raise(Library.Error, message: "Contract.call_function failed with #{inspect(reason)}") - end + # for some reason I failed to use defdelegate + apply(@contract_impl, :call_function, [address, function, args]) end # We do not need to check the transaction argument because _we_ are feeding it (after this step) diff --git a/lib/archethic/contracts/interpreter/library/common/contract_impl.ex b/lib/archethic/contracts/interpreter/library/common/contract_impl.ex new file mode 100644 index 000000000..6801cc628 --- /dev/null +++ b/lib/archethic/contracts/interpreter/library/common/contract_impl.ex @@ -0,0 +1,38 @@ +defmodule Archethic.Contracts.Interpreter.Library.Common.ContractImpl do + @moduledoc """ + this is not a behaviour because we define only few functions + """ + + alias Archethic.Contracts + alias Archethic.Contracts.Interpreter.Legacy.UtilsInterpreter + alias Archethic.Contracts.Interpreter.Library + + use Archethic.Tag + + @tag [:io] + @spec call_function(address :: binary(), function :: binary(), args :: list()) :: any() + def call_function(address, function, args) do + address = UtilsInterpreter.get_address(address, :call_function) + + unless is_binary(function), + do: + raise(Library.Error, + message: "Contract.call_function must have binary function got #{inspect(function)}" + ) + + unless is_list(args), + do: + raise(Library.Error, + message: "Contract.call_function must have list for args got #{inspect(args)}" + ) + + with {:ok, tx} <- Archethic.get_last_transaction(address), + {:ok, contract} <- Contracts.from_transaction(tx), + {:ok, result} <- Contracts.execute_function(contract, function, args) do + result + else + {:error, reason} -> + raise(Library.Error, message: "Contract.call_function failed with #{inspect(reason)}") + end + end +end