mirror of
https://github.com/Retropex/bitfeed.git
synced 2025-05-28 13:02:28 +02:00
Separate donation logic from tx streaming server
This commit is contained in:
parent
04e63cc6b0
commit
12118c93a3
2
server/.gitignore
vendored
2
server/.gitignore
vendored
@ -29,3 +29,5 @@ bitcoin_stream-*.tar
|
|||||||
# Logging files
|
# Logging files
|
||||||
/log/
|
/log/
|
||||||
*.log
|
*.log
|
||||||
|
|
||||||
|
.envrc
|
||||||
|
@ -1,94 +0,0 @@
|
|||||||
Application.ensure_all_started(:hackney)
|
|
||||||
|
|
||||||
defmodule BitcoinStream.Donations.Lightning do
|
|
||||||
@moduledoc """
|
|
||||||
Module for handling Bitcoin lightning invoices for donations
|
|
||||||
"""
|
|
||||||
|
|
||||||
use GenServer
|
|
||||||
|
|
||||||
alias BitcoinStream.Protocol.Block, as: BitcoinBlock
|
|
||||||
alias BitcoinStream.Protocol.Transaction, as: BitcoinTx
|
|
||||||
alias BitcoinStream.Mempool, as: Mempool
|
|
||||||
|
|
||||||
def child_spec() do
|
|
||||||
%{
|
|
||||||
id: BitcoinStream.Donations.Lightning,
|
|
||||||
start: {BitcoinStream.Donations.Lightning, :start_link, []}
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
@doc """
|
|
||||||
Start a new lightning handler agent
|
|
||||||
"""
|
|
||||||
def start_link() do
|
|
||||||
IO.puts("Starting Lightning Handler")
|
|
||||||
GenServer.start_link(__MODULE__, %{})
|
|
||||||
end
|
|
||||||
|
|
||||||
def init(arg) do
|
|
||||||
{:ok, arg}
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_invoice(id) do
|
|
||||||
btcpay_root = System.get_env("BTCPAY_ROOT");
|
|
||||||
btcpay_store_id = System.get_env("BTCPAY_STORE_ID")
|
|
||||||
api_route = "#{btcpay_root}/api/v1/stores/#{btcpay_store_id}/lightning/btc/invoices/#{id}"
|
|
||||||
btcpay_key = System.get_env("BTCPAY_KEY");
|
|
||||||
IO.puts("Getting Lightning invoice #{id}");
|
|
||||||
with {:ok, 200, _headers, body_ref} <- :hackney.request(
|
|
||||||
:get,
|
|
||||||
api_route,
|
|
||||||
[
|
|
||||||
{"authorization", "token #{btcpay_key}"}
|
|
||||||
]
|
|
||||||
),
|
|
||||||
{:ok, body} <- :hackney.body(body_ref) do
|
|
||||||
|
|
||||||
{:ok, body}
|
|
||||||
else
|
|
||||||
{:error, reason} ->
|
|
||||||
IO.puts("Lightning API failed");
|
|
||||||
IO.inspect(reason)
|
|
||||||
:error
|
|
||||||
_ ->
|
|
||||||
IO.puts("Lightning API failed: (unknown reason)");
|
|
||||||
:error
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_invoice(amount) do
|
|
||||||
millisatoshis = amount * 1000
|
|
||||||
btcpay_root = System.get_env("BTCPAY_ROOT");
|
|
||||||
btcpay_store_id = System.get_env("BTCPAY_STORE_ID")
|
|
||||||
api_route = "#{btcpay_root}/api/v1/stores/#{btcpay_store_id}/lightning/btc/invoices"
|
|
||||||
btcpay_key = System.get_env("BTCPAY_KEY");
|
|
||||||
IO.puts("Creating Lightning invoice for #{amount}");
|
|
||||||
with {:ok, api_request} <- Jason.encode(%{
|
|
||||||
amount: "#{millisatoshis}",
|
|
||||||
description: "Bitfeed donation",
|
|
||||||
expiry: 600
|
|
||||||
}),
|
|
||||||
{:ok, 200, _headers, body_ref} <- :hackney.request(
|
|
||||||
:post,
|
|
||||||
api_route,
|
|
||||||
[
|
|
||||||
{"authorization", "token #{btcpay_key}"},
|
|
||||||
{"content-type", "application/json"}
|
|
||||||
],
|
|
||||||
api_request
|
|
||||||
),
|
|
||||||
{:ok, body} <- :hackney.body(body_ref) do
|
|
||||||
|
|
||||||
{:ok, body}
|
|
||||||
else
|
|
||||||
{:error, reason} ->
|
|
||||||
IO.puts("Lightning API failed");
|
|
||||||
IO.inspect(reason)
|
|
||||||
:error
|
|
||||||
_ ->
|
|
||||||
IO.puts("Lightning API failed: (unknown reason)");
|
|
||||||
:error
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
@ -14,35 +14,6 @@ defmodule BitcoinStream.Router do
|
|||||||
json_decoder: Jason
|
json_decoder: Jason
|
||||||
plug :dispatch
|
plug :dispatch
|
||||||
|
|
||||||
match "/api/lightning/invoice/:id", via: :get do
|
|
||||||
with {:ok, result} <- Lightning.get_invoice(id) do
|
|
||||||
send_resp(conn, 200, result)
|
|
||||||
else
|
|
||||||
{:error, reason} ->
|
|
||||||
IO.puts("Invoice retrieval failed");
|
|
||||||
IO.inspect(reason)
|
|
||||||
send_resp(conn, 500, "?!?")
|
|
||||||
_ ->
|
|
||||||
IO.puts("Invoice retrieval failed: (unknown reason)");
|
|
||||||
send_resp(conn, 500, "!?!")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
post "/api/lightning/invoice" do
|
|
||||||
with %{"amount" => amount} <- conn.body_params,
|
|
||||||
{:ok, result} <- Lightning.create_invoice(amount) do
|
|
||||||
send_resp(conn, 200, result)
|
|
||||||
else
|
|
||||||
{:error, reason} ->
|
|
||||||
IO.puts("Invoice creation failed");
|
|
||||||
IO.inspect(reason)
|
|
||||||
send_resp(conn, 500, "?!?")
|
|
||||||
_ ->
|
|
||||||
IO.puts("Invoice creation failed: (unknown reason)");
|
|
||||||
send_resp(conn, 500, "!?!")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
match _ do
|
match _ do
|
||||||
send_resp(conn, 404, "404")
|
send_resp(conn, 404, "404")
|
||||||
end
|
end
|
||||||
|
@ -18,8 +18,7 @@ defmodule BitcoinStream.Server do
|
|||||||
keys: :duplicate,
|
keys: :duplicate,
|
||||||
name: Registry.BitcoinStream
|
name: Registry.BitcoinStream
|
||||||
),
|
),
|
||||||
BitcoinStream.Bridge.child_spec(port: 29000),
|
BitcoinStream.Bridge.child_spec(port: 29000)
|
||||||
BitcoinStream.Donations.Lightning.child_spec()
|
|
||||||
]
|
]
|
||||||
|
|
||||||
opts = [strategy: :one_for_one, name: BitcoinStream.Application]
|
opts = [strategy: :one_for_one, name: BitcoinStream.Application]
|
||||||
|
Loading…
Reference in New Issue
Block a user