Skip to content

Commit

Permalink
refactor: dedupe stamp ttl second conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Cafe137 committed Feb 14, 2025
1 parent 145f707 commit 7c7d645
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 46 deletions.
40 changes: 20 additions & 20 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
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.5.4",
"@upcoming/bee-js": "^0.5.5",
"axios": "^0.28.1",
"bignumber.js": "^9.1.2",
"buffer": "^6.0.3",
Expand Down
6 changes: 3 additions & 3 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 { convertAmountToSeconds, secondsToTimeString, waitUntilStampExists } from '../../utils'
import { secondsToTimeString, waitUntilStampExists } from '../../utils'
import { getHumanReadableFileSize } from '../../utils/file'

interface Props {
Expand Down Expand Up @@ -68,10 +68,10 @@ export function PostageStampAdvancedCreation({ onFinished }: Props): ReactElemen
return '-'
}

const pricePerBlock = BigInt(chainState.currentPrice)
const pricePerBlock = chainState.currentPrice

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

Expand Down
6 changes: 3 additions & 3 deletions src/pages/stamps/PostageStampStandardCreation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 { convertAmountToSeconds, secondsToTimeString, waitUntilStampExists } from '../../utils'
import { secondsToTimeString, waitUntilStampExists } from '../../utils'

interface Props {
onFinished: () => void
Expand Down Expand Up @@ -63,10 +63,10 @@ export function PostageStampStandardCreation({ onFinished }: Props): ReactElemen
const { enqueueSnackbar } = useSnackbar()

function getTtl(amount: bigint): string {
const pricePerBlock = BigInt(24000)
const pricePerBlock = 24000

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

Expand Down
31 changes: 12 additions & 19 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,43 +163,36 @@ export function formatEnum(string: string): string {
return (string.charAt(0).toUpperCase() + string.slice(1).toLowerCase()).replaceAll('_', ' ')
}

export function secondsToTimeString(seconds: number): string {
export function secondsToTimeString(seconds: number | bigint): string {
seconds = BigInt(seconds)
let unit = seconds

if (unit < 120) {
return `${seconds} seconds`
}
unit /= 60
unit /= BigInt(60)

if (unit < 120) {
return `${Math.round(unit)} minutes`
return `${unit} minutes`
}
unit /= 60
unit /= BigInt(60)

if (unit < 48) {
return `${Math.round(unit)} hours`
return `${unit} hours`
}
unit /= 24
unit /= BigInt(24)

if (unit < 14) {
return `${Math.round(unit)} days`
return `${unit} days`
}
unit /= 7
unit /= BigInt(7)

if (unit < 52) {
return `${Math.round(unit)} weeks`
return `${unit} weeks`
}
unit /= 52
unit /= BigInt(52)

return `${unit.toFixed(1)} years`
}

export function convertAmountToSeconds(amount: bigint, pricePerBlock: bigint): number {
// TODO: blocktime should come directly from the blockchain as it may differ between different networks
const blockTime = BigInt(5) // On mainnet there is 5 seconds between blocks

// See /~https://github.com/ethersphere/bee/blob/66f079930d739182c4c79eb6008784afeeba1096/pkg/debugapi/postage.go#L410-L413
return Number((amount * blockTime) / pricePerBlock)
return `${unit} years`
}

export function shortenText(text: string, length = 20, separator = '[…]'): string {
Expand Down

0 comments on commit 7c7d645

Please sign in to comment.