Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: upcoming bee js 0.10.0 #692

Draft
wants to merge 1 commit into
base: feat/bee-js-revamp
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@upcoming/bee-dashboard",
"version": "0.1.0",
"version": "0.3.0",
"description": "An app which helps users to setup their Bee node and do actions like cash out cheques",
"keywords": [
"bee",
Expand Down Expand Up @@ -30,7 +30,7 @@
"@material-ui/core": "4.12.3",
"@material-ui/icons": "4.11.2",
"@material-ui/lab": "4.0.0-alpha.57",
"@upcoming/bee-js": "^0.8.1",
"@upcoming/bee-js": "^0.10.0",
"axios": "^0.28.1",
"bignumber.js": "^9.1.2",
"buffer": "^6.0.3",
Expand Down
9 changes: 4 additions & 5 deletions src/pages/stamps/PostageStampAdvancedCreation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Context as BeeContext } from '../../providers/Bee'
import { Context as SettingsContext } from '../../providers/Settings'
import { Context as StampsContext } from '../../providers/Stamps'
import { ROUTES } from '../../routes'
import { secondsToTimeString, waitUntilStampExists } from '../../utils'
import { secondsToTimeString } from '../../utils'
import { getHumanReadableFileSize } from '../../utils/file'

interface Props {
Expand Down Expand Up @@ -71,7 +71,7 @@ export function PostageStampAdvancedCreation({ onFinished }: Props): ReactElemen
const pricePerBlock = chainState.currentPrice

return `${secondsToTimeString(
Utils.getStampTtlSeconds(amount, pricePerBlock),
Utils.getStampDuration(amount, pricePerBlock).toSeconds(),
)} (with price of ${pricePerBlock} PLUR per block)`
}

Expand Down Expand Up @@ -107,8 +107,7 @@ export function PostageStampAdvancedCreation({ onFinished }: Props): ReactElemen
immutableFlag: immutable,
}

const batchId = await beeApi.createPostageBatch(amount.toString(), depth, options)
await waitUntilStampExists(batchId, beeApi)
await beeApi.createPostageBatch(amount.toString(), depth, options)
await refresh()
onFinished()
} catch (e) {
Expand Down Expand Up @@ -173,7 +172,7 @@ export function PostageStampAdvancedCreation({ onFinished }: Props): ReactElemen
return '-'
}

const theoreticalMaximumVolume = getHumanReadableFileSize(Utils.getStampMaximumCapacityBytes(depth))
const theoreticalMaximumVolume = getHumanReadableFileSize(Utils.getStampTheoreticalBytes(depth))
const effectiveVolume = getHumanReadableFileSize(Utils.getStampEffectiveBytes(depth))

return (
Expand Down
17 changes: 8 additions & 9 deletions src/pages/stamps/PostageStampStandardCreation.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Box, Button, Grid, Slider, Typography } from '@material-ui/core'
import { Theme, createStyles, makeStyles } from '@material-ui/core/styles'
import { PostageBatchOptions, Utils } from '@upcoming/bee-js'
import { Duration, PostageBatchOptions, Utils } from '@upcoming/bee-js'
import { useSnackbar } from 'notistack'
import { ReactElement, useContext, useState } from 'react'
import { Link } from 'react-router-dom'
Expand All @@ -10,7 +10,7 @@ import { SwarmTextInput } from '../../components/SwarmTextInput'
import { Context as SettingsContext } from '../../providers/Settings'
import { Context as StampsContext } from '../../providers/Stamps'
import { ROUTES } from '../../routes'
import { secondsToTimeString, waitUntilStampExists } from '../../utils'
import { secondsToTimeString } from '../../utils'

interface Props {
onFinished: () => void
Expand Down Expand Up @@ -46,8 +46,8 @@ export function PostageStampStandardCreation({ onFinished }: Props): ReactElemen
const { refresh } = useContext(StampsContext)
const { beeApi } = useContext(SettingsContext)

const [depthInput, setDepthInput] = useState<number>(Utils.getDepthForCapacity(4))
const [amountInput, setAmountInput] = useState<bigint>(Utils.getAmountForTtl(30))
const [depthInput, setDepthInput] = useState<number>(Utils.getDepthForSize(4))
const [amountInput, setAmountInput] = useState<bigint>(Utils.getAmountForDuration(Duration.fromDays(30), 26500))
const [labelInput, setLabelInput] = useState('')
const [submitting, setSubmitting] = useState(false)
const [buttonValue, setButtonValue] = useState(4)
Expand All @@ -56,7 +56,7 @@ export function PostageStampStandardCreation({ onFinished }: Props): ReactElemen
if (typeof newValue !== 'number') {
return
}
const amountValue = Utils.getAmountForTtl(newValue)
const amountValue = Utils.getAmountForDuration(Duration.fromDays(newValue), 26500)
setAmountInput(amountValue)
}

