diff --git a/package-lock.json b/package-lock.json index 40e8ad1c..41c7c480 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,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", @@ -5452,13 +5452,13 @@ } }, "node_modules/@upcoming/bee-js": { - "version": "0.5.4", - "resolved": "https://registry.npmjs.org/@upcoming/bee-js/-/bee-js-0.5.4.tgz", - "integrity": "sha512-OMJRg686HwN/y/bWa9IbpRIbZtZB2K4inFjQ6KxIs+ijoZvQPiaUugJqb71lupEYp4xTYhOGyX8QpeVWOyPorg==", + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/@upcoming/bee-js/-/bee-js-0.5.5.tgz", + "integrity": "sha512-QM+5VR8AK8sYkyUHx1tIIVgHB6FWzWBIcxRA/G6CBIIVmI/XGHpzlh8ltyglBY4AM3QFfX8w3ZJgF51LznOegQ==", "license": "BSD-3-Clause", "dependencies": { "axios": "^0.28.1", - "cafe-utility": "^27.9.1", + "cafe-utility": "^27.10.0", "isomorphic-ws": "^4.0.1", "semver": "^7.3.5", "ws": "^8.7.0" @@ -5468,12 +5468,6 @@ "beeApiVersion": "7.2.0" } }, - "node_modules/@upcoming/bee-js/node_modules/cafe-utility": { - "version": "27.9.1", - "resolved": "https://registry.npmjs.org/cafe-utility/-/cafe-utility-27.9.1.tgz", - "integrity": "sha512-iDlOb7K+Uln+k4OMHjeynQOZYDk10nQTf5zD2FNWKn9wviAtOtKTVFBIMETdRnumLeS3Z0ywDgYJkv9KZOfpZg==", - "license": "MIT" - }, "node_modules/@upcoming/bee-js/node_modules/ws": { "version": "8.18.0", "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz", @@ -7043,6 +7037,12 @@ "node": ">= 0.8" } }, + "node_modules/cafe-utility": { + "version": "27.10.0", + "resolved": "https://registry.npmjs.org/cafe-utility/-/cafe-utility-27.10.0.tgz", + "integrity": "sha512-b1oLKxYtwPkmSi9E11w0se6bPn7yfr25l7PIJ7R4pZ1bYOotyMwHRi+UunJN2Y6TEWhXCYaeA+RI54ovX164Ag==", + "license": "MIT" + }, "node_modules/call-bind": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz", @@ -24429,22 +24429,17 @@ } }, "@upcoming/bee-js": { - "version": "0.5.4", - "resolved": "https://registry.npmjs.org/@upcoming/bee-js/-/bee-js-0.5.4.tgz", - "integrity": "sha512-OMJRg686HwN/y/bWa9IbpRIbZtZB2K4inFjQ6KxIs+ijoZvQPiaUugJqb71lupEYp4xTYhOGyX8QpeVWOyPorg==", + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/@upcoming/bee-js/-/bee-js-0.5.5.tgz", + "integrity": "sha512-QM+5VR8AK8sYkyUHx1tIIVgHB6FWzWBIcxRA/G6CBIIVmI/XGHpzlh8ltyglBY4AM3QFfX8w3ZJgF51LznOegQ==", "requires": { "axios": "^0.28.1", - "cafe-utility": "^27.9.1", + "cafe-utility": "^27.10.0", "isomorphic-ws": "^4.0.1", "semver": "^7.3.5", "ws": "^8.7.0" }, "dependencies": { - "cafe-utility": { - "version": "27.9.1", - "resolved": "https://registry.npmjs.org/cafe-utility/-/cafe-utility-27.9.1.tgz", - "integrity": "sha512-iDlOb7K+Uln+k4OMHjeynQOZYDk10nQTf5zD2FNWKn9wviAtOtKTVFBIMETdRnumLeS3Z0ywDgYJkv9KZOfpZg==" - }, "ws": { "version": "8.18.0", "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz", @@ -25681,6 +25676,11 @@ "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", "integrity": "sha512-pMhOfFDPiv9t5jjIXkHosWmkSyQbvsgEVNkz0ERHbuLh2T/7j4Mqqpz523Fe8MVY89KC6Sh/QfS2sM+SjgFDcw==" }, + "cafe-utility": { + "version": "27.10.0", + "resolved": "https://registry.npmjs.org/cafe-utility/-/cafe-utility-27.10.0.tgz", + "integrity": "sha512-b1oLKxYtwPkmSi9E11w0se6bPn7yfr25l7PIJ7R4pZ1bYOotyMwHRi+UunJN2Y6TEWhXCYaeA+RI54ovX164Ag==" + }, "call-bind": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz", diff --git a/package.json b/package.json index 3189aecc..fa1ad165 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/pages/stamps/PostageStampAdvancedCreation.tsx b/src/pages/stamps/PostageStampAdvancedCreation.tsx index 5249422a..3fa5036b 100644 --- a/src/pages/stamps/PostageStampAdvancedCreation.tsx +++ b/src/pages/stamps/PostageStampAdvancedCreation.tsx @@ -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 { @@ -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)` } diff --git a/src/pages/stamps/PostageStampStandardCreation.tsx b/src/pages/stamps/PostageStampStandardCreation.tsx index e1a06821..939e6fa4 100644 --- a/src/pages/stamps/PostageStampStandardCreation.tsx +++ b/src/pages/stamps/PostageStampStandardCreation.tsx @@ -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 @@ -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)` } diff --git a/src/utils/index.ts b/src/utils/index.ts index 5721f66a..6f9af497 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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 {