Add kredits API with wallet balance endpoint
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
e1ff5c479e
commit
caea2d0121
@ -1,3 +1,4 @@
|
|||||||
EJABBERD_API_URL='http://xmpp.example.com/api'
|
EJABBERD_API_URL='http://xmpp.example.com/api'
|
||||||
|
BTCPAY_API_URL='http://btcpay.example.com'
|
||||||
LNDHUB_API_URL='http://localhost:3023'
|
LNDHUB_API_URL='http://localhost:3023'
|
||||||
LNDHUB_PUBLIC_URL='https://lndhub.kosmos.org'
|
LNDHUB_PUBLIC_URL='https://lndhub.kosmos.org'
|
||||||
|
5
app/controllers/api/base_controller.rb
Normal file
5
app/controllers/api/base_controller.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
class Api::BaseController < ApplicationController
|
||||||
|
|
||||||
|
layout false
|
||||||
|
|
||||||
|
end
|
12
app/controllers/api/kredits_controller.rb
Normal file
12
app/controllers/api/kredits_controller.rb
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
class Api::KreditsController < Api::BaseController
|
||||||
|
|
||||||
|
def onchain_btc_balance
|
||||||
|
btcpay = BtcPay.new
|
||||||
|
balance = btcpay.onchain_wallet_balance
|
||||||
|
render json: balance
|
||||||
|
rescue
|
||||||
|
render json: { error: 'Failed to fetch wallet balance' },
|
||||||
|
status: 500
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
@ -9,12 +9,12 @@ class BtcPay
|
|||||||
end
|
end
|
||||||
|
|
||||||
def onchain_wallet_balance
|
def onchain_wallet_balance
|
||||||
wallet_info = get "stores/#{@store_id}/payment-methods/onchain/BTC/wallet"
|
res = get "stores/#{@store_id}/payment-methods/onchain/BTC/wallet"
|
||||||
|
|
||||||
{
|
{
|
||||||
balance: wallet_info["balance"].to_f,
|
balance: res["balance"].to_f,
|
||||||
unconfirmed_balance: wallet_info["unconfirmedBalance"].to_f,
|
unconfirmed_balance: res["unconfirmedBalance"].to_f,
|
||||||
confirmed_balance: wallet_info["confirmedBalance"].to_f
|
confirmed_balance: res["confirmedBalance"].to_f
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1 +1 @@
|
|||||||
xgPOFd8315z7lFtTR5/nD6WDBM2M6Grt/pmkCPdaqlw0WAmFKzbiRGFsXoUQ02JNzvT1/FVtBSsAcyK1Pdr1QQztlWC+/ywaflloMBS4//D8IEXvEgCK6uff5gcf1A==--WbFrw9advCJ4mqsK--HTVHZqO0ddG1toFpY0KKgQ==
|
IIjYiPSeZeMFhH8i8v8akXN4JrtGU+OsMQ8GAao/gVdesggriCBAQ8z+Vd0cmTf1SKYeT3OQDgygEekupr325P4eD9fZ+yi56EA/UMXQXMDVZAvZw7iwvKaOXpqisbWdJnomr1GXrHyR415Ce/Fxft3fgXDwMHJW2u+dDJgpE09uORnB9GXycFwHQmoIdXo=--iQ/Vcm0VcwHgUkwQ--tKHQW/45gM/s/NplqGPaxw==
|
@ -25,6 +25,10 @@ Rails.application.routes.draw do
|
|||||||
get 'lnurlpay/:address', to: 'lnurlpay#index', constraints: { address: /[^\/]+/}
|
get 'lnurlpay/:address', to: 'lnurlpay#index', constraints: { address: /[^\/]+/}
|
||||||
get 'lnurlpay/:address/invoice', to: 'lnurlpay#invoice', constraints: { address: /[^\/]+/}
|
get 'lnurlpay/:address/invoice', to: 'lnurlpay#invoice', constraints: { address: /[^\/]+/}
|
||||||
|
|
||||||
|
namespace :api do
|
||||||
|
get 'kredits/onchain_btc_balance', to: 'kredits#onchain_btc_balance'
|
||||||
|
end
|
||||||
|
|
||||||
namespace :admin do
|
namespace :admin do
|
||||||
root to: 'dashboard#index'
|
root to: 'dashboard#index'
|
||||||
get 'invitations', to: 'invitations#index'
|
get 'invitations', to: 'invitations#index'
|
||||||
|
43
spec/requests/api/kredits_spec.rb
Normal file
43
spec/requests/api/kredits_spec.rb
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
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