diff --git a/lib/xogmios/tx_submission.ex b/lib/xogmios/tx_submission.ex index 88c8941..330bbc8 100644 --- a/lib/xogmios/tx_submission.ex +++ b/lib/xogmios/tx_submission.ex @@ -29,18 +29,37 @@ defmodule Xogmios.TxSubmission do """ @spec submit_tx(pid() | atom(), String.t()) :: {:ok, any()} | {:error, any()} def submit_tx(client \\ __MODULE__, cbor) do - with {:ok, message} <- build_message(cbor), - {:ok, %Response{} = response} <- call_tx_submission(client, message) do + with {:ok, message} <- build_submit_message(cbor), + {:ok, %Response{} = response} <- call(client, message) do {:ok, response.result} end end - defp build_message(cbor), + @doc """ + Evaluates the execution units of scripts present in a given transaction. + + This function is synchronous and takes two arguments: + + 1. (Optional) A process reference. If none given, it defaults to the linked process `__MODULE__`. + 2. The CBOR of a transaction. Unlike `submit_tx/1`, this function does not expect the transaction to be signed. Please refer to the [official Ogmios docs](https://ogmios.dev/mini-protocols/local-tx-submission/#evaluating-transactions) for more details on the type of transaction that is accepted. + """ + @spec evaluate_tx(pid() | atom(), String.t()) :: {:ok, any()} | {:error, any()} + def evaluate_tx(client \\ __MODULE__, cbor) do + with {:ok, message} <- build_evaluate_message(cbor), + {:ok, %Response{} = response} <- call(client, message) do + {:ok, response.result} + end + end + + defp build_submit_message(cbor), do: {:ok, Messages.submit_tx(cbor)} - defp call_tx_submission(client, message) do + defp build_evaluate_message(cbor), + do: {:ok, Messages.evaluate_tx(cbor)} + + defp call(client, message) do try do - case GenServer.call(client, {:submit_tx, message}, @tx_submit_timeout) do + case GenServer.call(client, {:send, message}, @tx_submit_timeout) do {:ok, response} -> {:ok, response} {:error, reason} -> {:error, reason} end @@ -69,7 +88,7 @@ defmodule Xogmios.TxSubmission do end @impl true - def handle_call({:submit_tx, message}, from, state) do + def handle_call({:send, message}, from, state) do {:store_caller, _from} = send(state.ws_pid, {:store_caller, from}) :ok = :websocket_client.send(state.ws_pid, {:text, message}) {:noreply, state} diff --git a/lib/xogmios/tx_submission/messages.ex b/lib/xogmios/tx_submission/messages.ex index 4c82726..7529655 100644 --- a/lib/xogmios/tx_submission/messages.ex +++ b/lib/xogmios/tx_submission/messages.ex @@ -25,6 +25,23 @@ defmodule Xogmios.TxSubmission.Messages do json end + def evaluate_tx(cbor) do + json = ~s""" + { + "jsonrpc": "2.0", + "method": "evaluateTransaction", + "params": { + "transaction": { + "cbor": "#{cbor}" + } + } + } + """ + + validate_json!(json) + json + end + defp validate_json!(json) do case Jason.decode(json) do {:ok, _decoded} -> :ok