Skip to content

Commit

Permalink
Fix condition map restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmanzanera committed Dec 2, 2022
1 parent f688a11 commit 61aec50
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
10 changes: 5 additions & 5 deletions lib/archethic/contracts/interpreter/condition.ex
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ defmodule Archethic.Contracts.ConditionInterpreter do
{:%{}, _, _} ->
{node, acc}

{{:atom, _}, _, _} ->
{op, _, _} when op in [:==, :<, :>, :<=, :>=, :if] ->
{node, acc}

_ ->
{node, {:error, "must be a map"}}
{node, {:error, "must be a map or a code instruction starting by an comparator"}}
end
end

Expand All @@ -203,11 +203,11 @@ defmodule Archethic.Contracts.ConditionInterpreter do
{:%{}, _, _} ->
{node, acc}

{{:atom, _}, _, _} ->
{op, _, _} when op in [:==, :<, :>, :<=, :>=, :if] ->
{node, acc}

_ ->
{node, {:error, "must be a map"}}
{node, {:error, "must be a map or a code instruction starting by an comparator"}}
end
end

Expand Down Expand Up @@ -271,7 +271,7 @@ defmodule Archethic.Contracts.ConditionInterpreter do
{node, acc}
end

# Whitelist usage of maps in the field of a condition
# Whitelist usage of taps in the field of a condition
defp prewalk(node = {{:atom, _key}, _val}, acc = {:ok, %{scope: {:condition, _, _}}}) do
{node, acc}
end
Expand Down
10 changes: 6 additions & 4 deletions test/archethic/contracts/interpreter/condition_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ defmodule Archethic.Contracts.ConditionInterpreterTest do
end

test "parse invalid uco transfers type definition" do
assert {:error, "must be a map - uco_transfers"} =
assert {:error,
"must be a map or a code instruction starting by an comparator - uco_transfers"} =
"""
condition inherit: [
uco_transfers: [%{ to: "7F6661ACE282F947ACA2EF947D01BDDC90C65F09EE828BDADE2E3ED4258470B3", amount: 1040000000 }]
uco_transfers: [%{ "7F6661ACE282F947ACA2EF947D01BDDC90C65F09EE828BDADE2E3ED4258470B3" => 1040000000 }]
]
"""
|> Interpreter.sanitize_code()
Expand All @@ -150,10 +151,11 @@ defmodule Archethic.Contracts.ConditionInterpreterTest do
end

test "parse invalid token transfers type definition" do
assert {:error, "must be a map - token_transfers"} =
assert {:error,
"must be a map or a code instruction starting by an comparator - token_transfers"} =
"""
condition inherit: [
token_transfers: [%{ to: "7F6661ACE282F947ACA2EF947D01BDDC90C65F09EE828BDADE2E3ED4258470B3", amount: 1040000000 }]
token_transfers: [%{ "7F6661ACE282F947ACA2EF947D01BDDC90C65F09EE828BDADE2E3ED4258470B3" => 1040000000 }]
]
"""
|> Interpreter.sanitize_code()
Expand Down
26 changes: 13 additions & 13 deletions test/archethic/contracts/interpreter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@ defmodule Archethic.Contracts.InterpreterTest do
assert {:ok, _} =
"""
condition inherit: [
type: transfer,
uco_transfers: size() == 1
# TODO: to provide more security, we should check the destination address is within the previous transaction inputs
token_transfers: size() == 1
]
condition transaction: [
uco_transfers: size() > 0,
timestamp: transaction.timestamp < 1665750161
]
actions triggered_by: transaction do
# Get the amount of uco send to this contract
amount_send = transaction.uco_transfers[contract.address]
if amount_send > 0 do
# Convert UCO to the number of tokens to credit. Each UCO worth 10000 token
token_to_credit = amount_send * 10000
# Get the amount of uco send to this contract
amount_send = transaction.uco_transfers[contract.address]
if amount_send > 0 do
# Convert UCO to the number of tokens to credit. Each UCO worth 10 token
token_to_credit = amount_send * 10
# Send the new transaction
set_type transfer
add_token_transfer to: transaction.address, token_address: contract.address, amount: token_to_credit, token_id: token_id
end
# Send the new transaction
add_token_transfer to: transaction.address, token_address: contract.address, amount: token_to_credit
end
end
"""
|> Interpreter.parse()
Expand Down

0 comments on commit 61aec50

Please sign in to comment.