Skip to content

Commit

Permalink
Contract.call_function/3 is now mockable (for the playground) (#1265)
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne committed Sep 11, 2023
1 parent 29fa539 commit 52fe6ec
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
32 changes: 9 additions & 23 deletions lib/archethic/contracts/interpreter/library/common/contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 52fe6ec

Please sign in to comment.