Refactor BTCPay service and API, add lightning balance
This commit is contained in:
parent
074f9afcbb
commit
5a5f62e98a
@ -2,13 +2,14 @@ PRIMARY_DOMAIN=kosmos.org
|
||||
|
||||
REDIS_URL='redis://localhost:6379/0'
|
||||
|
||||
BTCPAY_API_URL='http://btcpay.example.com/api/v1'
|
||||
BTCPAY_STORE_ID='123456'
|
||||
|
||||
DISCOURSE_PUBLIC_URL='http://discourse.example.com'
|
||||
DISCOURSE_CONNECT_SECRET='discourse_connect_ftw'
|
||||
|
||||
EJABBERD_API_URL='http://xmpp.example.com/api'
|
||||
|
||||
BTCPAY_API_URL='http://btcpay.example.com/api/v1'
|
||||
|
||||
LNDHUB_API_URL='http://localhost:3026'
|
||||
LNDHUB_PUBLIC_URL='https://lndhub.kosmos.org'
|
||||
LNDHUB_PUBLIC_KEY='024cd3be18617f39cf645851e3ba63f51fc13f0bb09e3bb25e6fd4de556486d946'
|
||||
|
21
app/controllers/api/btcpay_controller.rb
Normal file
21
app/controllers/api/btcpay_controller.rb
Normal file
@ -0,0 +1,21 @@
|
||||
class Api::BtcpayController < Api::BaseController
|
||||
|
||||
def onchain_btc_balance
|
||||
balance = BtcpayManager::FetchOnchainWalletBalance.call
|
||||
render json: balance
|
||||
rescue => error
|
||||
Rails.logger.warn "Failed to fetch BTC wallet balance: #{error.message}"
|
||||
render json: { error: 'Failed to fetch wallet balance' },
|
||||
status: 500
|
||||
end
|
||||
|
||||
def lightning_btc_balance
|
||||
balance = BtcpayManager::FetchLightningWalletBalance.call
|
||||
render json: balance
|
||||
rescue => error
|
||||
Rails.logger.warn "Failed to fetch BTC lightning balance: #{error.message}"
|
||||
render json: { error: 'Failed to fetch wallet balance' },
|
||||
status: 500
|
||||
end
|
||||
|
||||
end
|
@ -1,13 +0,0 @@
|
||||
class Api::KreditsController < Api::BaseController
|
||||
|
||||
def onchain_btc_balance
|
||||
btcpay = BtcPay.new
|
||||
balance = btcpay.onchain_wallet_balance
|
||||
render json: balance
|
||||
rescue => error
|
||||
Rails.logger.warn "Failed to fetch kredits BTC wallet balance: #{error.message}"
|
||||
render json: { error: 'Failed to fetch wallet balance' },
|
||||
status: 500
|
||||
end
|
||||
|
||||
end
|
@ -1,32 +0,0 @@
|
||||
#
|
||||
# API Docs: https://docs.btcpayserver.org/API/Greenfield/v1/
|
||||
#
|
||||
class BtcPay
|
||||
def initialize
|
||||
@base_url = Setting.btcpay_api_url
|
||||
@store_id = Setting.btcpay_store_id
|
||||
@auth_token = Setting.btcpay_auth_token
|
||||
end
|
||||
|
||||
def onchain_wallet_balance
|
||||
res = get "stores/#{@store_id}/payment-methods/onchain/BTC/wallet"
|
||||
|
||||
{
|
||||
balance: res["balance"].to_f,
|
||||
unconfirmed_balance: res["unconfirmedBalance"].to_f,
|
||||
confirmed_balance: res["confirmedBalance"].to_f
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get(endpoint)
|
||||
res = Faraday.get("#{@base_url}/#{endpoint}", {}, {
|
||||
"Content-Type" => "application/json",
|
||||
"Accept" => "application/json",
|
||||
"Authorization" => "token #{@auth_token}"
|
||||
})
|
||||
|
||||
JSON.parse(res.body)
|
||||
end
|
||||
end
|
@ -0,0 +1,11 @@
|
||||
module BtcpayManager
|
||||
class FetchLightningWalletBalance < BtcpayManagerService
|
||||
def call
|
||||
res = get "stores/#{@store_id}/lightning/BTC/balance"
|
||||
|
||||
{
|
||||
balance: res["offchain"]["local"].to_i / 1000 # msats to sats
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
13
app/services/btcpay_manager/fetch_onchain_wallet_balance.rb
Normal file
13
app/services/btcpay_manager/fetch_onchain_wallet_balance.rb
Normal file
@ -0,0 +1,13 @@
|
||||
module BtcpayManager
|
||||
class FetchOnchainWalletBalance < BtcpayManagerService
|
||||
def call
|
||||
res = get "stores/#{@store_id}/payment-methods/onchain/BTC/wallet"
|
||||
|
||||
{
|
||||
balance: (res["balance"].to_f * 100000000).to_i, # BTC to sats
|
||||
unconfirmed_balance: (res["unconfirmedBalance"].to_f * 100000000).to_i,
|
||||
confirmed_balance: (res["confirmedBalance"].to_f * 100000000).to_i
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
22
app/services/btcpay_manager_service.rb
Normal file
22
app/services/btcpay_manager_service.rb
Normal file
@ -0,0 +1,22 @@
|
||||
#
|
||||
# API Docs: https://docs.btcpayserver.org/API/Greenfield/v1/
|
||||
#
|
||||
class BtcpayManagerService < LdapService
|
||||
def initialize
|
||||
@base_url = Setting.btcpay_api_url
|
||||
@store_id = Setting.btcpay_store_id
|
||||
@auth_token = Setting.btcpay_auth_token
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get(endpoint)
|
||||
res = Faraday.get("#{@base_url}/#{endpoint}", {}, {
|
||||
"Content-Type" => "application/json",
|
||||
"Accept" => "application/json",
|
||||
"Authorization" => "token #{@auth_token}"
|
||||
})
|
||||
|
||||
JSON.parse(res.body)
|
||||
end
|
||||
end
|
@ -54,7 +54,8 @@ Rails.application.routes.draw do
|
||||
post 'webhooks/lndhub', to: 'webhooks#lndhub'
|
||||
|
||||
namespace :api do
|
||||
get 'kredits/onchain_btc_balance', to: 'kredits#onchain_btc_balance'
|
||||
get 'btcpay/onchain_btc_balance', to: 'btcpay#onchain_btc_balance'
|
||||
get 'btcpay/lightning_btc_balance', to: 'btcpay#lightning_btc_balance'
|
||||
end
|
||||
|
||||
namespace :admin do
|
||||
|
@ -23,7 +23,7 @@ RSpec.describe 'Admin/global settings', type: :feature do
|
||||
scenario "Opening service settings shows page for first service" do
|
||||
visit admin_settings_services_path
|
||||
|
||||
expect(current_url).to eq(admin_settings_services_url(params: { s: "discourse" }))
|
||||
expect(current_url).to eq(admin_settings_services_url(params: { s: "btcpay" }))
|
||||
end
|
||||
|
||||
scenario "View service settings" do
|
||||
|
79
spec/requests/api/btcpay_spec.rb
Normal file
79
spec/requests/api/btcpay_spec.rb
Normal file
@ -0,0 +1,79 @@
|
||||
require 'rails_helper'
|
||||
require 'webmock/rspec'
|
||||
|
||||
RSpec.describe "/api/btcpay", type: :request do
|
||||
|
||||
describe "GET /onchain_btc_balance" do
|
||||
before do
|
||||
stub_request(:get, "http://btcpay.example.com/api/v1/stores/123456/payment-methods/onchain/BTC/wallet")
|
||||
.to_return(status: 200, headers: {}, body: {
|
||||
balance: 0.91108606,
|
||||
unconfirmedBalance: 0,
|
||||
confirmedBalance: 0.91108606
|
||||
}.to_json)
|
||||
end
|
||||
|
||||
it "returns a formatted result for the onchain wallet balance" do
|
||||
get api_btcpay_onchain_btc_balance_path
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
res = JSON.parse(response.body)
|
||||
expect(res["balance"]).to eq(91108606)
|
||||
expect(res["unconfirmed_balance"]).to eq(0)
|
||||
expect(res["confirmed_balance"]).to eq(91108606)
|
||||
end
|
||||
|
||||
context "upstream request error" do
|
||||
before do
|
||||
stub_request(:get, "http://btcpay.example.com/api/v1/stores/123456/payment-methods/onchain/BTC/wallet")
|
||||
.to_return(status: 500, headers: {}, body: "")
|
||||
end
|
||||
|
||||
it "returns a formatted error" do
|
||||
get api_btcpay_onchain_btc_balance_path
|
||||
|
||||
expect(response).to have_http_status(:server_error)
|
||||
|
||||
res = JSON.parse(response.body)
|
||||
expect(res["error"]).not_to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /lightning_btc_balance" do
|
||||
before do
|
||||
stub_request(:get, "http://btcpay.example.com/api/v1/stores/123456/lightning/BTC/balance")
|
||||
.to_return(status: 200, headers: {}, body: {
|
||||
offchain: {
|
||||
local: 4200000000
|
||||
},
|
||||
}.to_json)
|
||||
end
|
||||
|
||||
it "returns a formatted result for the onchain wallet balance" do
|
||||
get api_btcpay_lightning_btc_balance_path
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
res = JSON.parse(response.body)
|
||||
expect(res["balance"]).to eq(4200000)
|
||||
end
|
||||
|
||||
context "upstream request error" do
|
||||
before do
|
||||
stub_request(:get, "http://btcpay.example.com/api/v1/stores/123456/payment-methods/onchain/BTC/wallet")
|
||||
.to_return(status: 500, headers: {}, body: "")
|
||||
end
|
||||
|
||||
it "returns a formatted error" do
|
||||
get api_btcpay_onchain_btc_balance_path
|
||||
|
||||
expect(response).to have_http_status(:server_error)
|
||||
|
||||
res = JSON.parse(response.body)
|
||||
expect(res["error"]).not_to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,43 +0,0 @@
|
||||
require 'rails_helper'
|
||||
require 'webmock/rspec'
|
||||
|
||||
RSpec.describe "/api/kredits", type: :request do
|
||||
|
||||
describe "GET /onchain_btc_balance" do
|
||||
before do
|
||||
stub_request(:get, "http://btcpay.example.com/api/v1/stores/123456/payment-methods/onchain/BTC/wallet")
|
||||
.to_return(status: 200, headers: {}, body: {
|
||||
balance: 0.91108606,
|
||||
unconfirmedBalance: 0,
|
||||
confirmedBalance: 0.91108606
|
||||
}.to_json)
|
||||
end
|
||||
|
||||
it "returns a formatted result for the onchain wallet balance" do
|
||||
get api_kredits_onchain_btc_balance_path
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
res = JSON.parse(response.body)
|
||||
expect(res["balance"]).to eq(0.91108606)
|
||||
expect(res["unconfirmed_balance"]).to eq(0)
|
||||
expect(res["confirmed_balance"]).to eq(0.91108606)
|
||||
end
|
||||
|
||||
context "upstream request error" do
|
||||
before do
|
||||
stub_request(:get, "http://btcpay.example.com/api/v1/stores/123456/payment-methods/onchain/BTC/wallet")
|
||||
.to_return(status: 500, headers: {}, body: "")
|
||||
end
|
||||
|
||||
it "returns a formatted error" do
|
||||
get api_kredits_onchain_btc_balance_path
|
||||
|
||||
expect(response).to have_http_status(:server_error)
|
||||
|
||||
res = JSON.parse(response.body)
|
||||
expect(res["error"]).not_to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user