Skip to content

Commit

Permalink
refactor: Null check
Browse files Browse the repository at this point in the history
  • Loading branch information
memoyil committed Dec 29, 2024
1 parent 46b7855 commit d1863f5
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions apps/web/src/hooks/useMerkl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export function useMerklInfo(poolAddress?: string): {
const { data, isPending, refetch } = useQuery({
queryKey: [`fetchMerkl-${chainId}`],
queryFn: async () => {
if (!chainId) return undefined

const responsev4 = await fetch(
`${MERKL_API_V4}/opportunities?chainId=${chainId}&test=false&items=1000&action=POOL,HOLD`,
)
Expand All @@ -55,17 +57,17 @@ export function useMerklInfo(poolAddress?: string): {

const opportunities = merklDataV4?.filter(
(opportunity) =>
opportunity?.tokens?.[0].name?.toLowerCase().startsWith('pancake') ||
opportunity?.tokens?.[0]?.symbol?.toLowerCase().startsWith('Cake-LP') ||
opportunity?.protocol?.id?.toLowerCase().startsWith('pancakeswap'),
)

if (!chainId || !opportunities || !opportunities.length) return undefined
if (!opportunities || !opportunities.length) return undefined

const pools = await Promise.all(
opportunities.map(async (opportunity) => {
const responseCampaignV4 = await fetch(`${MERKL_API_V4}/opportunities/${opportunity.id}/campaigns`)
const campaignV4 = await responseCampaignV4.json()
return { ...opportunity, campaigns: campaignV4.campaigns }
return { ...opportunity, campaigns: campaignV4?.campaigns }
}),
)

Expand All @@ -79,6 +81,8 @@ export function useMerklInfo(poolAddress?: string): {
const { data: userData } = useQuery({
queryKey: [`fetchMerkl-${chainId}-${account}`],
queryFn: async () => {
if (!chainId) return undefined

const responsev4 = await fetch(`${MERKL_API_V4}/users/${account}/rewards?chainId=${chainId}`)

if (!responsev4.ok) {
Expand All @@ -87,7 +91,7 @@ export function useMerklInfo(poolAddress?: string): {

const merklDataV4 = await responsev4.json()

if (!chainId || !merklDataV4) return undefined
if (!merklDataV4) return undefined

return merklDataV4?.[0] || {}
},
Expand All @@ -114,23 +118,28 @@ export function useMerklInfo(poolAddress?: string): {

if (!hasMeanAPR) return false

const hasLiveDistribution = pool.campaigns.some((campaign) => {
const { startTimestamp, endTimestamp, whitelist, blacklist } = campaign
const startTimestampNumber = Number(startTimestamp)
const endTimestampNumber = Number(endTimestamp)
const isLive = startTimestampNumber < currentTimestamp && currentTimestamp < endTimestampNumber
if (!isLive) return false
const whitelistValid =
!whitelist || whitelist.length === 0 || whitelist.includes(account) || whitelist.includes(masterChefV3Address)

const blacklistValid =
!blacklist ||
blacklist.length === 0 ||
!blacklist.includes(account) ||
!blacklist.includes(masterChefV3Address)

return whitelistValid && blacklistValid
})
const hasLiveDistribution = Boolean(
pool.campaigns?.some((campaign) => {
const { startTimestamp, endTimestamp, whitelist, blacklist } = campaign
const startTimestampNumber = Number(startTimestamp)
const endTimestampNumber = Number(endTimestamp)
const isLive = startTimestampNumber <= currentTimestamp && currentTimestamp <= endTimestampNumber
if (!isLive) return false
const whitelistValid =
!whitelist ||
whitelist.length === 0 ||
whitelist.includes(account) ||
whitelist.includes(masterChefV3Address)

const blacklistValid =
!blacklist ||
blacklist.length === 0 ||
!blacklist.includes(account) ||
!blacklist.includes(masterChefV3Address)

return whitelistValid && blacklistValid
}),
)

return hasLiveDistribution
})
Expand Down

0 comments on commit d1863f5

Please sign in to comment.