-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enables manual balancing of channels
User can manually balance channels using a slicer. The channels are shown in a modal after clicking a button. NOTE: this is a prototype, not working yet.
- Loading branch information
Showing
8 changed files
with
1,601 additions
and
1,737 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import styled from '@emotion/styled'; | ||
import { Button, Col, Modal, Row, Slider } from 'antd'; | ||
import { LightningNode } from 'shared/types'; | ||
import { LightningNodeChannel } from 'lib/lightning/types'; | ||
import { useStoreActions, useStoreState } from 'store'; | ||
import { Network } from 'types'; | ||
|
||
const Styled = { | ||
Button: styled(Button)` | ||
width: 100%; | ||
`, | ||
}; | ||
|
||
interface Props { | ||
network: Network; | ||
} | ||
|
||
interface ChannelInfo { | ||
i: number; | ||
id: string; | ||
to: string; | ||
from: string; | ||
localBalance: string; | ||
remoteBalance: string; | ||
nextLocalBalance: number; | ||
} | ||
|
||
const AutoBalanceButton: React.FC<Props> = ({ network }) => { | ||
const { getChannels } = useStoreActions(s => s.lightning); | ||
const { links } = useStoreState(s => s.designer.activeChart); | ||
// const { notify } = useStoreActions(s => s.app); | ||
const [visible, setVisible] = useState(false); | ||
const [info, setInfo] = useState([] as ChannelInfo[]); | ||
|
||
const showModal = () => setVisible(true); | ||
const hideModal = () => setVisible(false); | ||
|
||
// Store all channels in an array and build a map nodeName->node. | ||
async function updateInfo() { | ||
const channels = [] as LightningNodeChannel[]; | ||
const id2Node = {} as Record<string, LightningNode>; | ||
const promisesToAwait = [] as Promise<unknown>[]; | ||
|
||
for (const node of network.nodes.lightning) { | ||
promisesToAwait.push( | ||
getChannels(node).then((nodeChannels: LightningNodeChannel[]) => { | ||
channels.push(...nodeChannels); | ||
id2Node[node.name] = node; | ||
}), | ||
); | ||
} | ||
await Promise.all(promisesToAwait); | ||
|
||
const info = [] as ChannelInfo[]; | ||
for (const channel of channels) { | ||
const { uniqueId: id, localBalance, remoteBalance } = channel; | ||
if (!links[id]) continue; | ||
const from = links[id].from.nodeId; | ||
const to = links[id].to.nodeId as string; | ||
const nextLocalBalance = Number(localBalance); | ||
const i = info.length; | ||
info.push({ i, id, to, from, localBalance, remoteBalance, nextLocalBalance }); | ||
} | ||
setInfo(info); | ||
} | ||
|
||
const setNextBalance = (value: number, index: number) => { | ||
info[index].nextLocalBalance = value; | ||
setInfo([...info]); | ||
}; | ||
|
||
useEffect(() => { | ||
updateInfo(); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<Styled.Button onClick={showModal}>Balance channels</Styled.Button> | ||
<Modal | ||
title="Balance Channels" | ||
open={visible} | ||
// onOk={handleClick} | ||
onCancel={hideModal} | ||
> | ||
{/* sliders */} | ||
{info.map((channel: ChannelInfo) => { | ||
const { to, from, id, i, remoteBalance, localBalance, nextLocalBalance } = | ||
channel; | ||
const total = Number(remoteBalance) + Number(localBalance); | ||
return ( | ||
<div key={id}> | ||
<Row> | ||
<Col span={12}> | ||
{from} | ||
<br /> | ||
{nextLocalBalance} | ||
</Col> | ||
<Col span={12} style={{ textAlign: 'right' }}> | ||
{to} | ||
<br /> | ||
{total - nextLocalBalance} | ||
</Col> | ||
</Row> | ||
<Slider | ||
value={nextLocalBalance} | ||
onChange={value => setNextBalance(value, i)} | ||
min={0} | ||
max={total} | ||
/> | ||
</div> | ||
); | ||
})} | ||
{/* end sliders */} | ||
<br /> | ||
<Styled.Button onClick={updateInfo}>Refresh</Styled.Button> | ||
</Modal> | ||
</> | ||
); | ||
}; | ||
|
||
export default AutoBalanceButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.