-
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,574 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,95 @@ | ||
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; | ||
} | ||
|
||
const AutoBalanceButton: React.FC<Props> = ({ network }) => { | ||
const { getChannels } = useStoreActions(s => s.lightning); | ||
// const { notify } = useStoreActions(s => s.app); | ||
const { links } = useStoreState(s => s.designer.activeChart); | ||
const [visible, setVisible] = useState(false); | ||
const [info, setInfo] = useState([] as any); | ||
|
||
const showModal = () => setVisible(true); | ||
const hideModal = () => setVisible(false); | ||
|
||
// Store all channels in an array and build a map nodeName->node. | ||
// const lnNodes = network.nodes.lightning; | ||
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 = []; | ||
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; | ||
info.push({ id, to, from, localBalance, remoteBalance }); | ||
} | ||
setInfo(info); | ||
} | ||
|
||
useEffect(() => { | ||
updateInfo(); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<Styled.Button onClick={showModal}>Balance channels</Styled.Button> | ||
<Modal | ||
title="Balance Channels" | ||
open={visible} | ||
// onOk={handleClick} | ||
onCancel={hideModal} | ||
> | ||
{info.map((t: any) => { | ||
const { to, from, id, remoteBalance, localBalance } = t; | ||
return ( | ||
<div key={id}> | ||
<Row> | ||
<Col span={12}> | ||
{from} | ||
<br /> | ||
{localBalance} | ||
</Col> | ||
<Col span={12} style={{ textAlign: 'right' }}> | ||
{to} | ||
<br /> | ||
{remoteBalance} | ||
</Col> | ||
</Row> | ||
<Slider min={0} max={localBalance + remoteBalance} /> | ||
</div> | ||
); | ||
})} | ||
</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.