diff --git a/src/helpers/helpers.ts b/src/helpers/helpers.ts index 5b155c01..3ae3e123 100644 --- a/src/helpers/helpers.ts +++ b/src/helpers/helpers.ts @@ -57,3 +57,10 @@ export const getAuthLnd = (auth: string) => { return lnd; }; + +export const getErrorMsg = (error: any): string => { + const code = error[0]; + const msg = error[1]; + + return JSON.stringify({ code, msg }); +}; diff --git a/src/schemas/mutations/channels/closeChannel.ts b/src/schemas/mutations/channels/closeChannel.ts index fda3c963..ead97d0f 100644 --- a/src/schemas/mutations/channels/closeChannel.ts +++ b/src/schemas/mutations/channels/closeChannel.ts @@ -1,53 +1,54 @@ -import { closeChannel as lnCloseChannel } from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { requestLimiter } from "../../../helpers/rateLimiter"; +import { closeChannel as lnCloseChannel } from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { requestLimiter } from '../../../helpers/rateLimiter'; import { - GraphQLBoolean, - GraphQLString, - GraphQLInt, - GraphQLNonNull -} from "graphql"; -import { CloseChannelType } from "../../../schemaTypes/mutation.ts/channels/closeChannel"; + GraphQLBoolean, + GraphQLString, + GraphQLInt, + GraphQLNonNull, +} from 'graphql'; +import { CloseChannelType } from '../../../schemaTypes/mutation.ts/channels/closeChannel'; +import { getErrorMsg } from '../../../helpers/helpers'; interface CloseChannelProps { - transaction_id: string; - transaction_vout: string; + transaction_id: string; + transaction_vout: string; } export const closeChannel = { - type: CloseChannelType, - args: { - id: { - type: new GraphQLNonNull(GraphQLString) + type: CloseChannelType, + args: { + id: { + type: new GraphQLNonNull(GraphQLString), + }, + forceClose: { + type: GraphQLBoolean, + }, + targetConfirmations: { + type: GraphQLInt, + }, + tokensPerVByte: { + type: GraphQLInt, + }, }, - forceClose: { - type: GraphQLBoolean - }, - targetConfirmations: { - type: GraphQLInt - }, - tokensPerVByte: { - type: GraphQLInt - } - }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "closeChannel", 1, "1s"); - const { lnd } = context; + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'closeChannel', 1, '1s'); + const { lnd } = context; - try { - const info: CloseChannelProps = await lnCloseChannel({ - lnd: lnd, - id: params.id, - target_confirmations: params.targetConfirmations, - tokens_per_vbyte: params.tokensPerVByte - }); - return { - transactionId: info.transaction_id, - transactionOutputIndex: info.transaction_vout - }; - } catch (error) { - logger.error("Error closing channel: %o", error); - throw new Error("Failed to close channel."); - } - } + try { + const info: CloseChannelProps = await lnCloseChannel({ + lnd: lnd, + id: params.id, + target_confirmations: params.targetConfirmations, + tokens_per_vbyte: params.tokensPerVByte, + }); + return { + transactionId: info.transaction_id, + transactionOutputIndex: info.transaction_vout, + }; + } catch (error) { + logger.error('Error closing channel: %o', error); + throw new Error(getErrorMsg(error)); + } + }, }; diff --git a/src/schemas/mutations/channels/openChannel.ts b/src/schemas/mutations/channels/openChannel.ts index 5ac2c2c5..27ad1573 100644 --- a/src/schemas/mutations/channels/openChannel.ts +++ b/src/schemas/mutations/channels/openChannel.ts @@ -1,48 +1,51 @@ -import { openChannel as lnOpenChannel } from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { requestLimiter } from "../../../helpers/rateLimiter"; -import { GraphQLBoolean, GraphQLString, GraphQLInt } from "graphql"; -import { OpenChannelType } from "../../../schemaTypes/mutation.ts/channels/openChannel"; +import { openChannel as lnOpenChannel } from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { + GraphQLBoolean, + GraphQLString, + GraphQLInt, + GraphQLNonNull, +} from 'graphql'; +import { OpenChannelType } from '../../../schemaTypes/mutation.ts/channels/openChannel'; +import { getErrorMsg } from '../../../helpers/helpers'; interface OpenChannelProps { - transaction_id: string; - transaction_vout: string; + transaction_id: string; + transaction_vout: string; } export const openChannel = { - type: OpenChannelType, - args: { - isPrivate: { - type: GraphQLBoolean + type: OpenChannelType, + args: { + isPrivate: { + type: GraphQLBoolean, + }, + amount: { + type: new GraphQLNonNull(GraphQLInt), + }, + partnerPublicKey: { + type: new GraphQLNonNull(GraphQLString), + }, }, - amount: { - type: GraphQLInt + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'openChannel', 1, '1s'); + const { lnd } = context; + + try { + const info: OpenChannelProps = await lnOpenChannel({ + lnd: lnd, + is_private: params.isPrivate, + local_tokens: params.amount, + partner_public_key: params.partnerPublicKey, + }); + return { + transactionId: info.transaction_id, + transactionOutputIndex: info.transaction_vout, + }; + } catch (error) { + logger.error('Error opening channel: %o', error); + throw new Error(getErrorMsg(error)); + } }, - partnerPublicKey: { - type: GraphQLString - } - }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "openChannel", 1, "1s"); - const { lnd } = context; - - if (!params.amount || !params.partnerPublicKey) - throw new Error("Amount and partner public key are required"); - - try { - const info: OpenChannelProps = await lnOpenChannel({ - lnd: lnd, - is_private: params.isPrivate, - local_tokens: params.amount, - partner_public_key: params.partnerPublicKey - }); - return { - transactionId: info.transaction_id, - transactionOutputIndex: info.transaction_vout - }; - } catch (error) { - logger.error("Error opening channel: %o", error); - throw new Error("Failed to open channel."); - } - } }; diff --git a/src/schemas/mutations/invoices/createInvoice.ts b/src/schemas/mutations/invoices/createInvoice.ts index 5c20c369..57843ac8 100644 --- a/src/schemas/mutations/invoices/createInvoice.ts +++ b/src/schemas/mutations/invoices/createInvoice.ts @@ -1,49 +1,50 @@ -import { createInvoice as createInvoiceRequest } from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { requestLimiter } from "../../../helpers/rateLimiter"; -import { GraphQLNonNull, GraphQLInt } from "graphql"; -import { InvoiceType } from "../../../schemaTypes/mutation.ts/invoice/createInvoice"; +import { createInvoice as createInvoiceRequest } from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { GraphQLNonNull, GraphQLInt } from 'graphql'; +import { InvoiceType } from '../../../schemaTypes/mutation.ts/invoice/createInvoice'; +import { getErrorMsg } from '../../../helpers/helpers'; interface InvoiceProps { - chain_address: string; - created_at: string; - description: string; - id: string; - request: string; - secret: string; - tokens: number; + chain_address: string; + created_at: string; + description: string; + id: string; + request: string; + secret: string; + tokens: number; } // TODO: Allow more params export const createInvoice = { - type: InvoiceType, - args: { - amount: { - type: new GraphQLNonNull(GraphQLInt) - } - }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "createInvoice", 1, "1s"); - const { lnd } = context; + type: InvoiceType, + args: { + amount: { + type: new GraphQLNonNull(GraphQLInt), + }, + }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'createInvoice', 1, '1s'); + const { lnd } = context; - try { - const invoice: InvoiceProps = await createInvoiceRequest({ - lnd: lnd, - tokens: params.amount - }); + try { + const invoice: InvoiceProps = await createInvoiceRequest({ + lnd: lnd, + tokens: params.amount, + }); - return { - chainAddress: invoice.chain_address, - createdAt: invoice.created_at, - description: invoice.description, - id: invoice.id, - request: invoice.request, - secret: invoice.secret, - tokens: invoice.tokens - }; - } catch (error) { - logger.error("Error creating invoice: %o", error); - throw new Error("Failed to create invoice."); - } - } + return { + chainAddress: invoice.chain_address, + createdAt: invoice.created_at, + description: invoice.description, + id: invoice.id, + request: invoice.request, + secret: invoice.secret, + tokens: invoice.tokens, + }; + } catch (error) { + logger.error('Error creating invoice: %o', error); + throw new Error(getErrorMsg(error)); + } + }, }; diff --git a/src/schemas/mutations/invoices/parsePayment.ts b/src/schemas/mutations/invoices/parsePayment.ts index f1606f07..24093888 100644 --- a/src/schemas/mutations/invoices/parsePayment.ts +++ b/src/schemas/mutations/invoices/parsePayment.ts @@ -1,78 +1,79 @@ -import { parsePaymentRequest } from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { requestLimiter } from "../../../helpers/rateLimiter"; -import { GraphQLString, GraphQLNonNull } from "graphql"; -import { ParsePaymentType } from "../../../schemaTypes/mutation.ts/invoice/parsePayment"; +import { parsePaymentRequest } from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { GraphQLString, GraphQLNonNull } from 'graphql'; +import { ParsePaymentType } from '../../../schemaTypes/mutation.ts/invoice/parsePayment'; +import { getErrorMsg } from '../../../helpers/helpers'; interface RouteProps { - base_fee_mtokens: string; - channel: string; - cltv_delta: number; - fee_rate: number; - public_key: string; + base_fee_mtokens: string; + channel: string; + cltv_delta: number; + fee_rate: number; + public_key: string; } interface RequestProps { - chain_addresses: string[]; - cltv_delta: number; - created_at: string; - description: string; - description_hash: string; - destination: string; - expires_at: string; - id: string; - is_expired: string; - mtokens: string; - network: string; - routes: RouteProps[]; - tokens: number; + chain_addresses: string[]; + cltv_delta: number; + created_at: string; + description: string; + description_hash: string; + destination: string; + expires_at: string; + id: string; + is_expired: string; + mtokens: string; + network: string; + routes: RouteProps[]; + tokens: number; } export const parsePayment = { - type: ParsePaymentType, - args: { - request: { - type: new GraphQLNonNull(GraphQLString) - } - }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "parsePayment", 1, "1s"); - const { lnd } = context; + type: ParsePaymentType, + args: { + request: { + type: new GraphQLNonNull(GraphQLString), + }, + }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'parsePayment', 1, '1s'); + const { lnd } = context; - try { - const request: RequestProps = await parsePaymentRequest({ - lnd: lnd, - request: params.request - }); + try { + const request: RequestProps = await parsePaymentRequest({ + lnd: lnd, + request: params.request, + }); - const routes = request.routes.map(route => { - return { - mTokenFee: route.base_fee_mtokens, - channel: route.channel, - cltvDelta: route.cltv_delta, - feeRate: route.fee_rate, - publicKey: route.public_key - }; - }); + const routes = request.routes.map(route => { + return { + mTokenFee: route.base_fee_mtokens, + channel: route.channel, + cltvDelta: route.cltv_delta, + feeRate: route.fee_rate, + publicKey: route.public_key, + }; + }); - return { - chainAddresses: request.chain_addresses, - cltvDelta: request.cltv_delta, - createdAt: request.created_at, - description: request.description, - descriptionHash: request.description_hash, - destination: request.destination, - expiresAt: request.expires_at, - id: request.id, - isExpired: request.is_expired, - mTokens: request.mtokens, - network: request.network, - routes: routes, - tokens: request.tokens - }; - } catch (error) { - logger.error("Error decoding request: %o", error); - throw new Error("Failed to decode request."); - } - } + return { + chainAddresses: request.chain_addresses, + cltvDelta: request.cltv_delta, + createdAt: request.created_at, + description: request.description, + descriptionHash: request.description_hash, + destination: request.destination, + expiresAt: request.expires_at, + id: request.id, + isExpired: request.is_expired, + mTokens: request.mtokens, + network: request.network, + routes: routes, + tokens: request.tokens, + }; + } catch (error) { + logger.error('Error decoding request: %o', error); + throw new Error(getErrorMsg(error)); + } + }, }; diff --git a/src/schemas/mutations/invoices/pay.ts b/src/schemas/mutations/invoices/pay.ts index 6520e224..5b57ba05 100644 --- a/src/schemas/mutations/invoices/pay.ts +++ b/src/schemas/mutations/invoices/pay.ts @@ -1,71 +1,72 @@ -import { pay as payRequest } from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { requestLimiter } from "../../../helpers/rateLimiter"; -import { GraphQLString, GraphQLNonNull } from "graphql"; -import { PayType } from "../../../schemaTypes/mutation.ts/invoice/pay"; +import { pay as payRequest } from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { GraphQLString, GraphQLNonNull } from 'graphql'; +import { PayType } from '../../../schemaTypes/mutation.ts/invoice/pay'; +import { getErrorMsg } from '../../../helpers/helpers'; interface HopProps { - channel: string; - channel_capacity: number; - fee_mtokens: string; - forward_mtokens: string; - timeout: number; + channel: string; + channel_capacity: number; + fee_mtokens: string; + forward_mtokens: string; + timeout: number; } interface RequestProps { - fee: number; - fee_mtokens: string; - hops: HopProps[]; - id: string; - is_confirmed: boolean; - is_outgoing: boolean; - mtokens: string; - secret: string; - tokens: number; + fee: number; + fee_mtokens: string; + hops: HopProps[]; + id: string; + is_confirmed: boolean; + is_outgoing: boolean; + mtokens: string; + secret: string; + tokens: number; } // TODO: Allow path payments as well export const pay = { - type: PayType, - args: { - request: { - type: new GraphQLNonNull(GraphQLString) - } - }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "payRequest", 1, "1s"); - const { lnd } = context; + type: PayType, + args: { + request: { + type: new GraphQLNonNull(GraphQLString), + }, + }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'payRequest', 1, '1s'); + const { lnd } = context; - try { - const payment: RequestProps = await payRequest({ - lnd: lnd, - request: params.request - }); + try { + const payment: RequestProps = await payRequest({ + lnd: lnd, + request: params.request, + }); - const hops = payment.hops.map(hop => { - return { - channel: hop.channel, - channelCapacity: hop.channel_capacity, - mTokenFee: hop.fee_mtokens, - forwardMTokens: hop.forward_mtokens, - timeout: hop.timeout - }; - }); + const hops = payment.hops.map(hop => { + return { + channel: hop.channel, + channelCapacity: hop.channel_capacity, + mTokenFee: hop.fee_mtokens, + forwardMTokens: hop.forward_mtokens, + timeout: hop.timeout, + }; + }); - return { - fee: payment.fee, - feeMTokens: payment.fee_mtokens, - hops: hops, - id: payment.id, - isConfirmed: payment.is_confirmed, - isOutgoing: payment.is_outgoing, - mtokens: payment.mtokens, - secret: payment.secret, - tokens: payment.tokens - }; - } catch (error) { - logger.error("Error paying request: %o", error); - throw new Error("Failed to pay request."); - } - } + return { + fee: payment.fee, + feeMTokens: payment.fee_mtokens, + hops: hops, + id: payment.id, + isConfirmed: payment.is_confirmed, + isOutgoing: payment.is_outgoing, + mtokens: payment.mtokens, + secret: payment.secret, + tokens: payment.tokens, + }; + } catch (error) { + logger.error('Error paying request: %o', error); + throw new Error(getErrorMsg(error)); + } + }, }; diff --git a/src/schemas/query/channels/channelBalance.ts b/src/schemas/query/channels/channelBalance.ts index fd6803bd..4d17655a 100644 --- a/src/schemas/query/channels/channelBalance.ts +++ b/src/schemas/query/channels/channelBalance.ts @@ -1,34 +1,36 @@ -import { getChannelBalance as getLnChannelBalance } from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { ChannelBalanceType } from "../../../schemaTypes/query/info/channelBalance"; -import { requestLimiter } from "../../../helpers/rateLimiter"; -import { GraphQLNonNull, GraphQLString } from "graphql"; -import { getAuthLnd } from "../../../helpers/helpers"; +import { getChannelBalance as getLnChannelBalance } from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { ChannelBalanceType } from '../../../schemaTypes/query/info/channelBalance'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { GraphQLNonNull, GraphQLString } from 'graphql'; +import { getAuthLnd, getErrorMsg } from '../../../helpers/helpers'; interface ChannelBalanceProps { - channel_balance: number; - pending_balance: number; + channel_balance: number; + pending_balance: number; } export const getChannelBalance = { - type: ChannelBalanceType, - args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "channelBalance", 1, "1s"); + type: ChannelBalanceType, + args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'channelBalance', 1, '1s'); - const lnd = getAuthLnd(params.auth); + const lnd = getAuthLnd(params.auth); - try { - const channelBalance: ChannelBalanceProps = await getLnChannelBalance({ - lnd: lnd - }); - return { - confirmedBalance: channelBalance.channel_balance, - pendingBalance: channelBalance.pending_balance - }; - } catch (error) { - logger.error("Error getting channel balance: %o", error); - throw new Error("Failed to get channel balance."); - } - } + try { + const channelBalance: ChannelBalanceProps = await getLnChannelBalance( + { + lnd: lnd, + }, + ); + return { + confirmedBalance: channelBalance.channel_balance, + pendingBalance: channelBalance.pending_balance, + }; + } catch (error) { + logger.error('Error getting channel balance: %o', error); + throw new Error(getErrorMsg(error)); + } + }, }; diff --git a/src/schemas/query/channels/channels.ts b/src/schemas/query/channels/channels.ts index ce714579..85d0daba 100644 --- a/src/schemas/query/channels/channels.ts +++ b/src/schemas/query/channels/channels.ts @@ -1,97 +1,97 @@ -import { GraphQLList, GraphQLNonNull, GraphQLString } from "graphql"; -import { getChannels as getLnChannels, getNode } from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { ChannelType } from "../../../schemaTypes/query/info/channels"; -import { requestLimiter } from "../../../helpers/rateLimiter"; -import { getAuthLnd } from "../../../helpers/helpers"; +import { GraphQLList, GraphQLNonNull, GraphQLString } from 'graphql'; +import { getChannels as getLnChannels, getNode } from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { ChannelType } from '../../../schemaTypes/query/info/channels'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { getAuthLnd, getErrorMsg } from '../../../helpers/helpers'; interface ChannelListProps { - channels: ChannelProps[]; + channels: ChannelProps[]; } interface ChannelProps { - capacity: number; - commit_transaction_fee: number; - commit_transaction_weight: number; - id: string; - is_active: boolean; - is_closing: boolean; - is_opening: boolean; - is_partner_initiated: boolean; - is_private: boolean; - is_static_remote_key: boolean; - local_balance: number; - local_reserve: number; - partner_public_key: string; - pending_payments: []; - received: number; - remote_balance: number; - remote_reserve: number; - sent: number; - time_offline: number; - time_online: number; - transaction_id: string; - transaction_vout: number; - unsettled_balance: number; + capacity: number; + commit_transaction_fee: number; + commit_transaction_weight: number; + id: string; + is_active: boolean; + is_closing: boolean; + is_opening: boolean; + is_partner_initiated: boolean; + is_private: boolean; + is_static_remote_key: boolean; + local_balance: number; + local_reserve: number; + partner_public_key: string; + pending_payments: []; + received: number; + remote_balance: number; + remote_reserve: number; + sent: number; + time_offline: number; + time_online: number; + transaction_id: string; + transaction_vout: number; + unsettled_balance: number; } export const getChannels = { - type: new GraphQLList(ChannelType), - args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "channels", 1, "1s"); + type: new GraphQLList(ChannelType), + args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'channels', 1, '1s'); - const lnd = getAuthLnd(params.auth); + const lnd = getAuthLnd(params.auth); - try { - const channelList: ChannelListProps = await getLnChannels({ - lnd - }); + try { + const channelList: ChannelListProps = await getLnChannels({ + lnd, + }); - const channels = channelList.channels.map(async channel => { - const nodeInfo = await getNode({ - lnd, - is_omitting_channels: true, - public_key: channel.partner_public_key - }); + const channels = channelList.channels.map(async channel => { + const nodeInfo = await getNode({ + lnd, + is_omitting_channels: true, + public_key: channel.partner_public_key, + }); - return { - capacity: channel.capacity, - commitTransactionFee: channel.commit_transaction_fee, - commitTransactionWeight: channel.commit_transaction_weight, - id: channel.id, - isActive: channel.is_active, - isClosing: channel.is_closing, - isOpening: channel.is_opening, - isPartnerInitiated: !channel.is_partner_initiated, - isPrivate: channel.is_private, - isStaticRemoteKey: channel.is_static_remote_key, - localBalance: channel.local_balance, - localReserve: channel.local_reserve, - partnerPublicKey: channel.partner_public_key, - received: channel.received, - remoteBalance: channel.remote_balance, - remoteReserve: channel.remote_reserve, - sent: channel.sent, - timeOffline: channel.time_offline, - timeOnline: channel.time_online, - transactionId: channel.transaction_id, - transactionVout: channel.transaction_vout, - unsettledBalance: channel.unsettled_balance, - partnerNodeInfo: { - alias: nodeInfo.alias, - capacity: nodeInfo.capacity, - channelCount: nodeInfo.channel_count, - color: nodeInfo.color, - lastUpdate: nodeInfo.updated_at - } - }; - }); + return { + capacity: channel.capacity, + commitTransactionFee: channel.commit_transaction_fee, + commitTransactionWeight: channel.commit_transaction_weight, + id: channel.id, + isActive: channel.is_active, + isClosing: channel.is_closing, + isOpening: channel.is_opening, + isPartnerInitiated: !channel.is_partner_initiated, + isPrivate: channel.is_private, + isStaticRemoteKey: channel.is_static_remote_key, + localBalance: channel.local_balance, + localReserve: channel.local_reserve, + partnerPublicKey: channel.partner_public_key, + received: channel.received, + remoteBalance: channel.remote_balance, + remoteReserve: channel.remote_reserve, + sent: channel.sent, + timeOffline: channel.time_offline, + timeOnline: channel.time_online, + transactionId: channel.transaction_id, + transactionVout: channel.transaction_vout, + unsettledBalance: channel.unsettled_balance, + partnerNodeInfo: { + alias: nodeInfo.alias, + capacity: nodeInfo.capacity, + channelCount: nodeInfo.channel_count, + color: nodeInfo.color, + lastUpdate: nodeInfo.updated_at, + }, + }; + }); - return channels; - } catch (error) { - logger.error("Error getting channels: %o", error); - throw new Error("Failed to get channels."); - } - } + return channels; + } catch (error) { + logger.error('Error getting channels: %o', error); + throw new Error(getErrorMsg(error)); + } + }, }; diff --git a/src/schemas/query/channels/closedChannels.ts b/src/schemas/query/channels/closedChannels.ts index 5a6ed113..6f7c94d6 100644 --- a/src/schemas/query/channels/closedChannels.ts +++ b/src/schemas/query/channels/closedChannels.ts @@ -1,99 +1,100 @@ -import { GraphQLList, GraphQLString } from "graphql"; -import { getClosedChannels as getLnClosedChannels } from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { ClosedChannelType } from "../../../schemaTypes/query/info/closedChannels"; -import { requestLimiter } from "../../../helpers/rateLimiter"; +import { GraphQLList, GraphQLString } from 'graphql'; +import { getClosedChannels as getLnClosedChannels } from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { ClosedChannelType } from '../../../schemaTypes/query/info/closedChannels'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { getErrorMsg } from '../../../helpers/helpers'; -const BREACH = "BREACH"; -const COOPERATIVE = "COOPERATIVE"; -const FUNDING = "FUNDING"; -const LOCAL = "LOCAL"; -const REMOTE = "REMOTE"; -const UNKNOWN = "UNKNOWN"; +const BREACH = 'BREACH'; +const COOPERATIVE = 'COOPERATIVE'; +const FUNDING = 'FUNDING'; +const LOCAL = 'LOCAL'; +const REMOTE = 'REMOTE'; +const UNKNOWN = 'UNKNOWN'; interface ChannelListProps { - channels: ChannelProps[]; + channels: ChannelProps[]; } interface ChannelProps { - capacity: number; - close_confirm_height: number; - close_transaction_id: string; - final_local_balance: number; - final_time_locked_balance: number; - id: string; - is_breach_close: boolean; - is_cooperative_close: boolean; - is_funding_cancel: boolean; - is_local_force_close: boolean; - is_remote_force_close: boolean; - partner_public_key: string; - transaction_id: string; - transaction_vout: number; + capacity: number; + close_confirm_height: number; + close_transaction_id: string; + final_local_balance: number; + final_time_locked_balance: number; + id: string; + is_breach_close: boolean; + is_cooperative_close: boolean; + is_funding_cancel: boolean; + is_local_force_close: boolean; + is_remote_force_close: boolean; + partner_public_key: string; + transaction_id: string; + transaction_vout: number; } const getCloseReason = ( - breach: boolean, - cooperative: boolean, - funding: boolean, - local: boolean, - remote: boolean + breach: boolean, + cooperative: boolean, + funding: boolean, + local: boolean, + remote: boolean, ): string => { - return breach - ? BREACH - : cooperative - ? COOPERATIVE - : funding - ? FUNDING - : local - ? LOCAL - : remote - ? REMOTE - : UNKNOWN; + return breach + ? BREACH + : cooperative + ? COOPERATIVE + : funding + ? FUNDING + : local + ? LOCAL + : remote + ? REMOTE + : UNKNOWN; }; export const getClosedChannels = { - type: new GraphQLList(ClosedChannelType), - args: { - type: { - type: GraphQLString - } - }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "closedChannels", 1, "1s"); - const { lnd } = context; + type: new GraphQLList(ClosedChannelType), + args: { + type: { + type: GraphQLString, + }, + }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'closedChannels', 1, '1s'); + const { lnd } = context; - try { - const closedChannels: ChannelListProps = await getLnClosedChannels({ - lnd: lnd - }); + try { + const closedChannels: ChannelListProps = await getLnClosedChannels({ + lnd: lnd, + }); - const channels = closedChannels.channels.map(channel => { - const closeReason = getCloseReason( - channel.is_breach_close, - channel.is_cooperative_close, - channel.is_funding_cancel, - channel.is_local_force_close, - channel.is_remote_force_close - ); + const channels = closedChannels.channels.map(channel => { + const closeReason = getCloseReason( + channel.is_breach_close, + channel.is_cooperative_close, + channel.is_funding_cancel, + channel.is_local_force_close, + channel.is_remote_force_close, + ); - return { - capacity: channel.capacity, - closeConfirmHeight: channel.close_confirm_height, - closeTransactionId: channel.close_transaction_id, - finalLocalBalance: channel.final_local_balance, - finalTimeLockedBalance: channel.final_time_locked_balance, - id: channel.id, - closeReason: closeReason, - partnerPublicKey: channel.partner_public_key, - transactionId: channel.transaction_id, - transactionVout: channel.transaction_vout - }; - }); - return channels; - } catch (error) { - logger.error("Error getting closed channels: %o", error); - throw new Error("Failed to get closed channels."); - } - } + return { + capacity: channel.capacity, + closeConfirmHeight: channel.close_confirm_height, + closeTransactionId: channel.close_transaction_id, + finalLocalBalance: channel.final_local_balance, + finalTimeLockedBalance: channel.final_time_locked_balance, + id: channel.id, + closeReason: closeReason, + partnerPublicKey: channel.partner_public_key, + transactionId: channel.transaction_id, + transactionVout: channel.transaction_vout, + }; + }); + return channels; + } catch (error) { + logger.error('Error getting closed channels: %o', error); + throw new Error(getErrorMsg(error)); + } + }, }; diff --git a/src/schemas/query/channels/pendingChannels.ts b/src/schemas/query/channels/pendingChannels.ts index f73b9aa3..3ab0ab79 100644 --- a/src/schemas/query/channels/pendingChannels.ts +++ b/src/schemas/query/channels/pendingChannels.ts @@ -1,77 +1,79 @@ import { - getPendingChannels as getLnPendingChannels, - getNode -} from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { PendingChannelType } from "../../../schemaTypes/query/info/pendingChannels"; -import { GraphQLList, GraphQLNonNull, GraphQLString } from "graphql"; -import { requestLimiter } from "../../../helpers/rateLimiter"; -import { getAuthLnd } from "../../../helpers/helpers"; + getPendingChannels as getLnPendingChannels, + getNode, +} from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { PendingChannelType } from '../../../schemaTypes/query/info/pendingChannels'; +import { GraphQLList, GraphQLNonNull, GraphQLString } from 'graphql'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { getAuthLnd, getErrorMsg } from '../../../helpers/helpers'; interface PendingChannelListProps { - pending_channels: PendingChannelProps[]; + pending_channels: PendingChannelProps[]; } interface PendingChannelProps { - close_transaction_id: string; - is_active: boolean; - is_closing: boolean; - is_opening: boolean; - local_balance: number; - local_reserve: number; - partner_public_key: string; - received: number; - remote_balance: number; - remote_reserve: number; - sent: number; + close_transaction_id: string; + is_active: boolean; + is_closing: boolean; + is_opening: boolean; + local_balance: number; + local_reserve: number; + partner_public_key: string; + received: number; + remote_balance: number; + remote_reserve: number; + sent: number; } export const getPendingChannels = { - type: new GraphQLList(PendingChannelType), - args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "pendingChannels", 1, "1s"); + type: new GraphQLList(PendingChannelType), + args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'pendingChannels', 1, '1s'); - const lnd = getAuthLnd(params.auth); + const lnd = getAuthLnd(params.auth); - try { - const pendingChannels: PendingChannelListProps = await getLnPendingChannels( - { - lnd: lnd + try { + const pendingChannels: PendingChannelListProps = await getLnPendingChannels( + { + lnd: lnd, + }, + ); + + const channels = pendingChannels.pending_channels.map( + async channel => { + const nodeInfo = await getNode({ + lnd, + is_omitting_channels: true, + public_key: channel.partner_public_key, + }); + + return { + isActive: channel.is_active, + isClosing: channel.is_closing, + isOpening: channel.is_opening, + localBalance: channel.local_balance, + localReserve: channel.local_reserve, + partnerPublicKey: channel.partner_public_key, + received: channel.received, + remoteBalance: channel.remote_balance, + remoteReserve: channel.remote_reserve, + sent: channel.sent, + partnerNodeInfo: { + alias: nodeInfo.alias, + capacity: nodeInfo.capacity, + channelCount: nodeInfo.channel_count, + color: nodeInfo.color, + lastUpdate: nodeInfo.updated_at, + }, + }; + }, + ); + return channels; + } catch (error) { + logger.error('Error getting pending channels: %o', error); + throw new Error(getErrorMsg(error)); } - ); - - const channels = pendingChannels.pending_channels.map(async channel => { - const nodeInfo = await getNode({ - lnd, - is_omitting_channels: true, - public_key: channel.partner_public_key - }); - - return { - isActive: channel.is_active, - isClosing: channel.is_closing, - isOpening: channel.is_opening, - localBalance: channel.local_balance, - localReserve: channel.local_reserve, - partnerPublicKey: channel.partner_public_key, - received: channel.received, - remoteBalance: channel.remote_balance, - remoteReserve: channel.remote_reserve, - sent: channel.sent, - partnerNodeInfo: { - alias: nodeInfo.alias, - capacity: nodeInfo.capacity, - channelCount: nodeInfo.channel_count, - color: nodeInfo.color, - lastUpdate: nodeInfo.updated_at - } - }; - }); - return channels; - } catch (error) { - logger.error("Error getting pending channels: %o", error); - throw new Error("Failed to get pending channels."); - } - } + }, }; diff --git a/src/schemas/query/general/chainBalance.ts b/src/schemas/query/general/chainBalance.ts index c34fadc6..2d9595a6 100644 --- a/src/schemas/query/general/chainBalance.ts +++ b/src/schemas/query/general/chainBalance.ts @@ -1,56 +1,62 @@ import { - getChainBalance as getBalance, - getPendingChainBalance as getPending -} from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { requestLimiter } from "../../../helpers/rateLimiter"; -import { GraphQLInt, GraphQLNonNull, GraphQLString } from "graphql"; -import { getAuthLnd } from "../../../helpers/helpers"; + getChainBalance as getBalance, + getPendingChainBalance as getPending, +} from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { GraphQLInt, GraphQLNonNull, GraphQLString } from 'graphql'; +import { getAuthLnd, getErrorMsg } from '../../../helpers/helpers'; interface ChainBalanceProps { - chain_balance: number; + chain_balance: number; } interface PendingChainBalanceProps { - pending_chain_balance: number; + pending_chain_balance: number; } export const getChainBalance = { - type: GraphQLInt, - args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "chainBalance", 1, "1s"); + type: GraphQLInt, + args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'chainBalance', 1, '1s'); - const lnd = getAuthLnd(params.auth); + const lnd = getAuthLnd(params.auth); - try { - const value: ChainBalanceProps = await getBalance({ - lnd: lnd - }); - return value.chain_balance; - } catch (error) { - logger.error("Error getting chain balance: %o", error); - throw new Error("Failed to get chain balance."); - } - } + try { + const value: ChainBalanceProps = await getBalance({ + lnd: lnd, + }); + return value.chain_balance; + } catch (error) { + logger.error('Error getting chain balance: %o', error); + throw new Error(getErrorMsg(error)); + } + }, }; export const getPendingChainBalance = { - type: GraphQLInt, - args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "pendingChainBalance", 1, "1s"); + type: GraphQLInt, + args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter( + context.ip, + params, + 'pendingChainBalance', + 1, + '1s', + ); - const lnd = getAuthLnd(params.auth); + const lnd = getAuthLnd(params.auth); - try { - const pendingValue: PendingChainBalanceProps = await getPending({ - lnd: lnd - }); - return pendingValue.pending_chain_balance; - } catch (error) { - logger.error("Error getting pending chain balance: %o", error); - throw new Error("Failed to get pending chain balance."); - } - } + try { + const pendingValue: PendingChainBalanceProps = await getPending({ + lnd: lnd, + }); + return pendingValue.pending_chain_balance; + } catch (error) { + logger.error('Error getting pending chain balance: %o', error); + throw new Error(getErrorMsg(error)); + } + }, }; diff --git a/src/schemas/query/general/networkInfo.ts b/src/schemas/query/general/networkInfo.ts index e2d0d13c..691ccf43 100644 --- a/src/schemas/query/general/networkInfo.ts +++ b/src/schemas/query/general/networkInfo.ts @@ -1,47 +1,48 @@ -import { getNetworkInfo as getLnNetworkInfo } from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { requestLimiter } from "../../../helpers/rateLimiter"; -import { NetworkInfoType } from "../../../schemaTypes/query/info/networkInfo"; -import { GraphQLNonNull, GraphQLString } from "graphql"; -import { getAuthLnd } from "../../../helpers/helpers"; +import { getNetworkInfo as getLnNetworkInfo } from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { NetworkInfoType } from '../../../schemaTypes/query/info/networkInfo'; +import { GraphQLNonNull, GraphQLString } from 'graphql'; +import { getAuthLnd, getErrorMsg } from '../../../helpers/helpers'; interface NetworkInfoProps { - average_channel_size: number; - channel_count: number; - max_channel_size: number; - median_channel_size: number; - min_channel_size: number; - node_count: number; - not_recently_updated_policy_count: number; - total_capacity: number; + average_channel_size: number; + channel_count: number; + max_channel_size: number; + median_channel_size: number; + min_channel_size: number; + node_count: number; + not_recently_updated_policy_count: number; + total_capacity: number; } export const getNetworkInfo = { - type: NetworkInfoType, - args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "networkInfo", 1, "1s"); + type: NetworkInfoType, + args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'networkInfo', 1, '1s'); - const lnd = getAuthLnd(params.auth); + const lnd = getAuthLnd(params.auth); - try { - const info: NetworkInfoProps = await getLnNetworkInfo({ - lnd: lnd - }); + try { + const info: NetworkInfoProps = await getLnNetworkInfo({ + lnd: lnd, + }); - return { - averageChannelSize: info.average_channel_size, - channelCount: info.channel_count, - maxChannelSize: info.max_channel_size, - medianChannelSize: info.median_channel_size, - minChannelSize: info.min_channel_size, - nodeCount: info.node_count, - notRecentlyUpdatedPolicyCount: info.not_recently_updated_policy_count, - totalCapacity: info.total_capacity - }; - } catch (error) { - logger.error("Error getting network info: %o", error); - throw new Error("Failed to get network info."); - } - } + return { + averageChannelSize: info.average_channel_size, + channelCount: info.channel_count, + maxChannelSize: info.max_channel_size, + medianChannelSize: info.median_channel_size, + minChannelSize: info.min_channel_size, + nodeCount: info.node_count, + notRecentlyUpdatedPolicyCount: + info.not_recently_updated_policy_count, + totalCapacity: info.total_capacity, + }; + } catch (error) { + logger.error('Error getting network info: %o', error); + throw new Error(getErrorMsg(error)); + } + }, }; diff --git a/src/schemas/query/general/nodeInfo.ts b/src/schemas/query/general/nodeInfo.ts index be3dc65e..35be061d 100644 --- a/src/schemas/query/general/nodeInfo.ts +++ b/src/schemas/query/general/nodeInfo.ts @@ -1,58 +1,58 @@ -import { getWalletInfo } from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { NodeInfoType } from "../../../schemaTypes/query/info/nodeInfo"; -import { requestLimiter } from "../../../helpers/rateLimiter"; -import { getAuthLnd } from "../../../helpers/helpers"; -import { GraphQLNonNull, GraphQLString } from "graphql"; +import { getWalletInfo } from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { NodeInfoType } from '../../../schemaTypes/query/info/nodeInfo'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { getAuthLnd, getErrorMsg } from '../../../helpers/helpers'; +import { GraphQLNonNull, GraphQLString } from 'graphql'; interface NodeInfoProps { - chains: string[]; - color: string; - active_channels_count: number; - alias: string; - current_block_hash: string; - current_block_height: number; - is_synced_to_chain: boolean; - is_synced_to_graph: boolean; - latest_block_at: string; - peers_count: number; - pending_channels_count: number; - public_key: string; - uris: string[]; - version: string; + chains: string[]; + color: string; + active_channels_count: number; + alias: string; + current_block_hash: string; + current_block_height: number; + is_synced_to_chain: boolean; + is_synced_to_graph: boolean; + latest_block_at: string; + peers_count: number; + pending_channels_count: number; + public_key: string; + uris: string[]; + version: string; } export const getNodeInfo = { - type: NodeInfoType, - args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "nodeInfo", 1, "1s"); + type: NodeInfoType, + args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'nodeInfo', 1, '1s'); - const lnd = getAuthLnd(params.auth); + const lnd = getAuthLnd(params.auth); - try { - const info: NodeInfoProps = await getWalletInfo({ - lnd: lnd - }); - return { - chains: info.chains, - color: info.color, - activeChannelsCount: info.active_channels_count, - alias: info.alias, - currentBlockHash: info.current_block_hash, - currentBlockHeight: info.current_block_height, - isSyncedToChain: info.is_synced_to_chain, - isSyncedToGraph: info.is_synced_to_graph, - latestBlockAt: info.latest_block_at, - peersCount: info.peers_count, - pendingChannelsCount: info.pending_channels_count, - publicKey: info.public_key, - uris: info.uris, - version: info.version - }; - } catch (error) { - logger.error("Error getting node info: %o", error); - throw new Error("Failed to get node info."); - } - } + try { + const info: NodeInfoProps = await getWalletInfo({ + lnd: lnd, + }); + return { + chains: info.chains, + color: info.color, + activeChannelsCount: info.active_channels_count, + alias: info.alias, + currentBlockHash: info.current_block_hash, + currentBlockHeight: info.current_block_height, + isSyncedToChain: info.is_synced_to_chain, + isSyncedToGraph: info.is_synced_to_graph, + latestBlockAt: info.latest_block_at, + peersCount: info.peers_count, + pendingChannelsCount: info.pending_channels_count, + publicKey: info.public_key, + uris: info.uris, + version: info.version, + }; + } catch (error) { + logger.error('Error getting node info: %o', error); + throw new Error(getErrorMsg(error)); + } + }, }; diff --git a/src/schemas/query/invoices/forwards.ts b/src/schemas/query/invoices/forwards.ts index de04cacc..f8e30288 100644 --- a/src/schemas/query/invoices/forwards.ts +++ b/src/schemas/query/invoices/forwards.ts @@ -1,49 +1,50 @@ -import { GraphQLList } from "graphql"; -import { getForwards as getLnForwards } from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { requestLimiter } from "../../../helpers/rateLimiter"; -import { GetForwardType } from "../../../schemaTypes/query/info/forwards"; +import { GraphQLList } from 'graphql'; +import { getForwards as getLnForwards } from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { GetForwardType } from '../../../schemaTypes/query/info/forwards'; +import { getErrorMsg } from '../../../helpers/helpers'; interface ForwardProps { - created_at: string; - fee: number; - fee_mtokens: string; - incoming_channel: string; - mtokens: string; - outgoing_channel: string; - tokens: number; + created_at: string; + fee: number; + fee_mtokens: string; + incoming_channel: string; + mtokens: string; + outgoing_channel: string; + tokens: number; } interface ForwardsProps { - forwards: ForwardProps[]; - next: string; + forwards: ForwardProps[]; + next: string; } export const getForwards = { - type: new GraphQLList(GetForwardType), - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "getForwards", 1, "1s"); - const { lnd } = context; + type: new GraphQLList(GetForwardType), + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'getForwards', 1, '1s'); + const { lnd } = context; - try { - const forwardsList: ForwardsProps = await getLnForwards({ - lnd: lnd - }); + try { + const forwardsList: ForwardsProps = await getLnForwards({ + lnd: lnd, + }); - const forwards = forwardsList.forwards.map(forward => ({ - createdAt: forward.created_at, - fee: forward.fee, - feeMtokens: forward.fee_mtokens, - incomingChannel: forward.incoming_channel, - mtokens: forward.mtokens, - outgoingChannel: forward.outgoing_channel, - tokens: forward.tokens - })); + const forwards = forwardsList.forwards.map(forward => ({ + createdAt: forward.created_at, + fee: forward.fee, + feeMtokens: forward.fee_mtokens, + incomingChannel: forward.incoming_channel, + mtokens: forward.mtokens, + outgoingChannel: forward.outgoing_channel, + tokens: forward.tokens, + })); - return forwards; - } catch (error) { - logger.error("Error getting forwards: %o", error); - throw new Error("Failed to get forwards."); - } - } + return forwards; + } catch (error) { + logger.error('Error getting forwards: %o', error); + throw new Error(getErrorMsg(error)); + } + }, }; diff --git a/src/schemas/query/invoices/invoices.ts b/src/schemas/query/invoices/invoices.ts index 71071bb4..fe218611 100644 --- a/src/schemas/query/invoices/invoices.ts +++ b/src/schemas/query/invoices/invoices.ts @@ -1,102 +1,102 @@ -import { GraphQLList, GraphQLNonNull, GraphQLString } from "graphql"; -import { getInvoices as getLnInvoices } from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { requestLimiter } from "../../../helpers/rateLimiter"; -import { GetInvoiceType } from "../../../schemaTypes/query/info/invoices"; -import { getAuthLnd } from "../../../helpers/helpers"; +import { GraphQLList, GraphQLNonNull, GraphQLString } from 'graphql'; +import { getInvoices as getLnInvoices } from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { GetInvoiceType } from '../../../schemaTypes/query/info/invoices'; +import { getAuthLnd, getErrorMsg } from '../../../helpers/helpers'; interface PaymentProps { - confirmed_at: string; - created_at: string; - created_height: number; - in_channel: string; - is_canceled: boolean; - is_confirmed: boolean; - is_held: boolean; - mtokens: string; - pending_index: number; - tokens: number; + confirmed_at: string; + created_at: string; + created_height: number; + in_channel: string; + is_canceled: boolean; + is_confirmed: boolean; + is_held: boolean; + mtokens: string; + pending_index: number; + tokens: number; } interface InvoiceProps { - chain_address: string; - confirmed_at: string; - created_at: string; - description: string; - description_hash: string; - expires_at: string; - id: string; - is_canceled: boolean; - is_confirmed: boolean; - is_held: boolean; - is_outgoing: boolean; - is_private: boolean; - payments: PaymentProps[]; - received: number; - received_mtokens: string; - request: string; - secret: string; - tokens: number; + chain_address: string; + confirmed_at: string; + created_at: string; + description: string; + description_hash: string; + expires_at: string; + id: string; + is_canceled: boolean; + is_confirmed: boolean; + is_held: boolean; + is_outgoing: boolean; + is_private: boolean; + payments: PaymentProps[]; + received: number; + received_mtokens: string; + request: string; + secret: string; + tokens: number; } interface InvoicesProps { - invoices: InvoiceProps[]; - next: string; + invoices: InvoiceProps[]; + next: string; } export const getInvoices = { - type: new GraphQLList(GetInvoiceType), - args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "getInvoices", 1, "1s"); + type: new GraphQLList(GetInvoiceType), + args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'getInvoices', 1, '1s'); - const lnd = getAuthLnd(params.auth); + const lnd = getAuthLnd(params.auth); - try { - const invoiceList: InvoicesProps = await getLnInvoices({ - lnd: lnd - }); + try { + const invoiceList: InvoicesProps = await getLnInvoices({ + lnd: lnd, + }); - const invoices = invoiceList.invoices.map(invoice => { - const payments = invoice.payments.map(payment => ({ - confirmedAt: payment.confirmed_at, - createdAt: payment.created_at, - createdHeight: payment.created_height, - inChannel: payment.in_channel, - isCanceled: payment.is_canceled, - isConfirmed: payment.is_confirmed, - isHeld: payment.is_held, - mtokens: payment.mtokens, - pendingIndex: payment.pending_index, - tokens: payment.tokens - })); + const invoices = invoiceList.invoices.map(invoice => { + const payments = invoice.payments.map(payment => ({ + confirmedAt: payment.confirmed_at, + createdAt: payment.created_at, + createdHeight: payment.created_height, + inChannel: payment.in_channel, + isCanceled: payment.is_canceled, + isConfirmed: payment.is_confirmed, + isHeld: payment.is_held, + mtokens: payment.mtokens, + pendingIndex: payment.pending_index, + tokens: payment.tokens, + })); - return { - chainAddress: invoice.chain_address, - confirmedAt: invoice.confirmed_at, - createdAt: invoice.created_at, - description: invoice.description, - descriptionHash: invoice.description_hash, - expiresAt: invoice.expires_at, - id: invoice.id, - isCanceled: invoice.is_canceled, - isConfirmed: invoice.is_confirmed, - isHeld: invoice.is_held, - isOutgoing: invoice.is_outgoing, - isPrivate: invoice.is_private, - payments: payments, - received: invoice.received, - receivedMtokens: invoice.received_mtokens, - request: invoice.request, - secret: invoice.secret, - tokens: invoice.tokens - }; - }); + return { + chainAddress: invoice.chain_address, + confirmedAt: invoice.confirmed_at, + createdAt: invoice.created_at, + description: invoice.description, + descriptionHash: invoice.description_hash, + expiresAt: invoice.expires_at, + id: invoice.id, + isCanceled: invoice.is_canceled, + isConfirmed: invoice.is_confirmed, + isHeld: invoice.is_held, + isOutgoing: invoice.is_outgoing, + isPrivate: invoice.is_private, + payments: payments, + received: invoice.received, + receivedMtokens: invoice.received_mtokens, + request: invoice.request, + secret: invoice.secret, + tokens: invoice.tokens, + }; + }); - return invoices; - } catch (error) { - logger.error("Error getting invoices: %o", error); - throw new Error("Failed to get invoices."); - } - } + return invoices; + } catch (error) { + logger.error('Error getting invoices: %o', error); + throw new Error(getErrorMsg(error)); + } + }, }; diff --git a/src/schemas/query/invoices/payments.ts b/src/schemas/query/invoices/payments.ts index e39a4485..dbb6554c 100644 --- a/src/schemas/query/invoices/payments.ts +++ b/src/schemas/query/invoices/payments.ts @@ -1,61 +1,61 @@ -import { GraphQLList, GraphQLNonNull, GraphQLString } from "graphql"; -import { getPayments as getLnPayments } from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { requestLimiter } from "../../../helpers/rateLimiter"; -import { GetPaymentType } from "../../../schemaTypes/query/info/payments"; -import { getAuthLnd } from "../../../helpers/helpers"; +import { GraphQLList, GraphQLNonNull, GraphQLString } from 'graphql'; +import { getPayments as getLnPayments } from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { GetPaymentType } from '../../../schemaTypes/query/info/payments'; +import { getAuthLnd, getErrorMsg } from '../../../helpers/helpers'; interface PaymentProps { - created_at: string; - destination: string; - fee: number; - fee_mtokens: string; - hops: string[]; - id: string; - is_confirmed: boolean; - is_outgoing: boolean; - mtokens: string; - request: string; - secret: string; - tokens: number; + created_at: string; + destination: string; + fee: number; + fee_mtokens: string; + hops: string[]; + id: string; + is_confirmed: boolean; + is_outgoing: boolean; + mtokens: string; + request: string; + secret: string; + tokens: number; } interface PaymentsProps { - payments: PaymentProps[]; + payments: PaymentProps[]; } export const getPayments = { - type: new GraphQLList(GetPaymentType), - args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "getPayments", 1, "1s"); + type: new GraphQLList(GetPaymentType), + args: { auth: { type: new GraphQLNonNull(GraphQLString) } }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'getPayments', 1, '1s'); - const lnd = getAuthLnd(params.auth); + const lnd = getAuthLnd(params.auth); - try { - const paymentList: PaymentsProps = await getLnPayments({ - lnd: lnd - }); + try { + const paymentList: PaymentsProps = await getLnPayments({ + lnd: lnd, + }); - const payments = paymentList.payments.map(payment => ({ - createdAt: payment.created_at, - destination: payment.destination, - fee: payment.fee, - feeMtokens: payment.fee_mtokens, - hops: payment.hops, - id: payment.id, - isConfirmed: payment.is_confirmed, - isOutgoing: payment.is_outgoing, - mtokens: payment.mtokens, - request: payment.request, - secret: payment.secret, - tokens: payment.tokens - })); + const payments = paymentList.payments.map(payment => ({ + createdAt: payment.created_at, + destination: payment.destination, + fee: payment.fee, + feeMtokens: payment.fee_mtokens, + hops: payment.hops, + id: payment.id, + isConfirmed: payment.is_confirmed, + isOutgoing: payment.is_outgoing, + mtokens: payment.mtokens, + request: payment.request, + secret: payment.secret, + tokens: payment.tokens, + })); - return payments; - } catch (error) { - logger.error("Error getting payments: %o", error); - throw new Error("Failed to get payments."); - } - } + return payments; + } catch (error) { + logger.error('Error getting payments: %o', error); + throw new Error(getErrorMsg(error)); + } + }, }; diff --git a/src/schemas/query/report/ForwardChannels.ts b/src/schemas/query/report/ForwardChannels.ts index e5a4721c..be2d87ca 100644 --- a/src/schemas/query/report/ForwardChannels.ts +++ b/src/schemas/query/report/ForwardChannels.ts @@ -1,68 +1,68 @@ -import { GraphQLString, GraphQLNonNull } from "graphql"; -import { getForwards as getLnForwards } from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { requestLimiter } from "../../../helpers/rateLimiter"; -import { subHours, subDays } from "date-fns"; -import { countArray } from "./Helpers"; -import { ForwardCompleteProps } from "./ForwardReport.interface"; -import { ForwardChannelsType } from "../../../schemaTypes/query/report/ForwardChannels"; -import { sortBy } from "underscore"; -import { getAuthLnd } from "../../../helpers/helpers"; +import { GraphQLString, GraphQLNonNull } from 'graphql'; +import { getForwards as getLnForwards } from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { subHours, subDays } from 'date-fns'; +import { countArray } from './Helpers'; +import { ForwardCompleteProps } from './ForwardReport.interface'; +import { ForwardChannelsType } from '../../../schemaTypes/query/report/ForwardChannels'; +import { sortBy } from 'underscore'; +import { getAuthLnd, getErrorMsg } from '../../../helpers/helpers'; export const getForwardChannelsReport = { - type: ForwardChannelsType, - args: { - auth: { type: new GraphQLNonNull(GraphQLString) }, - time: { type: GraphQLString }, - order: { type: GraphQLString } - }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter( - context.ip, - params, - "getForwardChannelsReport", - 1, - "1s" - ); + type: ForwardChannelsType, + args: { + auth: { type: new GraphQLNonNull(GraphQLString) }, + time: { type: GraphQLString }, + order: { type: GraphQLString }, + }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter( + context.ip, + params, + 'getForwardChannelsReport', + 1, + '1s', + ); - const lnd = getAuthLnd(params.auth); + const lnd = getAuthLnd(params.auth); - let startDate = new Date(); - const endDate = new Date(); + let startDate = new Date(); + const endDate = new Date(); - if (params.time === "month") { - startDate = subDays(endDate, 30); - } else if (params.time === "week") { - startDate = subDays(endDate, 7); - } else { - startDate = subHours(endDate, 24); - } + if (params.time === 'month') { + startDate = subDays(endDate, 30); + } else if (params.time === 'week') { + startDate = subDays(endDate, 7); + } else { + startDate = subHours(endDate, 24); + } - try { - const forwardsList: ForwardCompleteProps = await getLnForwards({ - lnd: lnd, - after: startDate, - before: endDate, - limit: 10000 - }); + try { + const forwardsList: ForwardCompleteProps = await getLnForwards({ + lnd: lnd, + after: startDate, + before: endDate, + limit: 10000, + }); - const incomingCount = countArray(forwardsList.forwards, true); - const outgoingCount = countArray(forwardsList.forwards, false); + const incomingCount = countArray(forwardsList.forwards, true); + const outgoingCount = countArray(forwardsList.forwards, false); - const sortedInCount = sortBy(incomingCount, params.order) - .reverse() - .slice(0, 5); - const sortedOutCount = sortBy(outgoingCount, params.order) - .reverse() - .slice(0, 5); + const sortedInCount = sortBy(incomingCount, params.order) + .reverse() + .slice(0, 5); + const sortedOutCount = sortBy(outgoingCount, params.order) + .reverse() + .slice(0, 5); - return { - incoming: JSON.stringify(sortedInCount), - outgoing: JSON.stringify(sortedOutCount) - }; - } catch (error) { - logger.error("Error getting forward channel report: %o", error); - throw new Error("Failed to get forward channel report."); - } - } + return { + incoming: JSON.stringify(sortedInCount), + outgoing: JSON.stringify(sortedOutCount), + }; + } catch (error) { + logger.error('Error getting forward channel report: %o', error); + throw new Error(getErrorMsg(error)); + } + }, }; diff --git a/src/schemas/query/report/ForwardReport.ts b/src/schemas/query/report/ForwardReport.ts index 7b729a9b..06544a7d 100644 --- a/src/schemas/query/report/ForwardReport.ts +++ b/src/schemas/query/report/ForwardReport.ts @@ -1,72 +1,79 @@ -import { GraphQLString, GraphQLNonNull } from "graphql"; -import { getForwards as getLnForwards } from "ln-service"; -import { logger } from "../../../helpers/logger"; -import { requestLimiter } from "../../../helpers/rateLimiter"; -import { groupBy } from "underscore"; +import { GraphQLString, GraphQLNonNull } from 'graphql'; +import { getForwards as getLnForwards } from 'ln-service'; +import { logger } from '../../../helpers/logger'; +import { requestLimiter } from '../../../helpers/rateLimiter'; +import { groupBy } from 'underscore'; import { - subHours, - subDays, - differenceInHours, - differenceInCalendarDays -} from "date-fns"; -import { reduceForwardArray } from "./Helpers"; -import { ForwardCompleteProps } from "./ForwardReport.interface"; -import { getAuthLnd } from "../../../helpers/helpers"; + subHours, + subDays, + differenceInHours, + differenceInCalendarDays, +} from 'date-fns'; +import { reduceForwardArray } from './Helpers'; +import { ForwardCompleteProps } from './ForwardReport.interface'; +import { getAuthLnd, getErrorMsg } from '../../../helpers/helpers'; export const getForwardReport = { - type: GraphQLString, - args: { - auth: { type: new GraphQLNonNull(GraphQLString) }, - time: { type: GraphQLString } - }, - resolve: async (root: any, params: any, context: any) => { - await requestLimiter(context.ip, params, "getForwardReport", 1, "1s"); + type: GraphQLString, + args: { + auth: { type: new GraphQLNonNull(GraphQLString) }, + time: { type: GraphQLString }, + }, + resolve: async (root: any, params: any, context: any) => { + await requestLimiter(context.ip, params, 'getForwardReport', 1, '1s'); - const lnd = getAuthLnd(params.auth); + const lnd = getAuthLnd(params.auth); - let startDate = new Date(); - const endDate = new Date(); - let days = 7; + let startDate = new Date(); + const endDate = new Date(); + let days = 7; - if (params.time === "month") { - startDate = subDays(endDate, 30); - days = 30; - } else if (params.time === "week") { - startDate = subDays(endDate, 7); - } else { - startDate = subHours(endDate, 24); - } + if (params.time === 'month') { + startDate = subDays(endDate, 30); + days = 30; + } else if (params.time === 'week') { + startDate = subDays(endDate, 7); + } else { + startDate = subHours(endDate, 24); + } - try { - const forwardsList: ForwardCompleteProps = await getLnForwards({ - lnd: lnd, - after: startDate, - before: endDate, - limit: 10000 - }); + try { + const forwardsList: ForwardCompleteProps = await getLnForwards({ + lnd: lnd, + after: startDate, + before: endDate, + limit: 10000, + }); - if (params.time === "month" || params.time === "week") { - const orderedDay = groupBy(forwardsList.forwards, item => { - return ( - days - differenceInCalendarDays(endDate, new Date(item.created_at)) - ); - }); + if (params.time === 'month' || params.time === 'week') { + const orderedDay = groupBy(forwardsList.forwards, item => { + return ( + days - + differenceInCalendarDays( + endDate, + new Date(item.created_at), + ) + ); + }); - const reducedOrderedDay = reduceForwardArray(orderedDay); + const reducedOrderedDay = reduceForwardArray(orderedDay); - return JSON.stringify(reducedOrderedDay); - } else { - const orderedHour = groupBy(forwardsList.forwards, item => { - return 24 - differenceInHours(endDate, new Date(item.created_at)); - }); + return JSON.stringify(reducedOrderedDay); + } else { + const orderedHour = groupBy(forwardsList.forwards, item => { + return ( + 24 - + differenceInHours(endDate, new Date(item.created_at)) + ); + }); - const reducedOrderedHour = reduceForwardArray(orderedHour); + const reducedOrderedHour = reduceForwardArray(orderedHour); - return JSON.stringify(reducedOrderedHour); - } - } catch (error) { - logger.error("Error getting forward report: %o", error); - throw new Error("Failed to get forward report."); - } - } + return JSON.stringify(reducedOrderedHour); + } + } catch (error) { + logger.error('Error getting forward report: %o', error); + throw new Error(getErrorMsg(error)); + } + }, };