Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mock invoice + lnbits invoice #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
LN_BITS_API_ENDPOINT=https://your-lnbits-node
LN_BITS_API_KEY=ADMIN_API_KEY
LN_BITS_API_KEY=ADMIN_API_KEY
MOCK_LN=true
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ Launch your Cashubrew server with a single command:
mix phx.server
```

## Environnement vars for the backend

Create an .env to setup your LN_BITS or Mock LN mode
```
LN_BITS_API_ENDPOINT=https://your-lnbits-node
LN_BITS_API_KEY=ADMIN_API_KEY
MOCK_LN=true

```

🌈 Voilà! Your mint is now live at `http://localhost:4000`.

## 🧙‍♂️ Wallet CLI Magic
Expand Down
7 changes: 7 additions & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ config :cashubrew, Cashubrew.Repo,
config :cashubrew, :repo, Cashubrew.Repo

config :cashubrew, ecto_repos: [Cashubrew.Repo]

# Use mock repo if MOCK_LN environment variable is set to "true"
if System.get_env("MOCK_LN") == "true" do
config :cashubrew, :ln, Cashubrew.Lightning.MockLightningNetworkService
else
config :cashubrew, :ln, Cashubrew.Lightning.LightningNetworkService
end
7 changes: 7 additions & 0 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ config :cashubrew, Cashubrew.Web.Endpoint, secret_key_base: System.get_env("SECR

# Do not print debug messages in production
config :logger, level: :info

# Use mock ln if MOCK_LN environment variable is set to "true"
if System.get_env("MOCK_LN") == "true" do
config :cashubrew, :ln, Cashubrew.Lightning.MockLightningNetworkService
else
config :cashubrew, :ln, Cashubrew.Lightning.LightningNetworkService
end
7 changes: 7 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ else

config :cashubrew, :repo, Cashubrew.Repo
end

# Use mock LN if MOCK_LN environment variable is set to "true"
if System.get_env("MOCK_LN") == "true" do
config :cashubrew, :ln, Cashubrew.Lightning.MockLightningNetworkService
else
config :cashubrew, :ln, Cashubrew.Lightning.LightningNetworkService
end
45 changes: 26 additions & 19 deletions lib/cashubrew/core/mint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
use GenServer
alias Cashubrew.Cashu.BlindSignature
alias Cashubrew.Crypto.BDHKE
alias Cashubrew.Lightning.LightningNetworkService
alias Cashubrew.Lightning.MockLightningNetworkService
alias Cashubrew.LNBitsApi
alias Cashubrew.Query.MeltTokens
alias Cashubrew.Schema.{Key, Keyset, MeltQuote, MeltTokens, MintConfiguration, MintQuote}

Expand Down Expand Up @@ -125,33 +122,43 @@

def handle_call({:create_mint_quote, amount, description}, _from, state) do
repo = Application.get_env(:cashubrew, :repo)
ln = Application.get_env(:cashubrew, :ln)

case LightningNetworkService.create_invoice(amount, description) do
{:ok, payment_request, _payment_hash} ->
IO.inspect(System.get_env("MOCK_LN"), label: "MOCK_LN")

Check warning on line 127 in lib/cashubrew/core/mint.ex

View workflow job for this annotation

GitHub Actions / Build and test

There should be no calls to `IO.inspect/1`.
IO.inspect(Application.get_env(:cashubrew, :ln), label: "Configured LN Module")

Check warning on line 128 in lib/cashubrew/core/mint.ex

View workflow job for this annotation

GitHub Actions / Build and test

There should be no calls to `IO.inspect/1`.
result = ln.create_invoice(amount, description)

case result do
{:ok, payment_request, payment_hash} ->
# 1 hour expiry
expiry = :os.system_time(:second) + 3600

attrs = %{
amount: amount,
payment_request: payment_request,
expiry: expiry,
description: description
# payment_hash: _payment_hash,
description: description,
payment_hash: payment_hash
}

case repo.insert(MintQuote.changeset(%MintQuote{}, attrs)) do
{:ok, quote} ->
{:reply, {:ok, quote}, state}

{:error, changeset} ->
{:reply, {:error, changeset}, state}
end
insert_quote(repo, attrs, state)

{:error, reason} ->
{:reply, {:error, reason}, state}
end
end

# Helper function to insert the quote into the database
defp insert_quote(repo, attrs, state) do
case repo.insert(MintQuote.changeset(%MintQuote{}, attrs)) do
{:ok, quote} ->
{:reply, {:ok, quote}, state}

{:error, changeset} ->
{:reply, {:error, changeset}, state}
end
end

def handle_call({:get_mint_quote, quote_id}, _from, state) do
repo = Application.get_env(:cashubrew, :repo)
quote = repo.get(MintQuote, quote_id)
Expand Down Expand Up @@ -258,10 +265,11 @@
end
end

# TODO
def handle_call({:create_melt_tokens, quote_id, inputs}, _from, state) do
repo = Application.get_env(:cashubrew, :repo)
IO.puts("inputs: #{inputs}")

# TODO
# Verify quote_id

{:ok, melt_find} = Cashubrew.Query.MeltTokens.get_melt_by_quote_id!(quote_id)
Expand All @@ -277,19 +285,18 @@

fee_reserve = 0
# Create and Saved melt quote
expiry = :os.system_time(:second) + 3600

attrs = %{
# quote_id
request: quote_id,
unit: quote_id,
amount: 0,
fee_reserve: 0,
expiry: 0,
fee_reserve: fee_reserve,
expiry: expiry,
request_lookup_id: quote_id
}

expiry = :os.system_time(:second) + 3600

case repo.insert(MeltTokens.changeset(%MeltTokens{}, attrs)) do
{:ok, melt_quote} ->
{:reply, {:ok, melt_quote}, state}
Expand Down
22 changes: 17 additions & 5 deletions lib/cashubrew/lightning/lightning_network_service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,28 @@ defmodule Cashubrew.Lightning.LightningNetworkService do
GenServer.call(__MODULE__, {:check_payment, payment_hash})
end

def handle_call({:create_invoice, amount, _description}, _from, state) do
unit_input = "sat"
def handle_call({:create_invoice, amount, description}, _from, state) do
amount =
case Integer.parse(amount) do
{int, _} -> int
# If parsing fails, assume it's already an integer
:error -> amount
end

attributes = %{
out: "false",
# out: false means it is an incoming payment request
out: false,
# amount in satoshis
amount: amount,
unit_input: unit_input
# description/memo for the invoice
memo: description
# unit_input: unit_input,
# expiry: 0,
# internal: false,
# webhook: "",
}

case LNBitsApi.post_data("api/v1/payments", attributes) do
case LNBitsApi.post_data("/api/v1/payments", attributes) do
{:ok, response_body} ->
IO.puts("Success create in: #{response_body}")

Expand Down
3 changes: 0 additions & 3 deletions lib/cashubrew/lightning/ln_bits.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ defmodule Cashubrew.LNBitsApi do
"""
Dotenv.load()

@api_endpoint System.get_env("LN_BITS_API_ENDPOINT")
@api_key System.get_env("LN_BITS_API_KEY")

def fetch_data(path, attributes) do
api_base_url = System.get_env("LN_BITS_API_ENDPOINT")
api_key = System.get_env("LN_BITS_API_KEY")
Expand Down
21 changes: 0 additions & 21 deletions lib/cashubrew/lightning/ln_lib.ex

This file was deleted.

4 changes: 2 additions & 2 deletions lib/cashubrew/web/controllers/mint_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ defmodule Cashubrew.Web.MintController do
end

def create_mint_quote(conn, %{"amount" => amount, "unit" => "sat"} = params) do
description = Map.get(params, "description")
unit = Map.get(params, "unit")

case Mint.create_mint_quote(amount, description) do
case Mint.create_mint_quote(amount, unit) do
{:ok, quote} ->
conn
|> put_status(:created)
Expand Down
Loading