fix: get current price from chain state (#286)

* fix: get current price from chain state

* refactor: do not allow optional currentPrice
This commit is contained in:
Cafe137
2022-01-20 15:49:41 +01:00
committed by GitHub
parent 63e79ae2aa
commit bc82e67561
3 changed files with 29 additions and 6 deletions
+11 -4
View File
@@ -6,8 +6,9 @@ import React, { ReactElement, useContext } from 'react'
import { Check } from 'react-feather' import { Check } from 'react-feather'
import { SwarmButton } from '../../components/SwarmButton' import { SwarmButton } from '../../components/SwarmButton'
import { SwarmTextInput } from '../../components/SwarmTextInput' import { SwarmTextInput } from '../../components/SwarmTextInput'
import { Context as BeeContext } from '../../providers/Bee'
import { Context as SettingsContext } from '../../providers/Settings' import { Context as SettingsContext } from '../../providers/Settings'
import { Context } from '../../providers/Stamps' import { Context as StampsContext } from '../../providers/Stamps'
import { import {
calculateStampPrice, calculateStampPrice,
convertAmountToSeconds, convertAmountToSeconds,
@@ -34,8 +35,10 @@ interface Props {
} }
export function PostageStampCreation({ onFinished }: Props): ReactElement { export function PostageStampCreation({ onFinished }: Props): ReactElement {
const { refresh } = useContext(Context) const { chainState } = useContext(BeeContext)
const { refresh } = useContext(StampsContext)
const { beeDebugApi } = useContext(SettingsContext) const { beeDebugApi } = useContext(SettingsContext)
const { enqueueSnackbar } = useSnackbar() const { enqueueSnackbar } = useSnackbar()
function getFileSize(depth: number): string { function getFileSize(depth: number): string {
@@ -55,10 +58,14 @@ export function PostageStampCreation({ onFinished }: Props): ReactElement {
} }
function getPrice(depth: number, amount: number): string { function getPrice(depth: number, amount: number): string {
if (isNaN(amount) || amount <= 0 || isNaN(depth) || depth < 17 || depth > 255) { const hasInvalidInput = isNaN(amount) || amount <= 0 || isNaN(depth) || depth < 17 || depth > 255
const isCurrentPriceAvailable = chainState && chainState.currentPrice
if (hasInvalidInput || !isCurrentPriceAvailable) {
return '-' return '-'
} }
const price = calculateStampPrice(depth, amount)
const price = calculateStampPrice(depth, amount, chainState.currentPrice)
return `${formatBzz(price)} BZZ` return `${formatBzz(price)} BZZ`
} }
+13
View File
@@ -1,4 +1,5 @@
import type { import type {
ChainState,
ChequebookAddressResponse, ChequebookAddressResponse,
Health, Health,
LastChequesResponse, LastChequesResponse,
@@ -44,6 +45,7 @@ interface ContextInterface {
peerBalances: Balance[] | null peerBalances: Balance[] | null
peerCheques: LastChequesResponse | null peerCheques: LastChequesResponse | null
settlements: Settlements | null settlements: Settlements | null
chainState: ChainState | null
latestBeeRelease: LatestBeeRelease | null latestBeeRelease: LatestBeeRelease | null
isLoading: boolean isLoading: boolean
isRefreshing: boolean isRefreshing: boolean
@@ -82,6 +84,7 @@ const initialValues: ContextInterface = {
peerBalances: null, peerBalances: null,
peerCheques: null, peerCheques: null,
settlements: null, settlements: null,
chainState: null,
latestBeeRelease: null, latestBeeRelease: null,
isLoading: true, isLoading: true,
isRefreshing: false, isRefreshing: false,
@@ -144,6 +147,8 @@ export function Provider({ children }: Props): ReactElement {
const [peerBalances, setPeerBalances] = useState<Balance[] | null>(null) const [peerBalances, setPeerBalances] = useState<Balance[] | null>(null)
const [peerCheques, setPeerCheques] = useState<LastChequesResponse | null>(null) const [peerCheques, setPeerCheques] = useState<LastChequesResponse | null>(null)
const [settlements, setSettlements] = useState<Settlements | null>(null) const [settlements, setSettlements] = useState<Settlements | null>(null)
const [chainState, setChainState] = useState<ChainState | null>(null)
const { latestBeeRelease } = useLatestBeeRelease() const { latestBeeRelease } = useLatestBeeRelease()
const [error, setError] = useState<Error | null>(initialValues.error) const [error, setError] = useState<Error | null>(initialValues.error)
@@ -177,6 +182,7 @@ export function Provider({ children }: Props): ReactElement {
setPeerBalances(null) setPeerBalances(null)
setPeerCheques(null) setPeerCheques(null)
setSettlements(null) setSettlements(null)
setChainState(null)
refresh() refresh()
}, [beeDebugApi]) // eslint-disable-line react-hooks/exhaustive-deps }, [beeDebugApi]) // eslint-disable-line react-hooks/exhaustive-deps
@@ -277,6 +283,12 @@ export function Provider({ children }: Props): ReactElement {
.then(setPeerCheques) .then(setPeerCheques)
.catch(() => setPeerCheques(null)), .catch(() => setPeerCheques(null)),
// Chain state
beeDebugApi
.getChainState()
.then(setChainState)
.catch(() => setChainState(null)),
// Chequebook balance // Chequebook balance
chequeBalanceWrapper() chequeBalanceWrapper()
.then(setChequebookBalance) .then(setChequebookBalance)
@@ -354,6 +366,7 @@ export function Provider({ children }: Props): ReactElement {
peerBalances, peerBalances,
peerCheques, peerCheques,
settlements, settlements,
chainState,
latestBeeRelease, latestBeeRelease,
isLoading, isLoading,
isRefreshing, isRefreshing,
+5 -2
View File
@@ -1,3 +1,4 @@
import { NumberString } from '@ethersphere/bee-js'
import { BigNumber } from 'bignumber.js' import { BigNumber } from 'bignumber.js'
/** /**
@@ -186,6 +187,8 @@ export function convertAmountToSeconds(amount: number): number {
return amount / 10 / 1 return amount / 10 / 1
} }
export function calculateStampPrice(depth: number, amount: number): number { export function calculateStampPrice(depth: number, amount: number, currentPrice: NumberString): number {
return (amount * 2 ** (depth - 16) * 2) / 1e16 const price = parseInt(currentPrice, 10)
return (amount * 2 ** (depth - 16) * price) / 1e16
} }