Skip to content

Commit

Permalink
chore: remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
antomor committed Nov 19, 2024
1 parent 005d3f1 commit d2a48cb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<BuilderRewardPercentage>()
Expand All @@ -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])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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],
)

Expand Down
22 changes: 22 additions & 0 deletions src/app/collective-rewards/rewards/utils/getPercentageData.ts
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions src/app/collective-rewards/rewards/utils/toPercentage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { parseEther } from 'viem'

export const toPercentage = (value: bigint) => Number((value * 100n) / parseEther('1'))

0 comments on commit d2a48cb

Please sign in to comment.