From d2a48cb393c219e621b07e3fb4d3fb49f87da550 Mon Sep 17 00:00:00 2001 From: Antonio Date: Tue, 19 Nov 2024 11:25:46 +0100 Subject: [PATCH] chore: remove duplicate code --- .../hooks/useGetBuilderRewardPercentage.ts | 21 +++------------- .../hooks/useGetBuildersRewardPercentage.ts | 25 +++---------------- .../rewards/utils/getPercentageData.ts | 22 ++++++++++++++++ .../rewards/utils/toPercentage.ts | 3 +++ 4 files changed, 32 insertions(+), 39 deletions(-) create mode 100644 src/app/collective-rewards/rewards/utils/getPercentageData.ts create mode 100644 src/app/collective-rewards/rewards/utils/toPercentage.ts diff --git a/src/app/collective-rewards/rewards/hooks/useGetBuilderRewardPercentage.ts b/src/app/collective-rewards/rewards/hooks/useGetBuilderRewardPercentage.ts index c523a759..b2fdabdc 100644 --- a/src/app/collective-rewards/rewards/hooks/useGetBuilderRewardPercentage.ts +++ b/src/app/collective-rewards/rewards/hooks/useGetBuilderRewardPercentage.ts @@ -2,14 +2,9 @@ import { BackersManagerAbi } from '@/lib/abis/v2/BackersManagerAbi' import { AVERAGE_BLOCKTIME } from '@/lib/constants' import { BackersManagerAddress } from '@/lib/contracts' import { useEffect, useState } from 'react' -import { Address, parseEther } from 'viem' +import { Address } from 'viem' import { useReadContract } from 'wagmi' - -interface BuilderRewardPercentage { - current: number - next: number - cooldownEndTime: bigint -} +import { BuilderRewardPercentage, getPercentageData } from '../utils/getPercentageData' export const useGetBuilderRewardPercentage = (builder: Address) => { const [rewardPercentageData, setRewardPercentageData] = useState() @@ -28,17 +23,7 @@ export const useGetBuilderRewardPercentage = (builder: Address) => { const [previous, next, cooldownEndTime] = data - const currentTimestamp = Math.floor(Date.now() / 1000) - const previousPercentage = Number((previous * 100n) / parseEther('1')) - const nextPercentage = Number((next * 100n) / parseEther('1')) - let currentPercentage = currentTimestamp < cooldownEndTime ? previousPercentage : nextPercentage - currentPercentage = Math.round(currentPercentage * 100) / 100 - - const percentageData: BuilderRewardPercentage = { - current: currentPercentage, - next: nextPercentage, - cooldownEndTime, - } + const percentageData = getPercentageData(previous, next, cooldownEndTime) setRewardPercentageData(percentageData) }, [data]) diff --git a/src/app/collective-rewards/rewards/hooks/useGetBuildersRewardPercentage.ts b/src/app/collective-rewards/rewards/hooks/useGetBuildersRewardPercentage.ts index cf43f0d8..45a73b16 100644 --- a/src/app/collective-rewards/rewards/hooks/useGetBuildersRewardPercentage.ts +++ b/src/app/collective-rewards/rewards/hooks/useGetBuildersRewardPercentage.ts @@ -2,14 +2,10 @@ import { BackersManagerAbi } from '@/lib/abis/v2/BackersManagerAbi' import { AVERAGE_BLOCKTIME } from '@/lib/constants' import { BackersManagerAddress } from '@/lib/contracts' import { useMemo } from 'react' -import { Address, parseEther } from 'viem' +import { Address } from 'viem' import { useReadContracts } from 'wagmi' - -export interface BuilderRewardPercentage { - current: number - next: number - cooldownEndTime: bigint -} +import { BuilderRewardPercentage, getPercentageData } from '../utils/getPercentageData' +import { toPercentage } from '../utils/toPercentage' export const useGetBuildersRewardPercentage = (builders: Address[]) => { const rewardPercentageCalls = useMemo( @@ -39,20 +35,7 @@ export const useGetBuildersRewardPercentage = (builders: Address[]) => { () => rewardSharesResult ?.map(share => share.result as [bigint, bigint, bigint]) - .map(([previous, next, cooldownEndTime]) => { - const currentTimestamp = Math.floor(Date.now() / 1000) - const previousPercentage = Number((previous * 100n) / parseEther('1')) - const nextPercentage = Number((next * 100n) / parseEther('1')) - let currentPercentage = currentTimestamp < cooldownEndTime ? previousPercentage : nextPercentage - currentPercentage = Math.round(currentPercentage * 100) / 100 - - const percentageData: BuilderRewardPercentage = { - current: currentPercentage, - next: nextPercentage, - cooldownEndTime, - } - return percentageData - }), + .map(([previous, next, cooldownEndTime]) => getPercentageData(previous, next, cooldownEndTime)), [rewardSharesResult], ) diff --git a/src/app/collective-rewards/rewards/utils/getPercentageData.ts b/src/app/collective-rewards/rewards/utils/getPercentageData.ts new file mode 100644 index 00000000..2a3e49a8 --- /dev/null +++ b/src/app/collective-rewards/rewards/utils/getPercentageData.ts @@ -0,0 +1,22 @@ +import { toPercentage } from './toPercentage' + +export interface BuilderRewardPercentage { + current: number + next: number + cooldownEndTime: bigint +} + +export const getPercentageData = (previous: bigint, next: bigint, cooldownEndTime: bigint) => { + const currentTimestamp = Math.floor(Date.now() / 1000) + const previousPercentage = toPercentage(previous) + const nextPercentage = toPercentage(next) + let currentPercentage = currentTimestamp < cooldownEndTime ? previousPercentage : nextPercentage + currentPercentage = Math.round(currentPercentage * 100) / 100 + + const percentageData: BuilderRewardPercentage = { + current: currentPercentage, + next: nextPercentage, + cooldownEndTime, + } + return percentageData +} diff --git a/src/app/collective-rewards/rewards/utils/toPercentage.ts b/src/app/collective-rewards/rewards/utils/toPercentage.ts new file mode 100644 index 00000000..1f316dac --- /dev/null +++ b/src/app/collective-rewards/rewards/utils/toPercentage.ts @@ -0,0 +1,3 @@ +import { parseEther } from 'viem' + +export const toPercentage = (value: bigint) => Number((value * 100n) / parseEther('1'))