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: Cast to atom the transaction.type #1014

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion lib/archethic/contracts/contract/constants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Archethic.Contracts.ContractConstants do

@doc """
Extract constants from a transaction into a map
This is a destructive operation. Some fields are not present in the resulting map.
"""
@spec from_transaction(Transaction.t()) :: map()
def from_transaction(%Transaction{
Expand Down Expand Up @@ -98,7 +99,7 @@ defmodule Archethic.Contracts.ContractConstants do
def to_transaction(constants) do
%Transaction{
address: Map.get(constants, "address"),
type: Map.get(constants, "type"),
type: String.to_existing_atom(Map.get(constants, "type")),
data: %TransactionData{
code: Map.get(constants, "code", ""),
content: Map.get(constants, "content", ""),
Expand Down
28 changes: 28 additions & 0 deletions test/archethic/contracts/contract/constants_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Archethic.Contracts.ContractConstantsTest do
use ArchethicCase

alias Archethic.TransactionFactory
alias Archethic.TransactionChain.Transaction
alias Archethic.Contracts.ContractConstants

test "from_transaction/1 should return a map" do
tx = TransactionFactory.create_valid_transaction()

constant =
tx
|> ContractConstants.from_transaction()

assert %{"type" => "transfer"} = constant
end

test "to_transaction/1 should return a transaction" do
tx = TransactionFactory.create_valid_transaction()

# from_transaction/1 is a destructive function, we can't check
# that result is equal to tx
assert %Transaction{type: :transfer} =
tx
|> ContractConstants.from_transaction()
|> ContractConstants.to_transaction()
end
end
Loading