-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |