From 9a5a21032fb0aebbd907d248a174c6d056a2d03c Mon Sep 17 00:00:00 2001 From: Anthony Potdevin Date: Wed, 30 Dec 2020 15:57:17 +0100 Subject: [PATCH] =?UTF-8?q?refactor:=20=E2=99=BB=EF=B8=8F=20getChannels=20?= =?UTF-8?q?query?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/schema/channel/resolvers.ts | 51 ++++++----- .../channel/resolvers/query/getChannels.ts | 8 +- server/schema/channel/types.ts | 6 +- .../getChannelFees.generated.tsx | 45 +++++----- .../__generated__/getChannels.generated.tsx | 41 ++++----- .../__snapshots__/queryTests.ts.snap | 84 ++++++++----------- src/graphql/queries/getChannelFees.ts | 28 +++---- src/graphql/queries/getChannels.ts | 24 +++--- src/graphql/types.ts | 7 +- src/views/channels/channels/ChannelCard.tsx | 4 +- src/views/channels/channels/Channels.tsx | 7 +- src/views/details/DetailsTable.tsx | 2 +- 12 files changed, 137 insertions(+), 170 deletions(-) diff --git a/server/schema/channel/resolvers.ts b/server/schema/channel/resolvers.ts index 670c51a0..3cc514b9 100644 --- a/server/schema/channel/resolvers.ts +++ b/server/schema/channel/resolvers.ts @@ -12,9 +12,11 @@ import { getClosedChannels } from './resolvers/query/getClosedChannels'; import { getPendingChannels } from './resolvers/query/getPendingChannels'; type ParentType = { - lnd: {}; - id: String; - localKey: String; + id: string; + partner_fee_info: { + lnd: {}; + localKey: String; + }; }; export const channelResolvers = { @@ -30,10 +32,11 @@ export const channelResolvers = { updateFees, updateMultipleFees, }, - Channel: { - channel: async (parent: ParentType) => { - const { lnd, id, localKey } = parent; - + channelType: { + partner_fee_info: async ({ + id, + partner_fee_info: { lnd, localKey }, + }: ParentType) => { if (!lnd) { logger.debug('ExpectedLNDToGetChannel'); return null; @@ -44,7 +47,9 @@ export const channelResolvers = { return null; } - const [channel, error] = await toWithError(getChannel({ lnd, id })); + const [channel, error] = await toWithError( + getChannel({ lnd, id }) + ); if (error) { logger.debug(`Error getting channel with id ${id}: %o`, error); @@ -54,19 +59,21 @@ export const channelResolvers = { let node_policies = null; let partner_node_policies = null; - (channel as GetChannelType).policies.forEach(policy => { - if (localKey && localKey === policy.public_key) { - node_policies = { - ...policy, - node: { lnd, publicKey: policy.public_key }, - }; - } else { - partner_node_policies = { - ...policy, - node: { lnd, publicKey: policy.public_key }, - }; - } - }); + if (channel) { + channel.policies.forEach(policy => { + if (localKey && localKey === policy.public_key) { + node_policies = { + ...policy, + node: { lnd, publicKey: policy.public_key }, + }; + } else { + partner_node_policies = { + ...policy, + node: { lnd, publicKey: policy.public_key }, + }; + } + }); + } return { ...(channel as GetChannelType), @@ -74,8 +81,6 @@ export const channelResolvers = { partner_node_policies, }; }, - }, - channelType: { pending_resume({ pending_payments }: ChannelType) { const total = pending_payments.reduce( (prev, current) => { diff --git a/server/schema/channel/resolvers/query/getChannels.ts b/server/schema/channel/resolvers/query/getChannels.ts index d405aba3..5ba970f7 100644 --- a/server/schema/channel/resolvers/query/getChannels.ts +++ b/server/schema/channel/resolvers/query/getChannels.ts @@ -9,11 +9,9 @@ import { GetChannelsType } from 'server/types/ln-service.types'; export const getChannels = async ( _: undefined, params: any, - context: ContextType + { ip, lnd }: ContextType ) => { - await requestLimiter(context.ip, 'channels'); - - const { lnd } = context; + await requestLimiter(ip, 'channels'); const { public_key, current_block_height } = await to(getWalletInfo({ lnd })); @@ -29,7 +27,7 @@ export const getChannels = async ( time_offline: Math.round((channel.time_offline || 0) / 1000), time_online: Math.round((channel.time_online || 0) / 1000), partner_node_info: { lnd, publicKey: channel.partner_public_key }, - partner_fee_info: { lnd, id: channel.id, localKey: public_key }, + partner_fee_info: { lnd, localKey: public_key }, channel_age: getChannelAge(channel.id, current_block_height), })); }; diff --git a/server/schema/channel/types.ts b/server/schema/channel/types.ts index 2a761f00..d5062e19 100644 --- a/server/schema/channel/types.ts +++ b/server/schema/channel/types.ts @@ -47,10 +47,6 @@ export const channelTypes = gql` partner_node_policies: nodePolicyType } - type Channel { - channel: singleChannelType - } - type channelBalanceType { confirmedBalance: Int! pendingBalance: Int! @@ -96,7 +92,7 @@ export const channelTypes = gql` transaction_vout: Int! unsettled_balance: Int! partner_node_info: Node! - partner_fee_info: Channel + partner_fee_info: singleChannelType channel_age: Int! pending_payments: [pendingPaymentType]! pending_resume: pendingResumeType! diff --git a/src/graphql/queries/__generated__/getChannelFees.generated.tsx b/src/graphql/queries/__generated__/getChannelFees.generated.tsx index 5c9e9ef5..f5a842de 100644 --- a/src/graphql/queries/__generated__/getChannelFees.generated.tsx +++ b/src/graphql/queries/__generated__/getChannelFees.generated.tsx @@ -18,16 +18,13 @@ export type ChannelFeesQuery = ( & Pick ) } ), partner_fee_info?: Types.Maybe<( - { __typename?: 'Channel' } - & { channel?: Types.Maybe<( - { __typename?: 'singleChannelType' } - & { node_policies?: Types.Maybe<( - { __typename?: 'nodePolicyType' } - & Pick - )>, partner_node_policies?: Types.Maybe<( - { __typename?: 'nodePolicyType' } - & Pick - )> } + { __typename?: 'singleChannelType' } + & { node_policies?: Types.Maybe<( + { __typename?: 'nodePolicyType' } + & Pick + )>, partner_node_policies?: Types.Maybe<( + { __typename?: 'nodePolicyType' } + & Pick )> } )> } )>> } @@ -48,21 +45,19 @@ export const ChannelFeesDocument = gql` } } partner_fee_info { - channel { - node_policies { - base_fee_mtokens - fee_rate - cltv_delta - max_htlc_mtokens - min_htlc_mtokens - } - partner_node_policies { - base_fee_mtokens - fee_rate - cltv_delta - max_htlc_mtokens - min_htlc_mtokens - } + node_policies { + base_fee_mtokens + fee_rate + cltv_delta + max_htlc_mtokens + min_htlc_mtokens + } + partner_node_policies { + base_fee_mtokens + fee_rate + cltv_delta + max_htlc_mtokens + min_htlc_mtokens } } } diff --git a/src/graphql/queries/__generated__/getChannels.generated.tsx b/src/graphql/queries/__generated__/getChannels.generated.tsx index 50b5e068..d9c0956a 100644 --- a/src/graphql/queries/__generated__/getChannels.generated.tsx +++ b/src/graphql/queries/__generated__/getChannels.generated.tsx @@ -23,16 +23,13 @@ export type GetChannelsQuery = ( & Pick ) } ), partner_fee_info?: Types.Maybe<( - { __typename?: 'Channel' } - & { channel?: Types.Maybe<( - { __typename?: 'singleChannelType' } - & { node_policies?: Types.Maybe<( - { __typename?: 'nodePolicyType' } - & Pick - )>, partner_node_policies?: Types.Maybe<( - { __typename?: 'nodePolicyType' } - & Pick - )> } + { __typename?: 'singleChannelType' } + & { node_policies?: Types.Maybe<( + { __typename?: 'nodePolicyType' } + & Pick + )>, partner_node_policies?: Types.Maybe<( + { __typename?: 'nodePolicyType' } + & Pick )> } )> } )>> } @@ -83,19 +80,17 @@ export const GetChannelsDocument = gql` } } partner_fee_info { - channel { - node_policies { - base_fee_mtokens - fee_rate - cltv_delta - max_htlc_mtokens - min_htlc_mtokens - } - partner_node_policies { - base_fee_mtokens - fee_rate - cltv_delta - } + node_policies { + base_fee_mtokens + fee_rate + cltv_delta + max_htlc_mtokens + min_htlc_mtokens + } + partner_node_policies { + base_fee_mtokens + fee_rate + cltv_delta } } } diff --git a/src/graphql/queries/__tests__/__snapshots__/queryTests.ts.snap b/src/graphql/queries/__tests__/__snapshots__/queryTests.ts.snap index d773d4a8..378cf9af 100644 --- a/src/graphql/queries/__tests__/__snapshots__/queryTests.ts.snap +++ b/src/graphql/queries/__tests__/__snapshots__/queryTests.ts.snap @@ -180,15 +180,13 @@ Object { Object { "id": "100x1x1", "partner_fee_info": Object { - "channel": Object { - "node_policies": null, - "partner_node_policies": Object { - "base_fee_mtokens": "2000", - "cltv_delta": 1000, - "fee_rate": 1000, - "max_htlc_mtokens": "10000", - "min_htlc_mtokens": "10000", - }, + "node_policies": null, + "partner_node_policies": Object { + "base_fee_mtokens": "2000", + "cltv_delta": 1000, + "fee_rate": 1000, + "max_htlc_mtokens": "10000", + "min_htlc_mtokens": "10000", }, }, "partner_node_info": Object { @@ -204,15 +202,13 @@ Object { Object { "id": "100x1x1", "partner_fee_info": Object { - "channel": Object { - "node_policies": null, - "partner_node_policies": Object { - "base_fee_mtokens": "2000", - "cltv_delta": 1000, - "fee_rate": 1000, - "max_htlc_mtokens": "10000", - "min_htlc_mtokens": "10000", - }, + "node_policies": null, + "partner_node_policies": Object { + "base_fee_mtokens": "2000", + "cltv_delta": 1000, + "fee_rate": 1000, + "max_htlc_mtokens": "10000", + "min_htlc_mtokens": "10000", }, }, "partner_node_info": Object { @@ -228,15 +224,13 @@ Object { Object { "id": "100x1x1", "partner_fee_info": Object { - "channel": Object { - "node_policies": null, - "partner_node_policies": Object { - "base_fee_mtokens": "2000", - "cltv_delta": 1000, - "fee_rate": 1000, - "max_htlc_mtokens": "10000", - "min_htlc_mtokens": "10000", - }, + "node_policies": null, + "partner_node_policies": Object { + "base_fee_mtokens": "2000", + "cltv_delta": 1000, + "fee_rate": 1000, + "max_htlc_mtokens": "10000", + "min_htlc_mtokens": "10000", }, }, "partner_node_info": Object { @@ -280,13 +274,11 @@ Object { "local_balance": 1000, "local_reserve": 1000, "partner_fee_info": Object { - "channel": Object { - "node_policies": null, - "partner_node_policies": Object { - "base_fee_mtokens": "2000", - "cltv_delta": 1000, - "fee_rate": 1000, - }, + "node_policies": null, + "partner_node_policies": Object { + "base_fee_mtokens": "2000", + "cltv_delta": 1000, + "fee_rate": 1000, }, }, "partner_node_info": Object { @@ -332,13 +324,11 @@ Object { "local_balance": 1000, "local_reserve": 1000, "partner_fee_info": Object { - "channel": Object { - "node_policies": null, - "partner_node_policies": Object { - "base_fee_mtokens": "2000", - "cltv_delta": 1000, - "fee_rate": 1000, - }, + "node_policies": null, + "partner_node_policies": Object { + "base_fee_mtokens": "2000", + "cltv_delta": 1000, + "fee_rate": 1000, }, }, "partner_node_info": Object { @@ -384,13 +374,11 @@ Object { "local_balance": 1000, "local_reserve": 1000, "partner_fee_info": Object { - "channel": Object { - "node_policies": null, - "partner_node_policies": Object { - "base_fee_mtokens": "2000", - "cltv_delta": 1000, - "fee_rate": 1000, - }, + "node_policies": null, + "partner_node_policies": Object { + "base_fee_mtokens": "2000", + "cltv_delta": 1000, + "fee_rate": 1000, }, }, "partner_node_info": Object { diff --git a/src/graphql/queries/getChannelFees.ts b/src/graphql/queries/getChannelFees.ts index 38e07cca..5eb1674b 100644 --- a/src/graphql/queries/getChannelFees.ts +++ b/src/graphql/queries/getChannelFees.ts @@ -14,21 +14,19 @@ export const CHANNEL_FEES = gql` } } partner_fee_info { - channel { - node_policies { - base_fee_mtokens - fee_rate - cltv_delta - max_htlc_mtokens - min_htlc_mtokens - } - partner_node_policies { - base_fee_mtokens - fee_rate - cltv_delta - max_htlc_mtokens - min_htlc_mtokens - } + node_policies { + base_fee_mtokens + fee_rate + cltv_delta + max_htlc_mtokens + min_htlc_mtokens + } + partner_node_policies { + base_fee_mtokens + fee_rate + cltv_delta + max_htlc_mtokens + min_htlc_mtokens } } } diff --git a/src/graphql/queries/getChannels.ts b/src/graphql/queries/getChannels.ts index d5d2a707..8c0f2eb9 100644 --- a/src/graphql/queries/getChannels.ts +++ b/src/graphql/queries/getChannels.ts @@ -44,19 +44,17 @@ export const GET_CHANNELS = gql` } } partner_fee_info { - channel { - node_policies { - base_fee_mtokens - fee_rate - cltv_delta - max_htlc_mtokens - min_htlc_mtokens - } - partner_node_policies { - base_fee_mtokens - fee_rate - cltv_delta - } + node_policies { + base_fee_mtokens + fee_rate + cltv_delta + max_htlc_mtokens + min_htlc_mtokens + } + partner_node_policies { + base_fee_mtokens + fee_rate + cltv_delta } } } diff --git a/src/graphql/types.ts b/src/graphql/types.ts index f3558dd2..c10e9d8b 100644 --- a/src/graphql/types.ts +++ b/src/graphql/types.ts @@ -627,11 +627,6 @@ export type SingleChannelType = { partner_node_policies?: Maybe; }; -export type Channel = { - __typename?: 'Channel'; - channel?: Maybe; -}; - export type ChannelBalanceType = { __typename?: 'channelBalanceType'; confirmedBalance: Scalars['Int']; @@ -681,7 +676,7 @@ export type ChannelType = { transaction_vout: Scalars['Int']; unsettled_balance: Scalars['Int']; partner_node_info: Node; - partner_fee_info?: Maybe; + partner_fee_info?: Maybe; channel_age: Scalars['Int']; pending_payments: Array>; pending_resume: PendingResumeType; diff --git a/src/views/channels/channels/ChannelCard.tsx b/src/views/channels/channels/ChannelCard.tsx index 3db5375c..00ef1260 100644 --- a/src/views/channels/channels/ChannelCard.tsx +++ b/src/views/channels/channels/ChannelCard.tsx @@ -155,7 +155,7 @@ export const ChannelCard: React.FC = ({ } = partner_node_info?.node || {}; const { base_fee_mtokens, fee_rate, cltv_delta } = - partner_fee_info?.channel?.partner_node_policies || {}; + partner_fee_info?.partner_node_policies || {}; const { base_fee_mtokens: node_base, @@ -163,7 +163,7 @@ export const ChannelCard: React.FC = ({ cltv_delta: node_cltv, max_htlc_mtokens, min_htlc_mtokens, - } = partner_fee_info?.channel?.node_policies || {}; + } = partner_fee_info?.node_policies || {}; const formatBalance = format({ amount: capacity }); const formatLocal = format({ amount: local_balance }); diff --git a/src/views/channels/channels/Channels.tsx b/src/views/channels/channels/Channels.tsx index e8c31e30..ae08f474 100644 --- a/src/views/channels/channels/Channels.tsx +++ b/src/views/channels/channels/Channels.tsx @@ -72,10 +72,10 @@ export const Channels: React.FC = () => { const { capacity, channel_count } = partner_node_info?.node || {}; const { base_fee_mtokens, fee_rate } = - partner_fee_info?.channel?.partner_node_policies || {}; + partner_fee_info?.partner_node_policies || {}; const { base_fee_mtokens: nodeBase, fee_rate: nodeRate } = - partner_fee_info?.channel?.node_policies || {}; + partner_fee_info?.node_policies || {}; const partner = Number(capacity) || 0; const channels = Number(channel_count) || 0; @@ -157,8 +157,7 @@ export const Channels: React.FC = () => { case 'feeRate': { const newArray = sortBy( channels, - channel => - channel?.partner_fee_info?.channel?.partner_node_policies?.fee_rate + channel => channel?.partner_fee_info?.partner_node_policies?.fee_rate ); return sortDirection === 'increase' ? newArray : newArray.reverse(); } diff --git a/src/views/details/DetailsTable.tsx b/src/views/details/DetailsTable.tsx index b6e7a5f0..d120c4ac 100644 --- a/src/views/details/DetailsTable.tsx +++ b/src/views/details/DetailsTable.tsx @@ -67,7 +67,7 @@ export const DetailsTable = ({ channels }: DetailsTableType) => { return ( channels.map(channel => { - const policies = channel?.partner_fee_info?.channel?.node_policies; + const policies = channel?.partner_fee_info?.node_policies; let finalPolicies = {}; if (policies) { finalPolicies = {