Skip to content

Commit

Permalink
style: cleanup
Browse files Browse the repository at this point in the history
Refactor usages of Repo so it is consistent with other modules.
  • Loading branch information
pik694 committed Oct 8, 2018
1 parent 8bb84df commit 6b44571
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
15 changes: 7 additions & 8 deletions apps/omg_watcher/lib/db/transaction_db.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ defmodule OMG.Watcher.DB.Transaction do

alias OMG.API.State.Transaction
alias OMG.API.Utxo
alias OMG.Watcher.DB.Repo
alias OMG.Watcher.DB

require Utxo
Expand Down Expand Up @@ -49,15 +48,15 @@ defmodule OMG.Watcher.DB.Transaction do

def get(hash) do
__MODULE__
|> Repo.get(hash)
|> DB.Repo.get(hash)
end

def get_by_blknum(blknum) do
Repo.all(from(__MODULE__, where: [blknum: ^blknum]))
DB.Repo.all(from(__MODULE__, where: [blknum: ^blknum]))
end

def get_by_position(blknum, txindex) do
Repo.one(from(__MODULE__, where: [blknum: ^blknum, txindex: ^txindex]))
DB.Repo.one(from(__MODULE__, where: [blknum: ^blknum, txindex: ^txindex]))
end

@spec get_tx_output(Utxo.Position.t()) :: map() | nil
Expand All @@ -70,7 +69,7 @@ defmodule OMG.Watcher.DB.Transaction do
preload: [outputs: o]
)

Repo.one(query)
DB.Repo.one(query)
end

@doc """
Expand Down Expand Up @@ -103,23 +102,23 @@ defmodule OMG.Watcher.DB.Transaction do
inputs: DB.TxOutput.get_inputs(raw_tx),
outputs: DB.TxOutput.create_outputs(raw_tx)
}
|> Repo.insert()
|> DB.Repo.insert()
end

@spec get_transaction_challenging_utxo(Utxo.Position.t()) :: {:ok, %__MODULE__{}} | {:error, :utxo_not_spent}
def get_transaction_challenging_utxo(position) do
# finding tx's input can be tricky
input =
DB.TxOutput.get_by_position(position)
|> Repo.preload([:spending_transaction])
|> DB.Repo.preload([:spending_transaction])

case input && input.spending_transaction do
nil ->
{:error, :utxo_not_spent}

tx ->
# transaction which spends output specified by position with outputs it created
tx = %__MODULE__{(tx |> Repo.preload([:outputs])) | inputs: [input]}
tx = %__MODULE__{(tx |> DB.Repo.preload([:outputs])) | inputs: [input]}

{:ok, tx}
end
Expand Down
9 changes: 4 additions & 5 deletions apps/omg_watcher/lib/db/txoutput_db.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ defmodule OMG.Watcher.DB.TxOutput do
alias OMG.API.State.Transaction
alias OMG.API.Utxo
alias OMG.Watcher.DB
alias OMG.Watcher.DB.Repo

require Utxo

Expand Down Expand Up @@ -75,7 +74,7 @@ defmodule OMG.Watcher.DB.TxOutput do
}
end

def get_all, do: Repo.all(__MODULE__)
def get_all, do: DB.Repo.all(__MODULE__)

@spec get_by_position(Utxo.Position.t()) :: map() | nil
def get_by_position(Utxo.position(blknum, _, _) = position) do
Expand All @@ -99,7 +98,7 @@ defmodule OMG.Watcher.DB.TxOutput do
preload: [:created_utxo]
)

deposit = Repo.one(query)
deposit = DB.Repo.one(query)
deposit && deposit.created_utxo
end

Expand All @@ -111,7 +110,7 @@ defmodule OMG.Watcher.DB.TxOutput do
preload: [:creating_transaction, :deposit]
)

Repo.all(query)
DB.Repo.all(query)
end

def get_balance(owner) do
Expand All @@ -123,7 +122,7 @@ defmodule OMG.Watcher.DB.TxOutput do
select: {t.currency, sum(t.amount)}
)

Repo.all(query)
DB.Repo.all(query)
|> Enum.map(fn {currency, amount} ->
# defends against sqlite that returns integer here
amount = amount |> Decimal.new() |> Decimal.to_integer()
Expand Down
2 changes: 1 addition & 1 deletion apps/omg_watcher/test/support/web/data_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule OMG.Watcher.DataCase do

using do
quote do
alias OMG.Watcher.DB.Repo
alias OMG.Watcher.DB

import Ecto
import Ecto.Changeset
Expand Down

0 comments on commit 6b44571

Please sign in to comment.