From 2f90393eb6efcbd9b4f7e2ef66f10626614d9626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Mon, 5 Jun 2023 13:53:24 +0300 Subject: [PATCH] Lndhub v2 service inherits from v1, only adds v2-specific code --- app/services/lndhub_v2.rb | 62 ++------------------------------------- 1 file changed, 3 insertions(+), 59 deletions(-) diff --git a/app/services/lndhub_v2.rb b/app/services/lndhub_v2.rb index 693f812..e0a84dd 100644 --- a/app/services/lndhub_v2.rb +++ b/app/services/lndhub_v2.rb @@ -1,9 +1,4 @@ -class LndhubV2 - attr_accessor :auth_token - - def initialize - @base_url = ENV["LNDHUB_API_URL"] - end +class LndhubV2 < Lndhub def post(endpoint, payload, options={}) headers = { "Content-Type" => "application/json" } @@ -12,64 +7,12 @@ class LndhubV2 elsif options[:admin_token] headers.merge!({ "Authorization" => "Bearer #{options[:admin_token]}" }) end - res = Faraday.post "#{@base_url}/#{endpoint}", payload.to_json, headers - - if res.status != 200 - Rails.logger.error "[lndhub] API request failed:" - Rails.logger.error res.body - #TODO add some kind of exception tracking/notifications - end + log_error(res) if res.status != 200 JSON.parse(res.body) end - def get(endpoint, auth_token) - res = Faraday.get("#{@base_url}/#{endpoint}", {}, { - "Content-Type" => "application/json", - "Accept" => "application/json", - "Authorization" => "Bearer #{auth_token}" - }) - - JSON.parse(res.body) - end - - def create(payload) - post "create", payload - end - - def authenticate(user) - credentials = post "auth?type=auth", { login: user.ln_account, password: user.ln_password } - self.auth_token = credentials["access_token"] - self.auth_token - end - - def balance(user_token=nil) - get "balance", user_token || auth_token - end - - def gettxs(user_token) - get "gettxs", user_token || auth_token - end - - def getuserinvoices(user_token) - get "getuserinvoices", user_token || auth_token - end - - def addinvoice(payload) - invoice = post "addinvoice", { - amt: payload[:amount], - memo: payload[:memo], - description_hash: payload[:description_hash] - } - - invoice["payment_request"] - end - - # - # V2 - # - def create_account(payload={}) post "v2/users", payload, admin_token: Rails.application.credentials.lndhub[:admin_token] end @@ -78,4 +21,5 @@ class LndhubV2 # Payload: { amount: 1000, description: "", description_hash: "" } post "v2/invoices", payload end + end