Skip to content

Commit

Permalink
Use TransactionSchema in RPC method
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix authored and bchamagne committed Sep 1, 2023
1 parent e975b86 commit 8c7d60d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 121 deletions.
22 changes: 9 additions & 13 deletions lib/archethic_web/api/jsonrpc/methods/estimate_transaction_fee.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ defmodule ArchethicWeb.API.JsonRPC.Method.EstimateTransactionFee do
alias Archethic.TransactionChain.Transaction

alias ArchethicWeb.API.JsonRPC.Method
alias ArchethicWeb.API.TransactionPayload

alias ArchethicWeb.WebUtils
alias ArchethicWeb.API.JsonRPC.TransactionSchema

@behaviour Method

Expand All @@ -20,21 +18,19 @@ defmodule ArchethicWeb.API.JsonRPC.Method.EstimateTransactionFee do
@spec validate_params(param :: map()) ::
{:ok, params :: Transaction.t()} | {:error, reasons :: map()}
def validate_params(%{"transaction" => transaction_params}) do
case TransactionPayload.changeset(transaction_params) do
{:ok, changeset = %{valid?: true}} ->
tx = changeset |> TransactionPayload.to_map() |> Transaction.cast()
{:ok, tx}

{:ok, changeset} ->
reasons = Ecto.Changeset.traverse_errors(changeset, &WebUtils.translate_error/1)
{:error, reasons}
case TransactionSchema.validate(transaction_params) do
:ok ->
{:ok, TransactionSchema.to_transaction(transaction_params)}

:error ->
{:error, %{transaction: ["must be an object"]}}
{:error, %{"transaction" => "Must be an object"}}

{:error, reasons} ->
{:error, reasons}
end
end

def validate_params(_), do: {:error, %{transaction: ["is required"]}}
def validate_params(_), do: {:error, %{"transaction" => "Is required"}}

@doc """
Execute the function to send a new tranaction in the network
Expand Down
22 changes: 9 additions & 13 deletions lib/archethic_web/api/jsonrpc/methods/send_transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ defmodule ArchethicWeb.API.JsonRPC.Method.SendTransaction do
alias Archethic.TransactionChain.Transaction

alias ArchethicWeb.API.JsonRPC.Method
alias ArchethicWeb.API.TransactionPayload
alias ArchethicWeb.API.JsonRPC.TransactionSchema

alias ArchethicWeb.TransactionSubscriber

alias ArchethicWeb.WebUtils

@behaviour Method

@doc """
Expand All @@ -20,21 +18,19 @@ defmodule ArchethicWeb.API.JsonRPC.Method.SendTransaction do
@spec validate_params(param :: map()) ::
{:ok, params :: Transaction.t()} | {:error, reasons :: map()}
def validate_params(%{"transaction" => transaction_params}) do
case TransactionPayload.changeset(transaction_params) do
{:ok, changeset = %{valid?: true}} ->
tx = changeset |> TransactionPayload.to_map() |> Transaction.cast()
{:ok, tx}

{:ok, changeset} ->
reasons = Ecto.Changeset.traverse_errors(changeset, &WebUtils.translate_error/1)
{:error, reasons}
case TransactionSchema.validate(transaction_params) do
:ok ->
{:ok, TransactionSchema.to_transaction(transaction_params)}

:error ->
{:error, %{transaction: ["must be an object"]}}
{:error, %{"transaction" => "Must be an object"}}

{:error, reasons} ->
{:error, reasons}
end
end

def validate_params(_), do: {:error, %{transaction: ["is required"]}}
def validate_params(_), do: {:error, %{"transaction" => "Is required"}}

@doc """
Execute the function to send a new tranaction in the network
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ defmodule ArchethicWeb.API.JsonRPC.Method.SimulateContractExecution do

alias ArchethicWeb.API.JsonRPC.Method
alias ArchethicWeb.API.JsonRPC.Error
alias ArchethicWeb.API.TransactionPayload

alias ArchethicWeb.WebUtils
alias ArchethicWeb.API.JsonRPC.TransactionSchema

@behaviour Method

Expand All @@ -26,21 +24,19 @@ defmodule ArchethicWeb.API.JsonRPC.Method.SimulateContractExecution do
@spec validate_params(param :: map()) ::
{:ok, params :: Transaction.t()} | {:error, reasons :: map()}
def validate_params(%{"transaction" => transaction_params}) do
case TransactionPayload.changeset(transaction_params) do
{:ok, changeset = %{valid?: true}} ->
tx = changeset |> TransactionPayload.to_map() |> Transaction.cast()
{:ok, tx}

{:ok, changeset} ->
reasons = Ecto.Changeset.traverse_errors(changeset, &WebUtils.translate_error/1)
{:error, reasons}
case TransactionSchema.validate(transaction_params) do
:ok ->
{:ok, TransactionSchema.to_transaction(transaction_params)}

:error ->
{:error, %{transaction: ["must be an object"]}}
{:error, %{"transaction" => "Must be an object"}}

{:error, reasons} ->
{:error, reasons}
end
end

def validate_params(_), do: {:error, %{transaction: ["is required"]}}
def validate_params(_), do: {:error, %{"transaction" => "Is required"}}

@doc """
Execute the function to send a new tranaction in the network
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ defmodule ArchethicWeb.API.JsonRPCControllerTest do
"address" => "0000a9f3bc500d0ed7d923e983eafc080113633456f53c400814e1d4f34c5fa67220",
"type" => type,
"data" => %{
"content" => "73616c7574",
"content" => "hello",
"code" => "",
"ownerships" => [],
"ledger" => %{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,15 @@ defmodule ArchethicWeb.API.JsonRPC.Methods.EstimateTransactionFeeTest do

describe "validate_params" do
test "should send error when transaction key is missing" do
assert {:error,
%{
transaction: [
"is required"
]
}} = EstimateTransactionFee.validate_params(%{})
assert {:error, %{"transaction" => "Is required"}} =
EstimateTransactionFee.validate_params(%{})
end

test "should send bad_request response for invalid transaction body" do
assert {:error,
%{
address: [
"can't be blank"
],
data: [
"can't be blank"
],
originSignature: [
"can't be blank"
],
previousPublicKey: [
"can't be blank"
],
previousSignature: [
"can't be blank"
],
type: [
"can't be blank"
],
version: [
"can't be blank"
]
"#" =>
"Required properties version, address, type, previousPublicKey, previousSignature, originSignature, data were not present."
}} = EstimateTransactionFee.validate_params(%{"transaction" => %{}})
end
end
Expand Down
30 changes: 3 additions & 27 deletions test/archethic_web/api/jsonrpc/methods/send_transaction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,14 @@ defmodule ArchethicWeb.API.JsonRPC.Methods.SendTransactionTest do

describe "validate_params" do
test "should send error when transaction key is missing" do
assert {:error,
%{
transaction: [
"is required"
]
}} = SendTransaction.validate_params(%{})
assert {:error, %{"transaction" => "Is required"}} = SendTransaction.validate_params(%{})
end

test "should send bad_request response for invalid transaction body" do
assert {:error,
%{
address: [
"can't be blank"
],
data: [
"can't be blank"
],
originSignature: [
"can't be blank"
],
previousPublicKey: [
"can't be blank"
],
previousSignature: [
"can't be blank"
],
type: [
"can't be blank"
],
version: [
"can't be blank"
]
"#" =>
"Required properties version, address, type, previousPublicKey, previousSignature, originSignature, data were not present."
}} = SendTransaction.validate_params(%{"transaction" => %{}})
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,15 @@ defmodule ArchethicWeb.API.JsonRPC.Methods.SimulateContractExecutionTest do

describe "validate_params" do
test "should send error when transaction key is missing" do
assert {:error,
%{
transaction: [
"is required"
]
}} = SimulateContractExecution.validate_params(%{})
assert {:error, %{"transaction" => "Is required"}} =
SimulateContractExecution.validate_params(%{})
end

test "should send bad_request response for invalid transaction body" do
assert {:error,
%{
address: [
"can't be blank"
],
data: [
"can't be blank"
],
originSignature: [
"can't be blank"
],
previousPublicKey: [
"can't be blank"
],
previousSignature: [
"can't be blank"
],
type: [
"can't be blank"
],
version: [
"can't be blank"
]
"#" =>
"Required properties version, address, type, previousPublicKey, previousSignature, originSignature, data were not present."
}} = SimulateContractExecution.validate_params(%{"transaction" => %{}})
end
end
Expand Down

0 comments on commit 8c7d60d

Please sign in to comment.