-
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
10 changed files
with
389 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
alias Satana.ETHTransactions |
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,52 @@ | ||
defmodule Satana.ETHTransactions do | ||
alias Satana.Blocknative | ||
alias Satana.ETHTransactions.Store | ||
alias Satana.ETHTransactions.Transaction | ||
alias Satana.Slack | ||
|
||
require Logger | ||
|
||
@spec list_transactions_by_status(String.t()) :: [Transaction.t()] | ||
def list_transactions_by_status(status) do | ||
Store.list_transactions() | ||
|> Enum.filter(&(&1.status == status)) | ||
end | ||
|
||
@spec add_eth_transaction(String.t()) :: :ok | {:error, String.t()} | ||
def add_eth_transaction(tx_id) do | ||
with :ok <- Store.add_transaction(tx_id, handle_transaction_in: {2, :minutes}, with: &handle_transaction/1), | ||
:ok <- Blocknative.add_eth_transaction_to_watch(tx_id) do | ||
text = "Transaction `#{tx_id}` has been registered :sunglasses:" | ||
Slack.send_message(text) | ||
|
||
:ok | ||
else | ||
{:error, :already_exists} -> | ||
{:error, "transaction #{tx_id} already exists"} | ||
|
||
{:error, msg} -> | ||
Logger.warn(fn -> "Rollback transaction #{tx_id} - #{msg}" end) | ||
|
||
Store.delete_transaction(tx_id) | ||
|
||
{:error, msg} | ||
end | ||
end | ||
|
||
defp handle_transaction(%Transaction{} = transaction) do | ||
if Transaction.pending?(transaction) do | ||
text = "It's been a while but transaction `#{transaction.tx_id}` is still pending :thinking_face:" | ||
Slack.send_message(text) | ||
end | ||
end | ||
|
||
@spec confirm_transaction!(String.t()) :: :ok | ||
def confirm_transaction!(tx_id) do | ||
:ok = Store.update_transaction(tx_id, &Transaction.confirm/1) | ||
|
||
text = "Transaction `#{tx_id}` has been confirmed :tada:" | ||
Slack.send_message(text) | ||
|
||
:ok | ||
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,95 @@ | ||
defmodule Satana.ETHTransactions.Store do | ||
use GenServer | ||
|
||
alias Satana.ETHTransactions.Transaction | ||
|
||
require Logger | ||
|
||
@spec start_link(Keyword.t()) :: GenServer.on_start() | ||
def start_link(opts \\ []) do | ||
GenServer.start_link(__MODULE__, opts, name: __MODULE__) | ||
end | ||
|
||
@spec add_transaction(String.t(), keyword()) :: :ok | {:error, :already_exists} | ||
def add_transaction(tx_id, handle_transaction_in: {num, unit}, with: callback_function) | ||
when unit in [:seconds, :minutes] and is_function(callback_function, 1) do | ||
GenServer.call(__MODULE__, {:add_new_transaction, tx_id, {num, unit}, callback_function}) | ||
end | ||
|
||
@spec delete_transaction(String.t()) :: :ok | ||
def delete_transaction(tx_id) do | ||
GenServer.call(__MODULE__, {:delete_transaction, tx_id}) | ||
end | ||
|
||
@spec update_transaction(String.t(), fun()) :: :ok | {:error, :not_found} | ||
def update_transaction(tx_id, update_function) when is_function(update_function, 1) do | ||
GenServer.call(__MODULE__, {:update_transaction, tx_id, update_function}) | ||
end | ||
|
||
@spec list_transactions :: [Transaction.t()] | ||
def list_transactions do | ||
GenServer.call(__MODULE__, :list_transactions) | ||
end | ||
|
||
## GenServer callbacks | ||
|
||
@impl GenServer | ||
def init(opts) do | ||
Logger.info(fn -> "Running #{__MODULE__}" end) | ||
initial_state = opts[:initial_state] || [] | ||
{:ok, initial_state} | ||
end | ||
|
||
@impl GenServer | ||
def handle_call({:add_new_transaction, tx_id, {num, unit}, callback_function}, _from, current_transactions) do | ||
if Enum.find(current_transactions, &(&1.tx_id == tx_id)) do | ||
{:reply, {:error, :already_exists}, current_transactions} | ||
else | ||
time = apply(:timer, unit, [num]) | ||
Process.send_after(self(), {:check_transaction_status, tx_id, callback_function}, time) | ||
new_transaction = Transaction.new(tx_id) | ||
|
||
{:reply, :ok, [new_transaction | current_transactions]} | ||
end | ||
end | ||
|
||
@impl GenServer | ||
def handle_call({:delete_transaction, tx_id}, _from, current_transactions) do | ||
index = Enum.find_index(current_transactions, &(&1.tx_id == tx_id)) | ||
|
||
if index do | ||
updated_transactions = List.delete_at(current_transactions, index) | ||
|
||
{:reply, :ok, updated_transactions} | ||
else | ||
{:reply, :ok, current_transactions} | ||
end | ||
end | ||
|
||
@impl GenServer | ||
def handle_call({:update_transaction, tx_id, update_function}, _from, current_transactions) do | ||
index = Enum.find_index(current_transactions, &(&1.tx_id == tx_id)) | ||
|
||
if index do | ||
updated_transactions = List.update_at(current_transactions, index, update_function) | ||
|
||
{:reply, :ok, updated_transactions} | ||
else | ||
{:reply, {:error, :not_found}, current_transactions} | ||
end | ||
end | ||
|
||
@impl GenServer | ||
def handle_call(:list_transactions, _from, transactions) do | ||
{:reply, transactions, transactions} | ||
end | ||
|
||
@impl GenServer | ||
def handle_info({:check_transaction_status, tx_id, callback_function}, transactions) do | ||
transaction = Enum.find(transactions, &(&1.tx_id == tx_id)) | ||
|
||
if transaction, do: callback_function.(transaction) | ||
|
||
{:noreply, transactions} | ||
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,28 @@ | ||
defmodule Satana.ETHTransactions.Transaction do | ||
@enforce_keys [:tx_id, :status] | ||
|
||
defstruct @enforce_keys | ||
|
||
@type t :: %__MODULE__{ | ||
tx_id: String.t(), | ||
status: String.t() | ||
} | ||
|
||
@spec new(String.t()) :: t() | ||
def new(tx_id) do | ||
%__MODULE__{ | ||
tx_id: tx_id, | ||
status: "pending" | ||
} | ||
end | ||
|
||
@spec confirm(t()) :: t() | ||
def confirm(%__MODULE__{} = transaction) do | ||
%{transaction | status: "confirmed"} | ||
end | ||
|
||
@spec pending?(t()) :: boolean() | ||
def pending?(%__MODULE__{status: status}) do | ||
status == "pending" | ||
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
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,97 @@ | ||
defmodule Satana.ETHTransactions.StoreTest do | ||
use ExUnit.Case | ||
|
||
alias Satana.ETHTransactions.Store | ||
alias Satana.ETHTransactions.Transaction | ||
|
||
defmodule Messenger do | ||
require Logger | ||
|
||
def send_message(text) do | ||
Logger.info(text) | ||
end | ||
end | ||
|
||
setup do | ||
start_supervised!(Store) | ||
|
||
:ok | ||
end | ||
|
||
test "behaves as expected" do | ||
{:ok, agent_pid} = Agent.start_link(fn -> [] end) | ||
|
||
tx_id1 = "0x1ded29a6bc74abbcc51313fc261b2f97c7f1fc5b9adbbdbaca00ac959f519e7b" | ||
tx_id2 = "0xeb84a81a973187ff9d79934723eb801c400ed941a2a34215ddb202db152e4b84" | ||
tx_id3 = "0x58c9efa287cedee606bfe264898fc24b22ca4e824883585962d1d0ad1140dfd4" | ||
|
||
callback_function = fn %Transaction{} = transaction -> | ||
if Transaction.pending?(transaction) do | ||
Agent.update(agent_pid, fn state -> [transaction.tx_id | state] end) | ||
end | ||
end | ||
|
||
# check initial state | ||
assert Store.list_transactions() == [] | ||
|
||
# add some transactions | ||
assert Store.add_transaction(tx_id1, | ||
handle_transaction_in: {2, :seconds}, | ||
with: callback_function | ||
) == :ok | ||
|
||
assert Store.add_transaction(tx_id2, | ||
handle_transaction_in: {2, :seconds}, | ||
with: callback_function | ||
) == :ok | ||
|
||
assert Store.add_transaction(tx_id3, | ||
handle_transaction_in: {2, :seconds}, | ||
with: callback_function | ||
) == :ok | ||
|
||
assert Store.add_transaction(tx_id1, | ||
handle_transaction_in: {2, :seconds}, | ||
with: callback_function | ||
) == {:error, :already_exists} | ||
|
||
# check state immediately after transactions added | ||
assert Store.list_transactions() == [ | ||
%Transaction{ | ||
tx_id: tx_id3, | ||
status: "pending" | ||
}, | ||
%Transaction{ | ||
tx_id: tx_id2, | ||
status: "pending" | ||
}, | ||
%Transaction{ | ||
tx_id: tx_id1, | ||
status: "pending" | ||
} | ||
] | ||
|
||
# modify state | ||
assert Store.update_transaction(tx_id1, &Transaction.confirm/1) == :ok | ||
assert Store.update_transaction("0x123", &Transaction.confirm/1) == {:error, :not_found} | ||
assert Store.delete_transaction(tx_id3) == :ok | ||
|
||
# wait for callback function to be called | ||
:timer.sleep(2_500) | ||
|
||
# check current state after state modification | ||
assert Store.list_transactions() == [ | ||
%Transaction{ | ||
tx_id: tx_id2, | ||
status: "pending" | ||
}, | ||
%Transaction{ | ||
tx_id: tx_id1, | ||
status: "confirmed" | ||
} | ||
] | ||
|
||
# check if callback function is called | ||
assert Agent.get(agent_pid, & &1) == [tx_id2] | ||
end | ||
end |
Oops, something went wrong.