Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smart Contracts: Contract.call_function/3 mockable #1265

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading