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

JSON RPC methods that used transaction as payload now only takes 1 param #1226

Merged
merged 2 commits into from
Aug 24, 2023
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 @@ -19,8 +19,8 @@ defmodule ArchethicWeb.API.JsonRPC.Method.EstimateTransactionFee do
"""
@spec validate_params(param :: map()) ::
{:ok, params :: Transaction.t()} | {:error, reasons :: list()}
def validate_params(params) do
case TransactionPayload.changeset(params) do
def validate_params(%{"transaction" => transaction_params}) do
case TransactionPayload.changeset(transaction_params) do
bchamagne marked this conversation as resolved.
Show resolved Hide resolved
changeset = %{valid?: true} ->
tx = changeset |> TransactionPayload.to_map() |> Transaction.cast()
{:ok, tx}
Expand All @@ -32,6 +32,8 @@ defmodule ArchethicWeb.API.JsonRPC.Method.EstimateTransactionFee do
end
end

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

@doc """
Execute the function to send a new tranaction in the network
"""
Expand Down
6 changes: 4 additions & 2 deletions lib/archethic_web/api/jsonrpc/methods/send_transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ defmodule ArchethicWeb.API.JsonRPC.Method.SendTransaction do
"""
@spec validate_params(param :: map()) ::
{:ok, params :: Transaction.t()} | {:error, reasons :: list()}
def validate_params(params) do
case TransactionPayload.changeset(params) do
def validate_params(%{"transaction" => transaction_params}) do
case TransactionPayload.changeset(transaction_params) do
changeset = %{valid?: true} ->
tx = changeset |> TransactionPayload.to_map() |> Transaction.cast()
{:ok, tx}
Expand All @@ -32,6 +32,8 @@ defmodule ArchethicWeb.API.JsonRPC.Method.SendTransaction do
end
end

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 @@ -22,8 +22,8 @@ defmodule ArchethicWeb.API.JsonRPC.Method.SimulateContractExecution do
"""
@spec validate_params(param :: map()) ::
{:ok, params :: Transaction.t()} | {:error, reasons :: list()}
def validate_params(params) do
case TransactionPayload.changeset(params) do
def validate_params(%{"transaction" => transaction_params}) do
case TransactionPayload.changeset(transaction_params) do
changeset = %{valid?: true} ->
tx = changeset |> TransactionPayload.to_map() |> Transaction.cast()
{:ok, tx}
Expand All @@ -35,6 +35,8 @@ defmodule ArchethicWeb.API.JsonRPC.Method.SimulateContractExecution do
end
end

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 @@ -88,7 +88,7 @@ defmodule ArchethicWeb.API.JsonRPCControllerTest do
defp valid_rpc_request(opts \\ []) do
id = Keyword.get(opts, :id, 1)
method = Keyword.get(opts, :method, "send_transaction")
params = Keyword.get(opts, :params, valid_tx_param())
params = Keyword.get(opts, :params, %{"transaction" => valid_tx_param()})

%{
"jsonrpc" => "2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ defmodule ArchethicWeb.API.JsonRPC.Methods.EstimateTransactionFeeTest do
end

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

test "should send bad_request response for invalid transaction body" do
assert {:error,
%{
Expand All @@ -54,7 +63,7 @@ defmodule ArchethicWeb.API.JsonRPC.Methods.EstimateTransactionFeeTest do
version: [
"can't be blank"
]
}} = EstimateTransactionFee.validate_params(%{})
}} = EstimateTransactionFee.validate_params(%{"transaction" => %{}})
end
end

Expand Down
11 changes: 10 additions & 1 deletion test/archethic_web/api/jsonrpc/methods/send_transaction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ defmodule ArchethicWeb.API.JsonRPC.Methods.SendTransactionTest do
end

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

test "should send bad_request response for invalid transaction body" do
assert {:error,
%{
Expand All @@ -65,7 +74,7 @@ defmodule ArchethicWeb.API.JsonRPC.Methods.SendTransactionTest do
version: [
"can't be blank"
]
}} = SendTransaction.validate_params(%{})
}} = SendTransaction.validate_params(%{"transaction" => %{}})
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ defmodule ArchethicWeb.API.JsonRPC.Methods.SimulateContractExecutionTest do
end

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

test "should send bad_request response for invalid transaction body" do
assert {:error,
%{
Expand All @@ -63,7 +72,7 @@ defmodule ArchethicWeb.API.JsonRPC.Methods.SimulateContractExecutionTest do
version: [
"can't be blank"
]
}} = SimulateContractExecution.validate_params(%{})
}} = SimulateContractExecution.validate_params(%{"transaction" => %{}})
end
end

Expand Down