-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Contract.call_function/3 is now mockable (for the playground) (#1265)
- Loading branch information
Showing
2 changed files
with
47 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
lib/archethic/contracts/interpreter/library/common/contract_impl.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |