diff --git a/src/schemaTypes/query/transactions/forwards.ts b/src/schemaTypes/query/transactions/forwards.ts index 94c2717f..d5b1de39 100644 --- a/src/schemaTypes/query/transactions/forwards.ts +++ b/src/schemaTypes/query/transactions/forwards.ts @@ -1,15 +1,27 @@ -import { GraphQLObjectType, GraphQLString } from 'graphql'; +import { GraphQLObjectType, GraphQLString, GraphQLList } from 'graphql'; import { GraphQLInt } from 'graphql'; -export const GetForwardType = new GraphQLObjectType({ - name: 'getForwardType', +export const ForwardType = new GraphQLObjectType({ + name: 'forwardType', fields: () => ({ created_at: { type: GraphQLString }, fee: { type: GraphQLInt }, fee_mtokens: { type: GraphQLString }, incoming_channel: { type: GraphQLString }, + incoming_alias: { type: GraphQLString }, + incoming_color: { type: GraphQLString }, mtokens: { type: GraphQLString }, outgoing_channel: { type: GraphQLString }, + outgoing_alias: { type: GraphQLString }, + outgoing_color: { type: GraphQLString }, tokens: { type: GraphQLInt }, }), }); + +export const GetForwardType = new GraphQLObjectType({ + name: 'getForwardType', + fields: () => ({ + token: { type: GraphQLString }, + forwards: { type: new GraphQLList(ForwardType) }, + }), +}); diff --git a/src/schemas/query/report/ForwardReport.ts b/src/schemas/query/report/ForwardReport.ts index 745f07e6..6b5f8438 100644 --- a/src/schemas/query/report/ForwardReport.ts +++ b/src/schemas/query/report/ForwardReport.ts @@ -42,7 +42,6 @@ export const getForwardReport = { lnd, after: startDate, before: endDate, - limit: 10000, }); if (params.time === 'month' || params.time === 'week') { diff --git a/src/schemas/query/transactions/forwards.ts b/src/schemas/query/transactions/forwards.ts index 435ff96f..9e3411fb 100644 --- a/src/schemas/query/transactions/forwards.ts +++ b/src/schemas/query/transactions/forwards.ts @@ -1,45 +1,120 @@ -import { GraphQLList, GraphQLString, GraphQLNonNull } from 'graphql'; -import { getForwards as getLnForwards } from 'ln-service'; +import { GraphQLString, GraphQLNonNull } from 'graphql'; +import { + getForwards as getLnForwards, + getChannel, + getNode, + getWalletInfo, +} from 'ln-service'; import { logger } from '../../../helpers/logger'; import { requestLimiter } from '../../../helpers/rateLimiter'; import { GetForwardType } from '../../../schemaTypes/query/transactions/forwards'; import { getErrorMsg, getAuthLnd } from '../../../helpers/helpers'; +import { sortBy } from 'underscore'; +import { ForwardCompleteProps } from '../report/ForwardReport.interface'; +import { subHours, subDays, subMonths, subYears } from 'date-fns'; -interface ForwardProps { - created_at: string; - fee: number; - fee_mtokens: string; - incoming_channel: string; - mtokens: string; - outgoing_channel: string; - tokens: number; +interface NodeProps { + alias: string; + color: string; } -interface ForwardsProps { - forwards: ForwardProps[]; - next: string; +interface ChannelsProps { + policies: { public_key: string }[]; } export const getForwards = { - type: new GraphQLList(GetForwardType), + type: GetForwardType, args: { auth: { type: new GraphQLNonNull(GraphQLString) }, - token: { type: GraphQLString }, + time: { type: GraphQLString }, }, resolve: async (root: any, params: any, context: any) => { await requestLimiter(context.ip, 'forwards'); const lnd = getAuthLnd(params.auth); - const props = params.token ? { token: params.token } : { limit: 25 }; + let startDate = new Date(); + const endDate = new Date(); - try { - const forwards: ForwardsProps = await getLnForwards({ + if (params.time === 'oneYear') { + startDate = subYears(endDate, 1); + } else if (params.time === 'sixMonths') { + startDate = subMonths(endDate, 6); + } else if (params.time === 'threeMonths') { + startDate = subMonths(endDate, 3); + } else if (params.time === 'month') { + startDate = subMonths(endDate, 1); + } else if (params.time === 'week') { + startDate = subDays(endDate, 7); + } else { + startDate = subHours(endDate, 24); + } + + const walletInfo: { public_key: string } = await getWalletInfo({ + lnd, + }); + + const getNodeAlias = async (id: string, publicKey: string) => { + const channelInfo: ChannelsProps = await getChannel({ lnd, - ...props, + id, }); - return forwards; + const partnerPublicKey = + channelInfo.policies[0].public_key !== publicKey + ? channelInfo.policies[0].public_key + : channelInfo.policies[1].public_key; + + const nodeInfo: NodeProps = await getNode({ + lnd, + is_omitting_channels: true, + public_key: partnerPublicKey, + }); + + return { + alias: nodeInfo.alias, + color: nodeInfo.color, + }; + }; + + const getAlias = (array: any[], publicKey: string) => + Promise.all( + array.map(async forward => { + const inNodeAlias = await getNodeAlias( + forward.incoming_channel, + publicKey, + ); + const outNodeAlias = await getNodeAlias( + forward.outgoing_channel, + publicKey, + ); + return { + incoming_alias: inNodeAlias.alias, + incoming_color: inNodeAlias.color, + outgoing_alias: outNodeAlias.alias, + outgoing_color: outNodeAlias.color, + ...forward, + }; + }), + ); + + try { + const forwardsList: ForwardCompleteProps = await getLnForwards({ + lnd, + after: startDate, + before: endDate, + }); + + const list = await getAlias( + forwardsList.forwards, + walletInfo.public_key, + ); + + const forwards = sortBy(list, 'created_at').reverse(); + return { + token: forwardsList.next, + forwards, + }; } catch (error) { logger.error('Error getting forwards: %o', error); throw new Error(getErrorMsg(error)); diff --git a/src/schemas/query/transactions/index.ts b/src/schemas/query/transactions/index.ts index 54afce14..83f5e86c 100644 --- a/src/schemas/query/transactions/index.ts +++ b/src/schemas/query/transactions/index.ts @@ -1,6 +1,6 @@ -import { getForwards } from './forwards'; +import { getForwards } from './Forwards'; import { getResume } from './resume'; -import { getChainTransactions } from './chainTransactions'; +import { getChainTransactions } from './ChainTransactions'; export const invoiceQueries = { getResume, diff --git a/src/schemas/query/transactions/resume.ts b/src/schemas/query/transactions/resume.ts index ac56a1ab..bbba7ee6 100644 --- a/src/schemas/query/transactions/resume.ts +++ b/src/schemas/query/transactions/resume.ts @@ -3,7 +3,7 @@ import { getPayments, getInvoices, getNode } from 'ln-service'; import { logger } from '../../../helpers/logger'; import { requestLimiter } from '../../../helpers/rateLimiter'; import { getAuthLnd, getErrorMsg } from '../../../helpers/helpers'; -import { PaymentsProps, InvoicesProps, NodeProps } from './resume.interface'; +import { PaymentsProps, InvoicesProps, NodeProps } from './Resume.interface'; import { compareDesc } from 'date-fns'; import { sortBy } from 'underscore'; import { GetResumeType } from '../../../schemaTypes/query/transactions/resume';