Skip to content

Commit

Permalink
Submit tx
Browse files Browse the repository at this point in the history
  • Loading branch information
caike committed Feb 18, 2024
1 parent 3442744 commit ea6a573
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/xogmios.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule Xogmios do
end
"""

alias Xogmios.TxSubmission
alias Xogmios.ChainSync
alias Xogmios.StateQuery

Expand Down Expand Up @@ -77,6 +78,10 @@ defmodule Xogmios do
ChainSync.start_link(client, opts)
end

def start_tx_submission_link(client, opts) do
TxSubmission.start_link(client, opts)
end

defmacro __using__(:state_query) do
quote do
use Xogmios.StateQuery
Expand All @@ -88,4 +93,10 @@ defmodule Xogmios do
use Xogmios.ChainSync
end
end

defmacro __using__(:tx_submission) do
quote do
use Xogmios.TxSubmission
end
end
end
82 changes: 82 additions & 0 deletions lib/xogmios/tx_submission.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
defmodule Xogmios.TxSubmission do
@moduledoc """
This module interfaces with the Tx Submission protocol.
"""

alias Xogmios.TxSubmission.Response
alias Xogmios.TxSubmission.Server

@doc """
Starts a new Tx Submission process linked to the current process.
This function should not be called directly, but rather via `Xogmios.start_tx_submission_link/2`
"""
@spec start_link(module(), start_options :: Keyword.t()) :: GenServer.on_start()
def start_link(client, opts) do
GenServer.start_link(client, opts, name: client)
end

@doc """
Submits a transaction to the server and returns a response.
This function is synchronous.
"""
@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
{:ok, response.result}
end
end

defp build_message(cbor) do
json = ~s"""
{
"jsonrpc": "2.0",
"method": "submitTransaction",
"params": {
"transaction": {
"cbor": "#{cbor}"
}
}
}
"""

{:ok, json}
end

defp call_tx_submission(client, message) do
case GenServer.call(client, {:submit_tx, message}) do
{:ok, response} -> {:ok, response}
{:error, reason} -> {:error, reason}
end
end

defmacro __using__(_opts) do
quote do
use GenServer

## Callbacks

@impl true
def init(args) do
url = Keyword.fetch!(args, :url)

case :websocket_client.start_link(url, Server, []) do
{:ok, ws_pid} ->
{:ok, %{ws_pid: ws_pid, response: nil, caller: nil}}

{:error, _} = error ->
error
end
end

@impl true
def handle_call({:submit_tx, 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}
end
end
end
end
6 changes: 6 additions & 0 deletions lib/xogmios/tx_submission/response.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule Xogmios.TxSubmission.Response do
@moduledoc false
# This module provides a common interface for responses from a Tx Submissions

defstruct [:result]
end
70 changes: 70 additions & 0 deletions lib/xogmios/tx_submission/server.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
defmodule Xogmios.TxSubmission.Server do
@moduledoc false

@behaviour :websocket_client

require Logger

alias Xogmios.TxSubmission.Response

defp handle_message(
%{"method" => _method, "result" => result},
state
) do
GenServer.reply(state.caller, {:ok, %Response{result: result}})
{:ok, state}
end

defp handle_message(_message, state) do
{:ok, state}
end

@impl true
def init(_args) do
{:once, %{caller: nil}}
end

@impl true
def onconnect(_arg0, state) do
{:ok, state}
end

@impl true
def ondisconnect(_reason, state) do
{:ok, state}
end

@impl true
def websocket_handle({:text, raw_message}, _conn, state) do
case Jason.decode(raw_message) do
{:ok, message} ->
handle_message(message, state)

{:error, reason} ->
Logger.warning("Error decoding message #{inspect(reason)}")
{:ok, state}
end
end

@impl true
def websocket_handle(_message, _conn, state) do
{:ok, state}
end

@impl true
def websocket_info({:store_caller, caller}, _req, state) do
# Stores caller of the query so that GenServer.reply knows
# who to return the response to
{:ok, %{state | caller: caller}}
end

@impl true
def websocket_info(_any, _arg1, state) do
{:ok, state}
end

@impl true
def websocket_terminate(_arg0, _arg1, _state) do
:ok
end
end

0 comments on commit ea6a573

Please sign in to comment.