Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 11 additions & 1 deletion lib/fester/indexer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@ defmodule Fester.Indexer do
alias Fester.Utxo

@doc """
Returns the list of utxos for a given address.
Returns the list of utxos for a given address
optimized for consumption by blaze-query getUnspentOutputs
"""
def list_utxos_by_address(address) do
from(u in Utxo, where: u.address == ^address and is_nil(u.consumed_at_slot))
|> Repo.all()
|> Enum.reduce(%{data: []}, &build_response/2)
end

defp build_response(%Utxo{utxo_ref: utxo_ref, txout_cbor: cbor}, acc) do
[tx_hash, index] = String.split(utxo_ref, "#")
data = Map.get(acc, :data)
data = [%{tx_hash: tx_hash, index: index, txout_cbor: cbor} | data]

Map.put(acc, :data, data)
end

@doc """
Expand Down
12 changes: 7 additions & 5 deletions lib/fester/indexer/db_indexer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ defmodule Fester.DBIndexer do
) do
Logger.info("Processing transaction with collaterals")

%{"collaterals" => collaterals_as_inputs} = transaction
%{"collaterals" => collaterals_as_inputs, "cbor" => cbor} = transaction

# Wrapping the collateral return in a list to make it consistent with the
# other calling of the process_transaction_outputs function.
collateral_return_as_outputs =
if transaction["collateral_return"], do: [transaction["collateral_return"]], else: []

with :ok <- process_transaction_inputs(slot, collaterals_as_inputs),
:ok <- process_transaction_outputs(slot, collateral_return_as_outputs, tx_id) do
:ok <- process_transaction_outputs(slot, collateral_return_as_outputs, tx_id, cbor) do
:ok
else
{:error, reason} ->
Expand All @@ -95,11 +95,12 @@ defmodule Fester.DBIndexer do
%{
"id" => tx_id,
"inputs" => inputs,
"outputs" => outputs
"outputs" => outputs,
"cbor" => cbor
} = transaction

with :ok <- process_transaction_inputs(slot, inputs),
:ok <- process_transaction_outputs(slot, outputs, tx_id) do
:ok <- process_transaction_outputs(slot, outputs, tx_id, cbor) do
:ok
else
{:error, reason} ->
Expand Down Expand Up @@ -139,7 +140,7 @@ defmodule Fester.DBIndexer do
:ok
end

defp process_transaction_outputs(slot, outputs, tx_id) do
defp process_transaction_outputs(slot, outputs, tx_id, cbor) do
utxo_attrs_list =
outputs
|> Enum.with_index()
Expand All @@ -151,6 +152,7 @@ defmodule Fester.DBIndexer do
utxo_ref: output_ref,
address: address,
value: value,
txout_cbor: cbor,
created_at_slot: slot
}
end)
Expand Down
1 change: 1 addition & 0 deletions lib/fester/utxo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Fester.Utxo do
field :created_at_slot, :integer
field :consumed_at_slot, :integer
field :value, :map
field :txout_cbor, :string

timestamps()
end
Expand Down
11 changes: 11 additions & 0 deletions lib/fester_web/controllers/utxos_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule FesterWeb.UtxosController do
use FesterWeb, :controller

alias Fester.Indexer

def index(conn, %{"address" => address}) do
utxos = Indexer.list_utxos_by_address(address)

json(conn, utxos)
end
end
1 change: 1 addition & 0 deletions lib/fester_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule FesterWeb.Router do

# Lists assets for an address
get "/address/:address/assets", AssetsController, :index
get "/address/:address/utxos", UtxosController, :index
end

# Enable Swoosh mailbox preview in development
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Fester.Repo.Migrations.AddTxoutCborToUtxos do
use Ecto.Migration

def change do
alter table(:utxos) do
add :txout_cbor, :text, null: false
end
end
end