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: raise on invalid amount #1012

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ defmodule Archethic.Contracts.Interpreter.Legacy.TransactionStatements do
@spec add_uco_transfer(Transaction.t(), list()) :: Transaction.t()
def add_uco_transfer(tx = %Transaction{}, args) when is_list(args) do
%{"to" => to, "amount" => amount} = Enum.into(args, %{})

if amount <= 0 do
raise ArgumentError, message: "Contract used add_uco_transfer with an invalid amount"
end

to = UtilsInterpreter.get_address(to, :add_uco_transfer)

update_in(
Expand Down Expand Up @@ -90,6 +95,10 @@ defmodule Archethic.Contracts.Interpreter.Legacy.TransactionStatements do
map_args =
%{"to" => to, "amount" => amount, "token_address" => token_address} = Enum.into(args, %{})

if amount <= 0 do
raise ArgumentError, message: "Contract used add_token_transfer with an invalid amount"
end

to = UtilsInterpreter.get_address(to, :add_token_transfer_to)

token_address =
Expand Down
54 changes: 54 additions & 0 deletions test/archethic/contracts/interpreter/library/contract_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,32 @@ defmodule Archethic.Contracts.Interpreter.Library.ContractTest do
}
} = sanitize_parse_execute(code)
end

test "should crash if the amount is 0" do
address = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

code = ~s"""
actions triggered_by: transaction do
transfer = [to: "#{Base.encode16(address)}", amount: 0]
Contract.add_uco_transfer transfer
end
"""

assert_raise(ArgumentError, fn -> sanitize_parse_execute(code) end)
end

test "should crash if the amount is negative" do
address = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

code = ~s"""
actions triggered_by: transaction do
transfer = [to: "#{Base.encode16(address)}", amount: -0.1]
Contract.add_uco_transfer transfer
end
"""

assert_raise(ArgumentError, fn -> sanitize_parse_execute(code) end)
end
end

# ----------------------------------------
Expand Down Expand Up @@ -252,6 +278,34 @@ defmodule Archethic.Contracts.Interpreter.Library.ContractTest do
}
} = sanitize_parse_execute(code)
end

test "should crash if the amount is 0" do
address = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
token_address = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

code = ~s"""
actions triggered_by: transaction do
transfer = [to: "#{Base.encode16(address)}", amount: 0, token_id: 1, token_address: "#{Base.encode16(token_address)}"]
Contract.add_token_transfer transfer
end
"""

assert_raise(ArgumentError, fn -> sanitize_parse_execute(code) end)
end

test "should crash if the amount is negative" do
address = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>
token_address = <<0::8, 0::8, :crypto.strong_rand_bytes(32)::binary>>

code = ~s"""
actions triggered_by: transaction do
transfer = [to: "#{Base.encode16(address)}", amount: -3, token_id: 1, token_address: "#{Base.encode16(token_address)}"]
Contract.add_token_transfer transfer
end
"""

assert_raise(ArgumentError, fn -> sanitize_parse_execute(code) end)
end
end

# ----------------------------------------
Expand Down
28 changes: 28 additions & 0 deletions test/archethic/contracts/interpreter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,34 @@ defmodule Archethic.Contracts.InterpreterTest do
data: %TransactionData{}
}

assert match?(
{:error, :contract_failure},
Interpreter.execute(
:transaction,
Contract.from_transaction!(contract_tx),
incoming_tx,
[incoming_tx]
)
)

code = """
@version 1
condition inherit: [
content: true
]

actions triggered_by: transaction do
Contract.add_uco_transfer amount: -1, to: "0000BFEF73346D20771614449D6BE9C705BF314067A0CF0ACBBF5E617EF5C978D0A1"
end
"""

contract_tx = %Transaction{
type: :contract,
data: %TransactionData{
code: code
}
}

assert match?(
{:error, :contract_failure},
Interpreter.execute(
Expand Down
Loading