Skip to content

Commit 5871223

Browse files
LevilkTheRealLevente Kiss
andauthored
feat: display effective capacity (#643)
* feat: add stamp effective volume display (#636) * refactor: make it cleaner (#636) --------- Co-authored-by: Levente Kiss <levente.kiss@solarpunk.bzz>
1 parent cc91f1d commit 5871223

2 files changed

Lines changed: 44 additions & 23 deletions

File tree

src/pages/stamps/PostageStampAdvancedCreation.tsx

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
import { PostageBatchOptions } from '@ethersphere/bee-js'
2-
import { Box, Grid, Typography, createStyles, makeStyles } from '@material-ui/core'
1+
import { PostageBatchOptions, Utils } from '@ethersphere/bee-js'
2+
import { Box, Grid, IconButton, Typography, createStyles, makeStyles } from '@material-ui/core'
33
import BigNumber from 'bignumber.js'
44
import { useSnackbar } from 'notistack'
55
import { ReactElement, useContext, useState } from 'react'
66
import { Link } from 'react-router-dom'
77
import Check from 'remixicon-react/CheckLineIcon'
8+
import Info from 'remixicon-react/InformationLineIcon'
89
import { SwarmButton } from '../../components/SwarmButton'
910
import { SwarmSelect } from '../../components/SwarmSelect'
1011
import { SwarmTextInput } from '../../components/SwarmTextInput'
1112
import { Context as BeeContext } from '../../providers/Bee'
1213
import { Context as SettingsContext } from '../../providers/Settings'
1314
import { Context as StampsContext } from '../../providers/Stamps'
1415
import { ROUTES } from '../../routes'
15-
import {
16-
calculateStampPrice,
17-
convertAmountToSeconds,
18-
convertDepthToBytes,
19-
secondsToTimeString,
20-
waitUntilStampExists,
21-
} from '../../utils'
16+
import { calculateStampPrice, convertAmountToSeconds, secondsToTimeString, waitUntilStampExists } from '../../utils'
2217
import { getHumanReadableFileSize } from '../../utils/file'
2318

2419
interface Props {
@@ -39,6 +34,14 @@ const useStyles = makeStyles(() =>
3934
},
4035
},
4136
},
37+
stampVolumeWrapper: {
38+
width: 'fit-content',
39+
'& button': {
40+
marginLeft: 4,
41+
width: 24,
42+
padding: 2,
43+
},
44+
},
4245
}),
4346
)
4447

@@ -58,14 +61,6 @@ export function PostageStampAdvancedCreation({ onFinished }: Props): ReactElemen
5861

5962
const { enqueueSnackbar } = useSnackbar()
6063

61-
function getFileSize(depth: number): string {
62-
if (isNaN(depth) || depth < 17 || depth > 255) {
63-
return '-'
64-
}
65-
66-
return `~${getHumanReadableFileSize(convertDepthToBytes(depth))}`
67-
}
68-
6964
function getTtl(amount: number): string {
7065
const isCurrentPriceAvailable = chainState && chainState.currentPrice
7166

@@ -171,6 +166,36 @@ export function PostageStampAdvancedCreation({ onFinished }: Props): ReactElemen
171166
setDepthInput(validDepthInput)
172167
}
173168

169+
function renderStampVolumesInfo() {
170+
const depth = parseInt(depthInput, 10)
171+
172+
if (depthError || isNaN(depth) || depth < 17 || depth > 255) {
173+
return '-'
174+
}
175+
176+
const theoreticalMaximumVolume = getHumanReadableFileSize(Utils.getStampMaximumCapacityBytes(depth))
177+
const effectiveVolume = getHumanReadableFileSize(Utils.getStampEffectiveBytes(depth))
178+
179+
return (
180+
<Grid item container alignItems="center" className={classes.stampVolumeWrapper}>
181+
<Typography>
182+
Theoretical: ~{theoreticalMaximumVolume} / Effective: ~{effectiveVolume}
183+
</Typography>
184+
<IconButton
185+
onClick={() =>
186+
window.open(
187+
'https://docs.ethswarm.org/docs/learn/technology/contracts/postage-stamp/#effective-utilisation-table',
188+
'_blank',
189+
'noopener,noreferrer',
190+
)
191+
}
192+
>
193+
<Info />
194+
</IconButton>
195+
</Grid>
196+
)
197+
}
198+
174199
return (
175200
<>
176201
<Box mb={4}>
@@ -190,9 +215,9 @@ export function PostageStampAdvancedCreation({ onFinished }: Props): ReactElemen
190215
<Box mb={2}>
191216
<SwarmTextInput name="depth" label="Depth" onChange={event => validateDepthInput(event.target.value)} />
192217
<Box mt={0.25} sx={{ bgcolor: '#f6f6f6' }} p={2}>
193-
<Grid container justifyContent="space-between">
218+
<Grid container justifyContent="space-between" alignItems="center">
194219
<Typography>Corresponding file size</Typography>
195-
<Typography>{!depthError && depthInput ? getFileSize(parseInt(depthInput, 10)) : '-'}</Typography>
220+
{renderStampVolumesInfo()}
196221
</Grid>
197222
</Box>
198223
{depthError && <Typography>{depthError}</Typography>}

src/utils/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,6 @@ export function secondsToTimeString(seconds: number): string {
203203
return `${unit.toFixed(1)} years`
204204
}
205205

206-
export function convertDepthToBytes(depth: number): number {
207-
return 2 ** depth * 4096
208-
}
209-
210206
export function convertAmountToSeconds(amount: number, pricePerBlock: number): number {
211207
// TODO: blocktime should come directly from the blockchain as it may differ between different networks
212208
const blockTime = 5 // On mainnet there is 5 seconds between blocks

0 commit comments

Comments
 (0)