-
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
3 changed files
with
95 additions
and
1 deletion.
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,50 @@ | ||
defmodule SatanaWeb.API.TransactionController do | ||
use SatanaWeb, :controller | ||
|
||
alias Satana.ETHTransactions | ||
|
||
@valid_statuses ~w(pending confirmed) | ||
|
||
def list_transactions(conn, %{"status" => status}) when status in @valid_statuses do | ||
transactions = | ||
status | ||
|> ETHTransactions.list_transactions_by_status() | ||
|> Enum.map(&Map.from_struct/1) | ||
|
||
conn | ||
|> put_status(:ok) | ||
|> json(%{transactions: transactions}) | ||
end | ||
|
||
def add_transaction(conn, %{"tx_ids" => tx_ids}) when is_list(tx_ids) do | ||
results = Enum.map(tx_ids, &add_eth_transaction/1) | ||
|
||
conn | ||
|> put_status(:ok) | ||
|> json(%{results: results}) | ||
end | ||
|
||
def add_transaction(conn, _) do | ||
conn | ||
|> put_status(:bad_request) | ||
|> json(%{message: "invalid params"}) | ||
end | ||
|
||
defp add_eth_transaction(tx_id) do | ||
case ETHTransactions.add_eth_transaction(tx_id) do | ||
:ok -> | ||
build_response(tx_id, true, nil) | ||
|
||
{:error, msg} -> | ||
build_response(tx_id, false, msg) | ||
end | ||
end | ||
|
||
defp build_response(tx_id, success, error_message) do | ||
%{ | ||
tx_id: tx_id, | ||
success: success, | ||
error_message: error_message | ||
} | ||
end | ||
end |
35 changes: 35 additions & 0 deletions
35
lib/satana_web/controllers/webhook/blocknative_controller.ex
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,35 @@ | ||
defmodule SatanaWeb.Webhook.BlocknativeController do | ||
use SatanaWeb, :controller | ||
|
||
alias Satana.ETHTransactions | ||
|
||
require Logger | ||
|
||
plug :basic_auth | ||
|
||
def handle_webhook(conn, %{"hash" => tx_id, "status" => "confirmed"}) do | ||
:ok = ETHTransactions.confirm_transaction!(tx_id) | ||
|
||
conn | ||
|> put_status(:ok) | ||
|> json(%{}) | ||
end | ||
|
||
def handle_webhook(conn, params) do | ||
Logger.info(fn -> inspect(params) end) | ||
|
||
conn | ||
|> put_status(:ok) | ||
|> json(%{}) | ||
end | ||
|
||
defp basic_auth(conn, _opts) do | ||
config = Satana.Blocknative.Config.new() | ||
|
||
Plug.BasicAuth.basic_auth( | ||
conn, | ||
username: config.basic_auth.username, | ||
password: config.basic_auth.password | ||
) | ||
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