Skip to content

Commit

Permalink
fix: Cakestaking rounded unlock timestamp error
Browse files Browse the repository at this point in the history
  • Loading branch information
memoyil committed Jan 6, 2025
1 parent 54d709a commit 7ac92c0
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions apps/web/src/views/CakeStaking/hooks/useRoundedUnlockTimestamp.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import { MAX_VECAKE_LOCK_WEEKS, WEEK } from 'config/constants/veCake'
import dayjs from 'dayjs'
import { useLockCakeData } from 'state/vecake/hooks'
import { useMemo } from 'react'
import { useCurrentBlockTimestamp } from './useCurrentBlockTimestamp'

export const useRoundedUnlockTimestamp = (startTimestamp?: number): bigint | undefined => {
const { cakeLockWeeks } = useLockCakeData()
const currentTimestamp = useCurrentBlockTimestamp()

const week = Number(cakeLockWeeks) || 0
const roundUnlockTimestamp = useMemo(() => {
const week = Number(cakeLockWeeks) || 0

const maxUnlockTimestamp = dayjs.unix(currentTimestamp).add(MAX_VECAKE_LOCK_WEEKS, 'weeks').unix()
const userUnlockTimestamp = dayjs
.unix(startTimestamp || currentTimestamp)
.add(week, 'weeks')
.unix()
const maxUnlockTimestamp = dayjs.unix(currentTimestamp).add(MAX_VECAKE_LOCK_WEEKS, 'weeks').unix()
const userUnlockTimestamp = dayjs
.unix(startTimestamp || currentTimestamp)
.add(week, 'weeks')
.unix()

const unlockTimestamp = userUnlockTimestamp > maxUnlockTimestamp ? maxUnlockTimestamp : userUnlockTimestamp
const roundUnlockTimestamp = BigInt(Math.floor(unlockTimestamp / WEEK) * WEEK)
const unlockTimestamp = userUnlockTimestamp > maxUnlockTimestamp ? maxUnlockTimestamp : userUnlockTimestamp
const flooredUnlockTimestamp = Math.floor(unlockTimestamp / WEEK) * WEEK

if (!flooredUnlockTimestamp) {
return undefined
}

return BigInt(flooredUnlockTimestamp)
}, [cakeLockWeeks, startTimestamp, currentTimestamp])

return roundUnlockTimestamp
}

0 comments on commit 7ac92c0

Please sign in to comment.