-
Notifications
You must be signed in to change notification settings - Fork 149
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
9 changed files
with
319 additions
and
23 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,158 @@ | ||
import React, { useState } from 'react'; | ||
import { PercentageOutlined, ReloadOutlined, SwapOutlined } from '@ant-design/icons'; | ||
import styled from '@emotion/styled'; | ||
import { Button, Col, Modal, Row, Slider } from 'antd'; | ||
import { ChannelInfo, LightningNode, PreInvoice } 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%; | ||
font-variant: small-caps; | ||
`, | ||
}; | ||
|
||
interface Props { | ||
network: Network; | ||
} | ||
|
||
const AutoBalanceButton: React.FC<Props> = ({ network }) => { | ||
const { getChannels } = useStoreActions(s => s.lightning); | ||
const { balanceChannels } = useStoreActions(s => s.network); | ||
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 hideModal = () => setVisible(false); | ||
const showModal = () => { | ||
updateInfo(); | ||
setVisible(true); | ||
}; | ||
|
||
const setNextBalance = (value: number, index: number) => { | ||
info[index].nextLocalBalance = value; | ||
setInfo([...info]); | ||
}; | ||
|
||
const autoBalance = () => { | ||
for (let index = 0; index < info.length; index += 1) { | ||
const { localBalance, remoteBalance } = info[index]; | ||
const halfAmount = Math.floor((Number(localBalance) + Number(remoteBalance)) / 2); | ||
info[index].nextLocalBalance = halfAmount; | ||
} | ||
setInfo([...info]); | ||
}; | ||
|
||
// 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); | ||
info.push({ id, to, from, localBalance, remoteBalance, nextLocalBalance }); | ||
} | ||
setInfo(info); | ||
} | ||
|
||
async function updateChannels() { | ||
const toPay = [] as PreInvoice[]; | ||
for (const { id, localBalance, nextLocalBalance } of info) { | ||
if (Number(localBalance) !== nextLocalBalance) { | ||
toPay.push({ channelId: id, nextLocalBalance }); | ||
} | ||
} | ||
balanceChannels({ id: network.id, toPay }) | ||
.then(() => notify({ message: 'Channels balanced!' })) | ||
.then(hideModal); | ||
} | ||
|
||
const CustomModalFooter = ( | ||
<Row gutter={10} justify="end"> | ||
<Col> | ||
<Styled.Button onClick={hideModal}>Close</Styled.Button> | ||
</Col> | ||
<Col> | ||
<Styled.Button onClick={updateChannels}>Update channels</Styled.Button> | ||
</Col> | ||
</Row> | ||
); | ||
|
||
return ( | ||
<> | ||
<Button onClick={showModal}> | ||
<SwapOutlined /> Balance channels | ||
</Button> | ||
<Modal | ||
title="Balance Channels" | ||
open={visible} | ||
onCancel={hideModal} | ||
footer={CustomModalFooter} | ||
style={{ fontVariant: 'small-caps' }} | ||
> | ||
{/* sliders */} | ||
{info.map((channel: ChannelInfo, index: number) => { | ||
const { to, from, id, 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, index)} | ||
min={0} | ||
max={total} | ||
/> | ||
</div> | ||
); | ||
})} | ||
{/* end sliders */} | ||
<br /> | ||
<Row gutter={10}> | ||
<Col span={12}> | ||
<Styled.Button onClick={updateInfo}> | ||
<ReloadOutlined /> Refresh | ||
</Styled.Button> | ||
</Col> | ||
<Col span={12}> | ||
<Styled.Button onClick={autoBalance}> | ||
<PercentageOutlined /> Auto Balance | ||
</Styled.Button> | ||
</Col> | ||
</Row> | ||
</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
Oops, something went wrong.