diff --git a/packages/app/src/api/query/blockNumber.ts b/packages/app/src/api/query/blockNumber.ts deleted file mode 100644 index 16522537c6..0000000000 --- a/packages/app/src/api/query/blockNumber.ts +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2024 @polkadot-cloud/polkadot-staking-dashboard authors & contributors -// SPDX-License-Identifier: GPL-3.0-only - -import { Base } from 'api/base' -import type { ChainId } from 'common-types' - -export class BlockNumber extends Base { - constructor(network: ChainId) { - super(network) - } - - async fetch() { - try { - const result = await this.unsafeApi.query.System.Number.getValue({ - at: 'best', - }) - return result - } catch (e) { - // Silently fail. - } - - return undefined - } -} diff --git a/packages/app/src/hooks/useFetchBlockNumber/index.tsx b/packages/app/src/hooks/useFetchBlockNumber/index.tsx deleted file mode 100644 index fd98f59fed..0000000000 --- a/packages/app/src/hooks/useFetchBlockNumber/index.tsx +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2024 @polkadot-cloud/polkadot-staking-dashboard authors & contributors -// SPDX-License-Identifier: GPL-3.0-only - -import { BlockNumber } from 'api/query/blockNumber' -import type { NetworkId } from 'common-types' -import { useApi } from 'contexts/Api' -import { useEffect, useState } from 'react' - -export const useFetchBlockNumber = (network: NetworkId) => { - const { isReady } = useApi() - const [blockNumber, setBlockNumber] = useState(0) - - const fetchPoolRewardsFrom = async () => { - const block = await new BlockNumber(network).fetch() - setBlockNumber(block) - } - - useEffect(() => { - if (isReady) { - fetchPoolRewardsFrom() - } - }, [isReady, network]) - - return blockNumber -} diff --git a/packages/app/src/pages/Overview/Payouts/ActiveGraph.tsx b/packages/app/src/pages/Overview/Payouts/ActiveGraph.tsx index c707a9ce1a..b8eecec874 100644 --- a/packages/app/src/pages/Overview/Payouts/ActiveGraph.tsx +++ b/packages/app/src/pages/Overview/Payouts/ActiveGraph.tsx @@ -4,6 +4,7 @@ import { useActiveAccounts } from 'contexts/ActiveAccounts' import { useApi } from 'contexts/Api' import { useNetwork } from 'contexts/Network' +import { getUnixTime } from 'date-fns' import { PayoutBar } from 'library/Graphs/PayoutBar' import { PayoutLine } from 'library/Graphs/PayoutLine' import { @@ -24,14 +25,12 @@ interface Props { inPool: boolean lineMarginTop: string setLastReward: (reward: RewardResult | undefined) => void - poolRewardsFrom: number } export const ActiveGraphInner = ({ nominating, inPool, lineMarginTop, setLastReward, - poolRewardsFrom, }: Props) => { const { activeEra } = useApi() const { network } = useNetwork() @@ -42,10 +41,16 @@ export const ActiveGraphInner = ({ who: activeAccount || '', fromEra: Math.max(activeEra.index.minus(1).toNumber(), 0), }) + + const days = 19 + const fromDate = new Date() + fromDate.setDate(fromDate.getDate() - days) + fromDate.setHours(0, 0, 0, 0) + const { data: poolRewardsData } = usePoolRewards({ chain: network, who: activeAccount || '', - from: poolRewardsFrom, + from: getUnixTime(fromDate), }) const nominatorRewards = nominatorRewardData?.allRewards ?? [] @@ -70,7 +75,7 @@ export const ActiveGraphInner = ({ return ( <>
{ const { i18n, t } = useTranslation('pages') const { - network, networkData: { units, brand: { token: Token }, @@ -39,7 +37,6 @@ export const Payouts = () => { const { containerRefs } = useUi() const { inPool } = useActivePool() const { pluginEnabled } = usePlugins() - const blockNumber = useFetchBlockNumber(network) const staking = !inSetup() || inPool const notStaking = !syncing && !staking @@ -116,13 +113,12 @@ export const Payouts = () => { transition: 'opacity 0.5s', }} > - {staking && pluginEnabled('staking_api') && blockNumber > 0 ? ( + {staking && pluginEnabled('staking_api') ? ( ) : ( diff --git a/packages/app/src/pages/Payouts/ActiveGraph.tsx b/packages/app/src/pages/Payouts/ActiveGraph.tsx index 6a62ff03b2..5c64f2e9c0 100644 --- a/packages/app/src/pages/Payouts/ActiveGraph.tsx +++ b/packages/app/src/pages/Payouts/ActiveGraph.tsx @@ -6,6 +6,7 @@ import { MaxPayoutDays } from 'consts' import { useActiveAccounts } from 'contexts/ActiveAccounts' import { useApi } from 'contexts/Api' import { useNetwork } from 'contexts/Network' +import { getUnixTime } from 'date-fns' import { PayoutBar } from 'library/Graphs/PayoutBar' import { PayoutLine } from 'library/Graphs/PayoutLine' import { removeNonZeroAmountAndSort } from 'library/Graphs/Utils' @@ -22,14 +23,12 @@ interface Props { nominating: boolean inPool: boolean setPayoutLists: (payouts: AnyApi[]) => void - poolRewardsFrom: number } export const ActiveGraphInner = ({ nominating, inPool, setPayoutLists, - poolRewardsFrom, }: Props) => { const { activeEra } = useApi() const { network } = useNetwork() @@ -40,10 +39,15 @@ export const ActiveGraphInner = ({ who: activeAccount || '', fromEra: Math.max(activeEra.index.minus(1).toNumber(), 0), }) + + const fromDate = new Date() + fromDate.setDate(fromDate.getDate() - MaxPayoutDays) + fromDate.setHours(0, 0, 0, 0) + const { data: poolRewardsData } = usePoolRewards({ chain: network, who: activeAccount || '', - from: poolRewardsFrom, + from: getUnixTime(fromDate), }) const allRewards = nominatorRewardsData?.allRewards ?? [] diff --git a/packages/app/src/pages/Payouts/index.tsx b/packages/app/src/pages/Payouts/index.tsx index 849a9b414d..27ff33af80 100644 --- a/packages/app/src/pages/Payouts/index.tsx +++ b/packages/app/src/pages/Payouts/index.tsx @@ -4,12 +4,10 @@ import { useSize } from '@w3ux/hooks' import type { PageProps } from 'common-types' import { useHelp } from 'contexts/Help' -import { useNetwork } from 'contexts/Network' import { usePlugins } from 'contexts/Plugins' import { useActivePool } from 'contexts/Pools/ActivePool' import { useStaking } from 'contexts/Staking' import { useUi } from 'contexts/UI' -import { useFetchBlockNumber } from 'hooks/useFetchBlockNumber' import { useSyncing } from 'hooks/useSyncing' import { CardHeaderWrapper, CardWrapper } from 'library/Card/Wrappers' import { @@ -37,10 +35,8 @@ export const Payouts = ({ page: { key } }: PageProps) => { const { inSetup } = useStaking() const { syncing } = useSyncing() const { containerRefs } = useUi() - const { network } = useNetwork() const { inPool } = useActivePool() const { pluginEnabled } = usePlugins() - const blockNumber = useFetchBlockNumber(network) const nominating = !inSetup() const staking = nominating || inPool @@ -122,12 +118,11 @@ export const Payouts = ({ page: { key } }: PageProps) => { transition: 'opacity 0.5s', }} > - {staking && pluginEnabled('staking_api') && blockNumber > 0 ? ( + {staking && pluginEnabled('staking_api') ? ( ) : (