feat: more queries

This commit is contained in:
AP 2019-11-09 07:02:56 +01:00
parent 9973c3c54f
commit f2c0d14409
15 changed files with 229 additions and 81 deletions

View File

@ -5,48 +5,28 @@ export const ChannelType = new GraphQLObjectType({
name: "channelType",
fields: () => {
return {
capacity: {
type: GraphQLInt
},
id: {
type: GraphQLString
},
isActive: {
type: GraphQLBoolean
},
isClosing: {
type: GraphQLBoolean
},
isOpening: {
type: GraphQLBoolean
},
isPartnerInitiated: {
type: GraphQLBoolean
},
isPrivate: {
type: GraphQLBoolean
},
localBalance: {
type: GraphQLInt
},
localReserve: {
type: GraphQLInt
},
partnerPublicKey: {
type: GraphQLString
},
recieved: {
type: GraphQLInt
},
remoteBalance: {
type: GraphQLInt
},
remoteReserve: {
type: GraphQLInt
},
sent: {
type: GraphQLInt
}
capacity: { type: GraphQLInt },
commitTransactionFee: { type: GraphQLInt },
commitTransactionWeight: { type: GraphQLInt },
id: { type: GraphQLString },
isActive: { type: GraphQLBoolean },
isClosing: { type: GraphQLBoolean },
isOpening: { type: GraphQLBoolean },
isPartnerInitiated: { type: GraphQLBoolean },
isPrivate: { type: GraphQLBoolean },
isStaticRemoteKey: { type: GraphQLBoolean },
localBalance: { type: GraphQLInt },
localReserve: { type: GraphQLInt },
partnerPublicKey: { type: GraphQLString },
recieved: { type: GraphQLInt },
remoteBalance: { type: GraphQLInt },
remoteReserve: { type: GraphQLInt },
sent: { type: GraphQLInt },
timeOffline: { type: GraphQLInt },
timeOnline: { type: GraphQLInt },
transactionId: { type: GraphQLString },
transactionVout: { type: GraphQLInt },
unsettledBalance: { type: GraphQLInt }
};
}
});

View File

@ -0,0 +1,47 @@
import {
GraphQLObjectType,
GraphQLBoolean,
GraphQLString,
GraphQLList
} from "graphql";
import { GraphQLInt } from "graphql";
const PaymentType = new GraphQLObjectType({
name: "paymentType",
fields: () => ({
confirmedAt: { type: GraphQLString },
createdAt: { type: GraphQLString },
createdHeight: { type: GraphQLInt },
inChannel: { type: GraphQLString },
isCanceled: { type: GraphQLBoolean },
isConfirmed: { type: GraphQLBoolean },
isHeld: { type: GraphQLBoolean },
mtokens: { type: GraphQLString },
pendingIndex: { type: GraphQLInt },
tokens: { type: GraphQLInt }
})
});
export const GetInvoiceType = new GraphQLObjectType({
name: "getInvoiceType",
fields: () => ({
chainAddress: { type: GraphQLString },
confirmedAt: { type: GraphQLString },
createdAt: { type: GraphQLString },
description: { type: GraphQLString },
descriptionHash: { type: GraphQLString },
expiresAt: { type: GraphQLString },
id: { type: GraphQLString },
isCanceled: { type: GraphQLBoolean },
isConfirmed: { type: GraphQLBoolean },
isHeld: { type: GraphQLBoolean },
isOutgoing: { type: GraphQLBoolean },
isPrivate: { type: GraphQLBoolean },
payments: { type: new GraphQLList(PaymentType) },
received: { type: GraphQLInt },
receivedMtokens: { type: GraphQLInt },
request: { type: GraphQLString },
secret: { type: GraphQLString },
tokens: { type: GraphQLInt }
})
});

View File

