diff --git a/lib/archethic/contracts.ex b/lib/archethic/contracts.ex index 9b3183df2..fc1ea3ae2 100644 --- a/lib/archethic/contracts.ex +++ b/lib/archethic/contracts.ex @@ -250,12 +250,15 @@ defmodule Archethic.Contracts do transaction, datetime ) do + maybe_state_utxo = State.get_utxo_from_transaction(contract_tx) + %{ "previous" => Constants.from_contract_transaction(contract_tx, contract_version), "next" => Constants.from_contract_transaction(transaction, contract_version), :time_now => DateTime.to_unix(datetime), :functions => functions, - :encrypted_seed => Contract.get_encrypted_seed(contract) + :encrypted_seed => Contract.get_encrypted_seed(contract), + :state => State.from_utxo(maybe_state_utxo) } end @@ -269,12 +272,15 @@ defmodule Archethic.Contracts do transaction, datetime ) do + maybe_state_utxo = State.get_utxo_from_transaction(contract_tx) + %{ "transaction" => Constants.from_transaction(transaction, contract_version), "contract" => Constants.from_contract_transaction(contract_tx, contract_version), :time_now => DateTime.to_unix(datetime), :functions => functions, - :encrypted_seed => Contract.get_encrypted_seed(contract) + :encrypted_seed => Contract.get_encrypted_seed(contract), + :state => State.from_utxo(maybe_state_utxo) } end diff --git a/test/archethic/contracts_test.exs b/test/archethic/contracts_test.exs index d1a121b59..f0bfa898c 100644 --- a/test/archethic/contracts_test.exs +++ b/test/archethic/contracts_test.exs @@ -196,6 +196,42 @@ defmodule Archethic.ContractsTest do ) end + test "should return true if condition is true based on state" do + code = """ + @version 1 + condition triggered_by: transaction, as: [ + type: State.get("key") == "value" + ] + + actions triggered_by: transaction do + Contract.set_content "hello" + end + """ + + state_utxo = State.to_utxo(%{"key" => "value"}) + + # some ucos are necessary for ContractFactory.create_valid_contract_tx + uco_utxo = %UnspentOutput{ + amount: 200_000_000, + from: ArchethicCase.random_address(), + type: :UCO, + timestamp: DateTime.utc_now() + } + + contract_tx = + ContractFactory.create_valid_contract_tx(code, state: state_utxo, inputs: [uco_utxo]) + + trigger_tx = TransactionFactory.create_valid_transaction([]) + + assert Contracts.valid_condition?( + {:transaction, nil, nil}, + Contract.from_transaction!(contract_tx), + trigger_tx, + nil, + DateTime.utc_now() + ) + end + test "should return false if condition is falsy" do code = """ @version 1