Expand All @@ -66,7 +66,7 @@ export function PostageStampStandardCreation({ onFinished }: Props): ReactElemen
const pricePerBlock = 24000

return `${secondsToTimeString(
Utils.getStampTtlSeconds(amount, pricePerBlock),
Utils.getStampDuration(amount, pricePerBlock).toSeconds(),
)} (with price of ${pricePerBlock} PLUR per block)`
}

Expand Down Expand Up @@ -96,8 +96,7 @@ export function PostageStampStandardCreation({ onFinished }: Props): ReactElemen
immutableFlag: true,
}

const batchId = await beeApi.createPostageBatch(amount.toString(), depth, options)
await waitUntilStampExists(batchId, beeApi)
await beeApi.createPostageBatch(amount.toString(), depth, options)
await refresh()
onFinished()
} catch (e) {
Expand All @@ -109,7 +108,7 @@ export function PostageStampStandardCreation({ onFinished }: Props): ReactElemen

function handleBatchSize(gigabytes: number) {
setButtonValue(gigabytes)
const capacity = Utils.getDepthForCapacity(gigabytes)
const capacity = Utils.getDepthForSize(gigabytes)
setDepthInput(capacity)
}

Expand Down
6 changes: 1 addition & 5 deletions src/pages/stamps/StampsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,9 @@ function StampsTable({ postageStamps }: Props): ReactElement | null {
)}`}
/>
<ExpandableListItem label="Amount" value={parseInt(stamp.amount, 10).toLocaleString()} />
<ExpandableListItem
label="Expires in"
value={stamp.batchTTL === -1 ? 'does not expire' : `${secondsToTimeString(stamp.batchTTL)}`}
/>
<ExpandableListItem label="Expires in" value={secondsToTimeString(stamp.duration.toSeconds())} />
<ExpandableListItem label="Label" value={stamp.label} />
<ExpandableListItem label="Usable" value={stamp.usable ? 'yes' : 'no'} />
<ExpandableListItem label="Exists" value={stamp.exists ? 'yes' : 'no'} />
<ExpandableListItem label="Immutable" value={stamp.immutableFlag ? 'yes' : 'no'} />
<ExpandableListItem label="Purchase Block Number" value={stamp.blockNumber} />
<ExpandableListItemActions>
Expand Down
2 changes: 1 addition & 1 deletion src/providers/Stamps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function Provider({ children }: Props): ReactElement {
setIsLoading(true)
const stamps = await beeApi.getAllPostageBatch()

setStamps(stamps.filter(x => x.exists).map(enrichStamp))
setStamps(stamps.map(enrichStamp))
setLastUpdate(Date.now())
setError(null)
} catch (e) {
Expand Down
17 changes: 5 additions & 12 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,27 +212,20 @@ interface Options {
}

export function waitUntilStampUsable(batchId: BatchId | string, bee: Bee, options?: Options): Promise<PostageBatch> {
return waitForStamp(batchId, bee, 'usable', options)
return waitForStamp(batchId, bee, options)
}

export function waitUntilStampExists(batchId: BatchId | string, bee: Bee, options?: Options): Promise<PostageBatch> {
return waitForStamp(batchId, bee, 'exists', options)
}

async function waitForStamp(
batchId: BatchId | string,
bee: Bee,
field: 'exists' | 'usable',
options?: Options,
): Promise<PostageBatch> {
async function waitForStamp(batchId: BatchId | string, bee: Bee, options?: Options): Promise<PostageBatch> {
const timeout = options?.timeout || DEFAULT_STAMP_USABLE_TIMEOUT
const pollingFrequency = options?.pollingFrequency || DEFAULT_POLLING_FREQUENCY

for (let i = 0; i < timeout; i += pollingFrequency) {
try {
const stamp = await bee.getPostageBatch(batchId)

if (stamp[field]) return stamp
if (stamp.usable) {
return stamp
}
} catch {
// ignore
}
Expand Down
Loading