Skip to content

Commit

Permalink
Add state to conditions' constants
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne committed Oct 2, 2023
1 parent 0765b59 commit 9f6c177
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/archethic/contracts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
36 changes: 36 additions & 0 deletions test/archethic/contracts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9f6c177

Please sign in to comment.