@ -1,4 +1,4 @@
import { getChannelBalance } from "ln-service";
import { getChannelBalance as getLnChannelBalance } from "ln-service";
import { logger } from "../../../helpers/logger";
import { ChannelBalanceType } from "../../../schemaTypes/query/info/channelBalance";
import { requestLimiter } from "../../../helpers/rateLimiter";
@ -8,14 +8,14 @@ interface ChannelBalanceProps {
pending_balance: number;
}
export const channelBalance = {
export const getChannelBalance = {
type: ChannelBalanceType,
resolve: async (root: any, params: any, context: any) => {
await requestLimiter(context.ip, params, "channelBalance", 1, "1s");
const { lnd } = context;
try {
const channelBalance: ChannelBalanceProps = await getChannelBalance({
const channelBalance: ChannelBalanceProps = await getLnChannelBalance({
lnd: lnd
});
return {

View File

@ -1,5 +1,5 @@
import { GraphQLList } from "graphql";
import { getChannels } from "ln-service";
import { getChannels as getLnChannels } from "ln-service";
import { logger } from "../../../helpers/logger";
import { ChannelType } from "../../../schemaTypes/query/info/channels";
import { requestLimiter } from "../../../helpers/rateLimiter";
@ -34,33 +34,41 @@ interface ChannelProps {
unsettled_balance: number;
}
export const channels = {
export const getChannels = {
type: new GraphQLList(ChannelType),
resolve: async (root: any, params: any, context: any) => {
await requestLimiter(context.ip, params, "channels", 1, "1s");
const { lnd } = context;
try {
const channelList: ChannelListProps = await getChannels({
const channelList: ChannelListProps = await getLnChannels({
lnd: lnd
});
const channels = channelList.channels.map((channel, index) => {
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,
recieved: channel.received,
remoteBalance: channel.remote_balance,
remoteReserve: channel.remote_reserve,
sent: channel.sent
sent: channel.sent,
timeOffline: channel.time_offline,
timeOnline: channel.time_online,
transactionId: channel.transaction_id,
transactionVout: channel.transaction_vout,
unsettledBalance: channel.unsettled_balance
};
});

View File

@ -1,5 +1,5 @@
import { GraphQLList, GraphQLString } from "graphql";
import { getClosedChannels } from "ln-service";
import { getClosedChannels as getLnClosedChannels } from "ln-service";
import { logger } from "../../../helpers/logger";
import { ClosedChannelType } from "../../../schemaTypes/query/info/closedChannels";
import { requestLimiter } from "../../../helpers/rateLimiter";
@ -52,7 +52,7 @@ const getCloseReason = (
: UNKNOWN;
};
export const closedChannels = {
export const getClosedChannels = {
type: new GraphQLList(ClosedChannelType),
args: {
type: {
@ -64,7 +64,7 @@ export const closedChannels = {
const { lnd } = context;
try {
const closedChannels: ChannelListProps = await getClosedChannels({
const closedChannels: ChannelListProps = await getLnClosedChannels({
lnd: lnd
});

View File

@ -0,0 +1,11 @@
import { getChannelBalance } from "./channelBalance";
import { getChannels } from "./channels";
import { getClosedChannels } from "./closedChannels";
import { getPendingChannels } from "./pendingChannels";
export const channelQueries = {
getChannelBalance,
getChannels,
getClosedChannels,
getPendingChannels
};

View File

@ -1,4 +1,4 @@
import { getPendingChannels } from "ln-service";
import { getPendingChannels as getLnPendingChannels } from "ln-service";
import { logger } from "../../../helpers/logger";
import { PendingChannelType } from "../../../schemaTypes/query/info/pendingChannels";
import { GraphQLList } from "graphql";
@ -22,14 +22,14 @@ interface PendingChannelProps {
sent: number;
}
export const pendingChannels = {
export const getPendingChannels = {
type: new GraphQLList(PendingChannelType),
resolve: async (root: any, params: any, context: any) => {
await requestLimiter(context.ip, params, "pendingChannels", 1, "1s");
const { lnd } = context;
try {
const pendingChannels: PendingChannelListProps = await getPendingChannels(
const pendingChannels: PendingChannelListProps = await getLnPendingChannels(
{
lnd: lnd
}

View File

@ -1,4 +1,4 @@
import { getChainBalance } from "ln-service";
import { getChainBalance as getLnChainBalance } from "ln-service";
import { logger } from "../../../helpers/logger";
import { GraphQLInt } from "graphql";
import { requestLimiter } from "../../../helpers/rateLimiter";
@ -7,14 +7,14 @@ interface ChainBalanceProps {
chain_balance: number;
}
export const chainBalance = {
export const getChainBalance = {
type: GraphQLInt,
resolve: async (root: any, params: any, context: any) => {
await requestLimiter(context.ip, params, "chainBalance", 1, "1s");
const { lnd } = context;
try {
const chainBalance: ChainBalanceProps = await getChainBalance({
const chainBalance: ChainBalanceProps = await getLnChainBalance({
lnd: lnd
});
return chainBalance.chain_balance;

View File

@ -0,0 +1,9 @@
import { getChainBalance } from "./chainBalance";
import { getNetworkInfo } from "./networkInfo";
import { getNodeInfo } from "./nodeInfo";
export const generalQueries = {
getChainBalance,
getNetworkInfo,
getNodeInfo
};

View File

@ -1,4 +1,4 @@
import { getNetworkInfo } from "ln-service";
import { getNetworkInfo as getLnNetworkInfo } from "ln-service";
import { logger } from "../../../helpers/logger";
import { requestLimiter } from "../../../helpers/rateLimiter";
import { NetworkInfoType } from "../../../schemaTypes/query/info/networkInfo";
@ -14,14 +14,14 @@ interface NetworkInfoProps {
total_capacity: number;
}
export const networkInfo = {
export const getNetworkInfo = {
type: NetworkInfoType,
resolve: async (root: any, params: any, context: any) => {
await requestLimiter(context.ip, params, "networkInfo", 1, "1s");
const { lnd } = context;
try {
const info: NetworkInfoProps = await getNetworkInfo({
const info: NetworkInfoProps = await getLnNetworkInfo({
lnd: lnd
});

View File

@ -20,7 +20,7 @@ interface NodeInfoProps {
version: string;
}
export const nodeInfo = {
export const getNodeInfo = {
type: NodeInfoType,
resolve: async (root: any, params: any, context: any) => {
await requestLimiter(context.ip, params, "nodeInfo", 1, "1s");

View File

@ -1,3 +1,9 @@
import { info } from "./info";
import { channelQueries } from "./channels";
import { generalQueries } from "./general";
import { invoiceQueries } from "./invoices";
export const query = { ...info };
export const query = {
...channelQueries,
...generalQueries,
...invoiceQueries
};

View File

@ -1,17 +0,0 @@
import { chainBalance } from "./chainBalance";
import { channelBalance } from "./channelBalance";
import { channels } from "./channels";
import { closedChannels } from "./closedChannels";
import { networkInfo } from "./networkInfo";
import { nodeInfo } from "./nodeInfo";
import { pendingChannels } from "./pendingChannels";
export const info = {
chainBalance,
channelBalance,
channels,
closedChannels,
networkInfo,
nodeInfo,
pendingChannels
};

View File

@ -0,0 +1,5 @@
import { getInvoices } from "../invoices/invoices";
export const invoiceQueries = {
getInvoices
};

View File

@ -0,0 +1,99 @@
import { GraphQLList } 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";
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;
}
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;
}
interface InvoicesProps {
invoices: InvoiceProps[];
next: string;
}
export const getInvoices = {
type: new GraphQLList(GetInvoiceType),
resolve: async (root: any, params: any, context: any) => {
await requestLimiter(context.ip, params, "channels", 1, "1s");
const { lnd } = context;
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
}));
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.");
}
}
};