Skip to content

Commit

Permalink
Add evaluate tx
Browse files Browse the repository at this point in the history
  • Loading branch information
caike committed Feb 22, 2024
1 parent 23c3864 commit ed8cdc2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
31 changes: 25 additions & 6 deletions lib/xogmios/tx_submission.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
17 changes: 17 additions & 0 deletions lib/xogmios/tx_submission/messages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ed8cdc2

Please sign in to comment.