feat: channel queries

This commit is contained in:
AP 2019-12-10 21:24:00 +01:00
parent a84131d5f2
commit 22a909b4d9
10 changed files with 233 additions and 53 deletions

View file

@ -0,0 +1,27 @@
import { GraphQLObjectType, GraphQLInt, GraphQLString } from 'graphql';
export const ChannelFeeType = new GraphQLObjectType({
name: 'channelFeeType',
fields: () => {
return {
alias: {
type: GraphQLString,
},
color: {
type: GraphQLString,
},
baseFee: {
type: GraphQLInt,
},
feeRate: {
type: GraphQLInt,
},
transactionId: {
type: GraphQLString,
},
transactionVout: {
type: GraphQLInt,
},
};
},
});

View file

@ -0,0 +1,15 @@
import { GraphQLObjectType, GraphQLInt } from 'graphql';
export const ChannelReportType = new GraphQLObjectType({
name: 'channelReportType',
fields: () => {
return {
local: {
type: GraphQLInt,
},
remote: {
type: GraphQLInt,
},
};
},
});

View file

@ -0,0 +1,87 @@
import { getFeeRates, getChannels, getNode } from 'ln-service';
import { logger } from '../../../helpers/logger';
import { requestLimiter } from '../../../helpers/rateLimiter';
import { GraphQLNonNull, GraphQLString, GraphQLList } from 'graphql';
import { getAuthLnd, getErrorMsg } from '../../../helpers/helpers';
import { ChannelFeeType } from '../../../schemaTypes/query/channels/channelFees';
interface GetChannelsProps {
channels: ChannelsProps[];
}
interface GetFeeRatesProps {
channels: ChannelFeesProps[];
}
interface ChannelsProps {
partner_public_key: number;
transaction_id: string;
}
interface ChannelFeesProps {
base_fee: number;
fee_rate: number;
transaction_id: string;
transaction_vout: number;
}
interface NodeProps {
alias: string;
color: string;
}
export const getChannelFees = {
type: new GraphQLList(ChannelFeeType),
args: { auth: { type: new GraphQLNonNull(GraphQLString) } },
resolve: async (root: any, params: any, context: any) => {
await requestLimiter(context.ip, 'channelFees');
const lnd = getAuthLnd(params.auth);
try {
const channels: GetChannelsProps = await getChannels({ lnd });
const channelFees: GetFeeRatesProps = await getFeeRates({ lnd });
const getConsolidated = () =>
Promise.all(
channels.channels.map(async channel => {
const nodeInfo: NodeProps = await getNode({
lnd,
is_omitting_channels: true,
public_key: channel.partner_public_key,
});
const fees = channelFees.channels.find(
channelFee =>
channelFee.transaction_id ===
channel.transaction_id,
);
if (!fees) return;
const {
base_fee,
fee_rate,
transaction_id,
transaction_vout,
} = fees;
return {
alias: nodeInfo.alias,
color: nodeInfo.color,
baseFee: base_fee,
feeRate: fee_rate,
transactionId: transaction_id,
transactionVout: transaction_vout,
};
}),
);
const consolidated = await getConsolidated();
return consolidated;
} catch (error) {
logger.error('Error getting channel fees: %o', error);
throw new Error(getErrorMsg(error));
}
},
};

View file

@ -0,0 +1,44 @@
import { getChannels } from 'ln-service';
import { logger } from '../../../helpers/logger';
import { requestLimiter } from '../../../helpers/rateLimiter';
import { GraphQLNonNull, GraphQLString } from 'graphql';
import { getAuthLnd, getErrorMsg } from '../../../helpers/helpers';
import { ChannelReportType } from '../../../schemaTypes/query/channels/channelReport';
interface GetChannelsProps {
channels: ChannelsProps[];
}
interface ChannelsProps {
remote_balance: number;
local_balance: number;
}
export const getChannelReport = {
type: ChannelReportType,
args: { auth: { type: new GraphQLNonNull(GraphQLString) } },
resolve: async (root: any, params: any, context: any) => {
await requestLimiter(context.ip, 'channelReport');
const lnd = getAuthLnd(params.auth);
try {
const channels: GetChannelsProps = await getChannels({ lnd });
const consolidated = channels.channels.reduce((p, c) => {
return {
remote_balance: p.remote_balance + c.remote_balance,
local_balance: p.local_balance + c.local_balance,
};
});
return {
local: consolidated.local_balance,
remote: consolidated.remote_balance,
};
} catch (error) {
logger.error('Error getting channel fees: %o', error);
throw new Error(getErrorMsg(error));
}
},
};

View file

@ -48,46 +48,52 @@ export const getChannels = {
lnd,
});
const channels = channelList.channels.map(async channel => {
const nodeInfo = await getNode({
lnd,
is_omitting_channels: true,
public_key: channel.partner_public_key,
});
const getChannelList = () =>
Promise.all(
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,
},
};
}),
);
const channels = await getChannelList();
return channels;
} catch (error) {
logger.error('Error getting channels: %o', error);

View file

@ -66,7 +66,7 @@ export const getClosedChannels = {
try {
const closedChannels: ChannelListProps = await getLnClosedChannels({
lnd: lnd,
lnd,
});
const channels = closedChannels.channels.map(channel => {

View file

@ -1,11 +1,15 @@
import { getChannelBalance } from "./channelBalance";
import { getChannels } from "./channels";
import { getClosedChannels } from "./closedChannels";
import { getPendingChannels } from "./pendingChannels";
import { getChannelBalance } from './channelBalance';
import { getChannels } from './channels';
import { getClosedChannels } from './closedChannels';
import { getPendingChannels } from './pendingChannels';
import { getChannelFees } from './channelFees';
import { getChannelReport } from './channelReport';
export const channelQueries = {
getChannelBalance,
getChannels,
getClosedChannels,
getPendingChannels
getChannelBalance,
getChannels,
getClosedChannels,
getPendingChannels,
getChannelFees,
getChannelReport,
};

View file

@ -25,9 +25,7 @@ export const getNetworkInfo = {
const lnd = getAuthLnd(params.auth);
try {
const info: NetworkInfoProps = await getLnNetworkInfo({
lnd: lnd,
});
const info: NetworkInfoProps = await getLnNetworkInfo({ lnd });
return {
averageChannelSize: info.average_channel_size,

View file

@ -29,9 +29,7 @@ export const getForwards = {
const lnd = getAuthLnd(params.auth);
try {
const forwardsList: ForwardsProps = await getLnForwards({
lnd: lnd,
});
const forwardsList: ForwardsProps = await getLnForwards({ lnd });
const forwards = forwardsList.forwards.map(forward => ({
createdAt: forward.created_at,

View file

@ -9,6 +9,7 @@ export const RateConfig: RateConfigProps = {
channelBalance: { max: 3, window: '1s' },
channelFees: { max: 3, window: '1s' },
channels: { max: 3, window: '1s' },
channelReport: { max: 3, window: '1s' },
closedChannels: { max: 3, window: '1s' },
pendingChannels: { max: 3, window: '1s' },
bitcoinFee: { max: 3, window: '1s' },