akkounts/app/controllers/wallet_controller.rb
Sebastian Kippe 884070a3cb
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing
Show available balance on wallet page
2021-11-21 16:47:55 -06:00

54 lines
1.5 KiB
Ruby

require "rqrcode"
class WalletController < ApplicationController
before_action :require_user_signed_in
before_action :authenticate_with_lndhub
def index
@current_section = :wallet
@wallet_url = "lndhub://#{current_user.ln_login}:#{current_user.ln_password}@#{ENV['LNDHUB_PUBLIC_URL']}"
qrcode = RQRCode::QRCode.new(@wallet_url)
@svg = qrcode.as_svg(
color: "000",
shape_rendering: "crispEdges",
module_size: 6,
standalone: true,
use_path: true
)
@balance = fetch_balance rescue nil
end
private
def authenticate_with_lndhub
if session["ln_auth_token"].present?
@ln_auth_token = session["ln_auth_token"]
else
res = Faraday.post("#{ENV["LNDHUB_API_URL"]}/auth?type=auth",
{ login: current_user.ln_login, password: current_user.ln_password }.to_json,
"Content-Type" => "application/json",
"Accept" => "application/json")
credentials = JSON.parse(res.body)
session["ln_auth_token"] = credentials["access_token"]
@ln_auth_token = credentials["access_token"]
end
rescue
# TODO add exception tracking
end
def fetch_balance
res = Faraday.get("#{ENV["LNDHUB_API_URL"]}/balance", {}, {
"Content-Type" => "application/json",
"Accept" => "application/json",
"Authorization" => "Bearer #{@ln_auth_token}"
})
data = JSON.parse(res.body)
data["BTC"]["AvailableBalance"]
end
end