Skip to content

Commit

Permalink
chore: 🔧 add bos score to channels (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
apotdevin authored Jan 8, 2021
1 parent 343f0e7 commit e438f85
Show file tree
Hide file tree
Showing 18 changed files with 726 additions and 402 deletions.
1 change: 1 addition & 0 deletions server/schema/channel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const channelTypes = gql`
channel_age: Int!
pending_payments: [pendingPaymentType]!
pending_resume: pendingResumeType!
bosScore: BosScore
}
type closeChannelType {
Expand Down
50 changes: 50 additions & 0 deletions server/schema/tbase/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ const getBosNodeScoresQuery = `
}
`;

const getLastNodeScoreQuery = `
query GetLastNodeScore($publicKey: String!, $token: String!) {
getLastNodeScore(publicKey: $publicKey, token: $token) {
alias
public_key
score
updated
position
}
}
`;

export const tbaseResolvers = {
Query: {
getBaseInfo: async (_: undefined, __: undefined, context: ContextType) => {
Expand Down Expand Up @@ -318,4 +330,42 @@ export const tbaseResolvers = {
return true;
},
},
channelType: {
bosScore: async (
{
partner_public_key,
}: {
partner_public_key: string;
},
_: undefined,
{ tokenAuth }: ContextType
) => {
if (!tokenAuth) {
return null;
}

const { data, error } = await graphqlFetchWithProxy(
appUrls.tbase,
getLastNodeScoreQuery,
{
publicKey: partner_public_key,
token: tokenAuth,
}
);

if (error) {
return null;
}

return (
data?.getLastNodeScore || {
alias: '',
public_key: partner_public_key,
score: 0,
updated: '',
position: 0,
}
);
},
},
};
2 changes: 2 additions & 0 deletions src/components/price/Price.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ interface GetPriceProps {
noUnit?: boolean;
}

export type FormatFnType = (options: GetPriceProps) => string;

export const getPrice = (
currency: string,
displayValues: boolean,
Expand Down
10 changes: 10 additions & 0 deletions src/graphql/queries/__generated__/getChannels.generated.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ Object {
"data": Object {
"getChannels": Array [
Object {
"bosScore": null,
"capacity": 1000,
"channel_age": 123356,
"commit_transaction_fee": 1000,
Expand Down Expand Up @@ -310,6 +311,7 @@ Object {
"unsettled_balance": 1000,
},
Object {
"bosScore": null,
"capacity": 1000,
"channel_age": 123356,
"commit_transaction_fee": 1000,
Expand Down Expand Up @@ -360,6 +362,7 @@ Object {
"unsettled_balance": 1000,
},
Object {
"bosScore": null,
"capacity": 1000,
"channel_age": 123356,
"commit_transaction_fee": 1000,
Expand Down
7 changes: 7 additions & 0 deletions src/graphql/queries/getChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ export const GET_CHANNELS = gql`
cltv_delta
}
}
bosScore {
alias
public_key
score
updated
position
}
}
}
`;
1 change: 1 addition & 0 deletions src/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ export type ChannelType = {
channel_age: Scalars['Int'];
pending_payments: Array<Maybe<PendingPaymentType>>;
pending_resume: PendingResumeType;
bosScore?: Maybe<BosScore>;
};

export type CloseChannelType = {
Expand Down
166 changes: 166 additions & 0 deletions src/views/channels/channels/ChannelBars.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import { FC } from 'react';
import { BalanceBars, SingleBar, SumBar } from 'src/components/balance';
import { ProgressBar } from 'src/components/generic/CardGeneric';
import { FormatFnType } from 'src/components/price/Price';
import { useConfigState } from 'src/context/ConfigContext';
import { ChannelType } from 'src/graphql/types';
import { getPercent } from 'src/utils/helpers';
import { ChannelStatsColumn, ChannelStatsLine } from './Channel.style';
import { WUMBO_MIN_SIZE } from './Channels';

const MAX_HTLCS = 483;

const getBar = (top: number, bottom: number) => {
const percent = (top / bottom) * 100;
return Math.min(percent, 100);
};

type ChannelBarsProps = {
info: ChannelType;
format: FormatFnType;
details: {
biggestRateFee: number;
biggestBaseFee: number;
biggestPartner: number;
mostChannels: number;
biggest: number;
};
};

export const ChannelBars: FC<ChannelBarsProps> = ({
info,
format,
details: {
biggestRateFee,
biggestBaseFee,
biggestPartner,
mostChannels,
biggest,
},
}) => {
const {
partner_fee_info,
partner_node_info,
local_balance,
remote_balance,
pending_resume,
} = info;

const { incoming_amount = 200, outgoing_amount = 150 } = pending_resume;

const { capacity: partnerNodeCapacity = 0, channel_count } =
partner_node_info?.node || {};

const { base_fee_mtokens, fee_rate } =
partner_fee_info?.partner_node_policies || {};

const { base_fee_mtokens: node_base, fee_rate: node_rate } =
partner_fee_info?.node_policies || {};

const { channelBarType, subBar } = useConfigState();

const maxRate = Math.min(fee_rate || 0, 10000);
const maxNodeRate = Math.min(node_rate || 0, 10000);

const localBalance = format({
amount: local_balance,
breakNumber: true,
noUnit: true,
});
const remoteBalance = format({
amount: remote_balance,
breakNumber: true,
noUnit: true,
});

switch (channelBarType) {
case 'fees':
return (
<ChannelStatsColumn>
<BalanceBars
local={getBar(maxNodeRate, biggestRateFee)}
remote={getBar(maxRate, biggestRateFee)}
height={10}
/>
<BalanceBars
local={getBar(Number(node_base), biggestBaseFee)}
remote={getBar(Number(base_fee_mtokens), biggestBaseFee)}
height={10}
/>
</ChannelStatsColumn>
);
case 'size':
return (
<ChannelStatsColumn>
<ChannelStatsLine>
<ProgressBar
order={0}
percent={getBar(Number(partnerNodeCapacity), biggestPartner)}
/>
<ProgressBar
order={4}
percent={getBar(
biggestPartner - Number(partnerNodeCapacity),
biggestPartner
)}
/>
</ChannelStatsLine>
{channel_count && (
<ChannelStatsLine>
<ProgressBar
order={6}
percent={getBar(channel_count, mostChannels)}
/>
<ProgressBar
order={4}
percent={getBar(mostChannels - channel_count, mostChannels)}
/>
</ChannelStatsLine>
)}
</ChannelStatsColumn>
);
case 'proportional':
return (
<ChannelStatsColumn>
{subBar === 'fees' && (
<SingleBar value={getBar(maxRate, 2000)} height={4} />
)}
<BalanceBars
local={getBar(local_balance, biggest)}
remote={getBar(remote_balance, biggest)}
formatLocal={localBalance}
formatRemote={remoteBalance}
withBorderColor={local_balance + remote_balance >= WUMBO_MIN_SIZE}
/>
</ChannelStatsColumn>
);
case 'htlcs':
return (
<ChannelStatsColumn>
{subBar === 'fees' && (
<SingleBar value={getBar(maxRate, 2000)} height={4} />
)}
<SumBar
values={[
getPercent(incoming_amount, MAX_HTLCS - incoming_amount),
getPercent(outgoing_amount, MAX_HTLCS - outgoing_amount),
]}
/>
</ChannelStatsColumn>
);
default:
return (
<ChannelStatsColumn>
{subBar === 'fees' && (
<SingleBar value={getBar(maxRate, 2000)} height={4} />
)}
<BalanceBars
local={getPercent(local_balance, remote_balance)}
remote={getPercent(remote_balance, local_balance)}
formatLocal={localBalance}
formatRemote={remoteBalance}
/>
</ChannelStatsColumn>
);
}
};
Loading

0 comments on commit e438f85

Please sign in to comment.