feat: add light node upgrade top up methods (#372)

* feat: add top up

* chore: remove console.log

* build: add pseudo-missing dependency

* feat: add missing top up components

* fix: crypto route

* feat(wip): add gift wallet logic

* fix: fix gift wallet flows

* feat: simplify flow without fund step

* feat: add loading screens

* fix: remove alert

* fix: prepend http if needed

* fix: fix bug that was reintroduced with merge

* refactor: rename minusEther to minusBaseUnits

* fix: remove unused setStartedAt

* build: remove unused dependency
This commit is contained in:
Cafe137
2022-06-02 09:28:43 +02:00
committed by GitHub
parent 026783924f
commit a768b4ea06
35 changed files with 1196 additions and 215 deletions
+107
View File
@@ -0,0 +1,107 @@
import { Box, Typography } from '@material-ui/core'
import { useSnackbar } from 'notistack'
import { ReactElement, useContext, useEffect, useState } from 'react'
import { ArrowDown, Check } from 'react-feather'
import { useNavigate, useParams } from 'react-router'
import ExpandableListItem from '../../components/ExpandableListItem'
import ExpandableListItemKey from '../../components/ExpandableListItemKey'
import { HistoryHeader } from '../../components/HistoryHeader'
import { Loading } from '../../components/Loading'
import { ProgressIndicator } from '../../components/ProgressIndicator'
import { SwarmButton } from '../../components/SwarmButton'
import { SwarmDivider } from '../../components/SwarmDivider'
import { Context as BeeContext } from '../../providers/Bee'
import { Context as TopUpContext } from '../../providers/TopUp'
import { ROUTES } from '../../routes'
import { sleepMs } from '../../utils'
import { restartBeeNode, upgradeToLightNode } from '../../utils/desktop'
import { ResolvedWallet } from '../../utils/wallet'
export function GiftCardFund(): ReactElement {
const { nodeAddresses, balance } = useContext(BeeContext)
const { jsonRpcProvider } = useContext(TopUpContext)
const [loading, setLoading] = useState(false)
const [wallet, setWallet] = useState<ResolvedWallet | null>(null)
const { privateKeyString } = useParams()
const { enqueueSnackbar } = useSnackbar()
const navigate = useNavigate()
useEffect(() => {
if (!privateKeyString) {
return
}
ResolvedWallet.make(privateKeyString).then(setWallet)
}, [privateKeyString])
if (!wallet || !balance) {
return <Loading />
}
async function onFund() {
if (!wallet || !nodeAddresses) {
return
}
setLoading(true)
try {
await wallet.transfer(nodeAddresses.ethereum)
enqueueSnackbar('Successfully funded node, restarting...', { variant: 'success' })
await sleepMs(5_000)
await upgradeToLightNode(jsonRpcProvider)
await restartBeeNode()
navigate(ROUTES.RESTART_LIGHT)
} catch (error) {
enqueueSnackbar(`Failed to fund/restart node: ${error}`, { variant: 'error' })
} finally {
setLoading(false)
}
}
return (
<>
<HistoryHeader>Top-up with gift code</HistoryHeader>
<Box mb={4}>
<ProgressIndicator index={1} steps={['Paste gift code', 'Fund your node']} />
</Box>
<Box mb={2}>
<Typography style={{ fontWeight: 'bold' }}>Send funds to your Bee node</Typography>
</Box>
<Box mb={4}>
<Typography>
Deposit all the funds from the gift wallet to your node wallet address. You can use the button below to
transfer all funds to your node.
</Typography>
</Box>
<SwarmDivider mb={4} />
<Box mb={0.25}>
<ExpandableListItemKey label="Gift wallet address" value={wallet.address || 'N/A'} />
</Box>
<Box mb={0.25}>
<ExpandableListItem label="XDAI balance" value={`${wallet.dai.toSignificantDigits(4)} XDAI`} />
</Box>
<Box mb={4}>
<ExpandableListItem label="BZZ balance" value={`${wallet.bzz.toSignificantDigits(4)} BZZ`} />
</Box>
<Box mb={4}>
<ArrowDown size={24} color="#aaaaaa" />
</Box>
<Box mb={0.25}>
<ExpandableListItemKey label="Node wallet address" value={nodeAddresses?.ethereum || 'N/A'} expanded />
</Box>
<Box mb={0.25}>
<ExpandableListItem label="XDAI balance" value={`${balance.dai.toSignificantDigits(4)} XDAI`} />
</Box>
<Box mb={2}>
<ExpandableListItem label="BZZ balance" value={`${balance.bzz.toSignificantDigits(4)} BZZ`} />
</Box>
<SwarmButton iconType={Check} onClick={onFund} disabled={loading} loading={loading}>
Send all funds to your node
</SwarmButton>
</>
)
}